44def merge_han_configs(template, parent_dir, out, options):
45 import sys, os
46 """
47 Merge the files in the input list to the output file.
48 @param inlist: list of filenames of input files
49 @param out: output filename
50 """
51 import re
52 from PyUtils.Helpers import release_metadata
53
54 rematch = re.compile(r'(\S*)\s+(\S*)\s+{')
55
56 kwlist =
set([
'algorithm',
'compositeAlgorithm',
'reference',
'thresholds'])
57 kwhash = {}
58 for kw in kwlist:
59 kwhash[kw] = {}
60
61 files = []
62
63
64 for dir in list_directories(parent_dir):
65 f = os.path.join(dir, template)
66 if not os.access(f, os.R_OK):
67 continue
68 if not _is_ok(dir, options):
69 print (dir, 'excluded from merge')
70 continue
71 print ('Processing', f)
72 files.append(f)
73 fobj = open(f, 'r')
74
75 reclevel = 0; reclist = []; kws = []
76
77 for line in fobj:
78 stripline = line.strip()
79 if len(stripline) == 0:
80 continue
81 if stripline[0] == '#':
82 continue
83 if stripline[0] == '}' and reclevel > 0:
84
85 reclevel -= 1; reclist.pop(); kws.pop()
86 continue
87 match = rematch.search(line)
88 if match is None:
89 continue
90 kws.append(match.group(1)); reclevel += 1; reclist.append(match.group(2))
91 if kws[-1] in kwlist:
92
93
94 fullname = '/'.join(reclist)
95
96 if fullname in kwhash[match.group(1)]:
97
98 print ('ERROR: repeated definition of %s %s' % (match.group(1), fullname))
99 print (' Current file is %s' % f)
100 print (' Earlier definition was in %s' % kwhash[match.group(1)][fullname])
101 print (' Please fix this. Merging will now stop. Output file has not been created.')
102 sys.exit(1)
103 else:
104 kwhash[match.group(1)][fullname] = f
105 fobj.close()
106
107
108 outobj = open(out, 'w')
109 outobj.write('# ****************************\n')
110 outobj.write('metadata GitInfo {\n')
111 outobj.write(' Hash = %s\n' % (release_metadata()['nightly release'] or "unknown"))
112 outobj.write('}\n')
113 outobj.write('# ****************************\n\n')
114 for f in files:
115 fobj = open(f, 'r')
116 for line in fobj:
117 if '$Id:' in line:
118 line += '# ' + f + '\n'
119 line = line.replace('$', '')
120 outobj.write(line)
121 outobj.write('\n')
122