9 from datetime
import datetime
12 def mergeFiles( fragFileNames, mergedFileName, commentChar, doMerge, ignoreMissing ):
14 startMark =
"%s --Beg " % commentChar
15 timeMark =
"%s --Date inserted: %s" % (commentChar, datetime.now())
16 endMark =
"%s --End " % commentChar
17 nameOffset = len(startMark)
19 basenames =
map(os.path.basename, fragFileNames)
21 isNewFile =
not os.path.exists(mergedFileName)
28 path_to_file = os.path.split(mergedFileName)[0]
29 if path_to_file
and not os.path.isdir(path_to_file):
31 os.makedirs(path_to_file)
32 open(mergedFileName,
'a')
34 mergedFile = open( mergedFileName,
'r+' )
42 for line
in mergedFile.readlines():
43 if line.startswith(startMark)
and line[nameOffset:].strip()
in basenames:
44 skipBlock = endMark + line[nameOffset:].strip()
46 while (len(newLines) > 0)
and (newLines[-1].strip() ==
''):
50 if line.startswith(skipBlock):
53 print "WARNING: missing end mark ('%s')" % skipBlock
56 for f
in fragFileNames:
57 if ignoreMissing
and not os.path.exists(f):
58 print "WARNING: '%s' does not exist, I'm ignoring it" % f
62 newLines.append(
'\n\n')
63 bf = os.path.basename(f)
64 newLines.append(startMark + bf +
'\n')
65 newLines.append(timeMark +
'\n')
66 fileData = open(f,
'r').read()
67 newLines.append(fileData)
68 if fileData
and fileData[-1] !=
'\n':
70 newLines.append(endMark + bf +
'\n')
73 mergedFile.truncate(0)
74 mergedFile.writelines(newLines)
82 if __name__ ==
"__main__":
84 from optparse
import OptionParser
85 parser = OptionParser(usage=
"usage: %prog [options]")
90 dest =
"fragFileNames",
92 help =
"The path and name of the file one wants to merge into the 'master' one"
97 dest =
"mergedFileName",
99 help =
"The path and name of the 'master' file which will hold the content of all the other fragment files"
104 dest =
"commentChar",
106 help =
"The type of the commenting character for the type of files at hand (this is an attempt at handling the largest possible use cases)"
111 action =
"store_true",
113 help =
"Switch to actually carry on with the merging procedure"
118 action =
"store_true",
120 help =
"Switch to remove our fragment file from the 'master' file"
127 help =
"Create the stamp file in the specified directory. If not specified"
128 +
" the directory of the source file is used."
132 action =
"store_true",
133 help =
"Do no create stamp files."
137 dest =
"ignoreMissing",
138 action =
"store_true",
139 help =
"Print a warning if a fragment file is missing, but do not fail."
142 (options, args) = parser.parse_args()
145 options.doMerge =
not options.unMerge
150 options.mergedFileName = args[-1]
151 options.fragFileNames += args[:-1]
154 if not options.fragFileNames
or \
155 not options.mergedFileName :
156 str(parser.print_help()
or "")
157 print "*** ERROR ***",sys.argv
161 if options.stampDir
is None:
162 stampFileName =
lambda x: x +
".stamp"
164 stampFileName =
lambda x: os.path.join(options.stampDir,
169 logging.basicConfig(level = logging.INFO)
171 if "GAUDI_BUILD_LOCK" in os.environ:
177 sc =
mergeFiles( options.fragFileNames, options.mergedFileName,
179 doMerge = options.doMerge,
180 ignoreMissing = options.ignoreMissing)
181 if not options.no_stamp:
182 for stamp
in map(stampFileName, options.fragFileNames):
def mergeFiles(fragFileNames, mergedFileName, commentChar, doMerge, ignoreMissing)
struct GAUDI_API map
Parametrisation class for map-like implementation.