-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.sh
More file actions
executable file
·70 lines (59 loc) · 1.88 KB
/
package.sh
File metadata and controls
executable file
·70 lines (59 loc) · 1.88 KB
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
67
68
69
70
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <pip-package-spec> [--prune-dist-info]"
exit 1
fi
PKG_SPEC="$1"
PRUNE_DIST_INFO="${2:-}"
BASENAME="$(
python - "$PKG_SPEC" <<'PY'
import re, sys
spec = sys.argv[1]
name = re.split(r'[<>=!~; ]', spec, 1)[0]
name = re.sub(r'\[.*\]','', name)
print(name.strip() or "package")
PY
)"
PYTAG="$(
python - <<'PY'
import sys
v = sys.version_info
print(f"{v.major}-{v.minor}")
PY
)"
OUTDIR="/package/layers"
WORKDIR="/tmp/pkgbuild/${BASENAME}"
DESTDIR="$WORKDIR/python/lib/python${PYTAG//-/.}/site-packages"
ZIPNAME="${BASENAME}${PYTAG}.zip"
rm -rf "$WORKDIR"
mkdir -p "$DESTDIR"
echo "==> Installing '$PKG_SPEC' into $DESTDIR (Python $(python -V 2>&1))"
python -m pip install \
--no-cache-dir \
--no-compile \
--upgrade \
-t "$DESTDIR" \
"$PKG_SPEC"
echo "==> Pruning non-essential files"
find "$DESTDIR" -type d -name "__pycache__" -print0 | xargs -0r rm -rf
find "$DESTDIR" -type f -name "*.pyc" -delete
find "$DESTDIR" -type f -name "*.pyo" -delete
find "$DESTDIR" -type d \( -iname "tests" -o -iname "test" -o -iname "testing" -o -iname "docs" -o -iname "examples" \) -print0 | xargs -0r rm -rf
if [[ -n "$PRUNE_DIST_INFO" ]]; then
echo "==> Aggressive prune of *.dist-info and *.egg-info (keeping METADATA)"
find "$DESTDIR" -type d -name "*.egg-info" -print0 | xargs -0r rm -rf
while IFS= read -r -d '' di; do
find "$di" -type f ! -name METADATA ! -name entry_points.txt -delete || true
find "$di" -mindepth 1 -type d -exec rm -rf {} + 2>/dev/null || true
done < <(find "$DESTDIR" -type d -name "*.dist-info" -print0)
fi
mkdir -p "$OUTDIR"
rm -f "$OUTDIR/$ZIPNAME"
echo "==> Creating zip: $ZIPNAME"
cd "$WORKDIR"
zip -9 -r "$OUTDIR/$ZIPNAME" . \
-x "*/__pycache__/*" "*.pyc" "*.pyo" \
"*/tests/*" "*/test/*" "*/testing/*" \
"*/docs/*" "*/examples/*" "*.so.debug"
echo "==> Wrote: $OUTDIR/$ZIPNAME"