Skip to content

Commit

Permalink
feature: add a python-path output
Browse files Browse the repository at this point in the history
Expose a `python-path` output containing the chosen Python executable path.
  • Loading branch information
mayeut committed May 23, 2022
1 parent fff15a2 commit fa35c81
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 4 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/test-pypy.yml
Expand Up @@ -37,10 +37,22 @@ jobs:
uses: actions/checkout@v2

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


- name: Check python-path
run: |
PATH_PYTHON=$(python -c 'import sys; print(sys.executable)')
SETUP_PYTHON=$('${{ steps.setup-python.outputs.python-path }}' -c 'import sys; print(sys.executable)')
if [ "${SETUP_PYTHON}" != "${PATH_PYTHON}" ]; then
echo "Executable mismatch. python in PATH is ${PATH_PYTHON}; python-path (${{ steps.setup-python.outputs.python-path }}) is ${SETUP_PYTHON}"
exit 1
fi
echo 'python-path: ${{ steps.setup-python.outputs.python-path }}'
shell: bash

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

Expand Down
38 changes: 37 additions & 1 deletion .github/workflows/test-python.yml
Expand Up @@ -23,9 +23,21 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: setup default python
- name: setup default python
id: setup-python
uses: ./

- name: Check python-path
run: |
PATH_PYTHON=$(python -c 'import sys; print(sys.executable)')
SETUP_PYTHON=$('${{ steps.setup-python.outputs.python-path }}' -c 'import sys; print(sys.executable)')
if [ "${SETUP_PYTHON}" != "${PATH_PYTHON}" ]; then
echo "Executable mismatch. python in PATH is ${PATH_PYTHON}; python-path (${{ steps.setup-python.outputs.python-path }}) is ${SETUP_PYTHON}"
exit 1
fi
echo 'python-path: ${{ steps.setup-python.outputs.python-path }}'
shell: bash

- name: Validate version
run: python --version

Expand All @@ -45,10 +57,22 @@ jobs:
uses: actions/checkout@v2

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

- name: Check python-path
run: |
PATH_PYTHON=$(python -c 'import sys; print(sys.executable)')
SETUP_PYTHON=$('${{ steps.setup-python.outputs.python-path }}' -c 'import sys; print(sys.executable)')
if [ "${SETUP_PYTHON}" != "${PATH_PYTHON}" ]; then
echo "Executable mismatch. python in PATH is ${PATH_PYTHON}; python-path (${{ steps.setup-python.outputs.python-path }}) is ${SETUP_PYTHON}"
exit 1
fi
echo 'python-path: ${{ steps.setup-python.outputs.python-path }}'
shell: bash

- name: Validate version
run: |
$pythonVersion = (python --version)
Expand All @@ -74,10 +98,22 @@ 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: |
PATH_PYTHON=$(python -c 'import sys; print(sys.executable)')
SETUP_PYTHON=$('${{ steps.setup-python.outputs.python-path }}' -c 'import sys; print(sys.executable)')
if [ "${SETUP_PYTHON}" != "${PATH_PYTHON}" ]; then
echo "Executable mismatch. python in PATH is ${PATH_PYTHON}; python-path (${{ steps.setup-python.outputs.python-path }}) is ${SETUP_PYTHON}"
exit 1
fi
echo 'python-path: ${{ 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
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
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
2 changes: 2 additions & 0 deletions action.yml
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 interpreter executable."
runs:
using: 'node16'
main: 'dist/setup/index.js'
Expand Down
9 changes: 8 additions & 1 deletion dist/setup/index.js
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
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
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

0 comments on commit fa35c81

Please sign in to comment.