diff --git a/.gitignore b/.gitignore index 71617404..cb967f45 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ copyparty.egg-info/ /dist/ /py2/ /sfx* +/pyz/ /unt/ /log/ diff --git a/copyparty/__main__.py b/copyparty/__main__.py old mode 100755 new mode 100644 index 17289afa..940d77ce --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -1391,12 +1391,16 @@ def run_argparse( return ret -def main(argv: Optional[list[str]] = None) -> None: +def main(argv: Optional[list[str]] = None, rsrc: Optional[str] = None) -> None: time.strptime("19970815", "%Y%m%d") # python#7980 if WINDOWS: os.system("rem") # enables colors init_E(E) + + if rsrc: # pyz + E.mod = rsrc + if argv is None: argv = sys.argv diff --git a/scripts/make-pyz.sh b/scripts/make-pyz.sh new file mode 100755 index 00000000..6c73eabe --- /dev/null +++ b/scripts/make-pyz.sh @@ -0,0 +1,61 @@ +#!/bin/bash +set -e +echo + +# port install gnutar gsed coreutils +gtar=$(command -v gtar || command -v gnutar) || true +[ ! -z "$gtar" ] && command -v gsed >/dev/null && { + tar() { $gtar "$@"; } + sed() { gsed "$@"; } + command -v grealpath >/dev/null && + realpath() { grealpath "$@"; } +} + +targs=(--owner=1000 --group=1000) +[ "$OSTYPE" = msys ] && + targs=() + +[ -e copyparty/__main__.py ] || cd .. +[ -e copyparty/__main__.py ] || { + echo "run me from within the project root folder" + echo + exit 1 +} + +[ -e sfx/copyparty/__main__.py ] || { + echo "run ./scripts/make-sfx.py first" + echo + exit 1 +} + +rm -rf pyz +mkdir -p pyz +cd pyz + +cp -pR ../sfx/{copyparty,partftpy} . +cp -pR ../sfx/{ftp,j2}/* . + +ts=$(date -u +%s) +hts=$(date -u +%Y-%m%d-%H%M%S) +ver="$(cat ../sfx/ver)" + +mkdir -p ../dist +pyz_out=../dist/copyparty.pyz + +echo creating z.tar +( cd copyparty + tar -cf z.tar "${targs[@]}" --numeric-owner web res + rm -rf web res +) + +echo creating loader +sed -r 's/^(VER = ).*/\1"'"$ver"'"/; s/^(STAMP = ).*/\1'$(date +%s)/ \ + <../scripts/ziploader.py \ + >__main__.py + +echo creating pyz +rm -f $pyz_out +zip -9 -q -r $pyz_out * + +echo done: +echo " $(realpath $pyz_out)" diff --git a/scripts/make-sfx.sh b/scripts/make-sfx.sh index 418793e1..fcc6f717 100755 --- a/scripts/make-sfx.sh +++ b/scripts/make-sfx.sh @@ -99,9 +99,6 @@ pybin=$(command -v python3 || command -v python) || { exit 1 } -[ $CSN ] || - CSN=sfx - langs= use_gz= zopf=2560 @@ -148,9 +145,9 @@ stamp=$( done | sort | tail -n 1 | sha1sum | cut -c-16 ) -rm -rf $CSN/* -mkdir -p $CSN build -cd $CSN +rm -rf sfx$CSN/* +mkdir -p sfx$CSN build +cd sfx$CSN tmpdir="$( printf '%s\n' "$TMPDIR" /tmp | @@ -386,11 +383,13 @@ git describe --tags >/dev/null 2>/dev/null && { ver="$(awk '/^VERSION *= \(/ { gsub(/[^0-9,a-g-]/,""); gsub(/,/,"."); print; exit}' < copyparty/__version__.py)" +echo "$ver" >ver # pyz + ts=$(date -u +%s) hts=$(date -u +%Y-%m%d-%H%M%S) # --date=@$ts (thx osx) mkdir -p ../dist -sfx_out=../dist/copyparty-$CSN +sfx_out=../dist/copyparty-sfx$CSN echo cleanup find -name '*.pyc' -delete @@ -542,7 +541,7 @@ gzres() { } -zdir="$tmpdir/cpp-mk$CSN" +zdir="$tmpdir/cpp-mksfx$CSN" [ -e "$zdir/$stamp" ] || rm -rf "$zdir" mkdir -p "$zdir" echo a > "$zdir/$stamp" diff --git a/scripts/rls.sh b/scripts/rls.sh index c9ea9f92..32023803 100755 --- a/scripts/rls.sh +++ b/scripts/rls.sh @@ -42,7 +42,7 @@ $f$s.py --version >/dev/null min=99999999 for ((a=0; a<$parallel; a++)); do while [ -e .sfx-run ]; do - CSN=sfx$a ./make-sfx.sh re "$@" + CSN=$a ./make-sfx.sh re "$@" sz=$(wc -c <$f$a$s.py | awk '{print$1}') [ $sz -ge $min ] && continue mv $f$a$s.py $f$s.py.$sz diff --git a/scripts/ziploader.py b/scripts/ziploader.py new file mode 100644 index 00000000..d06b7baa --- /dev/null +++ b/scripts/ziploader.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 + +import atexit +import os +import platform +import sys +import tarfile +import tempfile +import threading +import time +import traceback + + +VER = None +STAMP = None +WINDOWS = sys.platform in ["win32", "msys"] + + +def msg(*a, **ka): + if a: + a = ["[ZIP]", a[0]] + list(a[1:]) + + ka["file"] = sys.stderr + print(*a, **ka) + + +def utime(top): + # avoid cleaners + files = [os.path.join(dp, p) for dp, dd, df in os.walk(top) for p in dd + df] + try: + while True: + t = int(time.time()) + for f in [top] + files: + os.utime(f, (t, t)) + + time.sleep(78123) + except Exception as ex: + print("utime:", ex, f) + + +def confirm(rv): + msg() + msg("retcode", rv if rv else traceback.format_exc()) + if WINDOWS: + msg("*** hit enter to exit ***") + try: + input() + except: + pass + + sys.exit(rv or 1) + + +def run(): + import copyparty + from copyparty.__main__ import main as cm + + td = tempfile.TemporaryDirectory(prefix="") + atexit.register(td.cleanup) + rsrc = td.name + + try: + from importlib.resources import files + + f = files(copyparty).joinpath("z.tar").open("rb") + except: + from importlib.resources import open_binary + + f = open_binary("copyparty", "z.tar") + + with tarfile.open(fileobj=f) as tf: + try: + tf.extractall(rsrc, filter="tar") + except TypeError: + tf.extractall(rsrc) # nosec (archive is safe) + + f.close() + f = None + + msg(" rsrc dir:", rsrc) + msg() + + t = threading.Thread(target=utime, args=(rsrc,)) + t.daemon = True + t.start() + + cm(rsrc=rsrc) + + +def main(): + sysver = str(sys.version).replace("\n", "\n" + " " * 18) + pktime = time.strftime("%Y-%m-%d, %H:%M:%S", time.gmtime(STAMP)) + msg() + msg(" this is: copyparty", VER) + msg(" packed at:", pktime, "UTC,", STAMP) + msg("python bin:", sys.executable) + msg("python ver:", platform.python_implementation(), sysver) + + try: + run() + except SystemExit as ex: + c = ex.code + if c not in [0, -15]: + confirm(ex.code) + except KeyboardInterrupt: + pass + except: + confirm(0) + + +if __name__ == "__main__": + main()