File: manifestupdate.py

package info (click to toggle)
firefox-esr 52.8.1esr-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 1,983,244 kB
  • sloc: cpp: 4,810,275; ansic: 2,004,548; python: 451,282; java: 241,615; asm: 178,649; xml: 136,302; sh: 82,207; makefile: 22,575; perl: 15,783; objc: 4,389; yacc: 1,816; ada: 1,697; pascal: 1,519; lex: 1,257; cs: 879; exp: 499; php: 436; lisp: 258; awk: 152; sed: 51; ruby: 47; csh: 27
file content (89 lines) | stat: -rw-r--r-- 3,052 bytes parent folder | download | duplicates (4)
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