Skip to content

Commit

Permalink
NEW: setup_python.py
Browse files Browse the repository at this point in the history
Download and setup redistributable Python Interpreter from
https://github.com/indygreg/python-build-standalone/ if needed ;)
  • Loading branch information
jedie committed Sep 15, 2024
1 parent 4c45a52 commit d46ff4b
Show file tree
Hide file tree
Showing 10 changed files with 630 additions and 10 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/setup_python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: setup-python

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.12", "3.11", "3.10", "3.9"]
steps:
- uses: actions/checkout@v4

- name: 'Set up Python ${{ matrix.python-version }}'
uses: actions/setup-python@v5
# https://github.com/marketplace/actions/setup-python
with:
python-version: '${{ matrix.python-version }}'

- name: 'Just call --help with Python v${{ matrix.python-version }}'
run: |
python3 manageprojects/setup_python.py --help
- name: 'Call setup python script with Python v${{ matrix.python-version }}'
env:
PYTHONUNBUFFERED: 1
PYTHONWARNINGS: always
run: |
sudo python3 manageprojects/setup_python.py -vv
- name: 'Test the installed interpreter'
run: |
$(python3 manageprojects/setup_python.py) -VV
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Besides this, `manageprojects` also includes other generic helper for Python pac

* `publish_package()` - Build and upload a new release to PyPi, but with many pre-checks.
* `format-file` - Format/Check a Python source file with Darker & Co., useful as IDE action.
* `install_python.py` - Install Python interpreter, if needed, from official Python FTP server, verified.
* `install_python.py` - [Install Python interpreter, if needed, from official Python FTP server, verified.](https://github.com/jedie/manageprojects/blob/main/docs/install_python.md)
* `setup_python.py` - [Download and setup redistributable Python Interpreter, if needed.](https://github.com/jedie/manageprojects/blob/main/docs/setup_python.md)

Read below the `Helper` section.

Expand Down Expand Up @@ -347,6 +348,7 @@ See also git tags: https://github.com/jedie/manageprojects/tags
[comment]: <> (✂✂✂ auto generated history start ✂✂✂)

* [**dev**](https://github.com/jedie/manageprojects/compare/v0.18.0...main)
* 2024-09-15 - NEW: setup_python.py
* 2024-09-15 - Update requirements
* [v0.18.0](https://github.com/jedie/manageprojects/compare/v0.17.1...v0.18.0)
* 2024-08-29 - Fix wrong "module" in publish call :(
Expand Down
53 changes: 53 additions & 0 deletions docs/setup-python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Boot Redistributable Python

TODO: Write Docs ;)

## CLI

The CLI interface looks like e.g.:

```shell
$ python3 setup_python.py --help

usage: setup_python.py [-h] [-v] [--skip-temp-deletion] [--force-update] [major_version]

Download and setup redistributable Python Interpreter from https://github.com/indygreg/python-build-standalone/ if
needed ;)

positional arguments:
major_version Specify the Python version like: 3.10, 3.11, 3.12, ... (default: 3.12)

options:
-h, --help show this help message and exit
-v, --verbose Increase verbosity level (can be used multiple times, e.g.: -vv) (default: 0)
--skip-temp-deletion Skip deletion of temporary files (default: False)
--force-update Update local Python interpreter, even if it is up-to-date (default: False)

```
## Workflow - 1. Check system Python
If the system Python is the same major version as the required Python, we skip the download.
The script just returns the path to the system Python interpreter.
A local installed interpreter (e.g. in ~/.local) will be auto updated.
## Workflow - 7. print the path
If no errors occurred, the path to the Python interpreter will be printed to `stdout`.
So it's usable in shell scripts, like:
```shell
#!/usr/bin/env sh
set -e
PY_313_BIN=$(python3 setup_python.py -v 3.13)
echo "Python 3.13 used from: '${PY_313_BIN}'"
set -x
${PY_313_BIN} -VV
```
14 changes: 7 additions & 7 deletions manageprojects/install_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@

"""DocWrite: install_python.md # Install Python Interpreter
Download Python source code from official Python FTP server:
DocWriteMacro: manageprojects.tests.docwrite_macros.ftp_url"""
DocWriteMacro: manageprojects.tests.docwrite_macros_install_python.ftp_url"""
PY_FTP_INDEX_URL = 'https://www.python.org/ftp/python/'

"""DocWrite: install_python.md ## Supported Python Versions
The following major Python versions are supported and verified with GPG keys:
DocWriteMacro: manageprojects.tests.docwrite_macros.supported_python_versions
DocWriteMacro: manageprojects.tests.docwrite_macros_install_python.supported_python_versions
The GPG keys taken from the official Python download page: https://www.python.org/downloads/"""
GPG_KEY_IDS = {
# Thomas Wouters (3.12.x and 3.13.x source files and tags):
Expand All @@ -58,7 +58,7 @@

"""DocWrite: install_python.md ## Workflow - 3. Check local installed Python
We assume that the `make altinstall` will install local Python interpreter into:
DocWriteMacro: manageprojects.tests.docwrite_macros.default_install_prefix
DocWriteMacro: manageprojects.tests.docwrite_macros_install_python.default_install_prefix
See: https://docs.python.org/3/using/configure.html#cmdoption-prefix"""
DEFAULT_INSTALL_PREFIX = '/usr/local'

Expand Down Expand Up @@ -198,7 +198,7 @@ def install_python(

"""DocWrite: install_python.md ## Workflow - 2. Get latest Python release
We fetch the latest Python release from the Python FTP server, from:
DocWriteMacro: manageprojects.tests.docwrite_macros.ftp_url"""
DocWriteMacro: manageprojects.tests.docwrite_macros_install_python.ftp_url"""
# Get latest full version number of Python from Python FTP:
py_required_version = get_latest_versions(
html=get_html_page(PY_FTP_INDEX_URL),
Expand All @@ -225,7 +225,7 @@ def install_python(
"""DocWrite: install_python.md ## Workflow - 4. Download Python sources
The download will be done in a temporary directory. The directory will be deleted after the installation.
This can be skipped via CLI argument. The directory will be prefixed with:
DocWriteMacro: manageprojects.tests.docwrite_macros.temp_prefix"""
DocWriteMacro: manageprojects.tests.docwrite_macros_install_python.temp_prefix"""
with TemporaryDirectory(prefix=TEMP_PREFIX, delete=delete_temp) as temp_path:
base_url = f'{PY_FTP_INDEX_URL}{py_required_version}'

Expand Down Expand Up @@ -291,7 +291,7 @@ def get_parser() -> argparse.ArgumentParser:
```shell
$ python3 install_python.py --help
DocWriteMacro: manageprojects.tests.docwrite_macros.help
DocWriteMacro: manageprojects.tests.docwrite_macros_install_python.help
```
"""
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -349,6 +349,6 @@ def main() -> Path:
"""DocWrite: install_python.md ## Workflow - 7. print the path
If no errors occurred, the path to the Python interpreter will be printed to `stdout`.
So it's usable in shell scripts, like:
DocWriteMacro: manageprojects.tests.docwrite_macros.example_shell_script
DocWriteMacro: manageprojects.tests.docwrite_macros_install_python.example_shell_script
"""
print(python_path)
Loading

0 comments on commit d46ff4b

Please sign in to comment.