Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add a python-path output #405

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/test-pypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ jobs:
uses: actions/checkout@v2

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


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

- name: PyPy and Python version
run: python --version

Expand Down
17 changes: 16 additions & 1 deletion .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: setup default python
- 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

Expand All @@ -45,10 +50,15 @@ jobs:
uses: actions/checkout@v2

- 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: |
$pythonVersion = (python --version)
Expand All @@ -74,10 +84,15 @@ jobs:
uses: actions/checkout@v2

- name: setup-python 3.9.0-beta.4
id: setup-python
uses: ./
with:
python-version: '3.9.0-beta.4'

- 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)
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ jobs:
python-version: 3.8.1
- name: Verify 3.8.1
run: python __tests__/verify-python.py 3.8.1

- name: Run with setup-python 3.10
id: cp310
uses: ./
with:
python-version: "3.10"
- name: Verify 3.10
run: python __tests__/verify-python.py 3.10
- name: Run python-path sample 3.10
run: pipx run --python '${{ steps.cp310.outputs.python-path }}' nox --version
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ jobs:
```
More details on PyPy syntax and examples of using preview / nightly versions of PyPy can be found in the [Available versions of PyPy](#available-versions-of-pypy) section.

An output is available with the absolute path of the python interpreter executable if you need it:
```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
id: cp310
with:
python-version: "3.10"
- run: pipx run --python '${{ steps.cp310.outputs.python-path }}' nox --version
```

# Getting started with Python + Actions

Check out our detailed guide on using [Python with GitHub Actions](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-python-with-github-actions).
Expand Down
14 changes: 14 additions & 0 deletions __tests__/check-python-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -euo pipefail

PYTHON_PATH="$1"
PATH_EXECUTABLE=$(python -c 'import sys; print(sys.executable)')
PYTHON_PATH_EXECUTABLE=$("${PYTHON_PATH}" -c 'import sys; print(sys.executable)')
if [ "${PATH_EXECUTABLE}" != "${PYTHON_PATH_EXECUTABLE}" ]; then
echo "Executable mismatch."
echo "python in PATH is: ${PATH_EXECUTABLE}"
echo "python-path (${PYTHON_PATH}) is: ${PYTHON_PATH_EXECUTABLE}"
exit 1
fi
echo "python-path: ${PYTHON_PATH}"
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ outputs:
description: "The installed python version. Useful when given a version range as input."
cache-hit:
description: 'A boolean value to indicate a cache entry was found'
python-path:
description: "The absolute path to the Python executable."
runs:
using: 'node16'
main: 'dist/setup/index.js'
Expand Down
9 changes: 8 additions & 1 deletion dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52375,12 +52375,15 @@ function findPyPyVersion(versionSpec, architecture) {
}
const pipDir = utils_1.IS_WINDOWS ? 'Scripts' : 'bin';
const _binDir = path.join(installDir, pipDir);
const binaryExtension = utils_1.IS_WINDOWS ? '.exe' : '';
const pythonPath = path.join(utils_1.IS_WINDOWS ? installDir : _binDir, `python${binaryExtension}`);
const pythonLocation = pypyInstall.getPyPyBinaryPath(installDir);
core.exportVariable('pythonLocation', pythonLocation);
core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig');
core.addPath(pythonLocation);
core.addPath(_binDir);
core.setOutput('python-version', 'pypy' + resolvedPyPyVersion.trim());
core.setOutput('python-path', pythonPath);
return { resolvedPyPyVersion, resolvedPythonVersion };
});
}
Expand Down Expand Up @@ -57027,8 +57030,11 @@ function useCpythonVersion(version, architecture) {
core.exportVariable('LD_LIBRARY_PATH', pyLibPath + libPath);
}
}
const _binDir = binDir(installDir);
const binaryExtension = utils_1.IS_WINDOWS ? '.exe' : '';
const pythonPath = path.join(utils_1.IS_WINDOWS ? installDir : _binDir, `python${binaryExtension}`);
core.addPath(installDir);
core.addPath(binDir(installDir));
core.addPath(_binDir);
if (utils_1.IS_WINDOWS) {
// Add --user directory
// `installDir` from tool cache should look like $RUNNER_TOOL_CACHE/Python/<semantic version>/x64/
Expand All @@ -57042,6 +57048,7 @@ function useCpythonVersion(version, architecture) {
// On Linux and macOS, pip will create the --user directory and add it to PATH as needed.
const installed = versionFromPath(installDir);
core.setOutput('python-version', installed);
core.setOutput('python-path', pythonPath);
return { impl: 'CPython', version: installed };
});
}
Expand Down
6 changes: 6 additions & 0 deletions src/find-pypy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ export async function findPyPyVersion(

const pipDir = IS_WINDOWS ? 'Scripts' : 'bin';
const _binDir = path.join(installDir, pipDir);
const binaryExtension = IS_WINDOWS ? '.exe' : '';
const pythonPath = path.join(
IS_WINDOWS ? installDir : _binDir,
`python${binaryExtension}`
);
const pythonLocation = pypyInstall.getPyPyBinaryPath(installDir);
core.exportVariable('pythonLocation', pythonLocation);
core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig');
core.addPath(pythonLocation);
core.addPath(_binDir);
core.setOutput('python-version', 'pypy' + resolvedPyPyVersion.trim());
core.setOutput('python-path', pythonPath);

return {resolvedPyPyVersion, resolvedPythonVersion};
}
Expand Down
9 changes: 8 additions & 1 deletion src/find-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ export async function useCpythonVersion(
}
}

const _binDir = binDir(installDir);
const binaryExtension = IS_WINDOWS ? '.exe' : '';
const pythonPath = path.join(
IS_WINDOWS ? installDir : _binDir,
`python${binaryExtension}`
);
core.addPath(installDir);
core.addPath(binDir(installDir));
core.addPath(_binDir);

if (IS_WINDOWS) {
// Add --user directory
Expand All @@ -106,6 +112,7 @@ export async function useCpythonVersion(

const installed = versionFromPath(installDir);
core.setOutput('python-version', installed);
core.setOutput('python-path', pythonPath);

return {impl: 'CPython', version: installed};
}
Expand Down