Skip to content

Commit 53e1529

Browse files
authored
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
1 parent 3f82819 commit 53e1529

File tree

5 files changed

+82
-16
lines changed

5 files changed

+82
-16
lines changed

.github/workflows/test-python.yml

+25-11
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,46 @@ on:
1010
- '**.md'
1111
schedule:
1212
- cron: 30 3 * * *
13+
workflow_dispatch:
1314

1415
jobs:
15-
default-version:
16-
name: Setup default version
16+
setup-versions-from-manifest:
17+
name: Setup ${{ matrix.python }} ${{ matrix.os }}
1718
runs-on: ${{ matrix.os }}
1819
strategy:
1920
fail-fast: false
2021
matrix:
2122
os: [macos-latest, windows-latest, ubuntu-18.04, ubuntu-20.04]
23+
python: [3.5.4, 3.6.7, 3.7.5, 3.8.1]
2224
steps:
2325
- name: Checkout
24-
uses: actions/checkout@v2
26+
uses: actions/checkout@v3
2527

26-
- name: setup default python
28+
- name: setup-python ${{ matrix.python }}
2729
id: setup-python
2830
uses: ./
31+
with:
32+
python-version: ${{ matrix.python }}
2933

3034
- name: Check python-path
3135
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
3236
shell: bash
3337

3438
- name: Validate version
35-
run: python --version
39+
run: |
40+
$pythonVersion = (python --version)
41+
if ("Python ${{ matrix.python }}" -ne "$pythonVersion"){
42+
Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}"
43+
exit 1
44+
}
45+
$pythonVersion
46+
shell: pwsh
3647

37-
- name: Run simple python code
48+
- name: Run simple code
3849
run: python -c 'import math; print(math.factorial(5))'
3950

40-
setup-versions-from-manifest:
41-
name: Setup ${{ matrix.python }} ${{ matrix.os }}
51+
setup-versions-from-file:
52+
name: Setup ${{ matrix.python }} ${{ matrix.os }} version file
4253
runs-on: ${{ matrix.os }}
4354
strategy:
4455
fail-fast: false
@@ -47,13 +58,16 @@ jobs:
4758
python: [3.5.4, 3.6.7, 3.7.5, 3.8.1]
4859
steps:
4960
- name: Checkout
50-
uses: actions/checkout@v2
61+
uses: actions/checkout@v3
62+
63+
- name: build-version-file ${{ matrix.python }}
64+
run: echo ${{ matrix.python }} > .python-version
5165

5266
- name: setup-python ${{ matrix.python }}
5367
id: setup-python
5468
uses: ./
5569
with:
56-
python-version: ${{ matrix.python }}
70+
python-version-file: '.python-version'
5771

5872
- name: Check python-path
5973
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
@@ -81,7 +95,7 @@ jobs:
8195
os: [macos-latest, windows-latest, ubuntu-18.04, ubuntu-20.04]
8296
steps:
8397
- name: Checkout
84-
uses: actions/checkout@v2
98+
uses: actions/checkout@v3
8599

86100
- name: setup-python 3.9.0-beta.4
87101
id: setup-python

action.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ description: 'Set up a specific version of Python and add the command-line tools
44
author: 'GitHub'
55
inputs:
66
python-version:
7-
description: "Version range or exact version of a Python version to use, using SemVer's version range syntax."
8-
default: '3.x'
7+
description: "Version range or exact version of Python to use, using SemVer's version range syntax. Reads from .python-version if unset."
8+
python-version-file:
9+
description: "File containing the Python version to use. Example: .python-version"
910
cache:
1011
description: 'Used to specify a package manager for caching in the default directory. Supported values: pip, pipenv, poetry.'
1112
required: false

