From eafc18d1ed563e6cc334e2e1ef9827bcbc360896 Mon Sep 17 00:00:00 2001 From: Samuel Bishop Date: Mon, 9 Sep 2019 16:14:21 +0800 Subject: [PATCH] #1350 - Improvements to handling of License file(s) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added support for specifying a set of license files as part of the project config. - Moved the license file config into the package configuration. (`Poetry.create()`) this matches how similar configurations such as “readme” are configured. - Updated the builders to use the project level license file list instead of searching for their own (previously different) lists of license files. - Moved the existing auto-discovery behaviours to become the default case during `create()` if nothing specific was configured. - Fix #866 by modifing the base template used by `SdistBuilder`. It now uses `setuptools`, since as documented by @seifertm in issue #866 `pip` uses `setuptools` anyway and this clears the way to immediately supporting https://github.com/pypa/setuptools/pull/1767 once this is merged and released from `setuptools`. Until `setuptools` updates to support multiple license files, this will result in a single log line in the debug output from `setuptools` so this is ok to merge before the change to `setuptools` is merged. --- poetry/masonry/builders/builder.py | 5 +++-- poetry/masonry/builders/sdist.py | 8 +++++++- poetry/masonry/builders/wheel.py | 5 ++--- poetry/packages/package.py | 1 + poetry/poetry.py | 10 ++++++++++ 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/poetry/masonry/builders/builder.py b/poetry/masonry/builders/builder.py index ef38540743f..93536f3ecb3 100644 --- a/poetry/masonry/builders/builder.py +++ b/poetry/masonry/builders/builder.py @@ -146,8 +146,9 @@ def find_files_to_add(self, exclude_build=True): # type: (bool) -> list ) to_add.append(Path("pyproject.toml")) - # If a license file exists, add it - for license_file in self._path.glob("LICENSE*"): + # If any license files exist, add them. + for license_filename in list(self._package.license_files): + license_file = self._path / license_filename self._io.write_line( " - Adding: {}".format( license_file.relative_to(self._path) diff --git a/poetry/masonry/builders/sdist.py b/poetry/masonry/builders/sdist.py index 268f8f5c6f8..ae8381ba0a2 100644 --- a/poetry/masonry/builders/sdist.py +++ b/poetry/masonry/builders/sdist.py @@ -22,7 +22,7 @@ SETUP = """\ # -*- coding: utf-8 -*- -from distutils.core import setup +from setuptools import setup {before} setup_kwargs = {{ @@ -137,6 +137,12 @@ def build_setup(self): # type: () -> bytes else: pass + license_file_list = list(self._package.license_files) + if len(license_file_list) == 1: + extra.append("'license_file': \"{}\",".format(license_file_list[0])) + elif len(license_file_list) > 1: + extra.append("'license_file': \"{}\",".format(list(map(str, license_file_list)))) + if package_dir: before.append("package_dir = \\\n{}\n".format(pformat(package_dir))) extra.append("'package_dir': package_dir,") diff --git a/poetry/masonry/builders/wheel.py b/poetry/masonry/builders/wheel.py index 71423fefeb6..65a70fdb8a6 100644 --- a/poetry/masonry/builders/wheel.py +++ b/poetry/masonry/builders/wheel.py @@ -181,9 +181,8 @@ def _write_metadata(self, wheel): with self._write_to_zip(wheel, self.dist_info + "/entry_points.txt") as f: self._write_entry_points(f) - for base in ("COPYING", "LICENSE"): - for path in sorted(self._path.glob(base + "*")): - self._add_file(wheel, path, "%s/%s" % (self.dist_info, path.name)) + for license_file in self._package.license_files: + self._add_file(wheel, license_file, "%s/%s" % (self.dist_info, license_file.name)) with self._write_to_zip(wheel, self.dist_info + "/WHEEL") as f: self._write_wheel_file(f) diff --git a/poetry/packages/package.py b/poetry/packages/package.py index 648a476cfd4..d9f3ef513a8 100644 --- a/poetry/packages/package.py +++ b/poetry/packages/package.py @@ -53,6 +53,7 @@ def __init__(self, name, version, pretty_version=None): self.documentation_url = None self.keywords = [] self._license = None + self.license_files = set() self.readme = None self.source_name = "" diff --git a/poetry/poetry.py b/poetry/poetry.py index 3c53244830b..6de02e9d3a4 100644 --- a/poetry/poetry.py +++ b/poetry/poetry.py @@ -129,6 +129,16 @@ def create(cls, cwd): # type: (Path) -> Poetry if "readme" in local_config: package.readme = Path(poetry_file.parent) / local_config["readme"] + # Add any manually defined or automatically discovered license files. + if "license-files" in local_config: + for file in local_config["license-files"].items(): + package.license_files.add(Path(poetry_file.parent) / file) + else: + for license_file in poetry_file.parent.glob("LICENSE*"): + package.license_files.add(license_file.relative_to(poetry_file.parent)) + for license_file in poetry_file.parent.glob("COPYING*"): + package.license_files.add(license_file.relative_to(poetry_file.parent)) + if "platform" in local_config: package.platform = local_config["platform"]