summaryrefslogtreecommitdiffstats
path: root/taskcluster/scripts/misc/are-we-esmified-yet.py
blob: ff888b80c7e3eda5441e38754577a13b6343acbb (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3

# 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 json
import pathlib
import re
import subprocess
import sys

TBPL_FAILURE = 2

excluded_files = [
    # Testcase for loader.
    "js/xpconnect/tests/chrome/file_expandosharing.jsm",
    "js/xpconnect/tests/unit/environment_script.js",
    "js/xpconnect/tests/unit/bogus_element_type.jsm",
    "js/xpconnect/tests/unit/bogus_exports_type.jsm",
    "js/xpconnect/tests/unit/envChain.jsm",
    "js/xpconnect/tests/unit/envChain_subscript.jsm",
    "js/xpconnect/tests/unit/environment_checkscript.jsm",
    "js/xpconnect/tests/unit/environment_loadscript.jsm",
    "js/xpconnect/tests/unit/import_stack.jsm",
    "js/xpconnect/tests/unit/importer.jsm",
    "js/xpconnect/tests/unit/jsm_loaded-1.jsm",
    "js/xpconnect/tests/unit/jsm_loaded-2.jsm",
    "js/xpconnect/tests/unit/jsm_loaded-3.jsm",
    "js/xpconnect/tests/unit/not-esmified-not-exported.jsm",
    "js/xpconnect/tests/unit/recursive_importA.jsm",
    "js/xpconnect/tests/unit/recursive_importB.jsm",
    "js/xpconnect/tests/unit/ReturnCodeChild.jsm",
    "js/xpconnect/tests/unit/syntax_error.jsm",
    "js/xpconnect/tests/unit/TestBlob.jsm",
    "js/xpconnect/tests/unit/TestFile.jsm",
    "js/xpconnect/tests/unit/uninitialized_lexical.jsm",
    "dom/url/tests/file_url.jsm",
    "dom/url/tests/file_worker_url.jsm",
    "dom/url/tests/test_bug883784.jsm",
    "dom/workers/test/WorkerTest.jsm",
    "dom/encoding/test/file_stringencoding.jsm",
    "remote/shared/messagehandler/test/browser/resources/modules/root/invalid.jsm",
    "toolkit/actors/TestProcessActorChild.jsm",
    "toolkit/actors/TestProcessActorParent.jsm",
    "toolkit/actors/TestWindowChild.jsm",
    "toolkit/actors/TestWindowParent.jsm",
    # Testcase for build system.
    "python/mozbuild/mozbuild/test/backend/data/build/bar.jsm",
    "python/mozbuild/mozbuild/test/backend/data/build/baz.jsm",
    "python/mozbuild/mozbuild/test/backend/data/build/foo.jsm",
    "python/mozbuild/mozbuild/test/backend/data/build/qux.jsm",
    # EXPORTED_SYMBOLS inside testcase.
    "tools/lint/eslint/eslint-plugin-mozilla/tests/mark-exported-symbols-as-used.js",
]

if pathlib.Path(".hg").exists():
    mode = "hg"
elif pathlib.Path(".git").exists():
    mode = "git"
else:
    print(
        "Error: This script needs to be run inside mozilla-central checkout "
        "of either mercurial or git.",
        file=sys.stderr,
    )
    sys.exit(TBPL_FAILURE)


def new_files_struct():
    return {
        "jsm": [],
        "esm": [],
        "subdir": {},
    }


def put_file(files, kind, path):
    """Put a path into files tree structure."""

    if str(path) in excluded_files:
        return

    name = path.name

    current_files = files
    for part in path.parent.parts:
        if part not in current_files["subdir"]:
            current_files["subdir"][part] = new_files_struct()
        current_files = current_files["subdir"][part]

    current_files[kind].append(name)


def run(cmd):
    """Run command and return output as lines, excluding empty line."""
    lines = subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode()
    return filter(lambda x: x != "", lines.split("\n"))


def collect_jsm(files):
    """Collect JSM files."""
    kind = "jsm"

    # jsm files
    if mode == "hg":
        cmd = ["hg", "files", "set:glob:**/*.jsm"]
    else:
        cmd = ["git", "ls-files", "*.jsm"]
    for line in run(cmd):
        put_file(files, kind, pathlib.Path(line))

    # js files with EXPORTED_SYMBOLS
    if mode == "hg":
        cmd = ["hg", "files", "set:grep('EXPORTED_SYMBOLS = \[') and glob:**/*.js"]
        for line in run(cmd):
            put_file(files, kind, pathlib.Path(line))
    else:
        handled = {}
        cmd = ["git", "grep", "EXPORTED_SYMBOLS = \[", "*.js"]
        for line in run(cmd):
            m = re.search("^([^:]+):", line)
            if not m:
                continue
            path = m.group(1)
            if path in handled:
                continue
            handled[path] = True
            put_file(files, kind, pathlib.Path(path))


def collect_esm(files):
    """Collect system ESM files."""
    kind = "esm"

    # sys.mjs files
    if mode == "hg":
        cmd = ["hg", "files", "set:glob:**/*.sys.mjs"]
    else:
        cmd = ["git", "ls-files", "*.sys.mjs"]
    for line in run(cmd):
        put_file(files, kind, pathlib.Path(line))


def to_stat(files):
    """Convert files tree into status tree."""
    jsm = len(files["jsm"])
    esm = len(files["esm"])
    subdir = {}

    for key, sub_files in files["subdir"].items():
        sub_stat = to_stat(sub_files)

        subdir[key] = sub_stat
        jsm += sub_stat["jsm"]
        esm += sub_stat["esm"]

    stat = {
        "jsm": jsm,
        "esm": esm,
    }
    if len(subdir):
        stat["subdir"] = subdir

    return stat


if mode == "hg":
    cmd = ["hg", "parent", "--template", "{node}"]
    commit_hash = list(run(cmd))[0]

    cmd = ["hg", "parent", "--template", "{date|shortdate}"]
    date = list(run(cmd))[0]
else:
    cmd = ["git", "log", "-1", "--pretty=%H"]
    git_hash = list(run(cmd))[0]
    cmd = ["git", "cinnabar", "git2hg", git_hash]
    commit_hash = list(run(cmd))[0]

    cmd = ["git", "log", "-1", "--pretty=%cs"]
    date = list(run(cmd))[0]

files = new_files_struct()
collect_jsm(files)
collect_esm(files)

stat = to_stat(files)
stat["hash"] = commit_hash
stat["date"] = date

print(json.dumps(stat, indent=2))