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 lockFile = open( mergedFileName +
'.lock',
'a' )
40 fcntl.lockf( lockFile, fcntl.LOCK_EX )
41 except IOError, exc_value:
42 print "Problem when trying to lock {0}, IOError {1}".
format(mergedFile, exc_value[0])
45 mergedFile = open( mergedFileName,
'r' )
51 for line
in mergedFile.readlines():
52 if line.startswith(startMark)
and line[nameOffset:].strip()
in basenames:
53 skipBlock = endMark + line[nameOffset:].strip()
55 while (len(newLines) > 0)
and (newLines[-1].strip() ==
''):
59 if line.startswith(skipBlock):
62 print "WARNING: missing end mark ('%s')" % skipBlock
65 for f
in fragFileNames:
66 if ignoreMissing
and not os.path.exists(f):
67 print "WARNING: '%s' does not exist, I'm ignoring it" % f
71 newLines.append(
'\n\n')
72 bf = os.path.basename(f)
73 newLines.append(startMark + bf +
'\n')
74 newLines.append(timeMark +
'\n')
75 fileData = open(f,
'r').read()
76 newLines.append(fileData)
77 if fileData
and fileData[-1] !=
'\n':
79 newLines.append(endMark + bf +
'\n')
85 newFile = open( mergedFileName +
".new" ,
'w' )
86 newFile.writelines(newLines)
88 os.rename(mergedFileName +
".new",mergedFileName)
92 fcntl.lockf( lockFile, fcntl.LOCK_UN )
96 if __name__ ==
"__main__":
98 from optparse
import OptionParser
99 parser = OptionParser(usage=
"usage: %prog [options]")
104 dest =
"fragFileNames",
106 help =
"The path and name of the file one wants to merge into the 'master' one"
111 dest =
"mergedFileName",
113 help =
"The path and name of the 'master' file which will hold the content of all the other fragment files"
118 dest =
"commentChar",
120 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)"
125 action =
"store_true",
127 help =
"Switch to actually carry on with the merging procedure"
132 action =
"store_true",
134 help =
"Switch to remove our fragment file from the 'master' file"
141 help =
"Create the stamp file in the specified directory. If not specified"
142 +
" the directory of the source file is used."
146 action =
"store_true",
147 help =
"Do no create stamp files."
151 dest =
"ignoreMissing",
152 action =
"store_true",
153 help =
"Print a warning if a fragment file is missing, but do not fail."
156 (options, args) = parser.parse_args()
159 options.doMerge =
not options.unMerge
164 options.mergedFileName = args[-1]
165 options.fragFileNames += args[:-1]
168 if not options.fragFileNames
or \
169 not options.mergedFileName :
170 str(parser.print_help()
or "")
171 print "*** ERROR ***",sys.argv
175 if options.stampDir
is None:
176 stampFileName =
lambda x: x +
".stamp"
178 stampFileName =
lambda x: os.path.join(options.stampDir,
183 logging.basicConfig(level = logging.INFO)
185 if "GAUDI_BUILD_LOCK" in os.environ:
191 sc =
mergeFiles( options.fragFileNames, options.mergedFileName,
193 doMerge = options.doMerge,
194 ignoreMissing = options.ignoreMissing)
195 if not options.no_stamp:
196 for stamp
in map(stampFileName, options.fragFileNames):
def mergeFiles(fragFileNames, mergedFileName, commentChar, doMerge, ignoreMissing)
struct GAUDI_API map
Parametrisation class for map-like implementation.
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".