Skip to content

Commit f3b955f

Browse files
committed
Mock the actual setup provider defined in setup.py
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: #116 Signed-off-by: Bennati, Stefano <stefano.bennati@here.com>
1 parent 39588f1 commit f3b955f

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

src/python_inspector/setup_py_live_eval.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import ConfigParser as configparser
2020

2121
import mock
22-
import setuptools
2322
from commoncode.command import pushd
2423
from packvers.requirements import Requirement
2524

@@ -54,11 +53,24 @@ def iter_requirements(level, extras, setup_file):
5453
setup_requires = {}
5554
# change directory to setup.py path
5655
with pushd(os.path.dirname(setup_file)):
57-
with mock.patch.object(setuptools, "setup") as mock_setup:
58-
sys.path.append(os.path.dirname(setup_file))
59-
g = {"__file__": setup_file, "__name__": "__main__"}
60-
with open(setup_file) as sf:
61-
exec(sf.read(), g)
56+
with open(setup_file) as sf:
57+
file_contents = sf.read()
58+
setup_provider = re.findall(r"from ([a-z._]+) import setup", file_contents)
59+
if len(setup_provider) == 1:
60+
setup_provider = setup_provider[0]
61+
else:
62+
setup_provider = ""
63+
if not ((setup_provider == "distutils.core") or (setup_provider == "setuptools")):
64+
print(
65+
f"Warning: unable to recognize 'import {setup_provider}' in {setup_file}: "
66+
"defaulting to 'distutils.core'."
67+
)
68+
setup_provider = "distutils.core"
69+
exec(f"import {setup_provider}")
70+
with mock.patch.object(eval(setup_provider), "setup") as mock_setup:
71+
sys.path.append(os.path.dirname(setup_file))
72+
g = {"__file__": setup_file, "__name__": "__main__"}
73+
exec(file_contents, g)
6274
sys.path.pop()
6375
# removing the assertion `assert g["setup"]`` since this is not true for all cases
6476
# for example when setuptools.setup() is called instead of setup()

0 commit comments

Comments
 (0)