Skip to content

Commit 0d5c2e0

Browse files
authored
Merge pull request #1928 from github/aeisenberg/fix-python312
Add a fix for python 3.12
2 parents a2dc5ff + 94b6970 commit 0d5c2e0

File tree

9 files changed

+115
-5
lines changed

9 files changed

+115
-5
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Test that the workaround for python 3.12 on windows works
2+
3+
on:
4+
push:
5+
branches: [main, releases/v2]
6+
pull_request:
7+
# Run checks on reopened draft PRs to support triggering PR checks on draft PRs that were opened
8+
# by other workflows.
9+
types: [opened, synchronize, reopened, ready_for_review]
10+
schedule:
11+
# Weekly on Monday.
12+
- cron: '0 0 * * 1'
13+
workflow_dispatch:
14+
15+
jobs:
16+
test-setup-python-scripts:
17+
timeout-minutes: 45
18+
runs-on: windows-latest
19+
20+
steps:
21+
- uses: actions/setup-python@v4
22+
with:
23+
python-version: 3.12
24+
25+
- uses: actions/checkout@v4
26+
27+
- name: Prepare test
28+
uses: ./.github/actions/prepare-test
29+
with:
30+
version: default
31+
32+
- name: Initialize CodeQL
33+
uses: ./../action/init
34+
with:
35+
tools: latest
36+
languages: python
37+
38+
- name: Analyze
39+
uses: ./../action/analyze
40+
with:
41+
upload-database: false

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ See the [releases page](https://github.com/github/codeql-action/releases) for th
44

55
## [UNRELEASED]
66

7-
No user facing changes.
7+
- Add a workaround python 3.12, which is not supported in CodeQL CLI version 2.14.6 or earlier. If you are running an analysis on Windows and using python 3.12 or later, the CodeQL Action will switch to running python 3.11. If python 3.11 is not found, then the workflow will fail.
88

99
## 2.22.0 - 06 Oct 2023
1010

lib/init-action.js

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

lib/init-action.js.map

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

lib/init.js

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

lib/init.js.map

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

python-setup/check_python12.ps1

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#! /usr/bin/pwsh
3+
4+
# If we are running greater than or equal to python 3.12, change py to run version 3.11
5+
Write-Host "Checking python version"
6+
if ((py -3 -c "import sys; print(0 if sys.version_info >= (3, 12) else 1)") -eq "0") {
7+
Write-Host "python 3.12+ detected, setting PY_PYTHON3=3.11"
8+
# First make sure we have python 3.11 installed
9+
py -3.11 -c "import imp"
10+
if ($LASTEXITCODE -eq 0) {
11+
Write-Host "python 3.11 detected, using this version instead of 3.12+."
12+
Write-Output "PY_PYTHON3=3.11" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
13+
} else {
14+
Write-Host "FAILURE: Python 3.12+ is not supported, and Python 3.11 could not be detected on the system. Please install Python 3.11."
15+
exit 1
16+
}
17+
} else {
18+
Write-Host "python 3.12+ not detected, not making any changes."
19+
}

src/init-action.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ import { CodeQL } from "./codeql";
1717
import * as configUtils from "./config-utils";
1818
import { EnvVar } from "./environment";
1919
import { Feature, Features } from "./feature-flags";
20-
import { initCodeQL, initConfig, installPythonDeps, runInit } from "./init";
20+
import {
21+
checkInstallPython311,
22+
initCodeQL,
23+
initConfig,
24+
installPythonDeps,
25+
runInit,
26+
} from "./init";
2127
import { Language } from "./languages";
2228
import { getActionsLogger, Logger } from "./logging";
2329
import { parseRepositoryNwo } from "./repository";
@@ -277,6 +283,8 @@ async function run() {
277283
logger,
278284
);
279285

286+
await checkInstallPython311(config.languages, codeql);
287+
280288
if (
281289
config.languages.includes(Language.python) &&
282290
getRequiredInput("setup-python-dependencies") === "true"

src/init.ts

+25
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
FeatureEnablement,
1414
useCodeScanningConfigInCli,
1515
} from "./feature-flags";
16+
import { Language } from "./languages";
1617
import { Logger } from "./logging";
1718
import { RepositoryNwo } from "./repository";
1819
import { ToolsSource } from "./setup-codeql";
@@ -181,6 +182,30 @@ function processError(e: any): Error {
181182
return e;
182183
}
183184

185+
/**
186+
* If we are running python 3.12+ on windows, we need to switch to python 3.11.
187+
* This check happens in a powershell script.
188+
*/
189+
export async function checkInstallPython311(
190+
languages: Language[],
191+
codeql: CodeQL,
192+
) {
193+
if (
194+
languages.includes(Language.python) &&
195+
process.platform === "win32" &&
196+
!(await codeql.getVersion()).features?.supportsPython312
197+
) {
198+
const script = path.resolve(
199+
__dirname,
200+
"../python-setup",
201+
"check_python12.ps1",
202+
);
203+
await new toolrunner.ToolRunner(await safeWhich.safeWhich("powershell"), [
204+
script,
205+
]).exec();
206+
}
207+
}
208+
184209
export async function installPythonDeps(codeql: CodeQL, logger: Logger) {
185210
logger.startGroup("Setup Python dependencies");
186211

0 commit comments

Comments
 (0)