sfx-packer optimizations

This commit is contained in:
ed 2025-10-11 21:22:22 +00:00
parent dbd8f837e8
commit 46c205dd60

View file

@ -17,12 +17,29 @@ help() { exec cat <<'EOF'
# `re` does a repack of an sfx which you already executed once
# (grabs files from the sfx-created tempdir), overrides `clean`
#
# `gz` creates a gzip-compressed python sfx instead of bzip2
#
# `lang` limits which languages/translations to include,
# for example `lang eng` or `lang eng|nor`
#
# _____________________________________________________________________
# compression tweaks:
#
# `gz` creates a gzip-compressed python sfx instead of bzip2
# (improves compat with minimal and/or ancient pythons)
#
# `gzz 50` uses zopfli to create a gzip-compressed python sfx
# (better compression than regular gz without affecting compat)
#
# `xz` creates an xz-compressed python sfx instead of bzip2
# (tiny bit smaller, but needs modern python to run)
#
# `nopk` disables js/css compression; builds faster, and
# the sfx becomes smaller, but reduces runtime performance
#
# `udep` unpacks compressed js/css (use with `nopk` and `xz`);
# even smaller sfx, much worse RAM/network waste at runtime
# (only useful for jokes such as putting the sfx on a floppy)
#
# _____________________________________________________________________
# core features:
#
# `no-ftp` saves ~30k by removing the ftp server, disabling --ftp
@ -106,11 +123,14 @@ pybin=$(command -v python3 || command -v python) || {
langs=
use_gz=
use_xz=
zopf=2000
udep=
while [ ! -z "$1" ]; do
case $1 in
clean) clean=1 ; ;;
re) repack=1 ; ;;
xz) use_xz=1 ; ;;
gz) use_gz=1 ; ;;
gzz) shift;use_gzz=$1;use_gz=1; ;;
no-ftp) no_ftp=1 ; ;;
@ -124,6 +144,8 @@ while [ ! -z "$1" ]; do
dl-wd) dl_wd=1 ; ;;
ign-wd) ign_wd=1 ; ;;
fast) zopf= ; ;;
nopk) zopf=no ; ;;
udep) udep=1 ; ;;
lang) shift;langs="$1"; ;;
*) help ; ;;
esac
@ -529,6 +551,7 @@ done
gzres() {
local pk=
[ "$zopf" = no ] && return
[ $zopf ] && command -v zopfli && pk="zopfli --i$zopf"
[ $zopf ] && command -v pigz && pk="pigz -11 -I $zopf"
[ -z "$pk" ] && pk='gzip'
@ -553,55 +576,34 @@ gzres() {
wait
echo
}
gzres
zdir="$tmpdir/cpp-mksfx"
[ -e "$zdir/$stamp" ] || rm -rf "$zdir"
mkdir -p "$zdir"
echo a > "$zdir/$stamp"
nf=$(ls -1 "$zdir"/arc.* 2>/dev/null | wc -l)
[ $nf -ge 2 ] && [ ! $repack ] && use_zdir=1 || use_zdir=
[ $use_zdir ] || {
echo "$nf alts += 1"
gzres
[ $repack ] ||
tar -cf "$zdir/arc.$(date +%s)" copyparty/web/*.gz
}
[ $use_zdir ] && {
arcs=("$zdir"/arc.*)
n=$(( $RANDOM % ${#arcs[@]} ))
arc="${arcs[n]}"
echo "using $arc"
tar -xf "$arc"
for f in copyparty/web/*.gz; do
rm "${f%.*}"
done
}
[ $udep ] &&
find -iname '*.gz' | while IFS= read -r x; do gzip -d "$x"; done
echo gen tarlist
for d in copyparty partftpy magic j2 py2 py37 ftp; do find $d -type f || true; done | # strip_hints
sed -r 's/(.*)\.(.*)/\2 \1/' | LC_ALL=C sort |
sed -r 's/([^ ]*) (.*)/\2.\1/' | grep -vE '/list1?$' > list1
(grep -vE '\.gz$' list1; grep -E '\.gz$' list1) >list
(grep -vE '\.gz$' list1; grep -E '\.gz$' list1) >list || true
echo creating tar
tar -cf tar "${targs[@]}" --numeric-owner -T list
pc="bzip2 -"; pe=bz2
pc="bzip2 -"; pe=bz2; pl=$(echo {2..9})
[ $use_gz ] && pc="gzip -" && pe=gz
[ $use_gzz ] && pc="pigz -11 -I$use_gzz" && pe=gz
[ $use_gzz ] && pc="pigz -11 -I$use_gzz" && pe=gz && pl=0
[ $use_xz ] && pc="xz -zeT0 -" && pe=xz
echo compressing tar
for n in {2..9}; do cp tar t.$n; nice -n20 $pc$n t.$n & done; wait
for n in $pl; do cp tar t.$n; nice -n20 $pc$n t.$n & done; wait
minf=$(for f in t.*.$pe; do
s1=$(wc -c <$f)
s2=$(tr -d '\r\n\0' <$f | wc -c)
echo "$(( s2+(s1-s2)*3 )) $f"
done | sort -n | awk '{print$2;exit}')
mv -v $minf tar.bz2
rm t.* || true
rm t.* 2>/dev/null || true
exts=()
@ -613,13 +615,17 @@ suf=
sed -r 's/"r:bz2"/"r:gz"/' <$py >$py.t
py=$py.t
}
[ $use_xz ] && {
sed -r 's/"r:bz2"/"r:xz"/' <$py >$py.t
py=$py.t
}
"$pybin" $py --sfx-make tar.bz2 $ver $ts
mv sfx.out $sfx_out$suf.py
exts+=($suf.py)
[ $use_gz ] &&
rm $py
[ $use_gz ] && rm $py
[ $use_xz ] && rm $py
chmod 755 $sfx_out*