Skip to content

Commit 32be38e

Browse files
authored
Merge pull request #1431 from github/rasmuswl/poetry-always-install-pip
python-setup: Handle poetry `virtualenvs.options.no-pip = true`
2 parents 2073a69 + 5ed1e98 commit 32be38e

File tree

7 files changed

+45
-12
lines changed

7 files changed

+45
-12
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## [UNRELEASED]
44

5-
No user facing changes.
5+
- Python automatic dependency installation will no longer fail for projects using Poetry that specify `virtualenvs.options.no-pip = true` in their `poetry.toml`. [#1431](https://github.com/github/codeql-action/pull/1431).
66

77
## 2.1.38 - 12 Jan 2023
88

lib/analyze.js

+2-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/analyze.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python-setup/find_site_packages.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Print the path to the site-packages directory for the current Python environment.
3+
"""
4+
from __future__ import print_function
5+
6+
try:
7+
import pip
8+
import os
9+
print(os.path.dirname(os.path.dirname(pip.__file__)))
10+
except ImportError:
11+
import sys
12+
print("DEBUG: could not import pip", file=sys.stderr)
13+
# if you use poetry with `virtualenvs.options.no-pip = true` you might end up with a
14+
# virtualenv without pip, so the above trick doesn't actually work. See
15+
# https://python-poetry.org/docs/configuration/#virtualenvsoptionsno-pip
16+
#
17+
# A possible option is to install `pip` into the virtualenv created by poetry
18+
# (`poetry add pip`), but it turns out that doesn't always work :( for the test
19+
# poetry/requests-3, I was not allowed to install pip! So I did not pursue this
20+
# option further.
21+
#
22+
# Instead, testing `site.getsitepackages()` contains has the right path, whereas
23+
# `site.getusersitepackages()` is about the system python (very confusing).
24+
#
25+
# We can't use the environment variable POETRY_VIRTUALENVS_OPTIONS_NO_PIP because it
26+
# does not work, see https://github.com/python-poetry/poetry/issues/5906
27+
import site
28+
29+
if sys.platform.startswith("win32"):
30+
# On windows, the last entry of `site.getsitepackages()` has the right path
31+
print(site.getsitepackages()[-1])
32+
else:
33+
# on unix, the first entry of `site.getsitepackages()` has the right path
34+
print(site.getsitepackages()[0])

python-setup/tests/from_python_exe.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ def get_details(path_to_python_exe: str) -> Tuple[str, str]:
99
import_path = subprocess.check_output(
1010
[
1111
path_to_python_exe,
12-
"-c",
13-
"import os; import pip; print(os.path.dirname(os.path.dirname(pip.__file__)))",
12+
os.path.join(os.path.dirname(__file__), "..", "find_site_packages.py")
1413
],
1514
stdin=subprocess.DEVNULL,
1615
)
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[virtualenvs]
22
in-project = true
3+
4+
[virtualenvs.options]
5+
no-pip = true

src/analyze.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ async function setupPythonExtractor(logger: Logger) {
8888
return;
8989
}
9090

91+
const scriptsFolder = path.resolve(__dirname, "../python-setup");
92+
9193
let output = "";
9294
const options = {
9395
listeners: {
@@ -99,10 +101,7 @@ async function setupPythonExtractor(logger: Logger) {
99101

100102
await new toolrunner.ToolRunner(
101103
codeqlPython,
102-
[
103-
"-c",
104-
"import os; import pip; print(os.path.dirname(os.path.dirname(pip.__file__)))",
105-
],
104+
[path.join(scriptsFolder, "find_site_packages.py")],
106105
options
107106
).exec();
108107
logger.info(`Setting LGTM_INDEX_IMPORT_PATH=${output}`);

0 commit comments

Comments
 (0)