From 930f20f3c28f8ebf24323bf58a179cd6bac3be17 Mon Sep 17 00:00:00 2001 From: Deniz Taneli <7292227+dtaneli@users.noreply.github.com> Date: Sat, 27 Oct 2018 13:19:22 +0100 Subject: [PATCH] Setuptools will install licenses if included in setup.cfg Addressing #357 `python setup.py sdist` now includes the license file if `license_file` is included in `setup.cfg` unless it is explicitly excluded in `MANIFEST.in`. Co-Authored-By: Poyzan Nur Taneli <31743851+ptaneli@users.noreply.github.com> --- setuptools/command/egg_info.py | 1 + setuptools/command/sdist.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/setuptools/command/egg_info.py b/setuptools/command/egg_info.py index bd116e1f6cc..93100ab946d 100644 --- a/setuptools/command/egg_info.py +++ b/setuptools/command/egg_info.py @@ -568,6 +568,7 @@ def _should_suppress_warning(msg): def add_defaults(self): sdist.add_defaults(self) + self.check_license() self.filelist.append(self.template) self.filelist.append(self.manifest) rcfiles = list(walk_revctrl()) diff --git a/setuptools/command/sdist.py b/setuptools/command/sdist.py index bcfae4d82f6..6820be9d603 100644 --- a/setuptools/command/sdist.py +++ b/setuptools/command/sdist.py @@ -198,3 +198,28 @@ def read_manifest(self): continue self.filelist.append(line) manifest.close() + + def check_license(self): + """Read the setup configuration file ('setup.cfg') and use it to find + if a license is defined with the 'license_file' attribute. + If the license is declared and exists, it will be added to + 'self.filelist'. + """ + + from configparser import ConfigParser + cfg_file = 'setup.cfg' + log.debug("Reading configuration from %s", cfg_file) + parser = ConfigParser() + parser.read(cfg_file) + license_file = parser.get('metadata', 'license_file', fallback=None) + + if not license_file: + log.debug("license_file attribute is not defined in setup.cfg") + return + + if not os.path.exists(license_file): + log.warn("warning: Failed to find license file '%s' in setup.cfg", + license_file) + return + + self.filelist.append(license_file)