Skip to content

Commit b3b27e5

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 f2b19f9 commit b3b27e5

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/python_inspector/setup_py_live_eval.py

Lines changed: 14 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,20 @@ 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 = [""]
61+
if not ((setup_provider[0] == 'distutils.core') or (setup_provider[0] == 'setuptools')):
62+
print(f"Warning: unable to recognize 'import {setup_provider[0]}' in {setup_file}: "
63+
"defaulting to 'distutils.core'.")
64+
setup_provider = 'distutils.core'
65+
exec(f"import {setup_provider}")
66+
with mock.patch.object(eval(setup_provider), "setup") as mock_setup:
67+
sys.path.append(os.path.dirname(setup_file))
68+
g = {"__file__": setup_file, "__name__": "__main__"}
69+
exec(file_contents, g)
6270
sys.path.pop()
6371
# removing the assertion `assert g["setup"]`` since this is not true for all cases
6472
# for example when setuptools.setup() is called instead of setup()

0 commit comments

Comments
 (0)