|
#!/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 |