Skip to content

Commit

Permalink
add support for python-version-file (#336)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adilosa authored Jun 2, 2022
1 parent 3f82819 commit 53e1529
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
36 changes: 25 additions & 11 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,46 @@ on:
- '**.md'
schedule:
- cron: 30 3 * * *
workflow_dispatch:

jobs:
default-version:
name: Setup default version
setup-versions-from-manifest:
name: Setup ${{ matrix.python }} ${{ matrix.os }}
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@v2
uses: actions/checkout@v3

- name: setup default python
- name: setup-python ${{ matrix.python }}
id: setup-python
uses: ./
with:
python-version: ${{ matrix.python }}

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

- name: Validate version
run: python --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 python code
- name: Run simple code
run: python -c 'import math; print(math.factorial(5))'

setup-versions-from-manifest:
name: Setup ${{ matrix.python }} ${{ matrix.os }}
setup-versions-from-file:
name: Setup ${{ matrix.python }} ${{ matrix.os }} version file
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -47,13 +58,16 @@ 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: build-version-file ${{ matrix.python }}
run: echo ${{ matrix.python }} > .python-version

- name: setup-python ${{ matrix.python }}
id: setup-python
uses: ./
with:
python-version: ${{ matrix.python }}
python-version-file: '.python-version'

- name: Check python-path
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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* () {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
32 changes: 31 additions & 1 deletion src/setup-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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(
Expand All @@ -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();
Expand Down

0 comments on commit 53e1529

Please sign in to comment.