Skip to content

Instantly share code, notes, and snippets.

@williamcanin
Last active February 4, 2020 17:08
Show Gist options
  • Save williamcanin/7ab295790e841929cf83c0b1676269af to your computer and use it in GitHub Desktop.
Save williamcanin/7ab295790e841929cf83c0b1676269af to your computer and use it in GitHub Desktop.
Shell file for managing Python packages.
#!/usr/bin/env bash
# by: William C. Canin
# Version: 1.0.2
# Type: shellscript
# Executor: bash
# Description:
# This file is responsible for:
# * Create/Compile the Python package
# * Clear compilation/creation
# * Install the Python package
# * Reinstall the Python package
# * Uninstall the Python package
# * Upload the package to the Test PyPi server
# * Upload the package to the PyPi server
# Usage:
# $ bin/bootstrap { install [-i] | uninstall [-u] | reinstall [-r] |
# build [-b] | clear [-c] | testpypi [-tpp] | pypi [-pp] }
### {{ Global variables.
PACKAGE_NAME="< PACKAGE NAME >" ## Name of the package you are creating
PYTHON_EXEC="< PYTHON EXECUTABLE >" ## Executável em Python. E.g: python | python3
## }}
### {{ Function color for messages.
function _msg_header_ () {
printf "\e[0;36m→ %s\e[0m$2" "$1"
}
function _msg_reply_ () {
printf "\e[0;36m→ %s\e[0m$2" "$1"
}
function _msg_finish_ () {
printf "\e[0;32m✔ %s\e[0m\n" "$@"
}
function _msg_warning_ () {
printf "\e[0;33m⚠ %s\e[0m\n" "$@"
}
function _msg_error_ () {
printf "\e[0;31m✖ %s\e[0m\n" "$@"
}
## }}
## {{ BEGIN
### {{ Create virtual machine.
function _create_virtual_machine () {
if [[ ! -d "venv" ]]; then
_msg_header_ "Creating machine virtual ..." "\n"
$PYTHON_EXEC -m venv venv
_msg_finish_ "Machine virtual created!"
fi
}
## }}
### {{ Verify that the virtual machine exists, if it does not exist,
# shows the commands to execute.
function _verify_virtual_machine () {
if [[ ! $VIRTUAL_ENV ]]; then
_msg_header_ "Execute the commands:" "\n"
_msg_header_ "$ . venv/bin/activate" "\n"
_msg_header_ "$ pip install -r requirements-dev.txt && pip install -r requirements.txt" "\n"
exit 1
fi
}
## }}
### {{ Main.
function _help () {
cat << EOT
Welcome to the package manager.
Usage: $0 <options>
options:
> build | -b Compile the package.
> clean | -c Clears the build.
> install | -i Install the package.
> reinstall | -r Reinstall the package.
> uninstall | -u Uninstall the package.
> testpypi | -tpp Upload the package to the Test PyPi server.
> pypi | -pp Upload the package to the PyPi server.
Copyright (c) 2019-$(date +%Y) - William C. Canin
EOT
}
## }}
### {{ Main.
function _main (){
_create_virtual_machine
_verify_virtual_machine
case $1 in
build|-b|-B)
_msg_header_ "[ Clearing cache and compiling ]" "\n"
./"$0" clear
_msg_header_ "Compiling..." "\n"
./setup.py bdist bdist_wheel
_msg_finish_ "Compilation completed!"
;;
install|-i|-I)
_msg_header_ "[ Installed the package ]" "\n"
./"$0" build
./setup.py install
# ./setup.py develop
# shellcheck disable=SC1090
_msg_finish_ "Installation completed!"
;;
reinstall|-r|-R)
_msg_header_ "[ Reinstallation ]" "\n"
./"$0" uninstall
./"$0" install
_msg_finish_ "Reinstallation completed!"
;;
clear|-c|-C)
_msg_header_ "Cleaning build..." "\n"
rm -rf ./build ./dist ./**/*.egg-info
_msg_finish_ "Clean build!"
;;
uninstall|-u|-U)
_msg_header_ "Uninstalling the package..." "\n"
pip uninstall ${PACKAGE_NAME} -y
# shellcheck disable=SC1090
_msg_finish_ "Uninstall completed!"
;;
# To make it easier for the user to choose "-tpp" and "-pp", you can create a
# "Pypi" configuration file, "pypirc". This file is in your user's HOME.
# Here is an example of the file content for the "pypi.org" and
# "test.pypi.org" servers:
# [distutils]
# index-servers=
# pypi
# testpypi
# [pypi]
# repository: https://upload.pypi.org/legacy/
# username: you_username_pypi
# [testpypi]
# repository: https://test.pypi.org/legacy/
# username: you_username_testpypi
testpypi|-tpp)
_msg_header_ "[ Upload to Test Pypi (test.pypi.org) ]" "\n"
./"$0" build
_msg_header_ "Starting upload..."
twine upload --repository testpypi dist/*
_msg_finish_ "Upload complete!"
;;
pypi|-pp)
_msg_header_ "[ Upload to Pypi (pypi.org) ]" "\n"
./"$0" build
_msg_header_ "Starting upload..."
twine upload --repository pypi dist/*
_msg_finish_ "Upload complete!"
# twine upload dist/*
;;
help|-h)
_help
;;
*)
# shellcheck disable=SC2059
./"$0" help
;;
esac
exit 0
}
## }}
### Start.
_main "$1"
## END }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment