Zack Williams | 5aa37e1 | 2019-02-13 13:28:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # pypi-publish.sh - Publishes Python modules to PyPI |
| 4 | # |
| 5 | # Makes the following assumptions: |
| 6 | # - PyPI credentials are populated in ~/.pypirc |
| 7 | # - git repo is tagged with a SEMVER released version. If not, exit. |
Zack Williams | 96fecf0 | 2019-03-27 13:52:01 -0700 | [diff] [blame] | 8 | # - If required, Environmental variables can be set for: |
| 9 | # PYPI_INDEX - name of PyPI index to use (see contents of ~/.pypirc for reference) |
Zack Williams | 5aa37e1 | 2019-02-13 13:28:29 -0700 | [diff] [blame] | 10 | # PYPI_MODULE_DIRS - pipe-separated list of modules to be uploaded |
Zack Williams | 96fecf0 | 2019-03-27 13:52:01 -0700 | [diff] [blame] | 11 | # PYPI_PREP_COMMANDS - commands to run (in root directory) to prepare for sdist |
Zack Williams | 5aa37e1 | 2019-02-13 13:28:29 -0700 | [diff] [blame] | 12 | |
| 13 | set -eu -o pipefail |
| 14 | |
| 15 | echo "Using twine version:" |
| 16 | twine --version |
| 17 | |
| 18 | pypi_success=0 |
| 19 | |
| 20 | # environmental vars |
| 21 | WORKSPACE=${WORKSPACE:-.} |
Zack Williams | 96fecf0 | 2019-03-27 13:52:01 -0700 | [diff] [blame] | 22 | PYPI_PREP_COMMANDS=${PYPI_PREP_COMMANDS:-} |
Zack Williams | 5aa37e1 | 2019-02-13 13:28:29 -0700 | [diff] [blame] | 23 | PYPI_INDEX=${PYPI_INDEX:-testpypi} |
| 24 | PYPI_MODULE_DIRS=${PYPI_MODULE_DIRS:-.} |
| 25 | |
| 26 | # check that we're on a semver released version |
| 27 | GIT_VERSION=$(git tag -l --points-at HEAD) |
| 28 | |
| 29 | if [[ "$GIT_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 30 | then |
| 31 | echo "git has a SemVer released version tag: '$GIT_VERSION', publishing to PyPI" |
| 32 | else |
| 33 | echo "No SemVer released version tag found, exiting..." |
| 34 | exit 0 |
| 35 | fi |
| 36 | |
Zack Williams | 96fecf0 | 2019-03-27 13:52:01 -0700 | [diff] [blame] | 37 | # Run commands if PYPI_PREP_COMMANDS if not null |
| 38 | if [[ -n "$PYPI_PREP_COMMANDS" ]] |
| 39 | then |
| 40 | $PYPI_PREP_COMMANDS |
| 41 | fi |
| 42 | |
Zack Williams | 5aa37e1 | 2019-02-13 13:28:29 -0700 | [diff] [blame] | 43 | # iterate over $PYPI_MODULE_DIRS |
| 44 | # field separator is pipe character |
| 45 | IFS=$'|' |
| 46 | for pymod in $PYPI_MODULE_DIRS |
| 47 | do |
| 48 | pymoddir="$WORKSPACE/$pymod" |
| 49 | |
| 50 | if [ ! -f "$pymoddir/setup.py" ] |
| 51 | then |
| 52 | echo "Directory with python module not found at '$pymoddir'" |
| 53 | pypi_success=1 |
| 54 | else |
| 55 | pushd "$pymoddir" |
| 56 | |
| 57 | echo "Building python module in '$pymoddir'" |
| 58 | # Create source distribution |
| 59 | python setup.py sdist |
| 60 | |
| 61 | # Upload to PyPI |
| 62 | echo "Uploading to PyPI" |
| 63 | twine upload -r "$PYPI_INDEX" dist/* |
| 64 | |
| 65 | popd |
| 66 | fi |
| 67 | done |
| 68 | |
| 69 | exit $pypi_success |