Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Oct 6, 2023
1 parent 727001c commit b0105b2
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ignore JetBrains IDE files
.idea/
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# action-setup-python

This action provides the following functionality for GitHub Actions users:

- Installing a version of Python or PyPy and (by default) adding it to the PATH, including Python 2.7.

The action was developed after GitHub decided to remove support for Python 2.7 from their `actions/setup-python` action.

## Basic usage

See [action.yml](action.yml)

**Python**
```yaml
steps:
- uses: actions/checkout@v4
- uses: LizardByte/action-setup-python
with:
python-version: '3.10'
- run: python my_script.py
```
**Python 2.7**
```yaml
steps:
- uses: actions/checkout@v4
- uses: LizardByte/action-setup-python
with:
python-version: '2.7'
- run: python my_script.py
```
**PyPy**
```yaml
steps:
- uses: actions/checkout@v4
- uses: LizardByte/action-setup-python
with:
python-version: 'pypy3.9'
- run: python my_script.py
```
The `python-version` input is required.

If the version of Python is not `2.7` the action will use `actions/setup-python` and pass the version to that
action. Otherwise, it will install Python 2.7 on your platform.

## License

The scripts and documentation in this project are released under the [MIT License](LICENSE).

## Contributions

Contributions are welcome! See our
[Contributor's Guide](https://docs.lizardbyte.dev/en/latest/developers/code_of_conduct.html).
113 changes: 113 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
name: "Setup Python"
description: "Set up a specific version of Python, including Python 2.7, and add the command-line tools to the PATH."
author: "LizardByte"
inputs:
python-version:
description: |
Version range or exact version of Python or PyPy to use, using SemVer's version range syntax.
Reads from .python-version if unset.
required: true

runs:
using: "composite"
steps:
- name: Setup Python 3+
if: ${{ inputs.python-version != '2.7' }}
uses: actions/setup-python@v4
with:
python-version: ${{ inputs.python-version }}

- name: Windows Install
shell: bash
if: ${{ inputs.python-version == '2.7' && runner.os == 'Windows' }}
run: |
choco install python2 --version=2.7.18 -y --no-progress
- name: Linux Install
shell: bash
if: ${{ inputs.python-version == '2.7' && runner.os == 'Linux' }}
run: |
# get the version from /etc/os-release
version=$(grep -oP '(?<=VERSION_ID=")\d+' /etc/os-release)
echo "Ubuntu version: $version"
sudo apt-get update
if [[ $version == 20 ]]; then
sudo apt-get install python2.7 -y
elif [[ $version == 22 ]]; then
sudo apt-get install python2 python-pip -y
fi
- name: macOS Install
shell: bash
if: ${{ inputs.python-version == '2.7' && runner.os == 'macOS' }}
run: |
python_version=$(python --version 2>&1)
echo "Python version: ${python_version}"
if [[ "${python_version}" == *"Python 2.7"* ]]; then
echo "${python_version} already installed, nothing to do"
else
echo "Python 2.7 not installed"
exit 1
fi
- name: Setup Python Environment
if: ${{ inputs.python-version == '2.7' }}
shell: bash
run: |
echo "Current system Python Version:"
python --version
echo "Setting paths"
if [[ "${{ runner.os }}" == "Windows" ]]; then
export PATH="/c/Python27:/c/Python27/Scripts:$PATH"
echo "moving GitHub installed Python version"
mv "/c/hostedtoolcache/windows/Python/3.9.13" "/c/hostedtoolcache/windows/Python/3.9.13-backup"
# set venv path
venv_base_path="/c/tmp/python27/venv"
venv_base_path_windows="C:\\tmp\\python27\\venv"
venv_dir="Scripts"
elif [[ "${{ runner.os }}" == "Linux" ]]; then
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
venv_base_path="/tmp/python27/venv"
venv_dir="bin"
elif [[ "${{ runner.os }}" == "macOS" ]]; then
venv_base_path="/tmp/python27/venv"
venv_dir="bin"
fi
echo "New Python version:"
python --version
echo "Bootstrapping pip"
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
python get-pip.py
rm -f get-pip.py
echo "Installing virtualenv"
python -m pip install virtualenv
# create venv
python -m virtualenv ${venv_base_path}
# activate venv
source ${venv_base_path}/${venv_dir}/activate
# update
python -m pip --no-python-version-warning --disable-pip-version-check install --upgrade pip setuptools
# update the path environment, so the next steps can use the venv
# required to use the shell
if [[ "${{ runner.os }}" == "Windows" ]]; then
echo "${venv_base_path_windows}\\${venv_dir}" >> $GITHUB_PATH
else
echo "${venv_base_path}/${venv_dir}" >> $GITHUB_PATH
fi
# show python version
echo "Python venv version:"
python --version

0 comments on commit b0105b2

Please sign in to comment.