dist/setup/index.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -64527,12 +64527,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
6452764527
step((generator = generator.apply(thisArg, _arguments || [])).next());
6452864528
});
6452964529
};
64530+
var __importDefault = (this && this.__importDefault) || function (mod) {
64531+
return (mod && mod.__esModule) ? mod : { "default": mod };
64532+
};
6453064533
Object.defineProperty(exports, "__esModule", ({ value: true }));
6453164534
const core = __importStar(__nccwpck_require__(2186));
6453264535
const finder = __importStar(__nccwpck_require__(9996));
6453364536
const finderPyPy = __importStar(__nccwpck_require__(4003));
6453464537
const path = __importStar(__nccwpck_require__(1017));
6453564538
const os = __importStar(__nccwpck_require__(2037));
64539+
const fs_1 = __importDefault(__nccwpck_require__(7147));
6453664540
const cache_factory_1 = __nccwpck_require__(7549);
6453764541
const utils_1 = __nccwpck_require__(1314);
6453864542
function isPyPyVersion(versionSpec) {
@@ -64545,6 +64549,23 @@ function cacheDependencies(cache, pythonVersion) {
6454564549
yield cacheDistributor.restoreCache();
6454664550
});
6454764551
}
64552+
function resolveVersionInput() {
64553+
let version = core.getInput('python-version');
64554+
const versionFile = core.getInput('python-version-file');
64555+
if (version && versionFile) {
64556+
core.warning('Both python-version and python-version-file inputs are specified, only python-version will be used');
64557+
}
64558+
if (version) {
64559+
return version;
64560+
}
64561+
const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFile || '.python-version');
64562+
if (!fs_1.default.existsSync(versionFilePath)) {
64563+
throw new Error(`The specified python version file at: ${versionFilePath} does not exist`);
64564+
}
64565+
version = fs_1.default.readFileSync(versionFilePath, 'utf8');
64566+
core.info(`Resolved ${versionFile} as ${version}`);
64567+
return version;
64568+
}
6454864569
function run() {
6454964570
var _a;
6455064571
return __awaiter(this, void 0, void 0, function* () {
@@ -64556,7 +64577,7 @@ function run() {
6455664577
core.debug(`Python is expected to be installed into RUNNER_TOOL_CACHE==${process.env['RUNNER_TOOL_CACHE']}`);
6455764578
}
6455864579
try {
64559-
const version = core.getInput('python-version');
64580+
const version = resolveVersionInput();
6456064581
if (version) {
6456164582
let pythonVersion;
6456264583
const arch = core.getInput('architecture') || os.arch();

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setup-python",
3-
"version": "3.1.1",
3+
"version": "4.0.0",
44
"private": true,
55
"description": "Setup python action",
66
"main": "dist/index.js",

src/setup-python.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as finder from './find-python';
33
import * as finderPyPy from './find-pypy';
44
import * as path from 'path';
55
import * as os from 'os';
6+
import fs from 'fs';
67
import {getCacheDistributor} from './cache-distributions/cache-factory';
78
import {isCacheFeatureAvailable} from './utils';
89

@@ -21,6 +22,35 @@ async function cacheDependencies(cache: string, pythonVersion: string) {
2122
await cacheDistributor.restoreCache();
2223
}
2324

25+
function resolveVersionInput(): string {
26+
let version = core.getInput('python-version');
27+
const versionFile = core.getInput('python-version-file');
28+
29+
if (version && versionFile) {
30+
core.warning(
31+
'Both python-version and python-version-file inputs are specified, only python-version will be used'
32+
);
33+
}
34+
35+
if (version) {
36+
return version;
37+
}
38+
39+
const versionFilePath = path.join(
40+
process.env.GITHUB_WORKSPACE!,
41+
versionFile || '.python-version'
42+
);
43+
if (!fs.existsSync(versionFilePath)) {
44+
throw new Error(
45+
`The specified python version file at: ${versionFilePath} does not exist`
46+
);
47+
}
48+
version = fs.readFileSync(versionFilePath, 'utf8');
49+
core.info(`Resolved ${versionFile} as ${version}`);
50+
51+
return version;
52+
}
53+
2454
async function run() {
2555
if (process.env.AGENT_TOOLSDIRECTORY?.trim()) {
2656
core.debug(
@@ -33,7 +63,7 @@ async function run() {
3363
);
3464
}
3565
try {
36-
const version = core.getInput('python-version');
66+
const version = resolveVersionInput();
3767
if (version) {
3868
let pythonVersion: string;
3969
const arch: string = core.getInput('architecture') || os.arch();

0 commit comments

Comments
 (0)