Created
October 29, 2021 20:48
-
-
Save zhangt58/16ec0ccb13d7addcca6539b04a02df53 to your computer and use it in GitHub Desktop.
Build a wheel package from a git tracked Python repository, and publish it to PyPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Build a wheel package from a git tracked Python repository, | |
| # and publish it to PyPI. | |
| # | |
| # Tong Zhang, 2021-10-29 | |
| # | |
| # | |
| set -Eeuo pipefail | |
| trap cleanup SIGINT SIGTERM ERR EXIT | |
| cwd=$(pwd) | |
| usage() { | |
| cat <<EOF | |
| Usage: $(basename "${BASH_SOURCE[0]}") [-r repo_path -b branch] [-w whl] build [publish...] | |
| Build wheel package from a GIT managed Python repository, and publish | |
| the package to PyPI. | |
| Available options: | |
| -r, --repo Python GIT repository path | |
| -b, --branch Branch of --repo to work on | |
| -w, --wheel The path for a wheel package | |
| -h, --help Print this help and exit | |
| -v, --verbose Print script debug info | |
| build Buil wheel package | |
| publish Publish package to PyPI | |
| EOF | |
| exit | |
| } | |
| cleanup() { | |
| trap - SIGINT SIGTERM ERR EXIT | |
| for d in "${TMPDIRS[@]}" | |
| do | |
| rm -rf $d | |
| done | |
| } | |
| setup_colors() { | |
| if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then | |
| NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m' | |
| else | |
| NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW='' | |
| fi | |
| } | |
| msg() { | |
| echo >&2 -e "${1-}" | |
| } | |
| die() { | |
| local msg=$1 | |
| local code=${2-1} # default exit status 1 | |
| msg "$msg" | |
| exit "$code" | |
| } | |
| parse_params() { | |
| # default values of variables set from repo_path, branch | |
| repo_path='' | |
| branch='' | |
| wheel='' | |
| while :; do | |
| case "${1-}" in | |
| -r | --repo) | |
| repo_path="${2-}" | |
| shift | |
| ;; | |
| -b | --branch) | |
| branch="${2-}" | |
| shift | |
| ;; | |
| -w | --wheel) | |
| wheel="${2-}" | |
| shift | |
| ;; | |
| -h | --help) usage ;; | |
| -v | --verbose) set -x ;; | |
| --no-color) NO_COLOR=1 ;; | |
| -?*) die "Unknown option: $1" ;; | |
| *) break ;; | |
| esac | |
| shift | |
| done | |
| args=("$@") | |
| [[ -z "${repo_path-}" ]] && die "Missing required parameter: --repo" | |
| [[ -z "${branch-}" ]] && die "Missing required parameter: --branch" | |
| [[ ${#args[@]} -eq 0 ]] && die "Missing script arguments" | |
| return 0 | |
| } | |
| setup_colors | |
| parse_params "$@" | |
| # script logic here | |
| build() { | |
| tmpdir=`mktemp -d /tmp/bld-whl.XXXX` | |
| TMPDIRS+=($tmpdir) | |
| git clone -b ${branch} ${repo_path} ${tmpdir} | |
| cd ${tmpdir} | |
| python3 setup.py bdist_wheel | |
| [ ! -e ${repo_path}/dist ] && mkdir ${repo_path}/dist | |
| cp dist/*.whl ${cwd} | |
| } | |
| publish() { | |
| twine upload --repository pypi ${wheel} | |
| } | |
| TMPDIRS=() | |
| for cmd in ${args[*]-} | |
| do | |
| if [[ "x$cmd" == "xbuild" ]]; then | |
| msg "${RED}Building package...${NOFORMAT}" | |
| build | |
| elif [[ "x$cmd" == "xpublish" ]]; then | |
| msg "${RED}Publishing package...${NOFORMAT}" | |
| publish | |
| fi | |
| done |
Author
zhangt58
commented
Oct 29, 2021
Author
# build a wheel package
$ wheel-action.sh --repo <repo-path> --branch devel build
# publish a wheel package to PyPI
$ wheel-action.sh --repo <repo-path> --branch devel --wheel <wheel-path> publish
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment