Skip to content

Commit fa589f7

Browse files
add support to install multiple python versions
1 parent 13ae5bb commit fa589f7

File tree

4 files changed

+131
-49
lines changed

4 files changed

+131
-49
lines changed

.github/workflows/test-pypy.yml

+43
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,47 @@ jobs:
123123
EXECUTABLE=${EXECUTABLE/-/} # remove the first '-' in "pypy-X.Y" -> "pypyX.Y" to match executable name
124124
EXECUTABLE=${EXECUTABLE%%-*} # remove any -* suffixe
125125
${EXECUTABLE} --version
126+
shell: bash
127+
128+
setup-pypy-multiple-versions:
129+
runs-on: ${{ matrix.os }}
130+
strategy:
131+
fail-fast: false
132+
matrix:
133+
os: [ubuntu-latest, windows-latest, macos-latest]
134+
steps:
135+
- uses: actions/checkout@v3
136+
- name: Setup PyPy and check latest
137+
uses: ./
138+
with:
139+
python-version: |
140+
pypy-3.7-v7.3.x
141+
pypy3.9-nightly
142+
pypy3.8
143+
check-latest: true
144+
- name: PyPy and Python version
145+
run: python --version
146+
147+
- name: Run simple code
148+
run: python -c 'import math; print(math.factorial(5))'
149+
150+
- name: Assert PyPy is running
151+
run: |
152+
import platform
153+
assert platform.python_implementation().lower() == "pypy"
154+
shell: python
155+
156+
- name: Assert expected binaries (or symlinks) are present
157+
run: |
158+
EXECUTABLE="pypy-3.7-v7.3.x"
159+
EXECUTABLE=${EXECUTABLE/-/} # remove the first '-' in "pypy-X.Y" -> "pypyX.Y" to match executable name
160+
EXECUTABLE=${EXECUTABLE%%-*} # remove any -* suffixe
161+
${EXECUTABLE} --version
162+
shell: bash
163+
- name: Assert expected binaries (or symlinks) are present
164+
run: |
165+
EXECUTABLE='pypy3.8'
166+
EXECUTABLE=${EXECUTABLE/pypy-/pypy} # remove the first '-' in "pypy-X.Y" -> "pypyX.Y" to match executable name
167+
EXECUTABLE=${EXECUTABLE%%-*} # remove any -* suffixe
168+
${EXECUTABLE} --version
126169
shell: bash

.github/workflows/test-python.yml

+24-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,30 @@ jobs:
190190
- name: Validate version
191191
run: |
192192
$pythonVersion = (python --version)
193-
if ("$pythonVersion" -NotMatch "${{ matrix.python }}"){
194-
Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}"
193+
if ("$pythonVersion" -NotMatch "${{ matrix.python-version }}"){
194+
Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python-version }}"
195+
exit 1
196+
}
197+
$pythonVersion
198+
shell: pwsh
199+
setup-python-multiple-versions:
200+
runs-on: ${{ matrix.os }}
201+
strategy:
202+
fail-fast: false
203+
matrix:
204+
os: [ubuntu-latest, windows-latest, macos-latest]
205+
steps:
206+
- uses: actions/checkout@v3
207+
- name: Setup Python and check latest
208+
uses: ./
209+
with:
210+
python-version: ${{ matrix.python-version }}
211+
check-latest: true
212+
- name: Validate version
213+
run: |
214+
$pythonVersion = (python --version)
215+
if ("$pythonVersion" -NotMatch "${{ matrix.python-version }}"){
216+
Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python-version }}"
195217
exit 1
196218
}
197219
$pythonVersion

dist/setup/index.js

