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

Get python project name and version from files. #405

Merged
merged 1 commit into from
Sep 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions .github/actions/python-project-name/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2024 The Linux Foundation <https://linuxfoundation.org>

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:
Expand All @@ -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"

Expand Down
40 changes: 25 additions & 15 deletions .github/actions/python-project-version/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2024 The Linux Foundation <https://linuxfoundation.org>

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

Expand All @@ -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
31 changes: 12 additions & 19 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down