-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortModules.py
50 lines (38 loc) · 1.29 KB
/
sortModules.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 re
import string
import sys
import shutil
moduledef_pat = re.compile(r'^\$MODULE (.*)$') # Start of module definition
fin = open(sys.argv[1],'r')
fout = open('tmp.mod', 'w')
module = None # Current module name (string)
moduleDefs = {} # module name (string) -> module definition (concatenated string)
for line in fin.readlines():
# Get rid of CR characters (0x0D) and leading/trailing blanks
stripped = string.replace(line, '\x0D', '').strip()
# Check for start of module. Get the module name
match = moduledef_pat.match(stripped)
if match:
module = match.group(1)
moduleDefs[module] = line
# Check for end-of-module. Unset module name, store the line itself
elif line.startswith('$EndMODULE'):
moduleDefs[module] += line
module = None
# Check for end-of-library. Sort and write all modules
elif line.startswith('$EndLIBRARY'):
for name in sorted(moduleDefs.keys()):
sys.stdout.write("Module: '" + name + "'\n")
fout.write(moduleDefs[name])
fout.write(line)
# Inside a module definition? Append to the definition string
elif module:
moduleDefs[module] += line
# Header lines
else:
fout.write(line)
fin.close()
fout.close()
shutil.copy(sys.argv[1], sys.argv[1] + '.backup')
shutil.move('tmp.mod', sys.argv[1])