[SEBA-451]

Automation for publishing python modules to PyPI

Change-Id: If93f927ba61838b7e72b3a447a3144c5bfa417ef
diff --git a/jjb/shell/pypi-publish.sh b/jjb/shell/pypi-publish.sh
new file mode 100755
index 0000000..0f451cc
--- /dev/null
+++ b/jjb/shell/pypi-publish.sh
@@ -0,0 +1,61 @@
+#!/usr/bin/env bash
+
+# pypi-publish.sh - Publishes Python modules to PyPI
+#
+# Makes the following assumptions:
+# - PyPI credentials are populated in ~/.pypirc
+# - git repo is tagged with a SEMVER released version. If not, exit.
+# - If required, Environmental variables are set for:
+#     PYPI_INDEX - name of PyPI index to use (see contents of ~/.pypirc)
+#     PYPI_MODULE_DIRS - pipe-separated list of modules to be uploaded
+
+set -eu -o pipefail
+
+echo "Using twine version:"
+twine --version
+
+pypi_success=0
+
+# environmental vars
+WORKSPACE=${WORKSPACE:-.}
+PYPI_INDEX=${PYPI_INDEX:-testpypi}
+PYPI_MODULE_DIRS=${PYPI_MODULE_DIRS:-.}
+
+# check that we're on a semver released version
+GIT_VERSION=$(git tag -l --points-at HEAD)
+
+if [[ "$GIT_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
+then
+  echo "git has a SemVer released version tag: '$GIT_VERSION', publishing to PyPI"
+else
+  echo "No SemVer released version tag found, exiting..."
+  exit 0
+fi
+
+# iterate over $PYPI_MODULE_DIRS
+# field separator is pipe character
+IFS=$'|'
+for pymod in $PYPI_MODULE_DIRS
+do
+  pymoddir="$WORKSPACE/$pymod"
+
+  if [ ! -f "$pymoddir/setup.py" ]
+  then
+    echo "Directory with python module not found at '$pymoddir'"
+    pypi_success=1
+  else
+    pushd "$pymoddir"
+
+    echo "Building python module in '$pymoddir'"
+    # Create source distribution
+    python setup.py sdist
+
+    # Upload to PyPI
+    echo "Uploading to PyPI"
+    twine upload -r "$PYPI_INDEX" dist/*
+
+    popd
+  fi
+done
+
+exit $pypi_success