-
Notifications
You must be signed in to change notification settings - Fork 1
/
source2filename.py
50 lines (42 loc) · 1.44 KB
/
source2filename.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
#!/usr/bin/env python
import sys
import xml.etree.ElementTree as ET
import lxml.etree
import re
import os.path
def convert_source(filename):
#read input file
fin = open(filename, "rt")
#read file contents to string
data = fin.read()
# xmlstr is your xml in a string
root = lxml.etree.fromstring(data)
sources = root.find('sources')
packages = root.find('packages')
for package in packages:
classes = package.find('classes')
for clazz in classes:
file_not_found = True
for source in sources:
full_filename = source.text + '/' + clazz.attrib['filename']
if os.path.isfile(full_filename):
clazz.attrib['filename'] = full_filename
file_not_found = False
if file_not_found:
print("Warning: File {} not found in all sources; removing from sources.".format(clazz.attrib['filename']))
clazz.getparent().remove(clazz)
data = lxml.etree.tostring(root, pretty_print=True)
#close the input file
fin.close()
#open the input file in write mode
fin = open(filename, "wb")
#overrite the input file with the resulting data
fin.write(data)
#close the file
fin.close()
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: source2filename.py FILENAME")
sys.exit(1)
filename = sys.argv[1]
convert_source(filename)