Skip to content

Commit

Permalink
Mock the actual setup provider defined in setup.py
Browse files Browse the repository at this point in the history
Currently. `setup` is always mocked using `distutils.core`
but this might cause issues with certain packages.
Fix this behavior by parsing the `setup.py` file for the correct
module to import.

Closes: aboutcode-org#116

Signed-off-by: Bennati, Stefano <[email protected]>
  • Loading branch information
bennati committed Feb 20, 2023
1 parent 39588f1 commit f3b955f
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/python_inspector/setup_py_live_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import ConfigParser as configparser

import mock
import setuptools
from commoncode.command import pushd
from packvers.requirements import Requirement

Expand Down Expand Up @@ -54,11 +53,24 @@ def iter_requirements(level, extras, setup_file):
setup_requires = {}
# change directory to setup.py path
with pushd(os.path.dirname(setup_file)):
with mock.patch.object(setuptools, "setup") as mock_setup:
sys.path.append(os.path.dirname(setup_file))
g = {"__file__": setup_file, "__name__": "__main__"}
with open(setup_file) as sf:
exec(sf.read(), g)
with open(setup_file) as sf:
file_contents = sf.read()
setup_provider = re.findall(r"from ([a-z._]+) import setup", file_contents)
if len(setup_provider) == 1:
setup_provider = setup_provider[0]
else:
setup_provider = ""
if not ((setup_provider == "distutils.core") or (setup_provider == "setuptools")):
print(
f"Warning: unable to recognize 'import {setup_provider}' in {setup_file}: "
"defaulting to 'distutils.core'."
)
setup_provider = "distutils.core"
exec(f"import {setup_provider}")
with mock.patch.object(eval(setup_provider), "setup") as mock_setup:
sys.path.append(os.path.dirname(setup_file))
g = {"__file__": setup_file, "__name__": "__main__"}
exec(file_contents, g)
sys.path.pop()
# removing the assertion `assert g["setup"]`` since this is not true for all cases
# for example when setuptools.setup() is called instead of setup()
Expand Down

0 comments on commit f3b955f

Please sign in to comment.