diff --git a/.github/workflows/test-system.yml b/.github/workflows/test-system.yml index d3daaf5ed..1c669a55e 100644 --- a/.github/workflows/test-system.yml +++ b/.github/workflows/test-system.yml @@ -523,24 +523,28 @@ jobs: timeout-minutes: 10 name: "python3.9 via chocolatey" runs-on: windows-latest + env: + TEST_PYTHON_VERSION: "3.9.13" steps: - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 with: persist-credentials: false - name: "Install Python" - run: choco install python3 --verbose --version=3.9.13 + run: choco install python3 --verbose --version=$env:TEST_PYTHON_VERSION - name: "Download binary" uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: uv-windows-x86_64-${{ inputs.sha }} - - name: "Print Python path" - run: echo $(which python3) - + # Chocolatey updates the registry but doesn't update `GITHUB_PATH` and since figuring out what + # it added would be more work than it's worth, it's simpler to just reload the values from the + # registry. - name: "Validate global Python install" - run: py -3.9 ./scripts/check_system_python.py --uv ./uv.exe + run: | + $env:PATH = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") + python ./scripts/check_system_python.py --uv ./uv.exe --check-python-version $env:TEST_PYTHON_VERSION --check-path system-test-pyenv: timeout-minutes: 10 diff --git a/scripts/check_system_python.py b/scripts/check_system_python.py index fbfc5557e..090ed47fb 100755 --- a/scripts/check_system_python.py +++ b/scripts/check_system_python.py @@ -52,6 +52,17 @@ if __name__ == "__main__": required=False, help="Set if the system Python version must be explicitly specified, e.g., for prereleases.", ) + parser.add_argument( + "--check-python-version", + required=False, + help="Verify that this tool has been started with the specified python version. Omitting the patch number will match any patch number.", + ) + parser.add_argument( + "--check-path", + required=False, + action="store_true", + help="Attempt to verify that the PATH is set up so that this tool's python will match the python version that uv would automatically pick up.", + ) args = parser.parse_args() uv: str = os.path.abspath(args.uv) if args.uv else "uv" @@ -60,6 +71,29 @@ if __name__ == "__main__": ) python = ["--python", args.python] if args.python else [] + if args.check_python_version: + version = ".".join(map(str, sys.version_info[:3])) + if args.check_python_version != version and not version.startswith( + args.check_python_version + "." + ): + raise Exception( + f"Expected to be running {args.check_python_version} but we are on {version}." + ) + + if args.check_path: + process = subprocess.run( + ["python", "-c", "import sys; print(sys.executable)"], + check=True, + stdout=subprocess.PIPE, + ) + system_python_path = os.path.normcase(os.path.normpath(process.stdout)) + our_python_path = os.path.normcase(os.path.normpath(sys.executable)) + + if our_python_path != system_python_path: + raise Exception( + f"Script was ran with {our_python_path} but `python` resolves to {system_python_path}" + ) + # Create a temporary directory. with tempfile.TemporaryDirectory() as temp_dir: # Ensure that the package (`pylint`) isn't installed.