aboutsummaryrefslogtreecommitdiff
path: root/utils/mangle-jobs/mangle_helper.py
blob: 93735a953907b10e51a1770b0978cd28b61910b1 (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
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)