summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/metasummary.py
blob: d58ecd84dcf43de7501519cc8dd42d5b4f4f2e37 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# 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 logging
import os
import re
from collections import defaultdict
from urllib import parse as urlparse

import manifestupdate
from wptrunner import expected
from wptrunner.wptmanifest.backends import base
from wptrunner.wptmanifest.serializer import serialize

here = os.path.dirname(__file__)
logger = logging.getLogger(__name__)
yaml = None


class Compiler(base.Compiler):
    def visit_KeyValueNode(self, node):
        key_name = node.data
        values = []
        for child in node.children:
            values.append(self.visit(child))

        self.output_node.set(key_name, values)

    def visit_ConditionalNode(self, node):
        assert len(node.children) == 2
        # For conditional nodes, just return the subtree
        return node.children[0], self.visit(node.children[1])

    def visit_UnaryExpressionNode(self, node):
        raise NotImplementedError

    def visit_BinaryExpressionNode(self, node):
        raise NotImplementedError

    def visit_UnaryOperatorNode(self, node):
        raise NotImplementedError

    def visit_BinaryOperatorNode(self, node):
        raise NotImplementedError


class ExpectedManifest(base.ManifestItem):
    def __init__(self, node, test_path, url_base):
        """Object representing all the tests in a particular manifest

        :param name: Name of the AST Node associated with this object.
                     Should always be None since this should always be associated with
                     the root node of the AST.
        :param test_path: Path of the test file associated with this manifest.
        :param url_base: Base url for serving the tests in this manifest
        """
        if test_path is None:
            raise ValueError("ExpectedManifest requires a test path")
        if url_base is None:
            raise ValueError("ExpectedManifest requires a base url")
        base.ManifestItem.__init__(self, node)
        self.child_map = {}
        self.test_path = test_path
        self.url_base = url_base

    def append(self, child):
        """Add a test to the manifest"""
        base.ManifestItem.append(self, child)
        self.child_map[child.id] = child

    @property
    def url(self):
        return urlparse.urljoin(
            self.url_base, "/".join(self.test_path.split(os.path.sep))
        )


class DirectoryManifest(base.ManifestItem):
    pass


class TestManifestItem(base.ManifestItem):
    def __init__(self, node, **kwargs):
        """Tree node associated with a particular test in a manifest

        :param name: name of the test"""
        base.ManifestItem.__init__(self, node)
        self.subtests = {}

    @property
    def id(self):
        return urlparse.urljoin(self.parent.url, self.name)

    def append(self, node):
        """Add a subtest to the current test

        :param node: AST Node associated with the subtest"""
        child = base.ManifestItem.append(self, node)
        self.subtests[child.name] = child

    def get_subtest(self, name):
        """Get the SubtestNode corresponding to a particular subtest, by name

        :param name: Name of the node to return"""
        if name in self.subtests:
            return self.subtests[name]
        return None


class SubtestManifestItem(TestManifestItem):
    pass


def data_cls_getter(output_node, visited_node):
    # visited_node is intentionally unused
    if output_node is None:
        return ExpectedManifest
    if isinstance(output_node, ExpectedManifest):
        return TestManifestItem
    if isinstance(output_node, TestManifestItem):
        return SubtestManifestItem
    raise ValueError


def get_manifest(metadata_root, test_path, url_base):
    """Get the ExpectedManifest for a particular test path, or None if there is no
    metadata stored for that test path.

    :param metadata_root: Absolute path to the root of the metadata directory
    :param test_path: Path to the test(s) relative to the test root
    :param url_base: Base url for serving the tests in this manifest
    :param run_info: Dictionary of properties of the test run for which the expectation
                     values should be computed.
    """
    manifest_path = expected.expected_path(metadata_root, test_path)
    try:
        with open(manifest_path, "rb") as f:
            return compile(
                f,
                data_cls_getter=data_cls_getter,
                test_path=test_path,
                url_base=url_base,
            )
    except IOError:
        return None


def get_dir_manifest(path):
    """Get the ExpectedManifest for a particular test path, or None if there is no
    metadata stored for that test path.

    :param path: Full path to the ini file
    :param run_info: Dictionary of properties of the test run for which the expectation
                     values should be computed.
    """
    try:
        with open(path, "rb") as f:
            return compile(f, data_cls_getter=lambda x, y: DirectoryManifest)
    except IOError:
        return None


def compile(stream, data_cls_getter=None, **kwargs):
    return base.compile(Compiler, stream, data_cls_getter=data_cls_getter, **kwargs)


