1#This class applies patches to an XML file during do_patch().  The
2#patches themselves are specified in XML in a separate file that must
3#be in SRC_URI and end in .patch.xml.  The patch XML file must also
4#have a <targetFile> element that specifies the base name of the file
5#that needs to be patched.
6#
7#See patchxml.py for details on the XML patch format.
8#
9
10inherit python3native
11inherit obmc-phosphor-utils
12do_patch[depends] = "mrw-patch-native:do_populate_sysroot"
13
14
15def find_patch_files(d):
16    all_patches = listvar_to_list(d, 'SRC_URI')
17    xml_patches = [x for x in all_patches if x.endswith('.patch.xml') and
18                         x.startswith('file://')]
19
20    return [x.lstrip('file://') for x in xml_patches]
21
22
23def apply_xml_patch(base_patch_name, d):
24    import xml.etree.ElementTree as et
25    import subprocess
26
27    patch_file = os.path.join(d.getVar("WORKDIR", True), base_patch_name)
28
29    if not os.path.exists(patch_file):
30        bb.fatal("Could not find patch file " + patch_file +
31                 " specified in SRC_URI")
32
33    patch_tree = et.parse(patch_file)
34    patch_root = patch_tree.getroot()
35    target_file_element = patch_root.find("targetFile")
36
37    if target_file_element is None:
38        bb.fatal("Could not find <targetFile> element in patch file "
39                 + patch_file)
40    else:
41        xml = target_file_element.text
42
43    xml = os.path.join(d.getVar("S", True), xml)
44
45    if not os.path.exists(xml):
46        bb.fatal("Could not find XML file to patch: " + xml)
47
48    print("Applying XML fixes found in " + patch_file + " to " + xml)
49
50    cmd = []
51    cmd.append(os.path.join(d.getVar("bindir", True), "obmc-mrw/patchxml.py"))
52    cmd.append("-x")
53    cmd.append(xml)
54    cmd.append("-p")
55    cmd.append(patch_file)
56    cmd.append("-o")
57    cmd.append(xml + ".patched")
58
59    try:
60        subprocess.check_call(cmd)
61    except subprocess.CalledProcessError as e:
62        bb.fatal("Failed patching XML:\n%s" % e.output)
63
64    os.rename(xml, xml + ".orig")
65    os.rename(xml + ".patched", xml)
66
67
68python xmlpatch_do_patch() {
69
70    for patch in find_patch_files(d):
71        apply_xml_patch(patch, d)
72}
73
74do_patch[postfuncs] += "xmlpatch_do_patch"
75