Skip to content

Commit

Permalink
Merge pull request #77 from emfcamp/fix-app-install-module-names
Browse files Browse the repository at this point in the history
Fix app install module names
  • Loading branch information
b0xcat committed May 31, 2024
2 parents 04e7505 + 29f5927 commit f3b692f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
11 changes: 8 additions & 3 deletions modules/firmware_apps/app_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -358,13 +359,16 @@ 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:
if not i.name.startswith(prefix):
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:
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
48 changes: 48 additions & 0 deletions modules/lib/shutil.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit f3b692f

Please sign in to comment.