-
Notifications
You must be signed in to change notification settings - Fork 1
/
commentOutBadXML.py
executable file
·65 lines (50 loc) · 2.08 KB
/
commentOutBadXML.py
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
#!/usr/bin/env python
"""
Produce new XML file with filenames that appear in a text file commented out
"""
from __future__ import print_function
import os
import argparse
import xml.etree.ElementTree as ET
import sys
from CommentedTreeBuilder import CommentedTreeBuilder
def get_xml_tree(xml_filename):
with open(xml_filename) as f:
# hack to trick XML parsing - put under a root node
xml_str = "<data>%s</data>" % f.read()
parser = ET.XMLParser(target=CommentedTreeBuilder())
root = ET.fromstring(xml_str, parser)
return root
def get_filenames_from_plain_text(txt_filename):
with open(txt_filename) as f:
filenames = f.readlines()
# Convert // to /, remove \n, etc
filenames = [os.path.normpath(rf.strip()) for rf in filenames if rf.strip()]
return filenames
def comment_out_bad_xml(xml_filename, txt_filename, output_filename):
reference_filenames = get_filenames_from_plain_text(txt_filename)
with open(output_filename, 'w') as f:
root = get_xml_tree(xml_filename)
for child in root:
if child.tag != "In":
# this will print commented out lines
f.write(ET.tostring(child))
else:
filename = child.attrib['FileName']
if ".root" in filename:
filename = os.path.normpath(filename.strip())
if filename not in reference_filenames:
# print out line as it was before
f.write(ET.tostring(child))
else:
# comment it out
f.write("<!-- BAD %s -->\n" % ET.tostring(child).strip())
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("xml", help="Input XML file")
parser.add_argument("txt", help="Output text file")
args = parser.parse_args()
stem, ext = os.path.splitext(args.xml)
output_filename = stem + "_nobad" + ext
comment_out_bad_xml(args.xml, args.txt, output_filename)
print("Written updated file", output_filename)