-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdUpdate.py
executable file
·127 lines (102 loc) · 3.29 KB
/
mdUpdate.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python3.6
import sys
import xml.etree.ElementTree as ET
# -----------------------------------------------------------------------------
# replace the text between two lines starting with 'delimiter' in file 'fold'
# by the text between two lines starting with 'delimiter' in file 'ftouse'.
# Write new content into 'fnew' and keep the lines with the 'delimiter'.
# -----------------------------------------------------------------------------
# get text in file 'fn' beteween the lines starting with 'delimiter'
def blockbtwdelims (fn, delimiter):
blck = []
cnt = 0
with open(fn) as f:
for line in f:
if line.startswith(delimiter):
blck.append(line.rstrip())
if cnt > 0:
break
cnt += 1
else:
if cnt > 0:
blck.append(line.rstrip())
return blck
# -----------------------------------------------------------------------------
# get text in file 'fn' before any line starting with 'delimiter'
def blockbefdelims (fn, delimiter):
blck = []
with open(fn) as f:
for line in f:
if line.startswith(delimiter):
break
blck.append(line.rstrip())
return blck
# -----------------------------------------------------------------------------
# get text in file 'fn' after the text block delimited by lines starting with
# 'delimiter'
def blockaftdelims (fn, delimiter):
blck = []
cnt = 0
with open(fn) as f:
for line in f:
if line.startswith(delimiter):
if cnt < 2:
cnt += 1
continue
if cnt > 1:
blck.append(line.rstrip())
return blck
# -----------------------------------------------------------------------------
# concatenate two blocks of text
def addblocks(b0, b1):
b2 = b0
for l in b1:
b2.append(l.rstrip())
return b2
# -----------------------------------------------------------------------------
def main(initCard):
if len(sys.argv) < 4:
print ("Wrong number of arguments!")
print ("Usage:")
print (" purger.py cc fn2u fnold fnnew")
print ("")
print (" cc: 1: AO2D, 2: Helpers, 3: PWGs, 4: Joins")
print (" fn2u: file with new text")
print (" fnold: file with old text")
print (" fnnew: file with replaced text")
print ("")
exit()
cc = int(sys.argv[1])
fntouse = sys.argv[2]
fnold = sys.argv[3]
fnnew = sys.argv[4]
# get the 'delimiter' from initCard
tmp = None
if cc == 1:
tmp = initCard.find("O2general/delimAO2D")
elif cc == 2:
tmp = initCard.find("O2general/delimHelpers")
elif cc == 3:
tmp = initCard.find("O2general/delimPWGs")
elif cc == 4:
tmp = initCard.find("O2general/delimJoins")
else:
exit()
delimiter = tmp.text.strip()
print("Replacing ",delimiter)
# get replacement
b2u = blockbtwdelims(fntouse, delimiter)
if len(b2u) == 0:
exit()
# entire new text
bnew = addblocks(blockbefdelims(fnold, delimiter), b2u)
bnew = addblocks(bnew, blockaftdelims(fnold, delimiter))
# write new text to fnnew
with open(fnnew, 'w') as f:
for l in bnew:
print(l, file=f)
# -----------------------------------------------------------------------------
if __name__ == "__main__":
initCard = ET.parse("inputCard.xml")
main(initCard)
# -----------------------------------------------------------------------------