From ff706563d754e642609a7008f4568a5386f3e94c Mon Sep 17 00:00:00 2001 From: mayeut Date: Mon, 23 May 2022 20:59:37 +0200 Subject: [PATCH] feature: add a `python-path` output Expose a `python-path` output containing the chosen Python executable path. --- .github/workflows/test-pypy.yml | 7 ++++++- .github/workflows/test-python.yml | 17 ++++++++++++++++- .github/workflows/workflow.yml | 10 ++++++++++ README.md | 14 ++++++++++++++ __tests__/check-python-path.sh | 14 ++++++++++++++ action.yml | 2 ++ dist/setup/index.js | 9 ++++++++- src/find-pypy.ts | 6 ++++++ src/find-python.ts | 9 ++++++++- 9 files changed, 84 insertions(+), 4 deletions(-) create mode 100755 __tests__/check-python-path.sh diff --git a/.github/workflows/test-pypy.yml b/.github/workflows/test-pypy.yml index 0eb06c3..5f23b21 100644 --- a/.github/workflows/test-pypy.yml +++ b/.github/workflows/test-pypy.yml @@ -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 diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 8f23301..00043f4 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -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 @@ -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) @@ -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) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index fee5ae7..171e205 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -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 diff --git a/README.md b/README.md index 2ace1f7..7508d35 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/__tests__/check-python-path.sh b/__tests__/check-python-path.sh new file mode 100755 index 0000000..1ebee79 --- /dev/null +++ b/__tests__/check-python-path.sh @@ -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}" diff --git a/action.yml b/action.yml index bda521d..00c1e5e 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/dist/setup/index.js b/dist/setup/index.js index bf07395..6c83cfb 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -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 }; }); } @@ -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//x64/ @@ -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 }; }); } diff --git a/src/find-pypy.ts b/src/find-pypy.ts index 5342c64..1008ed6 100644 --- a/src/find-pypy.ts +++ b/src/find-pypy.ts @@ -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}; } diff --git a/src/find-python.ts b/src/find-python.ts index 3959ecf..0a3dde1 100644 --- a/src/find-python.ts +++ b/src/find-python.ts @@ -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 @@ -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}; }