From 7df349d07e6441a33427ad5c371f12bf6bedc529 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 26 Nov 2016 21:36:35 +0700 Subject: Added config module. --- setuptools/config.py | 367 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 367 insertions(+) create mode 100644 setuptools/config.py (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py new file mode 100644 index 00000000..94b2ab17 --- /dev/null +++ b/setuptools/config.py @@ -0,0 +1,367 @@ +import io +import os +import sys +from functools import partial + +from distutils.errors import DistutilsOptionError +from setuptools.py26compat import import_module +from setuptools.extern.six import string_types + + +class ConfigHandler(object): + """Handles metadata supplied in configuration files.""" + + section_prefix = None + + def __init__(self, target_obj, options): + sections = {} + + section_prefix = self.section_prefix + for section_name, section_options in options.items(): + if not section_name.startswith(section_prefix): + continue + + section_name = section_name.replace(section_prefix, '').strip(':') + sections[section_name] = section_options + + self.target_obj = target_obj + self.sections = sections + + @property + def parsers(self): + """Metadata item name to parser function mapping.""" + raise NotImplementedError( + '%s must provide .parsers property' % self.__class__.__name__) + + def __setitem__(self, option_name, value): + unknown = tuple() + target_obj = self.target_obj + + current_value = getattr(target_obj, option_name, unknown) + + if current_value is unknown: + raise KeyError(option_name) + + if current_value: + # Already inhabited. Skipping. + return + + parser = self.parsers.get(option_name) + if parser: + value = parser(value) + + setter = getattr(target_obj, 'set_%s' % option_name, None) + if setter is None: + setattr(target_obj, option_name, value) + else: + setter(value) + + @classmethod + def _parse_list(cls, value, separator=','): + """Represents value as a list. + + Value is split either by comma or by lines. + + :param value: + :param separator: List items separator character. + :rtype: list + """ + if isinstance(value, list): # _parse_complex case + return value + + if '\n' in value: + value = value.splitlines() + else: + value = value.split(separator) + + return [chunk.strip() for chunk in value] + + @classmethod + def _parse_dict(cls, value): + """Represents value as a dict. + + :param value: + :rtype: dict + """ + separator = '=' + result = {} + for line in cls._parse_list(value): + key, sep, val = line.partition(separator) + if sep != separator: + raise DistutilsOptionError( + 'Unable to parse option value to dict: %s' % value) + result[key.strip()] = val.strip() + + return result + + @classmethod + def _parse_bool(cls, value): + """Represents value as boolean. + + :param value: + :rtype: bool + """ + value = value.lower() + return value in ('1', 'true', 'yes') + + @classmethod + def _parse_file(cls, value): + """Represents value as a string, allowing including text + from nearest files using include(). + + Examples: + include: LICENSE + include: src/file.txt + + :param str value: + :rtype: str + """ + if not isinstance(value, string_types): + return value + + include_directive = 'file:' + if not value.startswith(include_directive): + return value + + filepath = value.replace(include_directive, '').strip() + + if os.path.isfile(filepath): + with io.open(filepath, encoding='utf-8') as f: + value = f.read() + + return value + + @classmethod + def _get_parser_compound(cls, *parse_methods): + """Returns parser function to represents value as a list. + + Parses a value applying given methods one after another. + + :param parse_methods: + :rtype: callable + """ + def parse(value): + parsed = value + + for method in parse_methods: + parsed = method(parsed) + + return parsed + + return parse + + @classmethod + def _parse_section_to_dict(cls, section_options, values_parser=None): + """Parses section options into a dictionary. + + Optionally applies a given parser to values. + + :param dict section_options: + :param callable values_parser: + :rtype: dict + """ + value = {} + values_parser = values_parser or (lambda val: val) + for key, (_, val) in section_options.items(): + value[key] = values_parser(val) + return value + + def parse_section(self, section_options): + """Parses configuration file section. + + :param dict section_options: + """ + for (name, (_, value)) in section_options.items(): + try: + self[name] = value + except KeyError: + raise DistutilsOptionError( + 'Unknown distribution option: %s' % name) + + def parse(self): + """Parses configuration file items from one + or more related sections. + + """ + for section_name, section_options in self.sections.items(): + + method_postfix = '' + if section_name: # [section:option] variant + method_postfix = '_%s' % section_name + + section_parser_method = getattr( + self, 'parse_section%s' % method_postfix, None) + + if section_parser_method is None: + raise DistutilsOptionError( + 'Unsupported distribution option section: [%s:%s]' % ( + self.section_prefix, section_name)) + + section_parser_method(section_options) + + +class ConfigMetadataHandler(ConfigHandler): + + section_prefix = 'metadata' + + @property + def parsers(self): + """Metadata item name to parser function mapping.""" + parse_list = self._parse_list + parse_file = self._parse_file + + return { + 'platforms': parse_list, + 'keywords': parse_list, + 'provides': parse_list, + 'requires': parse_list, + 'obsoletes': parse_list, + 'classifiers': self._get_parser_compound(parse_file, parse_list), + 'license': parse_file, + 'description': parse_file, + 'long_description': parse_file, + '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. + + :param value: + :rtype: str + + """ + attr_directive = 'attr:' + if not value.startswith(attr_directive): + return value + + attrs_path = value.replace(attr_directive, '').strip().split('.') + attr_name = attrs_path.pop() + + module_name = '.'.join(attrs_path) + module_name = module_name or '__init__' + + sys.path.insert(0, os.getcwd()) + try: + module = import_module(module_name) + version = getattr(module, attr_name) + + if callable(version): + version = version() + + if not isinstance(version, string_types): + if hasattr(version, '__iter__'): + version = '.'.join(map(str, version)) + else: + version = '%s' % version + + finally: + sys.path = sys.path[1:] + + return version + + +class ConfigOptionsHandler(ConfigHandler): + + section_prefix = 'options' + + @property + def parsers(self): + """Metadata item name to parser function mapping.""" + parse_list = self._parse_list + parse_list_semicolon = partial(self._parse_list, separator=';') + parse_bool = self._parse_bool + parse_dict = self._parse_dict + + return { + 'zip_safe': parse_bool, + 'use_2to3': parse_bool, + 'include_package_data': parse_bool, + 'package_dir': parse_dict, + 'use_2to3_fixers': parse_list, + 'use_2to3_exclude_fixers': parse_list, + 'convert_2to3_doctests': parse_list, + 'scripts': parse_list, + 'eager_resources': parse_list, + 'dependency_links': parse_list, + 'namespace_packages': parse_list, + 'install_requires': parse_list_semicolon, + 'setup_requires': parse_list_semicolon, + 'tests_require': parse_list_semicolon, + 'packages': self._parse_packages, + 'entry_points': self._parse_file, + } + + def _parse_packages(self, value): + """Parses `packages` option value. + + :param value: + :rtype: list + """ + find_directive = 'find:' + + if not value.startswith(find_directive): + return self._parse_list(value) + + from setuptools import find_packages + return find_packages() + + def parse_section_dependency_links(self, section_options): + """Parses `dependency_links` configuration file section. + + :param dict section_options: + """ + parsed = self._parse_section_to_dict(section_options) + self['dependency_links'] = list(parsed.values()) + + def parse_section_entry_points(self, section_options): + """Parses `entry_points` configuration file section. + + :param dict section_options: + """ + parsed = self._parse_section_to_dict(section_options, self._parse_list) + self['entry_points'] = parsed + + def _parse_package_data(self, section_options): + parsed = self._parse_section_to_dict(section_options, self._parse_list) + + root = parsed.get('*') + if root: + parsed[''] = root + del parsed['*'] + + return parsed + + def parse_section_package_data(self, section_options): + """Parses `package_data` configuration file section. + + :param dict section_options: + """ + self['package_data'] = self._parse_package_data(section_options) + + def parse_section_exclude_package_data(self, section_options): + """Parses `exclude_package_data` configuration file section. + + :param dict section_options: + """ + self['exclude_package_data'] = self._parse_package_data( + section_options) + + def parse_section_extras_require(self, section_options): + """Parses `extras_require` configuration file section. + + :param dict section_options: + """ + parse_list = partial(self._parse_list, separator=';') + self['extras_require'] = self._parse_section_to_dict( + section_options, parse_list) -- cgit v1.2.3 From 69130241500d78735375e36eca1b3dc6a7048dd6 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 26 Nov 2016 21:42:08 +0700 Subject: Metadata and options are now could be set in setup.cfg (see #394). --- setuptools/dist.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'setuptools') diff --git a/setuptools/dist.py b/setuptools/dist.py index 612040c8..c975abe0 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -19,6 +19,7 @@ from pkg_resources.extern import packaging from setuptools.depends import Require from setuptools import windows_support from setuptools.monkey import get_unpatched +from setuptools.config import ConfigMetadataHandler, ConfigOptionsHandler import pkg_resources @@ -342,6 +343,16 @@ class Distribution(_Distribution): if getattr(self, 'python_requires', None): self.metadata.python_requires = self.python_requires + def parse_config_files(self, filenames=None): + """Parses configuration files from various levels + and loads configuration. + + """ + _Distribution.parse_config_files(self, filenames=filenames) + + ConfigMetadataHandler(self.metadata, self.command_options).parse() + ConfigOptionsHandler(self, self.command_options).parse() + def parse_command_line(self): """Process features after parsing command line options""" result = _Distribution.parse_command_line(self) -- cgit v1.2.3 From 58a5c4ff662a19d07b81da4c7b08e851dc2f65c8 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 26 Nov 2016 21:42:27 +0700 Subject: Added tests for config module. --- setuptools/tests/test_config.py | 339 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 339 insertions(+) create mode 100644 setuptools/tests/test_config.py (limited to 'setuptools') diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py new file mode 100644 index 00000000..0ef7a994 --- /dev/null +++ b/setuptools/tests/test_config.py @@ -0,0 +1,339 @@ +import contextlib +import pytest +from distutils.errors import DistutilsOptionError +from setuptools.dist import Distribution +from setuptools.config import ConfigHandler + + +class ErrConfigHandler(ConfigHandler): + """Erroneous handler. Fails to implement required methods.""" + + +def fake_env(tmpdir, setup_cfg, setup_py=None): + + if setup_py is None: + setup_py = ( + 'from setuptools import setup\n' + 'setup()\n' + ) + + tmpdir.join('setup.py').write(setup_py) + tmpdir.join('setup.cfg').write(setup_cfg) + + package_name = 'fake_package' + dir_package = tmpdir.mkdir(package_name) + dir_package.join('__init__.py').write( + 'VERSION = (1, 2, 3)\n' + '\n' + 'VERSION_MAJOR = 1' + '\n' + 'def get_version():\n' + ' return [3, 4, 5, "dev"]\n' + '\n' + ) + + +@contextlib.contextmanager +def get_dist(tmpdir, kwargs_initial=None, parse=True): + kwargs_initial = kwargs_initial or {} + + with tmpdir.as_cwd(): + dist = Distribution(kwargs_initial) + dist.script_name = 'setup.py' + parse and dist.parse_config_files() + + yield dist + + +def test_parsers_implemented(): + + with pytest.raises(NotImplementedError): + handler = ErrConfigHandler(None, {}) + handler.parsers + + +class TestMetadata: + + def test_basic(self, tmpdir): + + fake_env( + tmpdir, + '[metadata]\n' + 'version = 10.1.1\n' + 'description = Some description\n' + 'long_description = file: README\n' + 'name = fake_name\n' + 'keywords = one, two\n' + 'provides = package, package.sub\n' + 'license = otherlic\n' + ) + + tmpdir.join('README').write('readme contents\nline2') + + meta_initial = { + # This will be used so `otherlic` won't replace it. + 'license': 'BSD 3-Clause License', + } + + with get_dist(tmpdir, meta_initial) as dist: + metadata = dist.metadata + + assert metadata.version == '10.1.1' + assert metadata.description == 'Some description' + assert metadata.long_description == 'readme contents\nline2' + assert metadata.provides == ['package', 'package.sub'] + assert metadata.license == 'BSD 3-Clause License' + assert metadata.name == 'fake_name' + assert metadata.keywords == ['one', 'two'] + + def test_version(self, tmpdir): + + fake_env( + tmpdir, + '[metadata]\n' + 'version = attr: fake_package.VERSION\n' + ) + with get_dist(tmpdir) as dist: + assert dist.metadata.version == '1.2.3' + + tmpdir.join('setup.cfg').write( + '[metadata]\n' + 'version = attr: fake_package.get_version\n' + ) + with get_dist(tmpdir) as dist: + assert dist.metadata.version == '3.4.5.dev' + + tmpdir.join('setup.cfg').write( + '[metadata]\n' + 'version = attr: fake_package.VERSION_MAJOR\n' + ) + with get_dist(tmpdir) as dist: + assert dist.metadata.version == '1' + + subpack = tmpdir.join('fake_package').mkdir('subpackage') + subpack.join('__init__.py').write('') + subpack.join('submodule.py').write('VERSION = (2016, 11, 26)') + + tmpdir.join('setup.cfg').write( + '[metadata]\n' + 'version = attr: fake_package.subpackage.submodule.VERSION\n' + ) + with get_dist(tmpdir) as dist: + assert dist.metadata.version == '2016.11.26' + + def test_unknown_meta_item(self, tmpdir): + + fake_env( + tmpdir, + '[metadata]\n' + 'name = fake_name\n' + 'unknown = some\n' + ) + with get_dist(tmpdir, parse=False) as dist: + with pytest.raises(DistutilsOptionError): + dist.parse_config_files() + + def test_usupported_section(self, tmpdir): + + fake_env( + tmpdir, + '[metadata:some]\n' + 'key = val\n' + ) + with get_dist(tmpdir, parse=False) as dist: + with pytest.raises(DistutilsOptionError): + dist.parse_config_files() + + def test_classifiers(self, tmpdir): + expected = { + 'Framework :: Django', + 'Programming Language :: Python :: 3.5', + } + + # From file. + fake_env( + tmpdir, + '[metadata]\n' + 'classifiers = file: classifiers\n' + ) + + tmpdir.join('classifiers').write( + 'Framework :: Django\n' + 'Programming Language :: Python :: 3.5\n' + ) + + with get_dist(tmpdir) as dist: + assert set(dist.metadata.classifiers) == expected + + # From section. + tmpdir.join('setup.cfg').write( + '[metadata:classifiers]\n' + 'Framework :: Django\n' + 'Programming Language :: Python :: 3.5\n' + ) + + with get_dist(tmpdir) as dist: + assert set(dist.metadata.classifiers) == expected + + +class TestOptions: + + def test_basic(self, tmpdir): + + fake_env( + tmpdir, + '[options]\n' + 'zip_safe = True\n' + 'use_2to3 = 1\n' + 'include_package_data = yes\n' + 'package_dir = b=c, =src\n' + 'packages = pack_a, pack_b.subpack\n' + 'namespace_packages = pack1, pack2\n' + 'use_2to3_fixers = your.fixers, or.here\n' + 'use_2to3_exclude_fixers = one.here, two.there\n' + 'convert_2to3_doctests = src/tests/one.txt, src/two.txt\n' + 'scripts = bin/one.py, bin/two.py\n' + 'eager_resources = bin/one.py, bin/two.py\n' + 'install_requires = docutils>=0.3; pack ==1.1, ==1.3; hey\n' + 'tests_require = mock==0.7.2; pytest\n' + 'setup_requires = docutils>=0.3; spack ==1.1, ==1.3; there\n' + 'dependency_links = http://some.com/here/1, ' + 'http://some.com/there/2\n' + ) + with get_dist(tmpdir) as dist: + assert dist.zip_safe + assert dist.use_2to3 + assert dist.include_package_data + assert dist.package_dir == {'': 'src', 'b': 'c'} + assert set(dist.packages) == {'pack_a', 'pack_b.subpack'} + assert set(dist.namespace_packages) == {'pack1', 'pack2'} + assert set(dist.use_2to3_fixers) == {'your.fixers', 'or.here'} + assert set(dist.use_2to3_exclude_fixers) == { + 'one.here', 'two.there'} + assert set(dist.convert_2to3_doctests) == { + 'src/tests/one.txt', 'src/two.txt'} + assert set(dist.scripts) == {'bin/one.py', 'bin/two.py'} + assert set(dist.dependency_links) == { + 'http://some.com/here/1', + 'http://some.com/there/2' + } + assert set(dist.install_requires) == { + 'docutils>=0.3', + 'pack ==1.1, ==1.3', + 'hey' + } + assert set(dist.setup_requires) == { + 'docutils>=0.3', + 'spack ==1.1, ==1.3', + 'there' + } + assert set(dist.tests_require) == { + 'mock==0.7.2', + 'pytest' + } + + def test_package_dir_fail(self, tmpdir): + fake_env( + tmpdir, + '[options]\n' + 'package_dir = a b\n' + ) + with get_dist(tmpdir, parse=False) as dist: + with pytest.raises(DistutilsOptionError): + dist.parse_config_files() + + def test_package_data(self, tmpdir): + fake_env( + tmpdir, + '[options:package_data]\n' + '* = *.txt, *.rst\n' + 'hello = *.msg\n' + '\n' + '[options:exclude_package_data]\n' + '* = fake1.txt, fake2.txt\n' + 'hello = *.dat\n' + ) + + with get_dist(tmpdir) as dist: + assert dist.package_data == { + '': ['*.txt', '*.rst'], + 'hello': ['*.msg'], + } + assert dist.exclude_package_data == { + '': ['fake1.txt', 'fake2.txt'], + 'hello': ['*.dat'], + } + + def test_packages(self, tmpdir): + fake_env( + tmpdir, + '[options]\n' + 'packages = find:\n' + ) + + with get_dist(tmpdir) as dist: + assert dist.packages == ['fake_package'] + + def test_extras_require(self, tmpdir): + fake_env( + tmpdir, + '[options:extras_require]\n' + 'pdf = ReportLab>=1.2; RXP\n' + 'rest = docutils>=0.3; pack ==1.1, ==1.3\n' + ) + + with get_dist(tmpdir) as dist: + assert dist.extras_require == { + 'pdf': ['ReportLab>=1.2', 'RXP'], + 'rest': ['docutils>=0.3', 'pack ==1.1, ==1.3'] + } + + def test_entry_points(self, tmpdir): + fake_env( + tmpdir, + '[options:entry_points]\n' + 'group1 = point1 = pack.module:func, ' + '.point2 = pack.module2:func_rest [rest]\n' + 'group2 = point3 = pack.module:func2\n' + ) + + with get_dist(tmpdir) as dist: + assert dist.entry_points == { + 'group1': [ + 'point1 = pack.module:func', + '.point2 = pack.module2:func_rest [rest]', + ], + 'group2': ['point3 = pack.module:func2'] + } + + expected = ( + '[blogtool.parsers]\n' + '.rst = some.nested.module:SomeClass.some_classmethod[reST]\n' + ) + + tmpdir.join('entry_points').write(expected) + + # From file. + tmpdir.join('setup.cfg').write( + '[options]\n' + 'entry_points = file: entry_points\n' + ) + + with get_dist(tmpdir) as dist: + assert dist.entry_points == expected + + def test_dependency_links(self, tmpdir): + expected = { + 'http://some.com/here/1', + 'http://some.com/there/2' + } + # From section. + fake_env( + tmpdir, + '[options:dependency_links]\n' + '1 = http://some.com/here/1\n' + '2 = http://some.com/there/2\n' + ) + + with get_dist(tmpdir) as dist: + assert set(dist.dependency_links) == expected -- cgit v1.2.3 From 280d8e98ba0c3c4e37e38dff79aaf6e9efaf4175 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 26 Nov 2016 22:21:38 +0700 Subject: Tests for config module 2.6 compatible. --- setuptools/tests/test_config.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'setuptools') diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 0ef7a994..d044cbac 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -145,10 +145,10 @@ class TestMetadata: dist.parse_config_files() def test_classifiers(self, tmpdir): - expected = { + expected = set([ 'Framework :: Django', 'Programming Language :: Python :: 3.5', - } + ]) # From file. fake_env( @@ -205,32 +205,32 @@ class TestOptions: assert dist.use_2to3 assert dist.include_package_data assert dist.package_dir == {'': 'src', 'b': 'c'} - assert set(dist.packages) == {'pack_a', 'pack_b.subpack'} - assert set(dist.namespace_packages) == {'pack1', 'pack2'} - assert set(dist.use_2to3_fixers) == {'your.fixers', 'or.here'} - assert set(dist.use_2to3_exclude_fixers) == { - 'one.here', 'two.there'} - assert set(dist.convert_2to3_doctests) == { - 'src/tests/one.txt', 'src/two.txt'} - assert set(dist.scripts) == {'bin/one.py', 'bin/two.py'} - assert set(dist.dependency_links) == { + assert set(dist.packages) == set(['pack_a', 'pack_b.subpack']) + assert set(dist.namespace_packages) == set(['pack1', 'pack2']) + assert set(dist.use_2to3_fixers) == set(['your.fixers', 'or.here']) + assert set(dist.use_2to3_exclude_fixers) == set([ + 'one.here', 'two.there']) + assert set(dist.convert_2to3_doctests) == set([ + 'src/tests/one.txt', 'src/two.txt']) + assert set(dist.scripts) == set(['bin/one.py', 'bin/two.py']) + assert set(dist.dependency_links) == set([ 'http://some.com/here/1', 'http://some.com/there/2' - } - assert set(dist.install_requires) == { + ]) + assert set(dist.install_requires) == set([ 'docutils>=0.3', 'pack ==1.1, ==1.3', 'hey' - } - assert set(dist.setup_requires) == { + ]) + assert set(dist.setup_requires) == set([ 'docutils>=0.3', 'spack ==1.1, ==1.3', 'there' - } - assert set(dist.tests_require) == { + ]) + assert set(dist.tests_require) == set([ 'mock==0.7.2', 'pytest' - } + ]) def test_package_dir_fail(self, tmpdir): fake_env( @@ -323,10 +323,10 @@ class TestOptions: assert dist.entry_points == expected def test_dependency_links(self, tmpdir): - expected = { + expected = set([ 'http://some.com/here/1', 'http://some.com/there/2' - } + ]) # From section. fake_env( tmpdir, -- cgit v1.2.3 From 810eb439a629e1b2bc2d078f138126356e95a9bc Mon Sep 17 00:00:00 2001 From: idle sign Date: Sun, 4 Dec 2016 10:41:54 +0700 Subject: Added ConfigHandler.strict_mode. --- setuptools/config.py | 21 +++++++++++++++++++-- setuptools/tests/test_config.py | 15 +++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index 94b2ab17..3546ace9 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -12,6 +12,16 @@ class ConfigHandler(object): """Handles metadata supplied in configuration files.""" section_prefix = None + """Prefix for config sections handled by this handler. + Must be provided by class heirs. + + """ + + strict_mode = True + """Flag. Whether unknown options in config should + raise DistutilsOptionError exception, or pass silently. + + """ def __init__(self, target_obj, options): sections = {} @@ -174,9 +184,11 @@ class ConfigHandler(object): for (name, (_, value)) in section_options.items(): try: self[name] = value + except KeyError: - raise DistutilsOptionError( - 'Unknown distribution option: %s' % name) + if self.strict_mode: + raise DistutilsOptionError( + 'Unknown distribution option: %s' % name) def parse(self): """Parses configuration file items from one @@ -203,6 +215,11 @@ class ConfigHandler(object): class ConfigMetadataHandler(ConfigHandler): section_prefix = 'metadata' + strict_mode = False + """We need to keep it loose, to be compatible with `pbr` package + which also uses `metadata` section. + + """ @property def parsers(self): diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index d044cbac..f1b1aa3f 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -130,8 +130,7 @@ class TestMetadata: 'unknown = some\n' ) with get_dist(tmpdir, parse=False) as dist: - with pytest.raises(DistutilsOptionError): - dist.parse_config_files() + dist.parse_config_files() # Skip unknown. def test_usupported_section(self, tmpdir): @@ -274,6 +273,18 @@ class TestOptions: with get_dist(tmpdir) as dist: assert dist.packages == ['fake_package'] + def test_unknown_options_item(self, tmpdir): + + fake_env( + tmpdir, + '[options]\n' + 'zip_safe = True\n' + 'usr_2to3 = 1\n' + ) + with get_dist(tmpdir, parse=False) as dist: + with pytest.raises(DistutilsOptionError): + dist.parse_config_files() + def test_extras_require(self, tmpdir): fake_env( tmpdir, -- cgit v1.2.3 From a5567b762cfe48a8e5a4aada5a997e5fd8072420 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sun, 4 Dec 2016 11:05:11 +0700 Subject: Implemented proper dangling option values support. --- setuptools/config.py | 4 +- setuptools/tests/test_config.py | 111 +++++++++++++++++++++++++++++++++++----- 2 files changed, 100 insertions(+), 15 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index 3546ace9..ef416995 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -70,7 +70,7 @@ class ConfigHandler(object): def _parse_list(cls, value, separator=','): """Represents value as a list. - Value is split either by comma or by lines. + Value is split either by separator (defaults to comma) or by lines. :param value: :param separator: List items separator character. @@ -84,7 +84,7 @@ class ConfigHandler(object): else: value = value.split(separator) - return [chunk.strip() for chunk in value] + return [chunk.strip() for chunk in value if chunk.strip()] @classmethod def _parse_dict(cls, value): diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index f1b1aa3f..b4bd089c 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -86,6 +86,27 @@ class TestMetadata: assert metadata.name == 'fake_name' assert metadata.keywords == ['one', 'two'] + def test_multiline(self, tmpdir): + + fake_env( + tmpdir, + '[metadata]\n' + 'name = fake_name\n' + 'keywords =\n' + ' one\n' + ' two\n' + 'classifiers =\n' + ' Framework :: Django\n' + ' Programming Language :: Python :: 3.5\n' + ) + with get_dist(tmpdir) as dist: + metadata = dist.metadata + assert metadata.keywords == ['one', 'two'] + assert metadata.classifiers == [ + 'Framework :: Django', + 'Programming Language :: Python :: 3.5', + ] + def test_version(self, tmpdir): fake_env( @@ -204,32 +225,96 @@ class TestOptions: assert dist.use_2to3 assert dist.include_package_data assert dist.package_dir == {'': 'src', 'b': 'c'} - assert set(dist.packages) == set(['pack_a', 'pack_b.subpack']) - assert set(dist.namespace_packages) == set(['pack1', 'pack2']) - assert set(dist.use_2to3_fixers) == set(['your.fixers', 'or.here']) - assert set(dist.use_2to3_exclude_fixers) == set([ - 'one.here', 'two.there']) - assert set(dist.convert_2to3_doctests) == set([ + assert dist.packages == ['pack_a', 'pack_b.subpack'] + assert dist.namespace_packages == ['pack1', 'pack2'] + assert dist.use_2to3_fixers == ['your.fixers', 'or.here'] + assert dist.use_2to3_exclude_fixers == ['one.here', 'two.there'] + assert dist.convert_2to3_doctests == ([ 'src/tests/one.txt', 'src/two.txt']) - assert set(dist.scripts) == set(['bin/one.py', 'bin/two.py']) - assert set(dist.dependency_links) == set([ + assert dist.scripts == ['bin/one.py', 'bin/two.py'] + assert dist.dependency_links == ([ 'http://some.com/here/1', 'http://some.com/there/2' ]) - assert set(dist.install_requires) == set([ + assert dist.install_requires == ([ 'docutils>=0.3', 'pack ==1.1, ==1.3', 'hey' ]) - assert set(dist.setup_requires) == set([ + assert dist.setup_requires == ([ 'docutils>=0.3', 'spack ==1.1, ==1.3', 'there' ]) - assert set(dist.tests_require) == set([ - 'mock==0.7.2', - 'pytest' + assert dist.tests_require == ['mock==0.7.2', 'pytest'] + + def test_multiline(self, tmpdir): + fake_env( + tmpdir, + '[options]\n' + 'package_dir = \n' + ' b=c\n' + ' =src\n' + 'packages = \n' + ' pack_a\n' + ' pack_b.subpack\n' + 'namespace_packages = \n' + ' pack1\n' + ' pack2\n' + 'use_2to3_fixers = \n' + ' your.fixers\n' + ' or.here\n' + 'use_2to3_exclude_fixers = \n' + ' one.here\n' + ' two.there\n' + 'convert_2to3_doctests = \n' + ' src/tests/one.txt\n' + ' src/two.txt\n' + 'scripts = \n' + ' bin/one.py\n' + ' bin/two.py\n' + 'eager_resources = \n' + ' bin/one.py\n' + ' bin/two.py\n' + 'install_requires = \n' + ' docutils>=0.3\n' + ' pack ==1.1, ==1.3\n' + ' hey\n' + 'tests_require = \n' + ' mock==0.7.2\n' + ' pytest\n' + 'setup_requires = \n' + ' docutils>=0.3\n' + ' spack ==1.1, ==1.3\n' + ' there\n' + 'dependency_links = \n' + ' http://some.com/here/1\n' + ' http://some.com/there/2\n' + ) + with get_dist(tmpdir) as dist: + assert dist.package_dir == {'': 'src', 'b': 'c'} + assert dist.packages == ['pack_a', 'pack_b.subpack'] + assert dist.namespace_packages == ['pack1', 'pack2'] + assert dist.use_2to3_fixers == ['your.fixers', 'or.here'] + assert dist.use_2to3_exclude_fixers == ['one.here', 'two.there'] + assert dist.convert_2to3_doctests == ( + ['src/tests/one.txt', 'src/two.txt']) + assert dist.scripts == ['bin/one.py', 'bin/two.py'] + assert dist.dependency_links == ([ + 'http://some.com/here/1', + 'http://some.com/there/2' + ]) + assert dist.install_requires == ([ + 'docutils>=0.3', + 'pack ==1.1, ==1.3', + 'hey' + ]) + assert dist.setup_requires == ([ + 'docutils>=0.3', + 'spack ==1.1, ==1.3', + 'there' ]) + assert dist.tests_require == ['mock==0.7.2', 'pytest'] def test_package_dir_fail(self, tmpdir): fake_env( -- cgit v1.2.3 From 49fc619dd4bc059ae823054f586753ebf35edeee Mon Sep 17 00:00:00 2001 From: idle sign Date: Sun, 4 Dec 2016 11:25:48 +0700 Subject: `dependency_links` as section not supported. --- setuptools/config.py | 8 -------- setuptools/tests/test_config.py | 16 ---------------- 2 files changed, 24 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index ef416995..9319f78f 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -333,14 +333,6 @@ class ConfigOptionsHandler(ConfigHandler): from setuptools import find_packages return find_packages() - def parse_section_dependency_links(self, section_options): - """Parses `dependency_links` configuration file section. - - :param dict section_options: - """ - parsed = self._parse_section_to_dict(section_options) - self['dependency_links'] = list(parsed.values()) - def parse_section_entry_points(self, section_options): """Parses `entry_points` configuration file section. diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index b4bd089c..bfc863ec 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -417,19 +417,3 @@ class TestOptions: with get_dist(tmpdir) as dist: assert dist.entry_points == expected - - def test_dependency_links(self, tmpdir): - expected = set([ - 'http://some.com/here/1', - 'http://some.com/there/2' - ]) - # From section. - fake_env( - tmpdir, - '[options:dependency_links]\n' - '1 = http://some.com/here/1\n' - '2 = http://some.com/there/2\n' - ) - - with get_dist(tmpdir) as dist: - assert set(dist.dependency_links) == expected -- cgit v1.2.3 From 566e9aee17dbe2cec92b9d793f2466681f2b1a7f Mon Sep 17 00:00:00 2001 From: idle sign Date: Sun, 4 Dec 2016 11:27:42 +0700 Subject: Future package imported. --- setuptools/config.py | 1 + 1 file changed, 1 insertion(+) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index 9319f78f..5c73ca62 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import, unicode_literals import io import os import sys -- cgit v1.2.3 From 68c03bee07c55a9c337f1cb98fc102a3710add4b Mon Sep 17 00:00:00 2001 From: idle sign Date: Sun, 4 Dec 2016 16:22:07 +0700 Subject: Section names now dot-separated to mimic .toml table names. --- setuptools/config.py | 2 +- setuptools/tests/test_config.py | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index 5c73ca62..a04c3ce8 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -32,7 +32,7 @@ class ConfigHandler(object): if not section_name.startswith(section_prefix): continue - section_name = section_name.replace(section_prefix, '').strip(':') + section_name = section_name.replace(section_prefix, '').strip('.') sections[section_name] = section_options self.target_obj = target_obj diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index bfc863ec..e53b5ffd 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -157,7 +157,7 @@ class TestMetadata: fake_env( tmpdir, - '[metadata:some]\n' + '[metadata.some]\n' 'key = val\n' ) with get_dist(tmpdir, parse=False) as dist: @@ -187,7 +187,7 @@ class TestMetadata: # From section. tmpdir.join('setup.cfg').write( - '[metadata:classifiers]\n' + '[metadata.classifiers]\n' 'Framework :: Django\n' 'Programming Language :: Python :: 3.5\n' ) @@ -329,11 +329,11 @@ class TestOptions: def test_package_data(self, tmpdir): fake_env( tmpdir, - '[options:package_data]\n' + '[options.package_data]\n' '* = *.txt, *.rst\n' 'hello = *.msg\n' '\n' - '[options:exclude_package_data]\n' + '[options.exclude_package_data]\n' '* = fake1.txt, fake2.txt\n' 'hello = *.dat\n' ) @@ -373,9 +373,11 @@ class TestOptions: def test_extras_require(self, tmpdir): fake_env( tmpdir, - '[options:extras_require]\n' + '[options.extras_require]\n' 'pdf = ReportLab>=1.2; RXP\n' - 'rest = docutils>=0.3; pack ==1.1, ==1.3\n' + 'rest = \n' + ' docutils>=0.3\n' + ' pack ==1.1, ==1.3\n' ) with get_dist(tmpdir) as dist: @@ -387,7 +389,7 @@ class TestOptions: def test_entry_points(self, tmpdir): fake_env( tmpdir, - '[options:entry_points]\n' + '[options.entry_points]\n' 'group1 = point1 = pack.module:func, ' '.point2 = pack.module2:func_rest [rest]\n' 'group2 = point3 = pack.module:func2\n' -- cgit v1.2.3 From 21333fe86db1888dbee134043ea8a2f85b69d439 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sun, 4 Dec 2016 17:11:37 +0700 Subject: Added `metadata` section aliases. --- setuptools/config.py | 22 ++++++++++++++++++++-- setuptools/tests/test_config.py | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index a04c3ce8..0c88df79 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -18,6 +18,12 @@ class ConfigHandler(object): """ + aliases = {} + """Options aliases. + For compatibility with various packages. E.g.: d2to1 and pbr. + + """ + strict_mode = True """Flag. Whether unknown options in config should raise DistutilsOptionError exception, or pass silently. @@ -48,6 +54,9 @@ class ConfigHandler(object): unknown = tuple() target_obj = self.target_obj + # Translate alias into real name. + option_name = self.aliases.get(option_name, option_name) + current_value = getattr(target_obj, option_name, unknown) if current_value is unknown: @@ -216,9 +225,18 @@ class ConfigHandler(object): class ConfigMetadataHandler(ConfigHandler): section_prefix = 'metadata' + + aliases = { + 'author-email': 'author_email', + 'home_page': 'url', + 'summary': 'description', + 'classifier': 'classifiers', + 'platform': 'platforms', + } + strict_mode = False - """We need to keep it loose, to be compatible with `pbr` package - which also uses `metadata` section. + """We need to keep it loose, to be partially compatible with + `pbr` and `d2to1` packages which also uses `metadata` section. """ diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index e53b5ffd..3fabfb94 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -86,6 +86,31 @@ class TestMetadata: assert metadata.name == 'fake_name' assert metadata.keywords == ['one', 'two'] + def test_aliases(self, tmpdir): + + fake_env( + tmpdir, + '[metadata]\n' + 'author-email = test@test.com\n' + 'home_page = http://test.test.com/test/\n' + 'summary = Short summary\n' + 'platform = a, b\n' + 'classifier =\n' + ' Framework :: Django\n' + ' Programming Language :: Python :: 3.5\n' + ) + + with get_dist(tmpdir) as dist: + metadata = dist.metadata + assert metadata.author_email == 'test@test.com' + assert metadata.url == 'http://test.test.com/test/' + assert metadata.description == 'Short summary' + assert metadata.platforms == ['a', 'b'] + assert metadata.classifiers == [ + 'Framework :: Django', + 'Programming Language :: Python :: 3.5', + ] + def test_multiline(self, tmpdir): fake_env( -- cgit v1.2.3 From 8998172299cd562c937e83383e9fb666e6209b30 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sun, 4 Dec 2016 17:24:05 +0700 Subject: `metadata` aliases update. --- setuptools/config.py | 2 +- setuptools/tests/test_config.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index 0c88df79..c6b93c4e 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -21,6 +21,7 @@ class ConfigHandler(object): aliases = {} """Options aliases. For compatibility with various packages. E.g.: d2to1 and pbr. + Note: `-` in keys is replaced with `_` by config parser. """ @@ -227,7 +228,6 @@ class ConfigMetadataHandler(ConfigHandler): section_prefix = 'metadata' aliases = { - 'author-email': 'author_email', 'home_page': 'url', 'summary': 'description', 'classifier': 'classifiers', diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 3fabfb94..08c5bd19 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -92,7 +92,7 @@ class TestMetadata: tmpdir, '[metadata]\n' 'author-email = test@test.com\n' - 'home_page = http://test.test.com/test/\n' + 'home-page = http://test.test.com/test/\n' 'summary = Short summary\n' 'platform = a, b\n' 'classifier =\n' -- cgit v1.2.3 From a5dadcf0eea5bda6991a77546787d1e657ae0411 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sun, 4 Dec 2016 17:35:44 +0700 Subject: _parse_attr() factored out. --- setuptools/config.py | 63 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 24 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index c6b93c4e..d8513a72 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -152,6 +152,37 @@ class ConfigHandler(object): return value + @classmethod + def _parse_attr(cls, value): + """Represents value as a module attribute. + + Examples: + attr: package.attr + attr: package.module.attr + + :param str value: + :rtype: str + """ + attr_directive = 'attr:' + if not value.startswith(attr_directive): + return value + + attrs_path = value.replace(attr_directive, '').strip().split('.') + attr_name = attrs_path.pop() + + module_name = '.'.join(attrs_path) + module_name = module_name or '__init__' + + sys.path.insert(0, os.getcwd()) + try: + module = import_module(module_name) + value = getattr(module, attr_name) + + finally: + sys.path = sys.path[1:] + + return value + @classmethod def _get_parser_compound(cls, *parse_methods): """Returns parser function to represents value as a list. @@ -277,32 +308,16 @@ class ConfigMetadataHandler(ConfigHandler): :rtype: str """ - attr_directive = 'attr:' - if not value.startswith(attr_directive): - return value + version = self._parse_attr(value) - attrs_path = value.replace(attr_directive, '').strip().split('.') - attr_name = attrs_path.pop() + if callable(version): + version = version() - module_name = '.'.join(attrs_path) - module_name = module_name or '__init__' - - sys.path.insert(0, os.getcwd()) - try: - module = import_module(module_name) - version = getattr(module, attr_name) - - if callable(version): - version = version() - - if not isinstance(version, string_types): - if hasattr(version, '__iter__'): - version = '.'.join(map(str, version)) - else: - version = '%s' % version - - finally: - sys.path = sys.path[1:] + if not isinstance(version, string_types): + if hasattr(version, '__iter__'): + version = '.'.join(map(str, version)) + else: + version = '%s' % version return version -- cgit v1.2.3 From af321fc6ad82c54a78e7c1a74601e0a6b34997da Mon Sep 17 00:00:00 2001 From: idle sign Date: Mon, 5 Dec 2016 21:55:48 +0700 Subject: `file:` directive sandboxed. --- setuptools/config.py | 12 +++++++++++- setuptools/tests/test_config.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index d8513a72..c2319ed5 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -128,7 +128,10 @@ class ConfigHandler(object): @classmethod def _parse_file(cls, value): """Represents value as a string, allowing including text - from nearest files using include(). + from nearest files using `file:` directive. + + Directive is sandboxed and won't reach anything outside + directory with setup.py. Examples: include: LICENSE @@ -144,7 +147,14 @@ class ConfigHandler(object): if not value.startswith(include_directive): return value + current_directory = os.getcwd() + filepath = value.replace(include_directive, '').strip() + filepath = os.path.abspath(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: diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 08c5bd19..9fb55b06 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -86,6 +86,18 @@ class TestMetadata: assert metadata.name == 'fake_name' assert metadata.keywords == ['one', 'two'] + def test_file_sandboxed(self, tmpdir): + + fake_env( + tmpdir, + '[metadata]\n' + 'long_description = file: ../../README\n' + ) + + with get_dist(tmpdir, parse=False) as dist: + with pytest.raises(DistutilsOptionError): + dist.parse_config_files() # file: out of sandbox + def test_aliases(self, tmpdir): fake_env( -- cgit v1.2.3 From acaece809ee3592c0d135a9a0a8e556db0a9e587 Mon Sep 17 00:00:00 2001 From: idle sign Date: Mon, 5 Dec 2016 22:07:16 +0700 Subject: Tests and docstrings update. --- setuptools/config.py | 6 +++--- setuptools/tests/test_config.py | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index c2319ed5..2dd42893 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -87,7 +87,7 @@ class ConfigHandler(object): :param separator: List items separator character. :rtype: list """ - if isinstance(value, list): # _parse_complex case + if isinstance(value, list): # _get_parser_compound case return value if '\n' in value: @@ -250,7 +250,7 @@ class ConfigHandler(object): for section_name, section_options in self.sections.items(): method_postfix = '' - if section_name: # [section:option] variant + if section_name: # [section.option] variant method_postfix = '_%s' % section_name section_parser_method = getattr( @@ -258,7 +258,7 @@ class ConfigHandler(object): if section_parser_method is None: raise DistutilsOptionError( - 'Unsupported distribution option section: [%s:%s]' % ( + 'Unsupported distribution option section: [%s.%s]' % ( self.section_prefix, section_name)) section_parser_method(section_options) diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 9fb55b06..259a396a 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -66,6 +66,8 @@ class TestMetadata: 'keywords = one, two\n' 'provides = package, package.sub\n' 'license = otherlic\n' + 'download_url = http://test.test.com/test/\n' + 'maintainer_email = test@test.com\n' ) tmpdir.join('README').write('readme contents\nline2') @@ -85,6 +87,8 @@ class TestMetadata: assert metadata.license == 'BSD 3-Clause License' assert metadata.name == 'fake_name' assert metadata.keywords == ['one', 'two'] + assert metadata.download_url == 'http://test.test.com/test/' + assert metadata.maintainer_email == 'test@test.com' def test_file_sandboxed(self, tmpdir): -- cgit v1.2.3 From 163f36449c2b8c19c272414bff0bf80c9f3f2c7d Mon Sep 17 00:00:00 2001 From: idle sign Date: Mon, 5 Dec 2016 23:13:35 +0700 Subject: Added API functions. --- setuptools/config.py | 78 +++++++++++++++++++++++++++++++++++++++++ setuptools/dist.py | 5 ++- setuptools/tests/test_config.py | 20 ++++++++++- 3 files changed, 99 insertions(+), 4 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index 2dd42893..6459e1de 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals import io import os import sys +from collections import defaultdict from functools import partial from distutils.errors import DistutilsOptionError @@ -9,6 +10,80 @@ from setuptools.py26compat import import_module from setuptools.extern.six import string_types +def read_configuration(filepath, find_others=False): + """Read given configuration file and returns options from it as a dict. + + :param str|unicode filepath: Path to configuration file + to get options from. + + :param bool find_others: Whether to search for other configuration files + which could be on in various places. + + :rtype: dict + """ + from setuptools.dist import Distribution, _Distribution + + dist = Distribution() + + filenames = dist.find_config_files() if find_others else [] + if filepath not in filenames: + filenames.append(filepath) + + _Distribution.parse_config_files(dist, filenames=filenames) + + handlers = parse_configuration(dist, dist.command_options) + + return configuration_to_dict(handlers) + + +def configuration_to_dict(handlers): + """Returns configuration data gathered by given handlers as a dict. + + :param list[ConfigHandler] handlers: Handlers list, + usually from parse_configuration() + + :rtype: dict + """ + config_dict = defaultdict(dict) + + for handler in handlers: + + obj_alias = handler.section_prefix + target_obj = handler.target_obj + + for option in handler.set_options: + getter = getattr(target_obj, 'get_%s' % option, None) + + if getter is None: + value = getattr(target_obj, option) + + else: + value = getter() + + config_dict[obj_alias][option] = value + + return config_dict + + +def parse_configuration(distribution, command_options): + """Performs additional parsing of configuration options + for a distribution. + + Returns a list of used option handlers. + + :param Distribution distribution: + :param dict command_options: + :rtype: list + """ + meta = ConfigMetadataHandler(distribution.metadata, command_options) + meta.parse() + + options = ConfigOptionsHandler(distribution, command_options) + options.parse() + + return [meta, options] + + class ConfigHandler(object): """Handles metadata supplied in configuration files.""" @@ -44,6 +119,7 @@ class ConfigHandler(object): self.target_obj = target_obj self.sections = sections + self.set_options = [] @property def parsers(self): @@ -77,6 +153,8 @@ class ConfigHandler(object): else: setter(value) + self.set_options.append(option_name) + @classmethod def _parse_list(cls, value, separator=','): """Represents value as a list. diff --git a/setuptools/dist.py b/setuptools/dist.py index c975abe0..c04e6426 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -19,7 +19,7 @@ from pkg_resources.extern import packaging from setuptools.depends import Require from setuptools import windows_support from setuptools.monkey import get_unpatched -from setuptools.config import ConfigMetadataHandler, ConfigOptionsHandler +from setuptools.config import parse_configuration import pkg_resources @@ -350,8 +350,7 @@ class Distribution(_Distribution): """ _Distribution.parse_config_files(self, filenames=filenames) - ConfigMetadataHandler(self.metadata, self.command_options).parse() - ConfigOptionsHandler(self, self.command_options).parse() + parse_configuration(self, self.command_options) def parse_command_line(self): """Process features after parsing command line options""" diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 259a396a..cd646dba 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -2,7 +2,7 @@ import contextlib import pytest from distutils.errors import DistutilsOptionError from setuptools.dist import Distribution -from setuptools.config import ConfigHandler +from setuptools.config import ConfigHandler, read_configuration class ErrConfigHandler(ConfigHandler): @@ -52,6 +52,24 @@ def test_parsers_implemented(): handler.parsers +class TestConfigurationReader: + + def test_basic(self, tmpdir): + fake_env( + tmpdir, + '[metadata]\n' + 'version = 10.1.1\n' + 'keywords = one, two\n' + '\n' + '[options]\n' + 'scripts = bin/a.py, bin/b.py\n' + ) + config_dict = read_configuration('%s' % tmpdir.join('setup.cfg')) + assert config_dict['metadata']['version'] == '10.1.1' + assert config_dict['metadata']['keywords'] == ['one', 'two'] + assert config_dict['options']['scripts'] == ['bin/a.py', 'bin/b.py'] + + class TestMetadata: def test_basic(self, tmpdir): -- cgit v1.2.3 From 6aae9fb3f2bf222466fc2fd0db5e22760c6239c6 Mon Sep 17 00:00:00 2001 From: idle sign Date: Mon, 5 Dec 2016 23:15:36 +0700 Subject: `strict_mode` removed to improve forward compatibility. --- setuptools/config.py | 10 +--------- setuptools/tests/test_config.py | 12 ------------ 2 files changed, 1 insertion(+), 21 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index 6459e1de..b2c0cea3 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -100,12 +100,6 @@ class ConfigHandler(object): """ - strict_mode = True - """Flag. Whether unknown options in config should - raise DistutilsOptionError exception, or pass silently. - - """ - def __init__(self, target_obj, options): sections = {} @@ -316,9 +310,7 @@ class ConfigHandler(object): self[name] = value except KeyError: - if self.strict_mode: - raise DistutilsOptionError( - 'Unknown distribution option: %s' % name) + pass # Keep silent for a new option may appear anytime. def parse(self): """Parses configuration file items from one diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index cd646dba..2e8510be 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -417,18 +417,6 @@ class TestOptions: with get_dist(tmpdir) as dist: assert dist.packages == ['fake_package'] - def test_unknown_options_item(self, tmpdir): - - fake_env( - tmpdir, - '[options]\n' - 'zip_safe = True\n' - 'usr_2to3 = 1\n' - ) - with get_dist(tmpdir, parse=False) as dist: - with pytest.raises(DistutilsOptionError): - dist.parse_config_files() - def test_extras_require(self, tmpdir): fake_env( tmpdir, -- cgit v1.2.3 From 1ca6f3bf272d8ba2c0d4161cc56a74c63c8afb82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 7 Dec 2016 14:59:34 +0200 Subject: Spelling fixes --- setuptools/command/build_py.py | 2 +- setuptools/tests/environment.py | 2 +- setuptools/tests/test_egg_info.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'setuptools') diff --git a/setuptools/command/build_py.py b/setuptools/command/build_py.py index 289e6fb8..b0314fd4 100644 --- a/setuptools/command/build_py.py +++ b/setuptools/command/build_py.py @@ -219,7 +219,7 @@ class build_py(orig.build_py, Mixin2to3): @staticmethod def _get_platform_patterns(spec, package, src_dir): """ - yield platfrom-specific path patterns (suitable for glob + yield platform-specific path patterns (suitable for glob or fn_match) from a glob-based spec (such as self.package_data or self.exclude_package_data) matching package in src_dir. diff --git a/setuptools/tests/environment.py b/setuptools/tests/environment.py index b0e3bd36..c67898ca 100644 --- a/setuptools/tests/environment.py +++ b/setuptools/tests/environment.py @@ -56,5 +56,5 @@ def run_setup_py(cmd, pypath=None, path=None, data = data.decode() data = unicodedata.normalize('NFC', data) - # communciate calls wait() + # communicate calls wait() return proc.returncode, data diff --git a/setuptools/tests/test_egg_info.py b/setuptools/tests/test_egg_info.py index dc41bc1f..75ae18df 100644 --- a/setuptools/tests/test_egg_info.py +++ b/setuptools/tests/test_egg_info.py @@ -237,7 +237,7 @@ class TestEggInfo(object): pkginfo = os.path.join(egg_info_dir, 'PKG-INFO') assert 'Requires-Python: >=1.2.3' in open(pkginfo).read().split('\n') - def test_manifest_maker_warning_suppresion(self): + def test_manifest_maker_warning_suppression(self): fixtures = [ "standard file not found: should have one of foo.py, bar.py", "standard file 'setup.py' not found" -- cgit v1.2.3 From a9350f32d3eeef3a1c53b243e763e60e211b72f6 Mon Sep 17 00:00:00 2001 From: idle sign Date: Wed, 7 Dec 2016 20:21:31 +0700 Subject: `read_configuration` now chdirs and tests for file. --- setuptools/config.py | 13 ++++++++++++- setuptools/tests/test_config.py | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'setuptools') diff --git a/setuptools/config.py b/setuptools/config.py index b2c0cea3..889dc683 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -5,7 +5,7 @@ import sys from collections import defaultdict from functools import partial -from distutils.errors import DistutilsOptionError +from distutils.errors import DistutilsOptionError, DistutilsFileError from setuptools.py26compat import import_module from setuptools.extern.six import string_types @@ -23,6 +23,15 @@ def read_configuration(filepath, find_others=False): """ from setuptools.dist import Distribution, _Distribution + filepath = os.path.abspath(filepath) + + if not os.path.isfile(filepath): + raise DistutilsFileError( + 'Configuration file %s does not exist.' % filepath) + + current_directory = os.getcwd() + os.chdir(os.path.dirname(filepath)) + dist = Distribution() filenames = dist.find_config_files() if find_others else [] @@ -33,6 +42,8 @@ def read_configuration(filepath, find_others=False): handlers = parse_configuration(dist, dist.command_options) + os.chdir(current_directory) + return configuration_to_dict(handlers) diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 2e8510be..21487720 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -1,6 +1,6 @@ import contextlib import pytest -from distutils.errors import DistutilsOptionError +from distutils.errors import DistutilsOptionError, DistutilsFileError from setuptools.dist import Distribution from setuptools.config import ConfigHandler, read_configuration @@ -69,6 +69,10 @@ class TestConfigurationReader: assert config_dict['metadata']['keywords'] == ['one', 'two'] assert config_dict['options']['scripts'] == ['bin/a.py', 'bin/b.py'] + def test_no_config(self, tmpdir): + with pytest.raises(DistutilsFileError): + read_configuration('%s' % tmpdir.join('setup.cfg')) + class TestMetadata: -- cgit v1.2.3 From 53b47e1dfa9dfb1e8b94172a4650409fc03d4048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 7 Dec 2016 15:00:43 +0200 Subject: Tell unittest.main not to exit, fixes #850. --- setuptools/command/test.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'setuptools') diff --git a/setuptools/command/test.py b/setuptools/command/test.py index 270674e2..9a5117be 100644 --- a/setuptools/command/test.py +++ b/setuptools/command/test.py @@ -225,10 +225,12 @@ class test(Command): del_modules.append(name) list(map(sys.modules.__delitem__, del_modules)) + exit_kwarg = {} if sys.version_info < (2, 7) else {"exit": False} unittest_main( None, None, self._argv, testLoader=self._resolve_as_ep(self.test_loader), testRunner=self._resolve_as_ep(self.test_runner), + **exit_kwarg ) @property -- cgit v1.2.3 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 +++++++++++++++++++++++++++++++++------- setuptools/tests/test_config.py | 16 ++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) (limited to 'setuptools') 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: diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 21487720..aaf78aef 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -73,6 +73,22 @@ class TestConfigurationReader: with pytest.raises(DistutilsFileError): read_configuration('%s' % tmpdir.join('setup.cfg')) + def test_ignore_errors(self, tmpdir): + fake_env( + tmpdir, + '[metadata]\n' + 'version = attr: none.VERSION\n' + 'keywords = one, two\n' + ) + with pytest.raises(ImportError): + read_configuration('%s' % tmpdir.join('setup.cfg')) + + config_dict = read_configuration( + '%s' % tmpdir.join('setup.cfg'), ignore_option_errors=True) + + assert config_dict['metadata']['keywords'] == ['one', 'two'] + assert 'version' not in config_dict['metadata'] + class TestMetadata: -- cgit v1.2.3 From b73891f82d5f1a353a2ad0090b1f5edece921508 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 10 Dec 2016 12:30:24 +0700 Subject: config tests refactored. --- setuptools/tests/test_config.py | 43 +++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'setuptools') diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index aaf78aef..35bdbad1 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -9,6 +9,13 @@ class ErrConfigHandler(ConfigHandler): """Erroneous handler. Fails to implement required methods.""" +def make_package_dir(name, base_dir): + dir_package = base_dir.mkdir(name) + init_file = dir_package.join('__init__.py') + init_file.write('') + return dir_package, init_file + + def fake_env(tmpdir, setup_cfg, setup_py=None): if setup_py is None: @@ -18,11 +25,12 @@ def fake_env(tmpdir, setup_cfg, setup_py=None): ) tmpdir.join('setup.py').write(setup_py) - tmpdir.join('setup.cfg').write(setup_cfg) + config = tmpdir.join('setup.cfg') + config.write(setup_cfg) + + package_dir, init_file = make_package_dir('fake_package', tmpdir) - package_name = 'fake_package' - dir_package = tmpdir.mkdir(package_name) - dir_package.join('__init__.py').write( + init_file.write( 'VERSION = (1, 2, 3)\n' '\n' 'VERSION_MAJOR = 1' @@ -31,6 +39,7 @@ def fake_env(tmpdir, setup_cfg, setup_py=None): ' return [3, 4, 5, "dev"]\n' '\n' ) + return package_dir, config @contextlib.contextmanager @@ -55,7 +64,7 @@ def test_parsers_implemented(): class TestConfigurationReader: def test_basic(self, tmpdir): - fake_env( + _, config = fake_env( tmpdir, '[metadata]\n' 'version = 10.1.1\n' @@ -64,7 +73,7 @@ class TestConfigurationReader: '[options]\n' 'scripts = bin/a.py, bin/b.py\n' ) - config_dict = read_configuration('%s' % tmpdir.join('setup.cfg')) + config_dict = read_configuration('%s' % config) assert config_dict['metadata']['version'] == '10.1.1' assert config_dict['metadata']['keywords'] == ['one', 'two'] assert config_dict['options']['scripts'] == ['bin/a.py', 'bin/b.py'] @@ -74,17 +83,17 @@ class TestConfigurationReader: read_configuration('%s' % tmpdir.join('setup.cfg')) def test_ignore_errors(self, tmpdir): - fake_env( + _, config = fake_env( tmpdir, '[metadata]\n' 'version = attr: none.VERSION\n' 'keywords = one, two\n' ) with pytest.raises(ImportError): - read_configuration('%s' % tmpdir.join('setup.cfg')) + read_configuration('%s' % config) config_dict = read_configuration( - '%s' % tmpdir.join('setup.cfg'), ignore_option_errors=True) + '%s' % config, ignore_option_errors=True) assert config_dict['metadata']['keywords'] == ['one', 'two'] assert 'version' not in config_dict['metadata'] @@ -188,7 +197,7 @@ class TestMetadata: def test_version(self, tmpdir): - fake_env( + _, config = fake_env( tmpdir, '[metadata]\n' 'version = attr: fake_package.VERSION\n' @@ -196,14 +205,14 @@ class TestMetadata: with get_dist(tmpdir) as dist: assert dist.metadata.version == '1.2.3' - tmpdir.join('setup.cfg').write( + config.write( '[metadata]\n' 'version = attr: fake_package.get_version\n' ) with get_dist(tmpdir) as dist: assert dist.metadata.version == '3.4.5.dev' - tmpdir.join('setup.cfg').write( + config.write( '[metadata]\n' 'version = attr: fake_package.VERSION_MAJOR\n' ) @@ -214,7 +223,7 @@ class TestMetadata: subpack.join('__init__.py').write('') subpack.join('submodule.py').write('VERSION = (2016, 11, 26)') - tmpdir.join('setup.cfg').write( + config.write( '[metadata]\n' 'version = attr: fake_package.subpackage.submodule.VERSION\n' ) @@ -250,7 +259,7 @@ class TestMetadata: ]) # From file. - fake_env( + _, config = fake_env( tmpdir, '[metadata]\n' 'classifiers = file: classifiers\n' @@ -265,7 +274,7 @@ class TestMetadata: assert set(dist.metadata.classifiers) == expected # From section. - tmpdir.join('setup.cfg').write( + config.write( '[metadata.classifiers]\n' 'Framework :: Django\n' 'Programming Language :: Python :: 3.5\n' @@ -454,7 +463,7 @@ class TestOptions: } def test_entry_points(self, tmpdir): - fake_env( + _, config = fake_env( tmpdir, '[options.entry_points]\n' 'group1 = point1 = pack.module:func, ' @@ -479,7 +488,7 @@ class TestOptions: tmpdir.join('entry_points').write(expected) # From file. - tmpdir.join('setup.cfg').write( + config.write( '[options]\n' 'entry_points = file: entry_points\n' ) -- 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 +++++++++++++++++++++++++++++++-- setuptools/tests/test_config.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) (limited to 'setuptools') 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. diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 35bdbad1..08e398b3 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -446,6 +446,44 @@ class TestOptions: with get_dist(tmpdir) as dist: assert dist.packages == ['fake_package'] + def test_find_directive(self, tmpdir): + dir_package, config = fake_env( + tmpdir, + '[options]\n' + 'packages = find:\n' + ) + + dir_sub_one, _ = make_package_dir('sub_one', dir_package) + dir_sub_two, _ = make_package_dir('sub_two', dir_package) + + with get_dist(tmpdir) as dist: + assert dist.packages == [ + 'fake_package', 'fake_package.sub_two', 'fake_package.sub_one'] + + config.write( + '[options]\n' + 'packages = find:\n' + '\n' + '[options.packages.find]\n' + 'where = .\n' + 'include =\n' + ' fake_package.sub_one\n' + ' two\n' + ) + with get_dist(tmpdir) as dist: + assert dist.packages == ['fake_package.sub_one'] + + config.write( + '[options]\n' + 'packages = find:\n' + '\n' + '[options.packages.find]\n' + 'exclude =\n' + ' fake_package.sub_one\n' + ) + with get_dist(tmpdir) as dist: + assert dist.packages == ['fake_package', 'fake_package.sub_two'] + def test_extras_require(self, tmpdir): fake_env( tmpdir, -- cgit v1.2.3 From f85a821b039a03eb0231e6bd0fc925a4e37f3911 Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 10 Dec 2016 13:48:03 +0700 Subject: Fixed test for `find()` results. --- setuptools/tests/test_config.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'setuptools') diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 08e398b3..677ccf2c 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -457,8 +457,9 @@ class TestOptions: dir_sub_two, _ = make_package_dir('sub_two', dir_package) with get_dist(tmpdir) as dist: - assert dist.packages == [ - 'fake_package', 'fake_package.sub_two', 'fake_package.sub_one'] + assert set(dist.packages) == set([ + 'fake_package', 'fake_package.sub_two', 'fake_package.sub_one' + ]) config.write( '[options]\n' @@ -482,7 +483,8 @@ class TestOptions: ' fake_package.sub_one\n' ) with get_dist(tmpdir) as dist: - assert dist.packages == ['fake_package', 'fake_package.sub_two'] + assert set(dist.packages) == set( + ['fake_package', 'fake_package.sub_two']) def test_extras_require(self, tmpdir): fake_env( -- cgit v1.2.3 From 35f3d1f37fdb137d5f5316058d49c96b9f28ca2d Mon Sep 17 00:00:00 2001 From: idle sign Date: Sat, 10 Dec 2016 15:23:49 +0700 Subject: `test_ignore_errors` side effect mitigated. --- setuptools/tests/test_config.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'setuptools') diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py index 677ccf2c..fa8d523b 100644 --- a/setuptools/tests/test_config.py +++ b/setuptools/tests/test_config.py @@ -98,6 +98,8 @@ class TestConfigurationReader: assert config_dict['metadata']['keywords'] == ['one', 'two'] assert 'version' not in config_dict['metadata'] + config.remove() + class TestMetadata: -- 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') 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