DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (772f7e11c7e5)

VCS Links

Line Code
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
import argparse
import logging
import os
from collections import defaultdict, namedtuple

from wptrunner.wptmanifest.serializer import serialize
from wptrunner.wptmanifest.backends import base
from wptrunner.wptmanifest.node import KeyValueNode

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


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 serialize(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):
        """Object representing all the tests in a particular manifest"""
        base.ManifestItem.__init__(self, node)
        self.child_map = {}

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

    def insert(self, child):
        self.append(child)
        self.node.append(child.node)

    def delete(self, child):
        del self.child_map[child.name]
        child.node.remove()
        child.remove()


class TestManifestItem(ExpectedManifest):
    def set_expected(self, other_manifest):
        for item in self.node.children:
            if isinstance(item, KeyValueNode) and item.data == "expected":
                assert "expected" in self._data
                item.remove()
                del self._data["expected"]
                break

        for item in other_manifest.node.children:
            if isinstance(item, KeyValueNode) and item.data == "expected":
                assert "expected" in other_manifest._data
                item.remove()
                self.node.children.insert(0, item)
                self._data["expected"] = other_manifest._data.pop("expected")
                break


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
    raise ValueError


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


def get_manifest(manifest_path):
    """Get the ExpectedManifest for a particular manifest path"""
    try:
        with open(manifest_path) as f:
            return compile(f,
                           data_cls_getter=data_cls_getter)
    except IOError:
        return None


def indent(str_data, indent=2):
    rv = []
    for line in str_data.splitlines():
        rv.append("%s%s" % (" " * indent, line))
    return "\n".join(rv)


class Differences(object):
    def __init__(self):
        self.added = []
        self.deleted = []
        self.modified = []

    def __nonzero__(self):
        return bool(self.added or self.deleted or self.modified)

    def __str__(self):
        modified = []
        for item in self.modified:
            if isinstance(item, TestModified):
                modified.append("  %s\n    %s\n%s" % (item[0], item[1], indent(str(item[2]), 4)))
            else:
                assert isinstance(item, ExpectedModified)
                modified.append("  %s\n    %s %s" % item)
        return "Added:\n%s\nDeleted:\n%s\nModified:\n%s\n" % (
            "\n".join("  %s:\n    %s" % item for item in self.added),
            "\n".join("  %s" % item for item in self.deleted),
            "\n".join(modified))


TestModified = namedtuple("TestModified", ["test", "test_manifest", "differences"])


ExpectedModified = namedtuple("ExpectedModified", ["test", "ancestor_manifest", "new_manifest"])


def compare_test(test, ancestor_manifest, new_manifest):
    changes = Differences()

    compare_expected(changes, None, ancestor_manifest, new_manifest)

    for subtest, ancestor_subtest_manifest in ancestor_manifest.child_map.iteritems():
        compare_expected(changes, subtest, ancestor_subtest_manifest,
                         new_manifest.child_map.get(subtest))

    for subtest, subtest_manifest in new_manifest.child_map.iteritems():
        if subtest not in ancestor_manifest.child_map:
            changes.added.append((subtest, subtest_manifest))

    return changes


def compare_expected(changes, subtest, ancestor_manifest, new_manifest):
    if (not (ancestor_manifest and ancestor_manifest.has_key("expected")) and
        (new_manifest and new_manifest.has_key("expected"))):
        changes.modified.append(ExpectedModified(subtest, ancestor_manifest, new_manifest))
    elif (ancestor_manifest and ancestor_manifest.has_key("expected") and
          not (new_manifest and new_manifest.has_key("expected"))):
        changes.deleted.append(subtest)
    elif (ancestor_manifest and ancestor_manifest.has_key("expected") and
          new_manifest and new_manifest.has_key("expected")):
        old_expected = ancestor_manifest.get("expected")
        new_expected = new_manifest.get("expected")
        if expected_values_changed(old_expected, new_expected):
            changes.modified.append(ExpectedModified(subtest, ancestor_manifest, new_manifest))


def expected_values_changed(old_expected, new_expected):
    if len(old_expected) != len(new_expected):
        return True

    old_dict = {}
    new_dict = {}
    for dest, cond_lines in [(old_dict, old_expected), (new_dict, new_expected)]:
        for cond_line in cond_lines:
            if isinstance(cond_line, tuple):
                condition, value = cond_line
            else:
                condition = None
                value = cond_line
            dest[condition] = value

    return new_dict != old_dict


def record_changes(ancestor_manifest, new_manifest):
    changes = Differences()

    for test, test_manifest in new_manifest.child_map.iteritems():
        if test not in ancestor_manifest.child_map:
            changes.added.append((test, test_manifest))
        else:
            ancestor_test_manifest = ancestor_manifest.child_map[test]
            test_differences = compare_test(test,
                                            ancestor_test_manifest,
                                            test_manifest)
            if test_differences:
                changes.modified.append(TestModified(test, test_manifest, test_differences))

    for test, test_manifest in ancestor_manifest.child_map.iteritems():
        if test not in new_manifest.child_map:
            changes.deleted.append(test)

    return changes


def apply_changes(current_manifest, changes):
    for test, test_manifest in changes.added:
        if test in current_manifest.child_map:
            current_manifest.delete(current_manifest.child_map[test])
        current_manifest.insert(test_manifest)

    for test in changes.deleted:
        if test in current_manifest.child_map:
            current_manifest.delete(current_manifest.child_map[test])

    for item in changes.modified:
        if isinstance(item, TestModified):
            test, new_manifest, test_changes = item
            if test in current_manifest.child_map:
                apply_changes(current_manifest.child_map[test], test_changes)
            else:
                current_manifest.insert(new_manifest)
        else:
            assert isinstance(item, ExpectedModified)
            subtest, ancestor_manifest, new_manifest = item
            if not subtest:
                current_manifest.set_expected(new_manifest)
            elif subtest in current_manifest.child_map:
                current_manifest.child_map[subtest].set_expected(new_manifest)
            else:
                current_manifest.insert(new_manifest)


def get_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("ancestor")
    parser.add_argument("current")
    parser.add_argument("new")
    parser.add_argument("dest", nargs="?")
    return parser


def get_parser_mergetool():
    parser = argparse.ArgumentParser()
    parser.add_argument("--no-overwrite", dest="overwrite", action="store_false")
    return parser



def make_changes(ancestor_manifest, current_manifest, new_manifest):
    changes = record_changes(ancestor_manifest, new_manifest)
    apply_changes(current_manifest, changes)

    return serialize(current_manifest.node)


def run(ancestor, current, new, dest):
    ancestor_manifest = get_manifest(ancestor)
    current_manifest = get_manifest(current)
    new_manifest = get_manifest(new)


    updated_current_str = make_changes(ancestor_manifest, current_manifest, new_manifest)

    if dest != "-":
        with open(dest, "wb") as f:
            f.write(updated_current_str)
    else:
        print(updated_current_str)