libretime/tools/extract-requirements.sh
Jonas L d55e58e416
chore: auto generate requirements.txt (#1651)
- force LC_ALL for the sort command
2022-02-28 20:02:01 +02:00

49 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Extract the dependencies from the setup.py files
# and save the result to requirements.txt.
#
# You can filter any extra require by adding the name as argument.
#
# Examples:
# tools/extract-requirements.sh
# tools/extract-requirements.sh dev
set -u
error() {
echo >&2 "error: $*"
exit 1
}
command -v python3 > /dev/null || error "python3 command not found!"
command -v sed > /dev/null || error "sed command not found!"
for setup_path in */setup.py; do
path="$(dirname "$setup_path")"
egg_path="$(echo "$path"/*.egg-info)"
# Only build egg if absent
if [[ ! -d "$egg_path" ]]; then
python3 "$setup_path" egg_info > /dev/null 2>&1 || true
fi
# Refresh egg_path with bash expansion
egg_path="$(echo "$path"/*.egg-info)"
# Remove entire extra section from require file
for arg in "$@"; do
sed --in-place "/^\[$arg\]/,/^\[/d" -- "$egg_path/requires.txt"
done
# Generate requirements.txt
cat << EOF > "$path/requirements.txt"
# This file is auto-generated by tools/extract-requirements.sh.
# Please do not edit this file, edit the setup.py file!
EOF
cat -- "$egg_path/requires.txt" |
sed '/^$/d' |
LC_ALL=en_US.UTF-8 sort >> "$path/requirements.txt"
done