summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/manifestupdate.py
blob: 047cc46160dd400a7337b41e4de83942b206428c (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# 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 configparser
import errno
import hashlib
import os
import sys

import manifestdownload
from mach.util import get_state_dir
from mozfile import load_source
from mozlog.structured import commandline
from wptrunner import wptcommandline

manifest = None


def do_delayed_imports(wpt_dir):
    global manifest
    load_source("localpaths", os.path.join(wpt_dir, "tests", "tools", "localpaths.py"))
    sys.path.insert(0, os.path.join(wpt_dir, "tools", "manifest"))
    import manifest


def create_parser():
    p = argparse.ArgumentParser()
    p.add_argument(
        "--rebuild", action="store_true", help="Rebuild manifest from scratch"
    )
    download_group = p.add_mutually_exclusive_group()
    download_group.add_argument(
        "--download",
        dest="download",
        action="store_true",
        default=None,
        help="Always download even if the local manifest is recent",
    )
    download_group.add_argument(
        "--no-download",
        dest="download",
        action="store_false",
        help="Don't try to download the manifest",
    )
    p.add_argument(
        "--no-update",
        action="store_false",
        dest="update",
        default=True,
        help="Just download the manifest, don't update",
    )
    p.add_argument(
        "--config",
        action="store",
        dest="config_path",
        default=None,
        help="Path to wptrunner config file",
    )
    p.add_argument(
        "--rewrite-config",
        action="store_true",
        default=False,
        help="Force the local configuration to be regenerated",
    )
    p.add_argument(
        "--cache-root",
        action="store",
        default=os.path.join(get_state_dir(), "cache", "wpt"),
        help="Path to use for the metadata cache",
    )
    commandline.add_logging_group(p)

    return p


def ensure_kwargs(kwargs):
    _kwargs = vars(create_parser().parse_args([]))
    _kwargs.update(kwargs)
    return _kwargs


def run(src_root, obj_root, logger=None, **kwargs):
    kwargs = ensure_kwargs(kwargs)

    if logger is None:
        from wptrunner import wptlogging

        logger = wptlogging.setup(kwargs, {"mach": sys.stdout})

    src_wpt_dir = os.path.join(src_root, "testing", "web-platform")

    do_delayed_imports(src_wpt_dir)

    if not kwargs["config_path"]:
        config_path = generate_config(
            logger,
            src_root,
            src_wpt_dir,
            os.path.join(obj_root, "_tests", "web-platform"),
            kwargs["rewrite_config"],
        )
    else:
        config_path = kwargs["config_path"]

    if not os.path.exists(config_path):
        logger.critical("Config file %s does not exist" % config_path)
        return None

    logger.debug("Using config path %s" % config_path)

    test_paths = wptcommandline.get_test_paths(wptcommandline.config.read(config_path))

    for paths in test_paths.values():
        if isinstance(paths, dict) and "manifest_path" not in paths:
            paths["manifest_path"] = os.path.join(
                paths["metadata_path"], "MANIFEST.json"
            )

    ensure_manifest_directories(logger, test_paths)

    local_config = read_local_config(src_wpt_dir)
    for section in ["manifest:upstream", "manifest:mozilla"]:
        url_base = local_config.get(section, "url_base")
        manifest_rel_path = os.path.join(
            local_config.get(section, "metadata"), "MANIFEST.json"
        )
        if isinstance(test_paths[url_base], dict):
            test_paths[url_base]["manifest_rel_path"] = manifest_rel_path
        else:
            test_paths[url_base].manifest_rel_path = manifest_rel_path

    if not kwargs["rebuild"] and kwargs["download"] is not False:
        force_download = False if kwargs["download"] is None else True
        manifestdownload.download_from_taskcluster(
            logger, src_root, test_paths, force=force_download
        )
    else:
        logger.debug("Skipping manifest download")

    update = kwargs["update"] or kwargs["rebuild"]
    manifests = load_and_update(
        logger,
        src_wpt_dir,
        test_paths,
        update=update,
        rebuild=kwargs["rebuild"],
        cache_root=kwargs["cache_root"],
    )

    return manifests


def ensure_manifest_directories(logger, test_paths):
    for paths in test_paths.values():
        manifest_path = (
            paths["manifest_path"] if isinstance(paths, dict) else paths.manifest_path
        )
        manifest_dir = os.path.dirname(manifest_path)
        if not os.path.exists(manifest_dir):
            logger.info("Creating directory %s" % manifest_dir)
            # Even though we just checked the path doesn't exist, there's a chance
            # of race condition with another process or thread having created it in
            # between. This happens during tests.
            try:
                os.makedirs(manifest_dir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
        elif not os.path.isdir(manifest_dir):
            raise IOError("Manifest directory is a file")


def read_local_config(wpt_dir):
    src_config_path = os.path.join(wpt_dir, "wptrunner.ini")

    parser = configparser.ConfigParser()
    success = parser.read(src_config_path)
    assert src_config_path in success
    return parser


def generate_config(logger, repo_root, wpt_dir, dest_path, force_rewrite=False):
    """Generate the local wptrunner.ini file to use locally"""
    if not os.path.exists(dest_path):
        # Even though we just checked the path doesn't exist, there's a chance
        # of race condition with another process or thread having created it in
        # between. This happens during tests.
        try:
            os.makedirs(dest_path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

    dest_config_path = os.path.join(dest_path, "wptrunner.local.ini")

    if not force_rewrite and os.path.exists(dest_config_path):
        logger.debug("Config is up to date, not regenerating")
        return dest_config_path

    logger.info("Creating config file %s" % dest_config_path)

    parser = read_local_config(wpt_dir)

    for section in ["manifest:upstream", "manifest:mozilla"]:
        meta_rel_path = parser.get(section, "metadata")
        tests_rel_path = parser.get(section, "tests")

        parser.set(
            section, "manifest", os.path.join(dest_path, meta_rel_path, "MANIFEST.json")
        )
        parser.set(section, "metadata", os.path.join(wpt_dir, meta_rel_path))
        parser.set(section, "tests", os.path.join(wpt_dir, tests_rel_path))

    parser.set(
        "paths",
        "prefs",
        os.path.abspath(os.path.join(wpt_dir, parser.get("paths", "prefs"))),
    )

    with open(dest_config_path, "wt") as config_file:
        parser.write(config_file)

    return dest_config_path


def load_and_update(
    logger,
    wpt_dir,
    test_paths,
    rebuild=False,
    config_dir=None,
    cache_root=None,
    update=True,
):
    rv = {}
    wptdir_hash = hashlib.sha256(os.path.abspath(wpt_dir).encode()).hexdigest()
    for url_base, paths in test_paths.items():
        manifest_path = (
            paths["manifest_path"] if isinstance(paths, dict) else paths.manifest_path
        )
        manifest_rel_path = (
            paths["manifest_rel_path"]
            if isinstance(paths, dict)
            else paths.manifest_rel_path
        )
        tests_path = (
            paths["tests_path"] if isinstance(paths, dict) else paths.tests_path
        )
        this_cache_root = os.path.join(
            cache_root, wptdir_hash, os.path.dirname(manifest_rel_path)
        )
        m = manifest.manifest.load_and_update(
            tests_path,
            manifest_path,
            url_base,
            update=update,
            rebuild=rebuild,
            working_copy=True,
            cache_root=this_cache_root,
        )
        path_data = {"url_base": url_base}
        if isinstance(paths, dict):
            path_data.update(paths)
        else:
            for key, value in paths.__dict__.items():
                path_data[key] = value

        rv[m] = path_data

    return rv


def log_error(logger, manifest_path, msg):
    logger.lint_error(
        path=manifest_path, message=msg, lineno=0, source="", linter="wpt-manifest"
    )