+26-17
Original file line numberDiff line numberDiff line change
@@ -66797,12 +66797,12 @@ function cacheDependencies(cache, pythonVersion) {
6679766797
});
6679866798
}
6679966799
function resolveVersionInput() {
66800-
let version = core.getInput('python-version');
66800+
let version = core.getMultilineInput('python-version');
6680166801
let versionFile = core.getInput('python-version-file');
66802-
if (version && versionFile) {
66802+
if (version.length && versionFile) {
6680366803
core.warning('Both python-version and python-version-file inputs are specified, only python-version will be used.');
6680466804
}
66805-
if (version) {
66805+
if (version.length) {
6680666806
return version;
6680766807
}
6680866808
if (versionFile) {
@@ -66834,21 +66834,30 @@ function run() {
6683466834
}
6683566835
core.debug(`Python is expected to be installed into ${process.env['RUNNER_TOOL_CACHE']}`);
6683666836
try {
66837-
const version = resolveVersionInput();
66837+
let versions;
66838+
const resolvedVersionInput = resolveVersionInput();
6683866839
const checkLatest = core.getBooleanInput('check-latest');
66839-
if (version) {
66840-
let pythonVersion;
66841-
const arch = core.getInput('architecture') || os.arch();
66842-
const updateEnvironment = core.getBooleanInput('update-environment');
66843-
if (isPyPyVersion(version)) {
66844-
const installed = yield finderPyPy.findPyPyVersion(version, arch, updateEnvironment, checkLatest);
66845-
pythonVersion = `${installed.resolvedPyPyVersion}-${installed.resolvedPythonVersion}`;
66846-
core.info(`Successfully set up PyPy ${installed.resolvedPyPyVersion} with Python (${installed.resolvedPythonVersion})`);
66847-
}
66848-
else {
66849-
const installed = yield finder.useCpythonVersion(version, arch, updateEnvironment, checkLatest);
66850-
pythonVersion = installed.version;
66851-
core.info(`Successfully set up ${installed.impl} (${pythonVersion})`);
66840+
if (Array.isArray(resolvedVersionInput)) {
66841+
versions = resolvedVersionInput;
66842+
}
66843+
else {
66844+
versions = [resolvedVersionInput];
66845+
}
66846+
if (versions.length) {
66847+
let pythonVersion = '';
66848+
for (const version of versions) {
66849+
const arch = core.getInput('architecture') || os.arch();
66850+
const updateEnvironment = core.getBooleanInput('update-environment');
66851+
if (isPyPyVersion(version)) {
66852+
const installed = yield finderPyPy.findPyPyVersion(version, arch, updateEnvironment, checkLatest);
66853+
pythonVersion = `${installed.resolvedPyPyVersion}-${installed.resolvedPythonVersion}`;
66854+
core.info(`Successfully set up PyPy ${installed.resolvedPyPyVersion} with Python (${installed.resolvedPythonVersion})`);
66855+
}
66856+
else {
66857+
const installed = yield finder.useCpythonVersion(version, arch, updateEnvironment, checkLatest);
66858+
pythonVersion = installed.version;
66859+
core.info(`Successfully set up ${installed.impl} (${pythonVersion})`);
66860+
}
6685266861
}
6685366862
const cache = core.getInput('cache');
6685466863
if (cache && utils_1.isCacheFeatureAvailable()) {

src/setup-python.ts

+38-30
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ async function cacheDependencies(cache: string, pythonVersion: string) {
2222
await cacheDistributor.restoreCache();
2323
}
2424

25-
function resolveVersionInput(): string {
26-
let version = core.getInput('python-version');
25+
function resolveVersionInput(): string | string[] {
26+
let version: string | string[] = core.getMultilineInput('python-version');
2727
let versionFile = core.getInput('python-version-file');
2828

29-
if (version && versionFile) {
29+
if (version.length && versionFile) {
3030
core.warning(
3131
'Both python-version and python-version-file inputs are specified, only python-version will be used.'
3232
);
3333
}
3434

35-
if (version) {
35+
if (version.length) {
3636
return version;
3737
}
3838

@@ -75,35 +75,43 @@ async function run() {
7575
`Python is expected to be installed into ${process.env['RUNNER_TOOL_CACHE']}`
7676
);
7777
try {
78-
const version = resolveVersionInput();
78+
let versions: string[];
79+
const resolvedVersionInput = resolveVersionInput();
7980
const checkLatest = core.getBooleanInput('check-latest');
8081

81-
if (version) {
82-
let pythonVersion: string;
83-
const arch: string = core.getInput('architecture') || os.arch();
84-
const updateEnvironment = core.getBooleanInput('update-environment');
85-
if (isPyPyVersion(version)) {
86-
const installed = await finderPyPy.findPyPyVersion(
87-
version,
88-
arch,
89-
updateEnvironment,
90-
checkLatest
91-
);
92-
pythonVersion = `${installed.resolvedPyPyVersion}-${installed.resolvedPythonVersion}`;
93-
core.info(
94-
`Successfully set up PyPy ${installed.resolvedPyPyVersion} with Python (${installed.resolvedPythonVersion})`
95-
);
96-
} else {
97-
const installed = await finder.useCpythonVersion(
98-
version,
99-
arch,
100-
updateEnvironment,
101-
checkLatest
102-
);
103-
pythonVersion = installed.version;
104-
core.info(`Successfully set up ${installed.impl} (${pythonVersion})`);
105-
}
82+
if (Array.isArray(resolvedVersionInput)) {
83+
versions = resolvedVersionInput as string[];
84+
} else {
85+
versions = [resolvedVersionInput as string];
86+
}
10687

88+
if (versions.length) {
89+
let pythonVersion = '';
90+
for (const version of versions) {
91+
const arch: string = core.getInput('architecture') || os.arch();
92+
const updateEnvironment = core.getBooleanInput('update-environment');
93+
if (isPyPyVersion(version)) {
94+
const installed = await finderPyPy.findPyPyVersion(
95+
version,
96+
arch,
97+
updateEnvironment,
98+
checkLatest
99+
);
100+
pythonVersion = `${installed.resolvedPyPyVersion}-${installed.resolvedPythonVersion}`;
101+
core.info(
102+
`Successfully set up PyPy ${installed.resolvedPyPyVersion} with Python (${installed.resolvedPythonVersion})`
103+
);
104+
} else {
105+
const installed = await finder.useCpythonVersion(
106+
version,
107+
arch,
108+
updateEnvironment,
109+
checkLatest
110+
);
111+
pythonVersion = installed.version;
112+
core.info(`Successfully set up ${installed.impl} (${pythonVersion})`);
113+
}
114+
}
107115
const cache = core.getInput('cache');
108116
if (cache && isCacheFeatureAvailable()) {
109117
await cacheDependencies(cache, pythonVersion);

0 commit comments

Comments
 (0)