-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile-requirements
executable file
·66 lines (55 loc) · 1.79 KB
/
compile-requirements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
set -e
called_as=("$0" "$@")
pip_compile_args=(--quiet --strip-extras --no-emit-find-links --allow-unsafe)
while [[ $1 == -* ]]; do
case "$1" in
-H|--hashes)
pip_compile_args+=(--generate-hashes --reuse-hashes)
;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
shift
done
infile_path=${1:?Give a name of requirements*.in file as an argument}
txtfile_path=${infile_path%.in}.txt
# Change to the dir of the infile_path and use basenames for the files
cd "$(dirname "$infile_path")"
infile=${infile_path##*/}
txtfile=${txtfile_path##*/}
# If pip-compile is in PATH use it, otherwise run through pipx
pip_compile=(pip-compile)
if ! type ${pip_compile[0]} &>/dev/null; then
pip_compile=(pipx run --spec pip-tools pip-compile)
fi
# Generate constraint files from requirements*.txt files if needed
if grep -q "^-c" "$infile"; then
# Setup a trap for removing temporary files on exit
tmp_files=()
remove_tmp_files() {
rm -f "${tmp_files[@]}"
}
trap remove_tmp_files EXIT
# Generate a constraint file for each "-c *.constraints" line
mapfile -t reqs < <(sed -nE 's/^-c *(.*).constraints.*/\1/p' < "$infile")
for reqfile in "${reqs[@]}"; do
tmp_files+=("$reqfile.constraints")
grep -vE '^( *#|-e)' < "$reqfile" > "$reqfile.constraints"
done
fi
echo -n "Generating $txtfile_path from $infile_path..."
"${pip_compile[@]}" "${pip_compile_args[@]}" "$infile"
# Post-process the output file with sed:
#
# 1. Fix the absolute path file URLs to relative paths.
# 2. Change the line having the "pip-compile" command
cwd_url="\(file://\)\?$PWD\(/\.\)\?"
sed -i "
s%^.* @ $cwd_url%.%;
s%$cwd_url%.%g;
s|^\(# *\)pip-compile.*|\1${called_as[*]}|;
" "$txtfile"
echo "done"