Skip to content

Commit

Permalink
Merge pull request #1928 from github/aeisenberg/fix-python312
Browse files Browse the repository at this point in the history
Add a fix for python 3.12
  • Loading branch information
aeisenberg committed Oct 6, 2023
2 parents a2dc5ff + 94b6970 commit 0d5c2e0
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 5 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/python312-windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Test that the workaround for python 3.12 on windows works

on:
push:
branches: [main, releases/v2]
pull_request:
# Run checks on reopened draft PRs to support triggering PR checks on draft PRs that were opened
# by other workflows.
types: [opened, synchronize, reopened, ready_for_review]
schedule:
# Weekly on Monday.
- cron: '0 0 * * 1'
workflow_dispatch:

jobs:
test-setup-python-scripts:
timeout-minutes: 45
runs-on: windows-latest

steps:
- uses: actions/setup-python@v4
with:
python-version: 3.12

- uses: actions/checkout@v4

- name: Prepare test
uses: ./.github/actions/prepare-test
with:
version: default

- name: Initialize CodeQL
uses: ./../action/init
with:
tools: latest
languages: python

- name: Analyze
uses: ./../action/analyze
with:
upload-database: false
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ See the [releases page](https://github.com/github/codeql-action/releases) for th

## [UNRELEASED]

No user facing changes.
- 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.

## 2.22.0 - 06 Oct 2023

Expand Down
1 change: 1 addition & 0 deletions lib/init-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/init-action.js.map

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion lib/init.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/init.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions python-setup/check_python12.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#! /usr/bin/pwsh

# If we are running greater than or equal to python 3.12, change py to run version 3.11
Write-Host "Checking python version"
if ((py -3 -c "import sys; print(0 if sys.version_info >= (3, 12) else 1)") -eq "0") {
Write-Host "python 3.12+ detected, setting PY_PYTHON3=3.11"
# First make sure we have python 3.11 installed
py -3.11 -c "import imp"
if ($LASTEXITCODE -eq 0) {
Write-Host "python 3.11 detected, using this version instead of 3.12+."
Write-Output "PY_PYTHON3=3.11" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
} else {
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."
exit 1
}
} else {
Write-Host "python 3.12+ not detected, not making any changes."
}
10 changes: 9 additions & 1 deletion src/init-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import { CodeQL } from "./codeql";
import * as configUtils from "./config-utils";
import { EnvVar } from "./environment";
import { Feature, Features } from "./feature-flags";
import { initCodeQL, initConfig, installPythonDeps, runInit } from "./init";
import {
checkInstallPython311,
initCodeQL,
initConfig,
installPythonDeps,
runInit,
} from "./init";
import { Language } from "./languages";
import { getActionsLogger, Logger } from "./logging";
import { parseRepositoryNwo } from "./repository";
Expand Down Expand Up @@ -277,6 +283,8 @@ async function run() {
logger,
);

await checkInstallPython311(config.languages, codeql);

if (
config.languages.includes(Language.python) &&
getRequiredInput("setup-python-dependencies") === "true"
Expand Down
25 changes: 25 additions & 0 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
FeatureEnablement,
useCodeScanningConfigInCli,
} from "./feature-flags";
import { Language } from "./languages";
import { Logger } from "./logging";
import { RepositoryNwo } from "./repository";
import { ToolsSource } from "./setup-codeql";
Expand Down Expand Up @@ -181,6 +182,30 @@ function processError(e: any): Error {
return e;
}

/**
* If we are running python 3.12+ on windows, we need to switch to python 3.11.
* This check happens in a powershell script.
*/
export async function checkInstallPython311(
languages: Language[],
codeql: CodeQL,
) {
if (
languages.includes(Language.python) &&
process.platform === "win32" &&
!(await codeql.getVersion()).features?.supportsPython312
) {
const script = path.resolve(
__dirname,
"../python-setup",
"check_python12.ps1",
);
await new toolrunner.ToolRunner(await safeWhich.safeWhich("powershell"), [
script,
]).exec();
}
}

export async function installPythonDeps(codeql: CodeQL, logger: Logger) {
logger.startGroup("Setup Python dependencies");

Expand Down

0 comments on commit 0d5c2e0

Please sign in to comment.