From 436da7a1a8c4c8764d7bbd7d8415faafe0205cff Mon Sep 17 00:00:00 2001 From: Xavier Barrachina Civera Date: Mon, 30 Sep 2024 13:17:11 +0200 Subject: [PATCH] Get python project name and version from files. Use pyproject.toml if present, otherwise setup.py. Signed-off-by: Xavier Barrachina Civera --- .../actions/python-project-name/action.yaml | 24 ++++++----- .../python-project-version/action.yaml | 40 ++++++++++++------- .github/workflows/actions.yaml | 31 ++++++-------- 3 files changed, 51 insertions(+), 44 deletions(-) diff --git a/.github/actions/python-project-name/action.yaml b/.github/actions/python-project-name/action.yaml index 1f9e8db..553a401 100644 --- a/.github/actions/python-project-name/action.yaml +++ b/.github/actions/python-project-name/action.yaml @@ -2,12 +2,12 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: 2024 The Linux Foundation -name: "Python Project Name" +name: "🐍 Get Python Project Name" description: "Returns the name of the Python Project" outputs: PYTHON_PROJECT_NAME: - description: "The name of the Python project from pyproject.toml" + description: "The name of the Python project from pyproject.toml or setup.py" value: ${{ steps.python-project-name.outputs.python_project_name }} runs: @@ -29,18 +29,22 @@ runs: fi if [ -f pyproject.toml ]; then - PYTHON_PROJECT_NAME=$(grep -E '^name = ".*"$' pyproject.toml | awk '{print $3}' | sed 's:"::g') + echo "Extracting project name from pyproject.toml." + PYTHON_PROJECT_NAME=$(pdm show --name) + + elif [ -f setup.py ]; then + echo "Extracting project name from setup.py." + pip install -q --upgrade pip + pip install --upgrade -q setuptools + PYTHON_PROJECT_NAME=$(python ./setup.py --name) + + else + echo "Error: neither pyproject.toml not setup.py was found"; exit 1 fi - # ToDo: get project name from other files - #elif [ -f setup.py ]; then - # PYTHON_PROJECT_NAME=$(grep -E '^name = ".*"$' setup.py | awk '{print $3}' | sed 's:"::g') - #else - # echo "Error: neither pyproject.toml not setup.py was found"; exit 1 - # fi echo "PYTHON_PROJECT_NAME: $PYTHON_PROJECT_NAME" - # Make available in the environment + # Make available in the environment echo "python_project_name=$PYTHON_PROJECT_NAME" >> "$GITHUB_ENV" echo "python_project_name=$PYTHON_PROJECT_NAME" >> "$GITHUB_OUTPUT" diff --git a/.github/actions/python-project-version/action.yaml b/.github/actions/python-project-version/action.yaml index 6cc57a3..be78fc9 100644 --- a/.github/actions/python-project-version/action.yaml +++ b/.github/actions/python-project-version/action.yaml @@ -2,21 +2,22 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: 2024 The Linux Foundation -name: "🐍 Get Project Version from pyproject.toml" +name: "🐍 Get Python Project Version" +description: "Returns the version of the Python Project" outputs: - VERSION: - description: "Version string from pyproject.toml" - value: ${{ steps.fetch.outputs.project_version }} + PYTHON_PROJECT_VERSION: + description: "The version of the Python project from pyproject.toml or setup.py" + value: ${{ steps.python-project-version.outputs.python_project_version }} runs: using: "composite" steps: - - name: "Get project version from pyproject.toml" - id: fetch + - name: "Capture Python project version" + id: python-project-version shell: bash run: | - # Check pushed tag against pyproject.toml + # Capture Python project version #SHELLCODESTART @@ -27,15 +28,24 @@ runs: export GITHUB_ENV="/dev/null" fi - if [ ! -f pyproject.toml ]; then - echo "Error: pyproject.toml file not found" - echo "Check that repository is checked out" - exit 1 + if [ -f pyproject.toml ]; then + echo "Extracting project version from pyproject.toml." + PYTHON_PROJECT_VERSION=$(pdm show --version) + + elif [ -f setup.py ]; then + echo "Extracting project version from setup.py." + pip install -q --upgrade pip + pip install --upgrade -q setuptools + PYTHON_PROJECT_VERSION=$(python ./setup.py --version) + + else + echo "Error: neither pyproject.toml not setup.py was found"; exit 1 fi - # Extract project version from pyproject.toml - PROJECT_VERSION=$(grep -E '^version = ".*"$' pyproject.toml | awk '{print $3}' | sed 's:"::g') - echo "Version from pyproject.toml: $PROJECT_VERSION" - echo "project_version=$PROJECT_VERSION" >> "$GITHUB_OUTPUT" + echo "PYTHON_PROJECT_VERSION: $PYTHON_PROJECT_VERSION" + + # Make available in the environment + echo "python_project_version=$PYTHON_PROJECT_VERSION" >> "$GITHUB_ENV" + echo "python_project_version=$PYTHON_PROJECT_VERSION" >> "$GITHUB_OUTPUT" #SHELLCODEEND diff --git a/.github/workflows/actions.yaml b/.github/workflows/actions.yaml index 13a8ef4..d09d1b9 100644 --- a/.github/workflows/actions.yaml +++ b/.github/workflows/actions.yaml @@ -48,27 +48,15 @@ jobs: # Does not currently work: https://github.com/actions/checkout/issues/1471 fetch-tags: true # The semantic-tag-fetch action currently contains a workaround for this behaviour - - - name: "Action: string-extract-with-regex" - id: string-extract-with-regex - # yamllint disable-line rule:line-length - uses: os-climate/osc-github-devops/.github/actions/string-extract-with-regex@main + - name: "Set up Python" + uses: actions/setup-python@v5 with: - filename: "pyproject.toml" - regex: '^name = ".*"$' - flags: "-E" + python-version: "3.11" - - name: "Validate: string-extract-with-regex" - shell: bash - run: | - # Check output from: string-extract-with-regex - PYTHON_PROJECT_NAME="${{ steps.string-extract-with-regex.outputs.extracted_string }}" - if [ "$PYTHON_PROJECT_NAME" != "osc-github-devops" ]; then - echo "ERROR: Python project name was not as expected" - echo "PYTHON_PROJECT_NAME: $PYTHON_PROJECT_NAME"; exit 1 - else - echo "Returned project name correct: $PYTHON_PROJECT_NAME" - fi + - name: "Install PDM tooling" + uses: pdm-project/setup-pdm@v4 + with: + python-version: "3.11" - name: "Action: python-project-name" id: python-project-name @@ -87,6 +75,11 @@ jobs: echo "Returned project name correct: $PYTHON_PROJECT_NAME" fi + - name: "Action: python-project-version" + id: python-project-version + # yamllint disable-line rule:line-length + uses: os-climate/osc-github-devops/.github/actions/python-project-version@main + - name: "Action: python-project-name-match-repo-name" id: python-project-name-match-repo-name # yamllint disable-line rule:line-length