Skip to content

Instantly share code, notes, and snippets.

@glenbot
Created August 19, 2014 18:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenbot/ac6fcdebb515a90013c6 to your computer and use it in GitHub Desktop.
Save glenbot/ac6fcdebb515a90013c6 to your computer and use it in GitHub Desktop.
Utility to help bootstrap python packages
#!/bin/bash
# A shell utility to help bootstrap python packages
# Author: Glen Zangirolami
# https://github.com/glenbot
set -o nounset
set -o errexit
# Check for dependencies
function check_dependencies() {
local GIT_INSTALLED=$(which git)
if [[ -z "${GIT_INSTALLED}" ]]; then
echo "WARNING: Git is required to be installed to auto initialize package as a repo"
fi
}
check_dependencies
# Set a default value if it isn't already set
function set_default() {
local DEFAULT_VALUE=$1
local VALUE=$2
if [[ -z "${VALUE}" ]]; then
echo "${DEFAULT_VALUE}"
else
echo "${VALUE}"
fi
}
# Check to make sure a yes no value occurs
function check_yes_no() {
local VALUE=$1
if [[ ! "${VALUE}" =~ [YyNn]+ ]]; then
echo false
else
echo true
fi
}
# Ask a series of questions to help with creating the packages
echo -e "Package name [Default: 'my_package']: \c"
read PACKAGE_NAME
PACKAGE_NAME=$(set_default "my_package" "${PACKAGE_NAME}")
echo -e "Package author [Default: 'Tim Peters']: \c"
read PACKAGE_AUTHOR
PACKAGE_AUTHOR=$(set_default "Tim Peters" "${PACKAGE_AUTHOR}")
echo -e "Package author email [Default: 'user@example.com']: \c"
read PACKAGE_AUTHOR_EMAIL
PACKAGE_AUTHOR_EMAIL=$(set_default "user@example.com" "${PACKAGE_AUTHOR_EMAIL}")
echo -e "Package URL [Default: 'http://www.example.com']: \c"
read PACKAGE_URL
PACKAGE_URL=$(set_default "http://www.example.com" "${PACKAGE_URL}")
echo -e "Package Version [Default: '0.0.1']: \c"
read PACKAGE_VERSION
PACKAGE_VERSION=$(set_default "0.0.1" "${PACKAGE_VERSION}")
while true; do
echo -e "Is your package going to use git version control (Y/N) [Default: N]? \c"
read PACKAGE_USES_GIT
PACKAGE_USES_GIT=$(set_default "N" "${PACKAGE_USES_GIT}")
if [[ $(check_yes_no "${PACKAGE_USES_GIT}") = "true" ]]; then
break
fi
done
while true; do
echo -e "Does your package require console entry scripts (Y/N) [Default: N]? \c"
read PACKAGE_USES_CONSOLE
PACKAGE_USES_CONSOLE=$(set_default "N" "${PACKAGE_USES_CONSOLE}")
if [[ $(check_yes_no "${PACKAGE_USES_CONSOLE}") = "true" ]]; then
break
fi
done
STATS=$(cat <<EOF
You are about to create a python package with the following values:
Name: ${PACKAGE_NAME}
Author: ${PACKAGE_AUTHOR}
Author Email: ${PACKAGE_AUTHOR_EMAIL}
Url: ${PACKAGE_URL}
Version: ${PACKAGE_VERSION}
Uses git: ${PACKAGE_USES_GIT}
Requires Console: ${PACKAGE_USES_CONSOLE}
EOF
)
echo "${STATS}"
while true; do
echo -e "\nIs this ok (Y/N) [Default: N]? \c"
read OK
OK=$(set_default "N" "${OK}")
if [[ $(check_yes_no "${OK}") = "true" ]]; then
break
fi
done
if [[ "${OK}" =~ [Yy]+ ]]; then
readonly BASE_PATH="output"
readonly BASE_PACKAGE_DIR="${BASE_PATH}/${PACKAGE_NAME}"
readonly PACKAGE_DIR="${BASE_PACKAGE_DIR}/${PACKAGE_NAME}"
echo -e "\nCreating package ${PACKAGE_NAME}"
# make the required directory structure
echo "Making directories ..."
mkdir -p "${PACKAGE_DIR}"
if [[ "${PACKAGE_USES_CONSOLE}" =~ [Yy]+ ]]; then
mkdir -p "${PACKAGE_DIR}/bin"
fi
# make the required files
echo "Creating files ..."
touch "${BASE_PACKAGE_DIR}/__init__.py"
touch "${PACKAGE_DIR}/__init__.py"
if [[ "${PACKAGE_USES_CONSOLE}" =~ [Yy]+ ]]; then
touch "${PACKAGE_DIR}/bin/__init__.py"
fi
SETUP_DOT_PY=$(cat <<EOF
"""${PACKAGE_NAME}"""
from distutils.core import setup
__version__ = "${PACKAGE_VERSION}"
setup(
author = "${PACKAGE_AUTHOR}",
author_email = "${PACKAGE_AUTHOR_EMAIL}",
description = "${PACKAGE_NAME}",
long_description = __doc__,
fullname = "${PACKAGE_NAME}",
name = "${PACKAGE_NAME}",
url = "${PACKAGE_URL}",
version = __version__,
platforms = ["Linux"],
packages = [
"${PACKAGE_NAME}"
],
install_requires = [],
classifiers = [
"Development Status :: 4 - Beta",
"Environment :: Server Environment",
"Intended Audience :: Developers",
"Operating System :: Linux",
"Programming Language :: Python",
]
)
EOF
)
if [[ "${PACKAGE_USES_CONSOLE}" =~ [Yy]+ ]]; then
SETUP_DOT_PY=$(cat <<EOF
"""${PACKAGE_NAME}"""
from distutils.core import setup
__version__ = "${PACKAGE_VERSION}"
setup(
author = "${PACKAGE_AUTHOR}",
author_email = "${PACKAGE_AUTHOR_EMAIL}",
description = "${PACKAGE_NAME}",
long_description = __doc__,
fullname = "${PACKAGE_NAME}",
name = "${PACKAGE_NAME}",
url = "${PACKAGE_URL}",
version = __version__,
platforms = ["Linux"],
packages = [
"${PACKAGE_NAME}",
"${PACKAGE_NAME}.bin"
],
install_requires = [],
entry_points = {
'console_scripts': [
"${PACKAGE_NAME} = ${PACKAGE_NAME}.bin.${PACKAGE_NAME}:main"
]
},
classifiers = [
"Development Status :: 4 - Beta",
"Environment :: Server Environment",
"Intended Audience :: Developers",
"Operating System :: Linux",
"Programming Language :: Python",
]
)
EOF
)
readonly BIN_FILE=$(cat <<EOF
def main():
print("A console script")
EOF)
echo "${BIN_FILE}" > "${PACKAGE_DIR}/bin/${PACKAGE_NAME}.py"
fi
echo "${SETUP_DOT_PY}" > "${BASE_PACKAGE_DIR}/setup.py"
if [[ "${PACKAGE_USES_GIT}" =~ [Yy]+ ]]; then
echo "Initializing GIT repo ..."
readonly CURRENT_DIR=$(pwd)
cd "${BASE_PACKAGE_DIR}" && git init &> /dev/null
cd "${CURRENT_DIR}"
fi
echo -e "\nProject created in ${BASE_PACKAGE_DIR}"
echo "Done."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment