Cotton code
118 lines · 4.9 KB · PythonRaw
#!/usr/bin/env python3"""The app registry's twin for the cut path (`scripts/lib/registry.ts` is the module).phone-cut.sh and phone-cut-identity.py are bash and Python, so the little theyneed of `apps/registry.json` is here: the flavours, one flavour's identity andwhether its app is cut at all (`is_cut`: only development and production are). Nothing herewrites the registry: a cut's record stays in its own cut.txt (`pnpm apps:cuts`). The registry test holds the two twinsequal on every flavour.    registry.py flavours <registry.json|->              the flavour names, one line, space-separated    registry.py bundle <registry.json|-> <flavour>      the flavour's bundle id    registry.py cut <registry.json|-> <flavour>         exit 0 when its app is cut; 1 naming why not    registry.py core-kit <registry.json|-> <flavour>    the kit its Rust core is built for (COTTON_CORE_KIT); an empty line is the whole core    registry.py app-core-kit <registry.json|-> <slug>   the same, by the app's slug (the Android path names apps by slug)"""import jsonimport sysREGISTRY_PATH = "apps/registry.json"def load(path):    if path == "-":        return json.load(sys.stdin)    with open(path, encoding="utf8") as f:        return json.load(f)def spell(registry):    """The one spelling, the same bytes `JSON.stringify(registry, null, 2) + "\\n"` writes."""    return json.dumps(registry, indent=2, ensure_ascii=False) + "\n"def flavours(registry):    """flavour name → {app, variant, bundle_id, app_group, extensions}, in the registry's order."""    out = {}    for app in registry["apps"]:        ios = app.get("ios")        if not ios:            continue        for variant in ("live", "dev"):            identity = ios.get(variant)            if not identity or not identity.get("flavour"):                continue            bundle = identity["bundleId"]            out[identity["flavour"]] = {                "app": app,                "variant": variant,                "bundle_id": bundle,                "app_group": f"group.{bundle}",                "extensions": {                    **({f"{ios['target']}ShareExtension": f"{bundle}.share-extension"} if ios["share"] else {}),                    **({f"{ios['target']}Provider": f"{bundle}.provider"} if ios.get("provider") else {}),                    **({f"{ios['target']}Notifications": f"{bundle}.notifications"} if ios.get("notifications") else {}),                    **({f"{ios['target']}Widgets": f"{bundle}.widgets"} if ios.get("widgets") else {}),                },            }    return outdef is_cut(app):    """Only an app with its App Store records is cut: development or production (registry.ts's isCut)."""    return app["phase"] in ("development", "production")def core_kit(app, registry_path):    """The kit an app's core carries the pictures of (crates/cotton-core/src/kit_pictures.rs).    It is the kit the app's own manifest names (`apps/cotton/App/Flavours/<slug>/flavour.json`,    the one the core reads at launch and holds itself to): a core built for a    kit refuses any other manifest. An app whose manifest names no kit    (Cotton's whole object, a business app that lists its tools) is built    whole, which is an empty line here.    """    import os    manifest = os.path.join(os.path.dirname(os.path.abspath(registry_path)), "cotton", "App", "Flavours", app["slug"], "flavour.json")    if not os.path.isfile(manifest):        return ""    return load(manifest).get("kit") or ""def main(argv):    if len(argv) >= 2 and argv[0] == "flavours":        print(" ".join(flavours(load(argv[1]))))        return 0    if len(argv) == 3 and argv[0] in ("core-kit", "app-core-kit") and argv[1] == "-":        print("core-kit reads the app's flavour.json beside the registry: name the registry's file, never -", file=sys.stderr)        return 2    if len(argv) == 3 and argv[0] == "app-core-kit":        apps = [app for app in load(argv[1])["apps"] if app["slug"] == argv[2]]        if not apps:            print(f"the registry declares no app '{argv[2]}'", file=sys.stderr)            return 1        print(core_kit(apps[0], argv[1]))        return 0    if len(argv) == 3 and argv[0] in ("bundle", "cut", "core-kit"):        found = flavours(load(argv[1])).get(argv[2])        if not found:            print(f"the registry declares no flavour '{argv[2]}'", file=sys.stderr)            return 1        if argv[0] == "core-kit":            print(core_kit(found["app"], argv[1]))            return 0        if argv[0] == "bundle":            print(found["bundle_id"])            return 0        app = found["app"]        if not is_cut(app):            print(f"{app['slug']} is {app['phase']} in apps/registry.json — only a development or production app is cut", file=sys.stderr)            return 1        return 0    print(__doc__, file=sys.stderr)    return 2if __name__ == "__main__":    sys.exit(main(sys.argv[1:]))