Skip to content

Commit

Permalink
Add support for license_files specified in setup.py (#466)
Browse files Browse the repository at this point in the history
Wheel now leans on setuptools to provide the necessary information.
  • Loading branch information
abravalheri committed Oct 20, 2022
1 parent 048b2ce commit eaa09fc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 58 deletions.
3 changes: 3 additions & 0 deletions docs/news.rst
Expand Up @@ -6,6 +6,9 @@ Release Notes
- Dropped support for Python < 3.7
- Updated vendored ``packaging`` to 21.3
- Replaced all uses of ``distutils`` with ``setuptools``
- The handling of ``license_files`` (including glob patterns and default
values) is now delegated to ``setuptools>=57.0.0`` (#466).
The package dependencies were updated to reflect this change.

**0.37.1 (2021-12-22)**

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -31,7 +31,7 @@ package_dir=
= src
packages = find:
python_requires = >=3.7
install_requires = setuptools >= 45.2.0
install_requires = setuptools >= 57.0.0
zip_safe = False

[options.packages.find]
Expand Down
35 changes: 2 additions & 33 deletions src/wheel/bdist_wheel.py
Expand Up @@ -15,7 +15,6 @@
import warnings
from collections import OrderedDict
from email.generator import BytesGenerator, Generator
from glob import iglob
from io import BytesIO
from shutil import rmtree
from sysconfig import get_config_var
Expand Down Expand Up @@ -432,38 +431,8 @@ def _ensure_relative(self, path):

@property
def license_paths(self):
metadata = self.distribution.get_option_dict("metadata")
files = set()
patterns = sorted(
{option for option in metadata.get("license_files", ("", ""))[1].split()}
)

if "license_file" in metadata:
warnings.warn(
'The "license_file" option is deprecated. Use '
'"license_files" instead.',
DeprecationWarning,
)
files.add(metadata["license_file"][1])

if "license_file" not in metadata and "license_files" not in metadata:
patterns = ("LICEN[CS]E*", "COPYING*", "NOTICE*", "AUTHORS*")

for pattern in patterns:
for path in iglob(pattern):
if path.endswith("~"):
log.debug(
f'ignoring license file "{path}" as it looks like a ' f"backup"
)
continue

if path not in files and os.path.isfile(path):
log.info(
f'adding license file "{path}" (matched pattern "{pattern}")'
)
files.add(path)

return files
metadata = self.distribution.metadata
return sorted(metadata.license_files or [])

def egg2dist(self, egginfo_path, distinfo_path):
"""Convert an .egg-info directory into a .dist-info directory"""
Expand Down
47 changes: 23 additions & 24 deletions tests/test_bdist_wheel.py
Expand Up @@ -34,21 +34,20 @@
"LICENSE~",
"AUTHORS~",
}


@pytest.fixture
def dummy_dist(tmpdir_factory):
basedir = tmpdir_factory.mktemp("dummy_dist")
basedir.join("setup.py").write(
"""\
SETUPPY_EXAMPLE = """\
from setuptools import setup
setup(
name='dummy_dist',
version='1.0'
version='1.0',
)
"""
)


@pytest.fixture
def dummy_dist(tmpdir_factory):
basedir = tmpdir_factory.mktemp("dummy_dist")
basedir.join("setup.py").write(SETUPPY_EXAMPLE)
for fname in DEFAULT_LICENSE_FILES | OTHER_IGNORED_FILES:
basedir.join(fname).write("")

Expand Down Expand Up @@ -83,21 +82,21 @@ def test_licenses_default(dummy_dist, monkeypatch, tmpdir):
assert set(wf.namelist()) == DEFAULT_FILES | license_files


def test_licenses_deprecated(dummy_dist, monkeypatch, tmpdir):
dummy_dist.join("setup.cfg").write("[metadata]\nlicense_file=licenses/DUMMYFILE")
monkeypatch.chdir(dummy_dist)
subprocess.check_call(
[sys.executable, "setup.py", "bdist_wheel", "-b", str(tmpdir), "--universal"]
)
with WheelFile("dist/dummy_dist-1.0-py2.py3-none-any.whl") as wf:
license_files = {"dummy_dist-1.0.dist-info/DUMMYFILE"}
assert set(wf.namelist()) == DEFAULT_FILES | license_files


def test_licenses_override(dummy_dist, monkeypatch, tmpdir):
dummy_dist.join("setup.cfg").write(
"[metadata]\nlicense_files=licenses/*\n LICENSE"
)
@pytest.mark.parametrize(
"config_file, config",
[
("setup.cfg", "[metadata]\nlicense_files=licenses/*\n LICENSE"),
("setup.cfg", "[metadata]\nlicense_files=licenses/*, LICENSE"),
(
"setup.py",
SETUPPY_EXAMPLE.replace(
")", " license_files=['licenses/DUMMYFILE', 'LICENSE'])"
),
),
],
)
def test_licenses_override(dummy_dist, monkeypatch, tmpdir, config_file, config):
dummy_dist.join(config_file).write(config)
monkeypatch.chdir(dummy_dist)
subprocess.check_call(
[sys.executable, "setup.py", "bdist_wheel", "-b", str(tmpdir), "--universal"]
Expand Down

0 comments on commit eaa09fc

Please sign in to comment.