apps.py (4611B)
1 #!/usr/bin/env python3 2 # the store's list of apps in our repo: every package with a .desktop file, its name, summary, categories and icon 3 # usage: apps.py REPO_DIR (writes REPO_DIR/apps.json and REPO_DIR/icons/, only looks at packages it hasn't seen) 4 import configparser, json, os, re, shutil, subprocess, sys, tempfile 5 from concurrent.futures import ThreadPoolExecutor 6 7 APK = os.environ.get("APK", "apk") 8 repo = sys.argv[1] 9 out_json = os.path.join(repo, "apps.json") 10 icons = os.path.join(repo, "icons") 11 cache_file = os.path.join(repo, "..", "apps-cache.json") 12 os.makedirs(icons, exist_ok=True) 13 cache = json.load(open(cache_file)) if os.path.exists(cache_file) else {} 14 15 # which packages have desktop files, from the index (no need to open every package) 16 dump = subprocess.run([APK, "adbdump", os.path.join(repo, "Packages.adb")], capture_output=True, text=True).stdout 17 pkgs, cur = {}, None 18 for line in dump.splitlines(): 19 m = re.match(r" - name: (.+)$", line) 20 if m: 21 cur = {"name": m.group(1)} 22 continue 23 m = re.match(r" (version|description): (.+)$", line) 24 if m and cur is not None: 25 cur[m.group(1)] = m.group(2) 26 if "version" in cur and "name" in cur: 27 pkgs[cur["name"]] = cur 28 29 30 def has_desktop(path): 31 r = subprocess.run([APK, "adbdump", path], capture_output=True, text=True).stdout 32 return "usr/share/applications" in r and ".desktop" in r 33 34 35 def real(root, p): 36 # a file inside the unpacked package, following symlinks that point inside it too 37 for _ in range(10): 38 if not os.path.islink(p): 39 return p if os.path.isfile(p) else None 40 t = os.readlink(p) 41 p = root + t if os.path.isabs(t) else os.path.join(os.path.dirname(p), t) 42 return None 43 44 45 def icon_file(root, name): 46 if not name: 47 return None 48 if os.path.isabs(name): 49 return real(root, root + name) 50 best = None 51 for size in ("128x128", "256x256", "96x96", "64x64", "48x48", "scalable", "512x512", "32x32"): 52 for ext in ("png", "svg"): 53 p = real(root, "%s/usr/share/icons/hicolor/%s/apps/%s.%s" % (root, size, name, ext)) 54 if p: 55 return p 56 for ext in ("png", "svg", "xpm"): 57 p = real(root, "%s/usr/share/pixmaps/%s.%s" % (root, name, ext)) 58 if p: 59 best = p 60 return best 61 62 63 def look(name): 64 try: 65 return look1(name) 66 except Exception as e: 67 print("%s: %s" % (name, e), file=sys.stderr) 68 return "%s-%s.apk" % (name, pkgs[name]["version"]), [] 69 70 71 def look1(name): 72 p = pkgs[name] 73 f = "%s-%s.apk" % (name, p["version"]) 74 path = os.path.join(repo, f) 75 if f in cache: 76 return f, cache[f] 77 if not os.path.exists(path) or not has_desktop(path): 78 return f, [] 79 tmp = tempfile.mkdtemp() 80 try: 81 subprocess.run([APK, "extract", "--allow-untrusted", "--no-chown", "--destination", tmp, path], 82 capture_output=True) 83 d = os.path.join(tmp, "usr/share/applications") 84 apps = [] 85 for desk in sorted(os.listdir(d)) if os.path.isdir(d) else []: 86 if not desk.endswith(".desktop"): 87 continue 88 c = configparser.RawConfigParser(strict=False, interpolation=None) 89 try: 90 c.read(os.path.join(d, desk), encoding="utf-8") 91 e = c["Desktop Entry"] 92 except Exception: 93 continue 94 if e.get("NoDisplay", "").lower() == "true" or e.get("Type", "Application") != "Application": 95 continue 96 if e.get("Terminal", "").lower() == "true": 97 continue 98 app = {"id": desk[:-8], "pkg": name, "name": e.get("Name", name), 99 "summary": e.get("Comment") or e.get("GenericName") or p.get("description", ""), 100 "categories": [x for x in e.get("Categories", "").split(";") if x]} 101 ic = icon_file(tmp, e.get("Icon", "")) 102 if ic: 103 dest = app["id"] + os.path.splitext(ic)[1] 104 shutil.copy(ic, os.path.join(icons, dest)) 105 app["icon"] = dest 106 apps.append(app) 107 return f, apps 108 finally: 109 shutil.rmtree(tmp, ignore_errors=True) 110 111 112 with ThreadPoolExecutor(os.cpu_count() or 4) as ex: 113 results = list(ex.map(look, sorted(pkgs))) 114 cache = {f: apps for f, apps in results} 115 json.dump(cache, open(cache_file, "w")) 116 apps = [a for _, lst in results for a in lst] 117 json.dump(apps, open(out_json, "w"), separators=(",", ":")) 118 print("%d apps from %d packages" % (len(apps), len({a["pkg"] for a in apps})))