diff options
Diffstat (limited to 'setuptools/config.py')
-rw-r--r-- | setuptools/config.py | 87 |
1 files changed, 40 insertions, 47 deletions
diff --git a/setuptools/config.py b/setuptools/config.py index d1456cac..0a2f51e2 100644 --- a/setuptools/config.py +++ b/setuptools/config.py @@ -317,22 +317,15 @@ class ConfigHandler: Examples: attr: package.attr attr: package.module.attr - literal_attr: package.attr - literal_attr: package.module.attr :param str value: :rtype: str """ attr_directive = 'attr:' - literal_attr_directive = 'literal_attr:' - if value.startswith(attr_directive): - directive = attr_directive - elif value.startswith(literal_attr_directive): - directive = literal_attr_directive - else: + if not value.startswith(attr_directive): return value - attrs_path = value.replace(directive, '').strip().split('.') + attrs_path = value.replace(attr_directive, '').strip().split('.') attr_name = attrs_path.pop() module_name = '.'.join(attrs_path) @@ -352,50 +345,50 @@ class ConfigHandler: elif '' in package_dir: # A custom parent directory was specified for all root modules parent_path = os.path.join(os.getcwd(), package_dir['']) - if directive == attr_directive: + + fpath = os.path.join(parent_path, *module_name.split('.')) + if os.path.exists(fpath + '.py'): + fpath += '.py' + elif os.path.isdir(fpath): + fpath = os.path.join(fpath, '__init__.py') + else: + raise DistutilsOptionError('Could not find module ' + module_name) + with open(fpath, 'rb') as fp: + src = fp.read() + found = False + top_level = ast.parse(src) + for statement in top_level.body: + if isinstance(statement, ast.Assign): + for target in statement.targets: + if isinstance(target, ast.Name) \ + and target.id == attr_name: + try: + value = ast.literal_eval(statement.value) + except ValueError: + found = False + else: + found = True + elif isinstance(target, ast.Tuple) \ + and any(isinstance(t, ast.Name) and t.id == attr_name + for t in target.elts): + try: + stmnt_value = ast.literal_eval(statement.value) + except ValueError: + found = False + else: + for t, v in zip(target.elts, stmnt_value): + if isinstance(t, ast.Name) \ + and t.id == attr_name: + value = v + found = True + if not found: + # Fall back to extracting attribute via importing sys.path.insert(0, parent_path) try: module = import_module(module_name) value = getattr(module, attr_name) finally: sys.path = sys.path[1:] - - elif directive == literal_attr_directive: - fpath = os.path.join(parent_path, *module_name.split('.')) - if os.path.exists(fpath + '.py'): - fpath += '.py' - elif os.path.isdir(fpath): - fpath = os.path.join(fpath, '__init__.py') - else: - raise DistutilsOptionError( - 'Could not find module ' + module_name - ) - with open(fpath, 'rb') as fp: - src = fp.read() - found = False - top_level = ast.parse(src) - for statement in top_level.body: - if isinstance(statement, ast.Assign): - for target in statement.targets: - if isinstance(target, ast.Name) \ - and target.id == attr_name: - value = ast.literal_eval(statement.value) - found = True - elif isinstance(target, ast.Tuple) \ - and any(isinstance(t, ast.Name) and t.id==attr_name - for t in target.elts): - stmnt_value = ast.literal_eval(statement.value) - for t,v in zip(target.elts, stmnt_value): - if isinstance(t, ast.Name) \ - and t.id == attr_name: - value = v - found = True - if not found: - raise DistutilsOptionError( - 'No literal assignment to {!r} found in file' - .format(attr_name) - ) - return value @classmethod |