def create_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("--out-dir", help="Directory to store output files")
    parser.add_argument(
        "--meta-dir", help="Directory containing wpt-metadata " "checkout to update."
    )
    return parser


def run(src_root, obj_root, logger_=None, **kwargs):
    logger_obj = logger_ if logger_ is not None else logger

    manifests = manifestupdate.run(src_root, obj_root, logger_obj, **kwargs)

    rv = {}
    dirs_seen = set()

    for meta_root, test_path, test_metadata in iter_tests(manifests):
        for dir_path in get_dir_paths(meta_root, test_path):
            if dir_path not in dirs_seen:
                dirs_seen.add(dir_path)
                dir_manifest = get_dir_manifest(dir_path)
                rel_path = os.path.relpath(dir_path, meta_root)
                if dir_manifest:
                    add_manifest(rv, rel_path, dir_manifest)
            else:
                break
        add_manifest(rv, test_path, test_metadata)

    if kwargs["out_dir"]:
        if not os.path.exists(kwargs["out_dir"]):
            os.makedirs(kwargs["out_dir"])
        out_path = os.path.join(kwargs["out_dir"], "summary.json")
        with open(out_path, "w") as f:
            json.dump(rv, f)
    else:
        print(json.dumps(rv, indent=2))

    if kwargs["meta_dir"]:
        update_wpt_meta(logger_obj, kwargs["meta_dir"], rv)


def get_dir_paths(test_root, test_path):
    if not os.path.isabs(test_path):
        test_path = os.path.join(test_root, test_path)
    dir_path = os.path.dirname(test_path)
    while dir_path != test_root:
        yield os.path.join(dir_path, "__dir__.ini")
        dir_path = os.path.dirname(dir_path)
        assert len(dir_path) >= len(test_root)


def iter_tests(manifests):
    for manifest in manifests.keys():
        for test_type, test_path, tests in manifest:
            url_base = manifests[manifest]["url_base"]
            metadata_base = manifests[manifest]["metadata_path"]
            expected_manifest = get_manifest(metadata_base, test_path, url_base)
            if expected_manifest:
                yield metadata_base, test_path, expected_manifest


def add_manifest(target, path, metadata):
    dir_name, file_name = path.rsplit(os.sep, 1)
    key = [dir_name]

    add_metadata(target, key, metadata)

    key.append("_tests")

    for test_metadata in metadata.children:
        key.append(test_metadata.name)
        add_metadata(target, key, test_metadata)
        add_filename(target, key, file_name)
        key.append("_subtests")
        for subtest_metadata in test_metadata.children:
            key.append(subtest_metadata.name)
            add_metadata(target, key, subtest_metadata)
            key.pop()
        key.pop()
        key.pop()


simple_props = [
    "disabled",
    "min-asserts",
    "max-asserts",
    "lsan-allowed",
    "leak-allowed",
    "bug",
]
statuses = set(["CRASH"])


def add_filename(target, key, filename):
    for part in key:
        if part not in target:
            target[part] = {}
        target = target[part]

    target["_filename"] = filename


def add_metadata(target, key, metadata):
    if not is_interesting(metadata):
        return

    for part in key:
        if part not in target:
            target[part] = {}
        target = target[part]

    for prop in simple_props:
        if metadata.has_key(prop):  # noqa W601
            target[prop] = get_condition_value_list(metadata, prop)

    if metadata.has_key("expected"):  # noqa W601
        intermittent = []
        values = metadata.get("expected")
        by_status = defaultdict(list)
        for item in values:
            if isinstance(item, tuple):
                condition, status = item
            else:
                condition = None
                status = item
            if isinstance(status, list):
                intermittent.append((condition, status))
                expected_status = status[0]
            else:
                expected_status = status
            by_status[expected_status].append(condition)
        for status in statuses:
            if status in by_status:
                target["expected_%s" % status] = [
                    serialize(item) if item else None for item in by_status[status]
                ]
        if intermittent:
            target["intermittent"] = [
                [serialize(cond) if cond else None, intermittent_statuses]
                for cond, intermittent_statuses in intermittent
            ]


def get_condition_value_list(metadata, key):
    conditions = []
    for item in metadata.get(key):
        if isinstance(item, tuple):
            assert len(item) == 2
            conditions.append((serialize(item[0]), item[1]))
        else:
            conditions.append((None, item))
    return conditions


