|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Script will exit if exit code is not 0. |
| 4 | +set -e |
| 5 | + |
| 6 | +# Define colors for log. |
| 7 | +_info() { echo "\033[1m[INFO]\033[0m $1" ; } |
| 8 | +_ok() { echo "\033[32m[OK]\033[0m $1" ; } |
| 9 | +_error() { echo "\033[31m[ERROR]\033[0m $1" ; } |
| 10 | +_warn() { echo "\033[33m[WARN]\033[0m $1" ; } |
| 11 | +_logo() { echo "\033[1m $1\033[0m" ; } |
| 12 | + |
| 13 | + |
| 14 | +# Header |
| 15 | +echo "" |
| 16 | +echo "" |
| 17 | +_logo " █▌ ▀▀▀▀▀▀ ║█ ╫█ ▐█µ ▐█ ▄██▀▀██▌ ██ ▀└ █▌ ╒█▌ █µ █▌ " |
| 18 | +_logo " ▐█ ███▌ ██ ████ ██ ██ ╟█ ▐█ ▐█ ║█ ╫█ ║██▌ ╓███ " |
| 19 | +_logo " ██ ▀▀▀▀▀▀ ╒█Γ└██▓█ █▌ ██▓█▌▐█ ██ ██ ██ ██ ██ ███▀ ██ " |
| 20 | +_logo "┌▀ ██ ▀██ ║█ ╙██ └██ ╓██ █▌ ╒█▌ ██ ██ ╒█▌ ╙ ╒█Γ " |
| 21 | +_logo " -▀▀▀▀ ╝▀▀▀▀▀▀ ▀▀ ▀ ▀▀ ▀ ▀▀█▀▀ ╘▀▀▀▀▀ ╝▀ ▀██▀ ╝▀ ╝▀ " |
| 22 | +_logo " " |
| 23 | +_logo " P Y T H O N B U I L D S C R I P T " |
| 24 | +_logo " ------------------------------------------------------------------------- " |
| 25 | +echo "" |
| 26 | +echo "" |
| 27 | + |
| 28 | +# Check if Python 3 is installed and install it if not. |
| 29 | +_info "Checking and installing requirements ..." |
| 30 | +if test ! "$(which python3)"; then |
| 31 | + _info "Python 3 is not installed! Installing with Homebrew ..." |
| 32 | + /bin/bash -c \ |
| 33 | + "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" |
| 34 | + brew update |
| 35 | + brew install python3 |
| 36 | +else |
| 37 | + _ok "Python 3 is installed." |
| 38 | +fi |
| 39 | + |
| 40 | +# Creating dist folder. |
| 41 | +mkdir -p dist/ |
| 42 | + |
| 43 | +# Delete the content of dist folder (to remove old .app builds). |
| 44 | +_info "Deleting old builds in /dist folder." |
| 45 | +rm -Rf dist/* |
| 46 | + |
| 47 | +# Check if venv is created and do so if not. |
| 48 | +if ! [ -d "./venv" ] ; then |
| 49 | + _info "Creating virtual environment, so we do not pollute the system." |
| 50 | + python3 -m venv venv |
| 51 | +else |
| 52 | + _ok "Looks like a virtual environment is already created." |
| 53 | +fi |
| 54 | + |
| 55 | +# Activate virtual environment, update and install requirements. |
| 56 | +_info "Installing and updating requirements." |
| 57 | +source venv/bin/activate |
| 58 | +pip install --upgrade pip |
| 59 | +python -m pip install pip-tools |
| 60 | +python -m pip install build twine |
| 61 | +pip install -r requirements.txt |
| 62 | + |
| 63 | +# Save name of the package. |
| 64 | +package_directory=$(find src -mindepth 2 -maxdepth 2 -type f -name '__init__.py' -exec dirname {} \;) |
| 65 | +package_name=$(basename "$package_directory") |
| 66 | + |
| 67 | +# Get the version of the package from __init__.py file. |
| 68 | +version=$(grep '__version__ =' "$package_directory/__init__.py" | sed -E 's/^.*"([^"]+)".*$/\1/') |
| 69 | + |
| 70 | +# Override the version variable of setup.cfg with the version number of __init__.py. |
| 71 | +tmpfile=$(mktemp) |
| 72 | +sed "s/^version =.*/version = $version/" setup.cfg > "$tmpfile" |
| 73 | +mv "$tmpfile" setup.cfg |
| 74 | +_ok "Updated the version variable in 'setup.cfg' to $version" |
| 75 | + |
| 76 | +# Build dist and verify. |
| 77 | +_info "Building the distribution files. This can take a while ..." |
| 78 | +if python -m build |
| 79 | +then |
| 80 | + twine check dist/* |
| 81 | + _ok "BUILD FINISHED!" |
| 82 | +else |
| 83 | + _error "BUILD FAILED!" |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | + |
| 87 | +# Upload build to PyPI. Credentials are in '~.pypirc'. |
| 88 | +_info "Uploading built package to PyPI index." |
| 89 | +if twine upload -r pypi dist/* |
| 90 | +then |
| 91 | + _ok "UPLOAD FINISHED!" |
| 92 | +else |
| 93 | + _error "UPLOAD FAILED!" |
| 94 | +fi |
| 95 | + |
| 96 | +# Generate sha256 hash files. |
| 97 | +_info "Generating sha256 checksum files..." |
| 98 | +cd dist || exit |
| 99 | +for file in *; do |
| 100 | + if [ -f "$file" ]; then |
| 101 | + shasum -a 256 "$file" > "$file.sha256" |
| 102 | + fi |
| 103 | +done |
| 104 | + |
| 105 | +# Validate sha256 checksums. |
| 106 | +if sha256sum -c *.sha256; then |
| 107 | + _ok "Checksums are valid." |
| 108 | + _ok "BUILD AND UPLOAD COMPLETED!" |
| 109 | +else |
| 110 | + _error "Checksums are not valid." |
| 111 | +fi |
| 112 | + |
| 113 | +# Cleanup egg files. |
| 114 | +cd .. || exit |
| 115 | +rm -rf "src/${package_name}.egg-info" |
| 116 | + |
| 117 | +# Exit out of virtual environment. |
| 118 | +deactivate |
| 119 | + |
| 120 | +_info "Exiting ..." |
| 121 | +exit 0 |
0 commit comments