File: gen_built_in_addons.py

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (113 lines) | stat: -rw-r--r-- 3,245 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import argparse
import json
import os.path
import sys

import buildconfig
import mozpack.path as mozpath

from mozpack.copier import FileRegistry
from mozpack.manifests import InstallManifest


# A list of build manifests, and their relative base paths, from which to
# extract lists of install files. These vary depending on which backend we're
# using, so nonexistent manifests are ignored.
manifest_paths = (
    ("", "_build_manifests/install/dist_bin"),
    ("", "faster/install_dist_bin"),
    ("browser", "faster/install_dist_bin_browser"),
)


def get_registry(paths):
    used_paths = set()

    registry = FileRegistry()
    for base, path in paths:
        full_path = mozpath.join(buildconfig.topobjdir, path)
        if not os.path.exists(full_path):
            continue

        used_paths.add(full_path)

        reg = FileRegistry()
        InstallManifest(full_path).populate_registry(reg)

        for p, f in reg:
            path = mozpath.join(base, p)
            try:
                registry.add(path, f)
            except Exception:
                pass

    return registry, used_paths


def get_child(base, path):
    """Returns the nearest parent of `path` which is an immediate child of
    `base`"""

    dirname = mozpath.dirname(path)
    while dirname != base:
        path = dirname
        dirname = mozpath.dirname(path)
    return path


def main(output, *args):
    parser = argparse.ArgumentParser(
        description="Produces a JSON manifest of built-in add-ons"
    )
    parser.add_argument(
        "--builtin-addons",
        type=str,
        dest="builtinsdir",
        action="store",
        help=(
            "The build sub-directory containing builtin add-ons to include in the omni.ja"
        ),
    )
    args = parser.parse_args(args)

    registry, inputs = get_registry(manifest_paths)

    dicts = {}
    for path in registry.match("dictionaries/*.dic"):
        base, ext = os.path.splitext(mozpath.basename(path))
        dicts[base] = path

    listing = {
        "dictionaries": dicts,
    }

    if args.builtinsdir:
        builtins = list()
        for p in registry.match("%s/*/manifest.json" % args.builtinsdir):
            dirname = mozpath.basename(get_child(args.builtinsdir, p))
            builtins_entry = dict()
            builtins_entry["res_url"] = f"resource://builtin-addons/{dirname}/"
            # collect addon id and version from each of the builtins manifest.json files.
            webext_manifest = json.loads(registry[p].read())
            builtins_entry["addon_id"] = webext_manifest["browser_specific_settings"][
                "gecko"
            ]["id"]
            builtins_entry["addon_version"] = webext_manifest["version"]
            builtins.append(builtins_entry)

        def sort_by_dirname(entry):
            return entry["res_url"]

        listing["builtins"] = sorted(builtins, key=sort_by_dirname)

    json.dump(listing, output, sort_keys=True)

    return inputs


if __name__ == "__main__":
    main(sys.stdout, *sys.argv[1:])