aboutsummaryrefslogtreecommitdiff
path: root/utils/mangle-jobs/mangle_helper.py
blob: 363d2c2e0a10da8888d59bf6e31364c78a3363ea (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
import base64
import lxml.etree


def get_build_config(tree):
    tag = tree.xpath("/project/properties//defaultValue")
    assert len(tag) == 1
    tag = tag[0]
    d = {}
    for l in base64.decodestring(tag.text).split("\n"):
        l = l.strip()
        if not l or l[0] == "#":
            continue
        k, v = l.split("=", 1)
        d[k] = v
    return d


def add_or_replace_node(tree, node_xpath, node_text):
    new_node = lxml.etree.fromstring(node_text)
    nodes = tree.xpath(node_xpath)
    assert len(nodes) < 2, "Please use more selective XPath expression"
    if nodes:
        # First, add new node after existing
        nodes[0].addnext(new_node)
        # Then, delete the original node
        nodes[0].getparent().remove(nodes[0])
    else:
        parent_xpath, _ = node_xpath.rsplit("/", 1)
        parent = tree.xpath(parent_xpath)[0]
        parent.append(new_node)


def add_child(tree, node_xpath, node_text):
    "Add new node (node_text) as last child of node_xpath."
    nodes = tree.xpath(node_xpath)
    assert len(nodes) == 1, "Found %d nodes with XPath %s, expected 1" % (len(nodes), node_xpath)
    new_node = lxml.etree.fromstring(node_text)
    nodes[0].append(new_node)


def add_sibling(tree, node_xpath, node_text):
    "Add new node (node_text) as next sibling after node_xpath."
    new_node = lxml.etree.fromstring(node_text)
    nodes = tree.xpath(node_xpath)
    assert len(nodes) > 0, "Node not found: %s" % node_xpath
    assert len(nodes) < 2, "Please use more selective XPath expression than %s" % node_xpath
    nodes[0].addnext(new_node)