From 53e15292cd6a522cb460037b6f031eadbd6f6920 Mon Sep 17 00:00:00 2001 From: Andrew DiLosa Date: Thu, 2 Jun 2022 07:37:57 -0700 Subject: [PATCH] add support for python-version-file (#336) * add support for python-version-file * Update action.yml * update to v4, remove python-version default * python-version overrides python-version-file, like setup-node * checks '.python-version' by default if nothing else specified * update tests, update to checkout@v3 * update build * appease the linter * remove old test for default python version * revert readme changes * update build --- .github/workflows/test-python.yml | 68 +++++++++++++++++++------------ action.yml | 5 ++- dist/setup/index.js | 23 ++++++++++- package.json | 2 +- src/setup-python.ts | 32 ++++++++++++++- 5 files changed, 98 insertions(+), 32 deletions(-) diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 00043f4..02835a9 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -10,33 +10,9 @@ on: - '**.md' schedule: - cron: 30 3 * * * + workflow_dispatch: jobs: - default-version: - name: Setup default version - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [macos-latest, windows-latest, ubuntu-18.04, ubuntu-20.04] - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: setup default python - id: setup-python - uses: ./ - - - name: Check python-path - run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}' - shell: bash - - - name: Validate version - run: python --version - - - name: Run simple python code - run: python -c 'import math; print(math.factorial(5))' - setup-versions-from-manifest: name: Setup ${{ matrix.python }} ${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -47,7 +23,7 @@ jobs: python: [3.5.4, 3.6.7, 3.7.5, 3.8.1] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: setup-python ${{ matrix.python }} id: setup-python @@ -72,6 +48,44 @@ jobs: - name: Run simple code run: python -c 'import math; print(math.factorial(5))' + setup-versions-from-file: + name: Setup ${{ matrix.python }} ${{ matrix.os }} version file + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest, windows-latest, ubuntu-18.04, ubuntu-20.04] + python: [3.5.4, 3.6.7, 3.7.5, 3.8.1] + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: build-version-file ${{ matrix.python }} + run: echo ${{ matrix.python }} > .python-version + + - name: setup-python ${{ matrix.python }} + id: setup-python + uses: ./ + with: + python-version-file: '.python-version' + + - name: Check python-path + run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}' + shell: bash + + - name: Validate version + run: | + $pythonVersion = (python --version) + if ("Python ${{ matrix.python }}" -ne "$pythonVersion"){ + Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}" + exit 1 + } + $pythonVersion + shell: pwsh + + - name: Run simple code + run: python -c 'import math; print(math.factorial(5))' + setup-pre-release-version-from-manifest: name: Setup 3.9.0-beta.4 ${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -81,7 +95,7 @@ jobs: os: [macos-latest, windows-latest, ubuntu-18.04, ubuntu-20.04] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: setup-python 3.9.0-beta.4 id: setup-python diff --git a/action.yml b/action.yml index 00c1e5e..27474e2 100644 --- a/action.yml +++ b/action.yml @@ -4,8 +4,9 @@ description: 'Set up a specific version of Python and add the command-line tools author: 'GitHub' inputs: python-version: - description: "Version range or exact version of a Python version to use, using SemVer's version range syntax." - default: '3.x' + description: "Version range or exact version of Python to use, using SemVer's version range syntax. Reads from .python-version if unset." + python-version-file: + description: "File containing the Python version to use. Example: .python-version" cache: description: 'Used to specify a package manager for caching in the default directory. Supported values: pip, pipenv, poetry.' required: false diff --git a/dist/setup/index.js b/dist/setup/index.js index 60ad858..9ab89d4 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -64527,12 +64527,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", ({ value: true })); const core = __importStar(__nccwpck_require__(2186)); const finder = __importStar(__nccwpck_require__(9996)); const finderPyPy = __importStar(__nccwpck_require__(4003)); const path = __importStar(__nccwpck_require__(1017)); const os = __importStar(__nccwpck_require__(2037)); +const fs_1 = __importDefault(__nccwpck_require__(7147)); const cache_factory_1 = __nccwpck_require__(7549); const utils_1 = __nccwpck_require__(1314); function isPyPyVersion(versionSpec) { @@ -64545,6 +64549,23 @@ function cacheDependencies(cache, pythonVersion) { yield cacheDistributor.restoreCache(); }); } +function resolveVersionInput() { + let version = core.getInput('python-version'); + const versionFile = core.getInput('python-version-file'); + if (version && versionFile) { + core.warning('Both python-version and python-version-file inputs are specified, only python-version will be used'); + } + if (version) { + return version; + } + const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFile || '.python-version'); + if (!fs_1.default.existsSync(versionFilePath)) { + throw new Error(`The specified python version file at: ${versionFilePath} does not exist`); + } + version = fs_1.default.readFileSync(versionFilePath, 'utf8'); + core.info(`Resolved ${versionFile} as ${version}`); + return version; +} function run() { var _a; return __awaiter(this, void 0, void 0, function* () { @@ -64556,7 +64577,7 @@ function run() { core.debug(`Python is expected to be installed into RUNNER_TOOL_CACHE==${process.env['RUNNER_TOOL_CACHE']}`); } try { - const version = core.getInput('python-version'); + const version = resolveVersionInput(); if (version) { let pythonVersion; const arch = core.getInput('architecture') || os.arch(); diff --git a/package.json b/package.json index 4fafdb9..d7d88f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "setup-python", - "version": "3.1.1", + "version": "4.0.0", "private": true, "description": "Setup python action", "main": "dist/index.js", diff --git a/src/setup-python.ts b/src/setup-python.ts index e8789de..f5f8c91 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -3,6 +3,7 @@ import * as finder from './find-python'; import * as finderPyPy from './find-pypy'; import * as path from 'path'; import * as os from 'os'; +import fs from 'fs'; import {getCacheDistributor} from './cache-distributions/cache-factory'; import {isCacheFeatureAvailable} from './utils'; @@ -21,6 +22,35 @@ async function cacheDependencies(cache: string, pythonVersion: string) { await cacheDistributor.restoreCache(); } +function resolveVersionInput(): string { + let version = core.getInput('python-version'); + const versionFile = core.getInput('python-version-file'); + + if (version && versionFile) { + core.warning( + 'Both python-version and python-version-file inputs are specified, only python-version will be used' + ); + } + + if (version) { + return version; + } + + const versionFilePath = path.join( + process.env.GITHUB_WORKSPACE!, + versionFile || '.python-version' + ); + if (!fs.existsSync(versionFilePath)) { + throw new Error( + `The specified python version file at: ${versionFilePath} does not exist` + ); + } + version = fs.readFileSync(versionFilePath, 'utf8'); + core.info(`Resolved ${versionFile} as ${version}`); + + return version; +} + async function run() { if (process.env.AGENT_TOOLSDIRECTORY?.trim()) { core.debug( @@ -33,7 +63,7 @@ async function run() { ); } try { - const version = core.getInput('python-version'); + const version = resolveVersionInput(); if (version) { let pythonVersion: string; const arch: string = core.getInput('architecture') || os.arch();