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 )
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(...)".