Commit a2cf6acd by Sébastien Eustace

Fix reading of some setup.py files

parent 713b813f
# Change Log # Change Log
## [Unreleased]
### Fixed
- Fixed reading of some `setup.py` files.
## [0.12.6] - 2018-11-05 ## [0.12.6] - 2018-11-05
### Changed ### Changed
......
...@@ -40,19 +40,21 @@ class SetupReader(object): ...@@ -40,19 +40,21 @@ class SetupReader(object):
if isinstance(directory, basestring): if isinstance(directory, basestring):
directory = Path(directory) directory = Path(directory)
result = cls.DEFAULT.copy()
for filename in cls.FILES: for filename in cls.FILES:
filepath = directory / filename filepath = directory / filename
if not filepath.exists(): if not filepath.exists():
continue continue
result = getattr(cls(), "read_{}".format(filename.replace(".", "_")))( new_result = getattr(cls(), "read_{}".format(filename.replace(".", "_")))(
filepath filepath
) )
if not cls._is_empty_result(result): for key in result.keys():
return result if new_result[key]:
result[key] = new_result[key]
return cls.DEFAULT return result
@classmethod @classmethod
def _is_empty_result(cls, result): # type: (Dict[str, Any]) -> bool def _is_empty_result(cls, result): # type: (Dict[str, Any]) -> bool
...@@ -146,7 +148,27 @@ class SetupReader(object): ...@@ -146,7 +148,27 @@ class SetupReader(object):
self, elements self, elements
): # type: (List[Any]) -> Tuple[Optional[ast.Call], Optional[List[Any]]] ): # type: (List[Any]) -> Tuple[Optional[ast.Call], Optional[List[Any]]]
funcdefs = [] funcdefs = []
for element in elements: for i, element in enumerate(elements):
if isinstance(element, ast.If) and i == len(elements) - 1:
# Checking if the last element is an if statement
# and if it is 'if __name__ == "__main__"' which
# could contain the call to setup()
test = element.test
if not isinstance(test, ast.Compare):
continue
left = test.left
if not isinstance(left, ast.Name):
continue
if left.id != "__name__":
continue
setup_call, body = self._find_sub_setup_call([element])
if not setup_call:
continue
return setup_call, body + elements
if not isinstance(element, ast.Expr): if not isinstance(element, ast.Expr):
if isinstance(element, ast.FunctionDef): if isinstance(element, ast.FunctionDef):
funcdefs.append(element) funcdefs.append(element)
...@@ -173,17 +195,19 @@ class SetupReader(object): ...@@ -173,17 +195,19 @@ class SetupReader(object):
self, elements self, elements
): # type: (List[Any]) -> Tuple[Optional[ast.Call], Optional[List[Any]]] ): # type: (List[Any]) -> Tuple[Optional[ast.Call], Optional[List[Any]]]
for element in elements: for element in elements:
if not isinstance(element, ast.FunctionDef): if not isinstance(element, (ast.FunctionDef, ast.If)):
continue continue
setup_call = self._find_setup_call(element.body) setup_call = self._find_setup_call(element.body)
if setup_call: if setup_call != (None, None):
setup_call, body = setup_call setup_call, body = setup_call
body = elements + body body = elements + body
return setup_call, body return setup_call, body
return None, None
def _find_install_requires( def _find_install_requires(
self, call, body self, call, body
): # type: (ast.Call, Iterable[Any]) -> List[str] ): # type: (ast.Call, Iterable[Any]) -> List[str]
......
...@@ -132,3 +132,20 @@ def test_setup_reader_read_setup_kwargs(setup): ...@@ -132,3 +132,20 @@ def test_setup_reader_read_setup_kwargs(setup):
assert expected_install_requires == result["install_requires"] assert expected_install_requires == result["install_requires"]
assert expected_extras_require == result["extras_require"] assert expected_extras_require == result["extras_require"]
assert expected_python_requires == result["python_requires"] assert expected_python_requires == result["python_requires"]
@pytest.mark.skipif(not PY35, reason="AST parsing does not work for Python <3.4")
def test_setup_reader_read_setup_call_in_main(setup):
result = SetupReader.read_from_directory(setup("pyyaml"))
expected_name = "PyYAML"
expected_version = "3.13"
expected_install_requires = []
expected_extras_require = {}
expected_python_requires = None
assert expected_name == result["name"]
assert expected_version == result["version"]
assert expected_install_requires == result["install_requires"]
assert expected_extras_require == result["extras_require"]
assert expected_python_requires == result["python_requires"]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment