Skip to content

Instantly share code, notes, and snippets.

@neuroticnerd
Created September 17, 2016 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neuroticnerd/74268ced5a16c7c5508411a736779895 to your computer and use it in GitHub Desktop.
Save neuroticnerd/74268ced5a16c7c5508411a736779895 to your computer and use it in GitHub Desktop.
Convenience wrapper for Python packaging using twine

Twine wrapper script

Even once you've figured out the intricacies of Python packaging and figure out why its a good idea to use twine, its still nice to have a few convenience tools to make using it easier.

The pypackage.sh file should be placed in ~/.scripts as ~/.scripts/pypackage and then in your ~/.bash_profile you should have export PATH="$HOME/.scripts:$PATH" so that it can be found from the command line. You can also place the function from the other file into you ~/.bash_profile as an easy way to test the pip-install-ability from the pypi test server.

# extra function to be included in .bash_profile
# > piptest armory
# evaluates to: > pip install --extra-index-url https://testpypi.python.org/pypi armory
function piptest() {
pip install --extra-index-url https://testpypi.python.org/pypi $@
}
#!/usr/bin/env bash
set -o errexit
function error_on_line {
echo "Error occurred on line $1"
if [ $NEED_POP ]; then
popd
fi
exit 1
}
trap 'error_on_line $LINENO' ERR
function usage {
echo "Usage: $1 [OPTS]"
echo ""
echo "This provides wrapper functionality to easily bootstrap"
echo "the necessary operations for distributing a python package."
echo ""
echo "NOTE: this requires the twine python package!"
echo ""
echo "The following options are also available:"
echo ""
echo " -c"
echo " Don't clean dist files."
echo ""
echo " -d"
echo " Specify a directory to use that is not where this was run from."
echo ""
echo " -h"
echo " Prints this usage information and quits."
echo ""
echo " -l"
echo " Operate on the live server instead of the test server."
echo ""
echo " -p"
echo " Create the distribution packages."
echo ""
echo " -r"
echo " Register the distribution packages."
echo ""
echo " -u"
echo " Additionally uploads the dist files to the server."
echo ""
}
NEED_POP=0
CLEAN_DIST=1
TEST_RUN=1
UPLOAD_DIST=0
CREATE_DIST=0
REGISTER_PKG=0
while getopts "cd:hlpru" opt; do
case $opt in
c)
CLEAN_DIST=0
;;
d)
BASE_DIR=$OPTARG
;;
h)
usage $0
exit 0
;;
l)
TEST_RUN=0
;;
p)
CREATE_DIST=1
;;
r)
REGISTER_PKG=1
;;
u)
UPLOAD_DIST=1
;;
\?)
usage $0
exit 1
;;
esac
done
# setup the working directory
if [ $BASE_DIR ]; then
if [[ -e "$BASE_DIR" ]]; then
pushd $BASE_DIR
NEED_POP=1
else
echo "specified directory does not exist! $BASE_DIR"
fi
else
echo "using current directory."
fi
# switch between testing and live
if [ ${TEST_RUN} -eq 1 ]; then
echo "TEST mode enabled"
SERVER_ARGS="-r test"
else
echo "LIVE mode enabled"
SERVER_ARGS=""
fi
# make sure that dist files are removed if needed
if [ ${CLEAN_DIST} -eq 1 ]; then
echo "cleaning dist files..."
rm -v dist/* || true
fi
# run the packaging commands
if [ ${CREATE_DIST} -eq 1 ]; then
python setup.py sdist bdist_wheel
else
echo "skipping dist creation."
fi
# register the package
if [ ${REGISTER_PKG} -eq 1 ]; then
for filename in dist/*; do
echo "twine register $SERVER_ARGS $filename"
twine register $SERVER_ARGS "$filename"
done
else
echo "skipping package register."
fi
# upload the dist files
if [ ${UPLOAD_DIST} -eq 1 ]; then
echo "twine upload $SERVER_ARGS -s dist/*"
twine upload $SERVER_ARGS -s dist/*
else
echo "skipping dist file uploads."
fi
# cleanup if needed
if [ ${NEED_POP} -eq 1 ]; then
popd
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment