This commit is contained in:
ed 2021-04-24 20:08:07 +02:00
parent c9c3302664
commit b6d3d791a5

View file

@ -29,7 +29,6 @@ PY2 = sys.version_info[0] == 2
WINDOWS = sys.platform in ["win32", "msys"] WINDOWS = sys.platform in ["win32", "msys"]
sys.dont_write_bytecode = True sys.dont_write_bytecode = True
me = os.path.abspath(os.path.realpath(__file__)) me = os.path.abspath(os.path.realpath(__file__))
cpp = None
def eprint(*args, **kwargs): def eprint(*args, **kwargs):
@ -209,11 +208,11 @@ def yieldfile(fn):
def hashfile(fn): def hashfile(fn):
hasher = hashlib.md5() h = hashlib.md5()
for block in yieldfile(fn): for block in yieldfile(fn):
hasher.update(block) h.update(block)
return hasher.hexdigest() return h.hexdigest()
def unpack(): def unpack():
@ -222,9 +221,10 @@ def unpack():
tag = "v" + str(STAMP) tag = "v" + str(STAMP)
withpid = "{}.{}".format(name, os.getpid()) withpid = "{}.{}".format(name, os.getpid())
top = tempfile.gettempdir() top = tempfile.gettempdir()
final = os.path.join(top, name) opj = os.path.join
mine = os.path.join(top, withpid) final = opj(top, name)
tar = os.path.join(mine, "tar") mine = opj(top, withpid)
tar = opj(mine, "tar")
try: try:
if tag in os.listdir(final): if tag in os.listdir(final):
@ -233,28 +233,24 @@ def unpack():
except: except:
pass pass
nwrite = 0 sz = 0
os.mkdir(mine) os.mkdir(mine)
with open(tar, "wb") as f: with open(tar, "wb") as f:
for buf in get_payload(): for buf in get_payload():
nwrite += len(buf) sz += len(buf)
f.write(buf) f.write(buf)
if nwrite != SIZE: ck = hashfile(tar)
t = "\n\n bad file:\n expected {} bytes, got {}\n".format(SIZE, nwrite) if ck != CKSUM:
raise Exception(t) t = "\n\nexpected {} ({} byte)\nobtained {} ({} byte)\nsfx corrupt"
raise Exception(t.format(CKSUM, SIZE, ck, sz))
cksum = hashfile(tar)
if cksum != CKSUM:
t = "\n\n bad file:\n {} expected,\n {} obtained\n".format(CKSUM, cksum)
raise Exception(t)
with tarfile.open(tar, "r:bz2") as tf: with tarfile.open(tar, "r:bz2") as tf:
tf.extractall(mine) tf.extractall(mine)
os.remove(tar) os.remove(tar)
with open(os.path.join(mine, tag), "wb") as f: with open(opj(mine, tag), "wb") as f:
f.write(b"h\n") f.write(b"h\n")
try: try:
@ -284,7 +280,7 @@ def unpack():
for fn in u8(os.listdir(top)): for fn in u8(os.listdir(top)):
if fn.startswith(name) and fn not in [name, withpid]: if fn.startswith(name) and fn not in [name, withpid]:
try: try:
old = os.path.join(top, fn) old = opj(top, fn)
if time.time() - os.path.getmtime(old) > 10: if time.time() - os.path.getmtime(old) > 10:
shutil.rmtree(old) shutil.rmtree(old)
except: except:
@ -307,9 +303,8 @@ def get_payload():
if ofs < 0: if ofs < 0:
raise Exception("could not find archive marker") raise Exception("could not find archive marker")
# start reading from the final b"\n" # start at final b"\n"
fpos = ofs + len(ptn) - 3 fpos = ofs + len(ptn) - 3
# msg("tar found at", fpos)
f.seek(fpos) f.seek(fpos)
dpos = 0 dpos = 0
leftovers = b"" leftovers = b""
@ -371,10 +366,8 @@ def confirm(rv):
sys.exit(rv) sys.exit(rv)
def run(tmp, j2ver): def run(tmp, j2):
global cpp msg("jinja2:", j2 or "bundled")
msg("jinja2:", j2ver or "bundled")
msg("sfxdir:", tmp) msg("sfxdir:", tmp)
msg() msg()
@ -394,16 +387,16 @@ def run(tmp, j2ver):
t.start() t.start()
ld = [tmp, os.path.join(tmp, "dep-j2")] ld = [tmp, os.path.join(tmp, "dep-j2")]
if j2ver: if j2:
del ld[-1] del ld[-1]
for x in ld: for x in ld:
sys.path.insert(0, x) sys.path.insert(0, x)
try: try:
from copyparty.__main__ import main as copyparty from copyparty.__main__ import main as p
copyparty() p()
except SystemExit as ex: except SystemExit as ex:
if ex.code: if ex.code:
@ -446,11 +439,11 @@ def main():
tmp = unpack() tmp = unpack()
try: try:
from jinja2 import __version__ as j2ver from jinja2 import __version__ as j2
except: except:
j2ver = None j2 = None
run(tmp, j2ver) run(tmp, j2)
if __name__ == "__main__": if __name__ == "__main__":