Skip to content

Commit ff5ca12

Browse files
authored
Merge pull request #1257 from github/rasmuswl/fix-ubuntu22.04-venv-creation
python-setup: Fix venv creation in Ubuntu 22.04
2 parents 417059f + 32ca2cf commit ff5ca12

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

.github/workflows/python-deps.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
strategy:
2727
fail-fast: false
2828
matrix:
29-
os: [ubuntu-latest, macos-latest]
29+
os: [ubuntu-latest, ubuntu-22.04, macos-latest]
3030
python_deps_type: [pipenv, poetry, requirements, setup_py]
3131
python_version: [2, 3]
3232
exclude:
@@ -36,6 +36,9 @@ jobs:
3636
# Python2 and pipenv are not supported since pipenv v2021.11.5
3737
- python_version: 2
3838
python_deps_type: pipenv
39+
# Python2 is not available on ubuntu-22.04 by default -- see https://github.com/github/codeql-action/pull/1257
40+
- python_version: 2
41+
os: ubuntu-22.04
3942

4043

4144
env:
@@ -63,6 +66,7 @@ jobs:
6366
6467
case ${{ matrix.os }} in
6568
ubuntu-latest*) basePath="/opt";;
69+
ubuntu-22.04*) basePath="/opt";;
6670
macos-latest*) basePath="/Users/runner";;
6771
esac
6872
echo ${basePath}
@@ -86,7 +90,7 @@ jobs:
8690
strategy:
8791
fail-fast: false
8892
matrix:
89-
os: [ubuntu-latest, macos-latest]
93+
os: [ubuntu-latest, ubuntu-22.04, macos-latest]
9094

9195
steps:
9296
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
@@ -109,6 +113,7 @@ jobs:
109113
110114
case ${{ matrix.os }} in
111115
ubuntu-latest*) basePath="/opt";;
116+
ubuntu-22.04*) basePath="/opt";;
112117
macos-latest*) basePath="/Users/runner";;
113118
esac
114119
echo ${basePath}

python-setup/auto_install_packages.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
from tempfile import mkdtemp
77
from typing import Optional
8+
import shutil
89

910
import extractor_version
1011

@@ -167,6 +168,19 @@ def install_packages(codeql_base_dir) -> Optional[str]:
167168

168169
# get_extractor_version returns the Python version the extractor thinks this repo is using
169170
version = extractor_version.get_extractor_version(codeql_base_dir, quiet=False)
171+
sys.stdout.flush()
172+
sys.stderr.flush()
173+
174+
if version == 2 and not sys.platform.startswith('win32'):
175+
# On Ubuntu 22.04 'python2' is not available by default. We want to give a slightly better
176+
# error message than a traceback + `No such file or directory: 'python2'`
177+
if shutil.which("python2") is None:
178+
sys.exit(
179+
"Python package installation failed: we detected this code as Python 2, but the 'python2' executable was not available. "
180+
"To enable automatic package installation, please install 'python2' before the 'github/codeql-action/init' step, "
181+
"for example by running 'sudo apt install python2' (Ubuntu 22.04). "
182+
"If your code is not Python 2, but actually Python 3, please file a bug report at https://github.com/github/codeql-action/issues/new"
183+
)
170184

171185
if os.path.exists('requirements.txt'):
172186
print('Found requirements.txt, will install packages with pip', flush=True)

python-setup/install_tools.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#! /usr/bin/pwsh
22

3-
py -2 -m pip install --user --upgrade pip setuptools wheel
4-
py -3 -m pip install --user --upgrade pip setuptools wheel
3+
# while waiting for the next release of `virtualenv` after v20.16.5, we install an older
4+
# version of `setuptools` to ensure that binaries are always put under
5+
# `<venv-path>/bin`, which wouldn't always happen with the GitHub actions version of
6+
# Ubuntu 22.04. See https://github.com/github/codeql-action/issues/1249
7+
py -2 -m pip install --user --upgrade pip 'setuptools<60' wheel
8+
py -3 -m pip install --user --upgrade pip 'setuptools<60' wheel
59

610
# virtualenv is a bit nicer for setting up virtual environment, since it will provide up-to-date versions of
711
# pip/setuptools/wheel which basic `python3 -m venv venv` won't

python-setup/install_tools.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ set -e
1111
export PATH="$HOME/.local/bin:$PATH"
1212

1313
# Setup Python 3 dependency installation tools.
14-
python3 -m pip install --user --upgrade pip setuptools wheel
14+
15+
# we install an older version of `setuptools` to ensure that binaries are always put
16+
# under `<venv-path>/bin`, which wouldn't always happen with the GitHub actions version
17+
# of Ubuntu 22.04. See https://github.com/github/codeql-action/issues/1249. The the next
18+
# release of `virtualenv` after v20.16.5 will include a fix for this, so we can remove
19+
# this bit of the logic again.
20+
python3 -m pip install --user --upgrade pip 'setuptools<60' wheel
1521

1622
# virtualenv is a bit nicer for setting up virtual environment, since it will provide up-to-date versions of
1723
# pip/setuptools/wheel which basic `python3 -m venv venv` won't
@@ -39,7 +45,7 @@ if command -v python2 >/dev/null 2>&1; then
3945
curl --location --fail https://bootstrap.pypa.io/pip/2.7/get-pip.py | python2
4046
fi
4147

42-
python2 -m pip install --user --upgrade pip setuptools wheel
48+
python2 -m pip install --user --upgrade pip 'setuptools<60' wheel
4349

4450
python2 -m pip install --user 'virtualenv!=20.12.0'
4551
fi

0 commit comments

Comments
 (0)