6 MergeConfigs: merges han text configuration files, warning on name conflicts
7 @author Peter Onyisi <ponyisi@hep.uchicago.edu>
13 returns a list of directories in parent_dir
14 Directory 'common' will appear first if present
15 if recurse = True, also includes all subdirectories in the tree
16 If user has defined DQCONFIG_DIR_WHITELIST env var, will filter based on that list
17 Example values would be: DQCONFIG_DIR_WHITELIST=L1Calo:HLT
20 whitelist = os.getenv(
'DQCONFIG_DIR_WHITELIST',
'').
split(
":")
23 for root, dum1, dum2
in os.walk(parent_dir):
24 if root == parent_dir:
29 subobjs = os.listdir(parent_dir)
30 rv = [os.join(parent_dir, x)
for x
in subobjs]
31 rv = [x
for x
in rv
if stat.S_ISDIR(os.stat(x))]
37 elif x.split(
"/")[-1]
in whitelist
or whitelist==[
'']:
42 return not dir.startswith(tuple(options.exclude), 2)
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
52 from PyUtils.Helpers
import release_metadata
54 rematch = re.compile(
r'(\S*)\s+(\S*)\s+{')
56 kwlist =
set([
'algorithm',
'compositeAlgorithm',
'reference',
'thresholds'])
65 f = os.path.join(dir, template)
66 if not os.access(f, os.R_OK):
68 if not _is_ok(dir, options):
69 print (dir,
'excluded from merge')
71 print (
'Processing', f)
75 reclevel = 0; reclist = []; kws = []
78 stripline = line.strip()
79 if len(stripline) == 0:
81 if stripline[0] ==
'#':
83 if stripline[0] ==
'}' and reclevel > 0:
85 reclevel -= 1; reclist.pop(); kws.pop()
87 match = rematch.search(line)
90 kws.append(match.group(1)); reclevel += 1; reclist.append(match.group(2))
94 fullname =
'/'.
join(reclist)
96 if fullname
in kwhash[match.group(1)]:
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.')
104 kwhash[match.group(1)][fullname] = f
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"))
113 outobj.write(
'# ****************************\n\n')
118 line +=
'# ' + f +
'\n'
119 line = line.replace(
'$',
'')
123 _usage = (
'%prog [options] templatename parent_dir outfile\n\n'
124 'example: %prog cosmics_run.config . cosmics_run_merged.config')
126 if __name__ ==
'__main__':
129 parser = optparse.OptionParser(usage=_usage)
130 parser.add_option(
'--exclude', action=
'append', metavar=
'DIRECTORY',
132 help=
'Exclude following directory from merge; can be repeated for multiple directories')
133 options, args = parser.parse_args()