From 56dea7f0334f60603d4ca6a884ca523fe3389ef3 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 10 Dec 2016 12:06:26 +0700 Subject: `read_configuration()` now accepts `ignore_option_errors`. --- setuptools/config.py | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 889dc683..007d24e2 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -10,7 +10,8 @@ from setuptools.py26compat import import_module from setuptools.extern.six import string_types -def read_configuration(filepath, find_others=False): +def read_configuration( + filepath, find_others=False, ignore_option_errors=False): """Read given configuration file and returns options from it as a dict. :param str|unicode filepath: Path to configuration file @@ -19,6 +20,11 @@ def read_configuration(filepath, find_others=False): :param bool find_others: Whether to search for other configuration files which could be on in various places. + :param bool ignore_option_errors: Whether to silently ignore + options, values of which could not be resolved (e.g. due to exceptions + in directives such as file:, attr:, etc.). + If False exceptions are propagated as expected. + :rtype: dict """ from setuptools.dist import Distribution, _Distribution @@ -40,7 +46,9 @@ def read_configuration(filepath, find_others=False): _Distribution.parse_config_files(dist, filenames=filenames) - handlers = parse_configuration(dist, dist.command_options) + handlers = parse_configuration( + dist, dist.command_options, + ignore_option_errors=ignore_option_errors) os.chdir(current_directory) @@ -76,7 +84,8 @@ def configuration_to_dict(handlers): return config_dict -def parse_configuration(distribution, command_options): +def parse_configuration( + distribution, command_options, ignore_option_errors=False): """Performs additional parsing of configuration options for a distribution. @@ -84,12 +93,18 @@ def parse_configuration(distribution, command_options): :param Distribution distribution: :param dict command_options: + :param bool ignore_option_errors: Whether to silently ignore + options, values of which could not be resolved (e.g. due to exceptions + in directives such as file:, attr:, etc.). + If False exceptions are propagated as expected. :rtype: list """ - meta = ConfigMetadataHandler(distribution.metadata, command_options) + meta = ConfigMetadataHandler( + distribution.metadata, command_options, ignore_option_errors) meta.parse() - options = ConfigOptionsHandler(distribution, command_options) + options = ConfigOptionsHandler( + distribution, command_options, ignore_option_errors) options.parse() return [meta, options] @@ -111,7 +126,7 @@ class ConfigHandler(object): """ - def __init__(self, target_obj, options): + def __init__(self, target_obj, options, ignore_option_errors=False): sections = {} section_prefix = self.section_prefix @@ -122,6 +137,7 @@ class ConfigHandler(object): section_name = section_name.replace(section_prefix, '').strip('.') sections[section_name] = section_options + self.ignore_option_errors = ignore_option_errors self.target_obj = target_obj self.sections = sections self.set_options = [] @@ -148,9 +164,19 @@ class ConfigHandler(object): # Already inhabited. Skipping. return + skip_option = False parser = self.parsers.get(option_name) if parser: - value = parser(value) + try: + value = parser(value) + + except Exception: + skip_option = True + if not self.ignore_option_errors: + raise + + if skip_option: + return setter = getattr(target_obj, 'set_%s' % option_name, None) if setter is None: -- cgit v1.2.3 From a262947e39e6125ee15d3752a2124acf0c62bda6 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 10 Dec 2016 13:33:57 +0700 Subject: Implemented find() configuration support for `packages`. --- setuptools/config.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 007d24e2..743575f0 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -361,7 +361,10 @@ class ConfigHandler(object): method_postfix = '_%s' % section_name section_parser_method = getattr( - self, 'parse_section%s' % method_postfix, None) + self, + # Dots in section names are tranlsated into dunderscores. + ('parse_section%s' % method_postfix).replace('.', '__'), + None) if section_parser_method is None: raise DistutilsOptionError( @@ -481,8 +484,34 @@ class ConfigOptionsHandler(ConfigHandler): if not value.startswith(find_directive): return self._parse_list(value) + # Read function arguments from a dedicated section. + find_kwargs = self.parse_section_packages__find( + self.sections.get('packages.find', {})) + from setuptools import find_packages - return find_packages() + + return find_packages(**find_kwargs) + + def parse_section_packages__find(self, section_options): + """Parses `packages.find` configuration file section. + + To be used in conjunction with _parse_packages(). + + :param dict section_options: + """ + section_data = self._parse_section_to_dict( + section_options, self._parse_list) + + valid_keys = ['where', 'include', 'exclude'] + + find_kwargs = dict( + [(k, v) for k, v in section_data.items() if k in valid_keys and v]) + + where = find_kwargs.get('where') + if where is not None: + find_kwargs['where'] = where[0] # cast list to single val + + return find_kwargs def parse_section_entry_points(self, section_options): """Parses `entry_points` configuration file section. -- cgit v1.2.3 From c471788dbccf4fcf669d141e6f1325c1b43b8b94 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 10 Dec 2016 22:24:01 +0700 Subject: Proper finalization for `read_configuration()`. --- setuptools/config.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 743575f0..d71ff028 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -38,19 +38,21 @@ def read_configuration( current_directory = os.getcwd() os.chdir(os.path.dirname(filepath)) - dist = Distribution() + try: + dist = Distribution() - filenames = dist.find_config_files() if find_others else [] - if filepath not in filenames: - filenames.append(filepath) + filenames = dist.find_config_files() if find_others else [] + if filepath not in filenames: + filenames.append(filepath) - _Distribution.parse_config_files(dist, filenames=filenames) + _Distribution.parse_config_files(dist, filenames=filenames) - handlers = parse_configuration( - dist, dist.command_options, - ignore_option_errors=ignore_option_errors) + handlers = parse_configuration( + dist, dist.command_options, + ignore_option_errors=ignore_option_errors) - os.chdir(current_directory) + finally: + os.chdir(current_directory) return configuration_to_dict(handlers) -- cgit v1.2.3 From ff371f18f0076bc63da05334f7e551c1cc29e10d Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 1 Jan 2017 22:34:28 -0500 Subject: Strip out vendored packages and require them instead. Ref #581. --- setuptools/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index d71ff028..19b39629 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -7,7 +7,7 @@ from functools import partial from distutils.errors import DistutilsOptionError, DistutilsFileError from setuptools.py26compat import import_module -from setuptools.extern.six import string_types +from six import string_types def read_configuration( -- cgit v1.2.3 From 50830de19302234b6741e2d1b853fbf07fbedf95 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 4 Feb 2017 15:21:12 +0700 Subject: Dropped support for classifiers subsection handling in setup.cfg (see #952). --- setuptools/config.py | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 19b39629..39a01f88 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -412,17 +412,6 @@ class ConfigMetadataHandler(ConfigHandler): 'version': self._parse_version, } - def parse_section_classifiers(self, section_options): - """Parses configuration file section. - - :param dict section_options: - """ - classifiers = [] - for begin, (_, rest) in section_options.items(): - classifiers.append('%s :%s' % (begin.title(), rest)) - - self['classifiers'] = classifiers - def _parse_version(self, value): """Parses `version` option value. -- cgit v1.2.3 From 3d0cc355fb5e8012cb8c72f0e25042a5a44f31d6 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 24 Feb 2017 11:49:51 -0500 Subject: Revert "Merge pull request #933 from pypa/feature/581-depend-not-bundle" This reverts commit 089cdeb489a0fa94d11b7307b54210ef9aa40511, reversing changes made to aaec654d804cb78dbb6391afff721a63f26a71cd. --- setuptools/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 39a01f88..0149316c 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -7,7 +7,7 @@ from functools import partial from distutils.errors import DistutilsOptionError, DistutilsFileError from setuptools.py26compat import import_module -from six import string_types +from setuptools.extern.six import string_types def read_configuration( -- cgit v1.2.3 From abaf7c4dd76c56eb509f6a5f6caa2f8e886801b6 Mon Sep 17 00:00:00 2001 From: Marcel Bargull Date: Fri, 7 Apr 2017 13:42:51 +0200 Subject: Fixes #999: support python_requires, py_modules in configuration files --- setuptools/config.py | 1 + 1 file changed, 1 insertion(+) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 39a01f88..252f2deb 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -462,6 +462,7 @@ class ConfigOptionsHandler(ConfigHandler): 'tests_require': parse_list_semicolon, 'packages': self._parse_packages, 'entry_points': self._parse_file, + 'py_modules': parse_list, } def _parse_packages(self, value): -- cgit v1.2.3 From 986ff42f117e8ac3cc952bfc2c043f7de8ba7084 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 9 Aug 2017 23:16:13 +0300 Subject: Allow adding few files @ metadata.long_description --- setuptools/config.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 06a61d16..4d3eff93 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -245,8 +245,8 @@ class ConfigHandler(object): directory with setup.py. Examples: - include: LICENSE - include: src/file.txt + file: LICENSE + file: src/file.txt :param str value: :rtype: str @@ -408,7 +408,7 @@ class ConfigMetadataHandler(ConfigHandler): 'classifiers': self._get_parser_compound(parse_file, parse_list), 'license': parse_file, 'description': parse_file, - 'long_description': parse_file, + 'long_description': self._get_parser_compound(parse_list, lambda l: '\n'.join(map(parse_file, l))), 'version': self._parse_version, } -- cgit v1.2.3 From b699f94728c82691bed9b67a48b170951535fa38 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Mon, 28 Aug 2017 10:22:21 +0300 Subject: Support list of files passed to `file:` directive * `file:` not accepts comma-separated list of filenames * files' contents are glues with an LF separator --- setuptools/config.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 4d3eff93..b6627c80 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -246,30 +246,38 @@ class ConfigHandler(object): Examples: file: LICENSE - file: src/file.txt + file: README.rst, CHANGELOG.md, src/file.txt :param str value: :rtype: str """ + include_directive = 'file:' + file_contents = [] + if not isinstance(value, string_types): return value - include_directive = 'file:' if not value.startswith(include_directive): return value + filepaths = value[len(include_directive):] + filepaths = filepaths.split(',') + filepaths = map(str.strip, filepaths) + filepaths = map(os.path.abspath, filepaths) + current_directory = os.getcwd() - filepath = value.replace(include_directive, '').strip() - filepath = os.path.abspath(filepath) + for filepath in filepaths: + if not filepath.startswith(current_directory): + raise DistutilsOptionError( + '`file:` directive can not access %s' % filepath) - if not filepath.startswith(current_directory): - raise DistutilsOptionError( - '`file:` directive can not access %s' % filepath) + if os.path.isfile(filepath): + with io.open(filepath, encoding='utf-8') as f: + file_contents.append(f.read()) - if os.path.isfile(filepath): - with io.open(filepath, encoding='utf-8') as f: - value = f.read() + if file_contents: + value = '\n'.join(file_contents) return value @@ -408,7 +416,7 @@ class ConfigMetadataHandler(ConfigHandler): 'classifiers': self._get_parser_compound(parse_file, parse_list), 'license': parse_file, 'description': parse_file, - 'long_description': self._get_parser_compound(parse_list, lambda l: '\n'.join(map(parse_file, l))), + 'long_description': parse_file, 'version': self._parse_version, } -- cgit v1.2.3 From c5c4304d8c60b6f985c8c7607f6950d31d1b2e33 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Mon, 28 Aug 2017 10:39:29 +0300 Subject: Convert path to str, which is needed under Py 2 --- setuptools/config.py | 1 + 1 file changed, 1 insertion(+) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index b6627c80..6fa50d20 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -262,6 +262,7 @@ class ConfigHandler(object): filepaths = value[len(include_directive):] filepaths = filepaths.split(',') + filepaths = map(str, filepaths) # Needed for Python 2 filepaths = map(str.strip, filepaths) filepaths = map(os.path.abspath, filepaths) -- cgit v1.2.3 From fc59fe8ea080f7d469c6c388fa878c4ede3e6557 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 28 Aug 2017 08:54:02 -0400 Subject: Using generator comprehension, avoid casting filepath to bytes on Python 2 --- setuptools/config.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 6fa50d20..23fc25ea 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -260,11 +260,8 @@ class ConfigHandler(object): if not value.startswith(include_directive): return value - filepaths = value[len(include_directive):] - filepaths = filepaths.split(',') - filepaths = map(str, filepaths) # Needed for Python 2 - filepaths = map(str.strip, filepaths) - filepaths = map(os.path.abspath, filepaths) + spec = value[len(include_directive):] + filepaths = (os.path.abspath(path.strip()) for path in spec.split(',')) current_directory = os.getcwd() -- cgit v1.2.3 From 3af152a26ee2ef889b8d7ab4428c975ba0c8e85b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 28 Aug 2017 09:05:42 -0400 Subject: Extract method for reading local file. Now return results directly instead of for/append loop. --- setuptools/config.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 23fc25ea..2b156269 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -252,7 +252,6 @@ class ConfigHandler(object): :rtype: str """ include_directive = 'file:' - file_contents = [] if not isinstance(value, string_types): return value @@ -262,22 +261,24 @@ class ConfigHandler(object): spec = value[len(include_directive):] filepaths = (os.path.abspath(path.strip()) for path in spec.split(',')) + return '\n'.join( + self._read_local_file(path) + for path in filepath + if os.path.isfile(path) + ) + + @staticmethod + def _read_local_file(filepath): + """ + Read contents of filepath. Raise error if the file + isn't in the current directory. + """ + if not filepath.startswith(os.getcwd()): + raise DistutilsOptionError( + '`file:` directive can not access %s' % filepath) - current_directory = os.getcwd() - - for filepath in filepaths: - if not filepath.startswith(current_directory): - raise DistutilsOptionError( - '`file:` directive can not access %s' % filepath) - - if os.path.isfile(filepath): - with io.open(filepath, encoding='utf-8') as f: - file_contents.append(f.read()) - - if file_contents: - value = '\n'.join(file_contents) - - return value + with io.open(filepath, encoding='utf-8') as f: + return f.read() @classmethod def _parse_attr(cls, value): -- cgit v1.2.3 From 7e549f6eb72e37f50a1183e018ad2f3b2c952285 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 28 Aug 2017 09:09:41 -0400 Subject: Correct typo --- setuptools/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 2b156269..7ba02184 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -263,7 +263,7 @@ class ConfigHandler(object): filepaths = (os.path.abspath(path.strip()) for path in spec.split(',')) return '\n'.join( self._read_local_file(path) - for path in filepath + for path in filepaths if os.path.isfile(path) ) -- cgit v1.2.3 From 3c25384fce3f6134a342ab32b7afc54cc6066fa3 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 28 Aug 2017 09:13:37 -0400 Subject: Use proper reference. --- setuptools/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 7ba02184..75f829fb 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -262,7 +262,7 @@ class ConfigHandler(object): spec = value[len(include_directive):] filepaths = (os.path.abspath(path.strip()) for path in spec.split(',')) return '\n'.join( - self._read_local_file(path) + cls._read_local_file(path) for path in filepaths if os.path.isfile(path) ) -- cgit v1.2.3 From 9e708961e7fa64e56c9469ca52163d7e2df31477 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 28 Aug 2017 09:32:25 -0400 Subject: Need to perform the local assertion before checking for existence. --- setuptools/config.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'setuptools/config.py') diff --git a/setuptools/config.py b/setuptools/config.py index 75f829fb..9a62e2ec 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -262,21 +262,20 @@ class ConfigHandler(object): spec = value[len(include_directive):] filepaths = (os.path.abspath(path.strip()) for path in spec.split(',')) return '\n'.join( - cls._read_local_file(path) + cls._read_file(path) for path in filepaths - if os.path.isfile(path) + if (cls._assert_local(path) or True) + and os.path.isfile(path) ) @staticmethod - def _read_local_file(filepath): - """ - Read contents of filepath. Raise error if the file - isn't in the current directory. - """ + def _assert_local(filepath): if not filepath.startswith(os.getcwd()): raise DistutilsOptionError( '`file:` directive can not access %s' % filepath) + @staticmethod + def _read_file(filepath): with io.open(filepath, encoding='utf-8') as f: return f.read() -- cgit v1.2.3