def is_interesting(metadata):
    if any(metadata.has_key(prop) for prop in simple_props):  # noqa W601
        return True

    if metadata.has_key("expected"):  # noqa W601
        for expected_value in metadata.get("expected"):
            # Include both expected and known intermittent values
            if isinstance(expected_value, tuple):
                expected_value = expected_value[1]
            if isinstance(expected_value, list):
                return True
            if expected_value in statuses:
                return True
            return True
    return False


def update_wpt_meta(logger, meta_root, data):
    global yaml
    import yaml

    if not os.path.exists(meta_root) or not os.path.isdir(meta_root):
        raise ValueError("%s is not a directory" % (meta_root,))

    with WptMetaCollection(meta_root) as wpt_meta:
        for dir_path, dir_data in sorted(data.items()):
            for test, test_data in dir_data.get("_tests", {}).items():
                add_test_data(logger, wpt_meta, dir_path, test, None, test_data)
                for subtest, subtest_data in test_data.get("_subtests", {}).items():
                    add_test_data(
                        logger, wpt_meta, dir_path, test, subtest, subtest_data
                    )


def add_test_data(logger, wpt_meta, dir_path, test, subtest, test_data):
    triage_keys = ["bug"]

    for key in triage_keys:
        if key in test_data:
            value = test_data[key]
            for cond_value in value:
                if cond_value[0] is not None:
                    logger.info("Skipping conditional metadata")
                    continue
                cond_value = cond_value[1]
                if not isinstance(cond_value, list):
                    cond_value = [cond_value]
                for bug_value in cond_value:
                    bug_link = get_bug_link(bug_value)
                    if bug_link is None:
                        logger.info("Could not extract bug: %s" % value)
                        continue
                    meta = wpt_meta.get(dir_path)
                    meta.set(test, subtest, product="firefox", bug_url=bug_link)


bugzilla_re = re.compile(r"https://bugzilla\.mozilla\.org/show_bug\.cgi\?id=\d+")
bug_re = re.compile(r"(?:[Bb][Uu][Gg])?\s*(\d+)")


def get_bug_link(value):
    value = value.strip()
    m = bugzilla_re.match(value)
    if m:
        return m.group(0)
    m = bug_re.match(value)
    if m:
        return "https://bugzilla.mozilla.org/show_bug.cgi?id=%s" % m.group(1)


class WptMetaCollection(object):
    def __init__(self, root):
        self.root = root
        self.loaded = {}

    def __enter__(self):
        return self

    def __exit__(self, *args, **kwargs):
        for item in self.loaded.itervalues():
            item.write(self.root)
        self.loaded = {}

    def get(self, dir_path):
        if dir_path not in self.loaded:
            meta = WptMeta.get_or_create(self.root, dir_path)
            self.loaded[dir_path] = meta
        return self.loaded[dir_path]


class WptMeta(object):
    def __init__(self, dir_path, data):
        assert "links" in data and isinstance(data["links"], list)
        self.dir_path = dir_path
        self.data = data

    @staticmethod
    def meta_path(meta_root, dir_path):
        return os.path.join(meta_root, dir_path, "META.yml")

    def path(self, meta_root):
        return self.meta_path(meta_root, self.dir_path)

    @classmethod
    def get_or_create(cls, meta_root, dir_path):
        if os.path.exists(cls.meta_path(meta_root, dir_path)):
            return cls.load(meta_root, dir_path)
        return cls(dir_path, {"links": []})

    @classmethod
    def load(cls, meta_root, dir_path):
        with open(cls.meta_path(meta_root, dir_path), "r") as f:
            data = yaml.safe_load(f)
        return cls(dir_path, data)

    def set(self, test, subtest, product, bug_url):
        target_link = None
        for link in self.data["links"]:
            link_product = link.get("product")
            if link_product:
                link_product = link_product.split("-", 1)[0]
            if link_product is None or link_product == product:
                if link["url"] == bug_url:
                    target_link = link
                    break

        if target_link is None:
            target_link = {
                "product": product.encode("utf8"),
                "url": bug_url.encode("utf8"),
                "results": [],
            }
            self.data["links"].append(target_link)

        if "results" not in target_link:
            target_link["results"] = []

        has_result = any(
            (result["test"] == test and result.get("subtest") == subtest)
            for result in target_link["results"]
        )
        if not has_result:
            data = {"test": test.encode("utf8")}
            if subtest:
                data["subtest"] = subtest.encode("utf8")
            target_link["results"].append(data)

    def write(self, meta_root):
        path = self.path(meta_root)
        dirname = os.path.dirname(path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        with open(path, "wb") as f:
            yaml.safe_dump(self.data, f, default_flow_style=False, allow_unicode=True)