Skip to content

Commit 7fc41c6

Browse files
RasmusWLDaverlo
authored andcommitted
Update python scripts to handle setup.py
1 parent 5701037 commit 7fc41c6

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

python-setup/auto_install_packages.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def install_packages_with_pipenv():
5050
return python_executable_path
5151

5252

53-
def install_requirements_txt_packages(version: int, requirements_txt_path: str):
53+
def _create_venv(version: int):
5454
# create temporary directory ... that just lives "forever"
5555
venv_path = mkdtemp(prefix='codeql-action-python-autoinstall-')
5656

@@ -62,14 +62,42 @@ def install_requirements_txt_packages(version: int, requirements_txt_path: str):
6262
elif version == 3:
6363
_check_call(['python3', '-m', 'virtualenv', venv_path])
6464

65+
return venv_path
66+
67+
68+
def install_requirements_txt_packages(version: int):
69+
venv_path = _create_venv(version)
6570
venv_pip = os.path.join(venv_path, 'bin', 'pip')
71+
venv_python = os.path.join(venv_path, 'bin', 'python')
72+
6673
try:
67-
_check_call([venv_pip, 'install', '-r', requirements_txt_path])
74+
_check_call([venv_pip, 'install', '-r', 'requirements.txt'])
6875
except subprocess.CalledProcessError:
69-
sys.exit('package installation with pip failed, see error above')
76+
sys.exit('package installation with `pip install -r requirements.txt` failed, see error above')
77+
78+
return venv_python
79+
7080

81+
def install_with_setup_py(version: int):
82+
venv_path = _create_venv(version)
83+
venv_pip = os.path.join(venv_path, 'bin', 'pip')
7184
venv_python = os.path.join(venv_path, 'bin', 'python')
7285

86+
try:
87+
# We have to choose between `python setup.py develop` and `pip install -e .`.
88+
# Modern projects use `pip install -e .` and I wasn't able to see any downsides
89+
# to doing so. However, `python setup.py develop` has some downsides -- from
90+
# https://stackoverflow.com/a/19048754 :
91+
# > Note that it is highly recommended to use pip install . (install) and pip
92+
# > install -e . (developer install) to install packages, as invoking setup.py
93+
# > directly will do the wrong things for many dependencies, such as pull
94+
# > prereleases and incompatible package versions, or make the package hard to
95+
# > uninstall with pip.
96+
97+
_check_call([venv_pip, 'install', '-e', '.'])
98+
except subprocess.CalledProcessError:
99+
sys.exit('package installation with `pip install -e .` failed, see error above')
100+
73101
return venv_python
74102

75103

@@ -89,7 +117,11 @@ def install_packages() -> str:
89117

90118
if os.path.exists('requirements.txt'):
91119
print('Found requirements.txt, will install packages with pip', flush=True)
92-
return install_requirements_txt_packages(version, 'requirements.txt')
120+
return install_requirements_txt_packages(version)
121+
122+
if os.path.exists('setup.py'):
123+
print('Found setup.py, will install package with pip in editable mode', flush=True)
124+
return install_with_setup_py(version)
93125

94126
print("was not able to install packages automatically", flush=True)
95127

@@ -107,9 +139,3 @@ def install_packages() -> str:
107139
if python_executable_path is not None:
108140
print("Setting CODEQL_PYTHON={}".format(python_executable_path))
109141
print("::set-env name=CODEQL_PYTHON::{}".format(python_executable_path))
110-
111-
# TODO:
112-
# - no packages
113-
# - poetry without version
114-
# - pipenv without version
115-
# - pipenv without lockfile

0 commit comments

Comments
 (0)