summaryrefslogtreecommitdiff
path: root/testing/web-platform/manifestupdate.py
blob: 7b15a55eb9e437e384eda937543890079737f00b (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
import argparse
import imp
import os
import sys

from mozlog.structured import commandline
from wptrunner.wptcommandline import get_test_paths, set_from_config
from wptrunner.testloader import ManifestLoader

def create_parser():
    p = argparse.ArgumentParser()
    p.add_argument("--check-clean", action="store_true",
                   help="Check that updating the manifest doesn't lead to any changes")
    commandline.add_logging_group(p)

    return p


def update(logger, wpt_dir, check_clean=True):
    localpaths = imp.load_source("localpaths",
                                 os.path.join(wpt_dir, "tests", "tools", "localpaths.py"))
    kwargs = {"config": os.path.join(wpt_dir, "wptrunner.ini"),
              "tests_root": None,
              "metadata_root": None}

    set_from_config(kwargs)
    config = kwargs["config"]
    test_paths = get_test_paths(config)

    if check_clean:
        old_manifests = {}
        for data in test_paths.itervalues():
            path = os.path.join(data["metadata_path"], "MANIFEST.json")
            with open(path) as f:
                old_manifests[path] = f.readlines()

    try:
        ManifestLoader(test_paths, force_manifest_update=True).load()

        rv = 0

        if check_clean:
            clean = diff_manifests(logger, old_manifests)
            if not clean:
                rv = 1
    finally:
        if check_clean:
            for path, data in old_manifests.iteritems():
                logger.info("Restoring manifest %s" % path)
                with open(path, "w") as f:
                    f.writelines(data)

    return rv

def diff_manifests(logger, old_manifests):
    logger.info("Diffing old and new manifests")
    import difflib

    clean = True
    for path, old in old_manifests.iteritems():
        with open(path) as f:
            new = f.readlines()

        if old != new:
            clean = False
            sm = difflib.SequenceMatcher(a=old, b=new)
            for group in sm.get_grouped_opcodes():
                logged = False
                message = []
                for op, old_0, old_1, new_0, new_1 in group:
                    if op != "equal" and not logged:
                        logged = True
                        logger.lint_error(path=path,
                                          message="Manifest changed",
                                          lineno=(old_0 + 1),
                                          source="\n".join(old[old_0:old_1]),
                                          linter="wpt-manifest")
                    if op == "equal":
                        message.extend(' ' + line for line in old[old_0:old_1])
                    if op in ('replace', 'delete'):
                        message.extend('-' + line for line in old[old_0:old_1])
                    if op in ('replace', 'insert'):
                        message.extend('+' + line for line in new[new_0:new_1])
                logger.info("".join(message))
    if clean:
        logger.info("No differences found")

    return clean