From 8826833385053b488e7d34145226d1557ea4a235 Mon Sep 17 00:00:00 2001 From: b0xcat Date: Fri, 31 May 2024 17:58:06 +0200 Subject: [PATCH 1/2] Turn repo name from github into something that's a valid module name in python --- modules/firmware_apps/app_store.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/firmware_apps/app_store.py b/modules/firmware_apps/app_store.py index 2c2edb4..1cc7bad 100644 --- a/modules/firmware_apps/app_store.py +++ b/modules/firmware_apps/app_store.py @@ -343,8 +343,9 @@ def install_app(app): tar_bytesio = io.BytesIO(tar) print("Validating") - prefix = find_app_root_dir(TarFile(fileobj=tar_bytesio)) + prefix = find_app_root_dir(TarFile(fileobj=tar_bytesio)).rstrip('/') tar_bytesio.seek(0) + print(f"Found app prefix: {prefix}") app_py_info = find_app_py_file(prefix, TarFile(fileobj=tar_bytesio)) print(f"Found app.py at: {app_py_info.name}") tar_bytesio.seek(0) @@ -358,6 +359,8 @@ def install_app(app): except OSError: pass + app_module_name = '_'.join(prefix.split('-')[0:-1]) + t = TarFile(fileobj=tar_bytesio) for i in t: if i: @@ -365,6 +368,7 @@ def install_app(app): continue if i.type == DIRTYPE: dirname = f"{APP_DIR}/{i.name}" + dirname = dirname.replace(prefix, app_module_name, 1) print(f"Dirname: {dirname}") if not dir_exists(dirname): try: @@ -375,6 +379,7 @@ def install_app(app): pass else: filename = f"{APP_DIR}/{i.name}" + filename = filename.replace(prefix, app_module_name, 1) print(f"Filename: {filename}") f = t.extractfile(i) if f: @@ -386,7 +391,7 @@ def install_app(app): "name": app["manifest"]["app"]["name"], "hidden": False, } - json_path = f"{APP_DIR}/{prefix}/metadata.json" + json_path = f"{APP_DIR}/{app_module_name}/metadata.json" print(f"Json path: {json_path}") with open(json_path, "w+") as internal_manifest_file_handler: json.dump(internal_manifest, internal_manifest_file_handler) @@ -425,7 +430,7 @@ def find_app_root_dir(tar): def find_app_py_file(prefix, tar) -> tarfile.TarInfo: print("Finding app.py...") found_app_py = False - expected_path = f"{prefix}app.py" + expected_path = f"{prefix}/app.py" app_py_info = None for i, f in enumerate(tar): From 29f59273ab7ec9bce1012c2db73c668cf51b6a3c Mon Sep 17 00:00:00 2001 From: b0xcat Date: Fri, 31 May 2024 17:58:38 +0200 Subject: [PATCH 2/2] Add shutil lib for rmtree --- modules/lib/shutil.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 modules/lib/shutil.py diff --git a/modules/lib/shutil.py b/modules/lib/shutil.py new file mode 100644 index 0000000..9e72c8e --- /dev/null +++ b/modules/lib/shutil.py @@ -0,0 +1,48 @@ +# Reimplement, because CPython3.3 impl is rather bloated +import os +from collections import namedtuple + +_ntuple_diskusage = namedtuple("usage", ("total", "used", "free")) + + +def rmtree(d): + if not d: + raise ValueError + + for name, type, *_ in os.ilistdir(d): + path = d + "/" + name + if type & 0x4000: # dir + rmtree(path) + else: # file + os.unlink(path) + os.rmdir(d) + + +def copyfileobj(src, dest, length=512): + if hasattr(src, "readinto"): + buf = bytearray(length) + while True: + sz = src.readinto(buf) + if not sz: + break + if sz == length: + dest.write(buf) + else: + b = memoryview(buf)[:sz] + dest.write(b) + else: + while True: + buf = src.read(length) + if not buf: + break + dest.write(buf) + + +def disk_usage(path): + bit_tuple = os.statvfs(path) + blksize = bit_tuple[0] # system block size + total = bit_tuple[2] * blksize + free = bit_tuple[3] * blksize + used = total - free + + return _ntuple_diskusage(total, used, free)