9 from datetime
import datetime
12 def mergeFiles( fragFileNames, mergedFileName, commentChar, doMerge ):
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:
59 newLines.append(
'\n\n')
60 bf = os.path.basename(f)
61 newLines.append(startMark + bf +
'\n')
62 newLines.append(timeMark +
'\n')
63 fileData = open(f,
'r').read()
64 newLines.append(fileData)
65 if fileData
and fileData[-1] !=
'\n':
67 newLines.append(endMark + bf +
'\n')
70 mergedFile.truncate(0)
71 mergedFile.writelines(newLines)
79 if __name__ ==
"__main__":
81 from optparse
import OptionParser
82 parser = OptionParser(usage=
"usage: %prog [options]")
87 dest =
"fragFileNames",
89 help =
"The path and name of the file one wants to merge into the 'master' one"
94 dest =
"mergedFileName",
96 help =
"The path and name of the 'master' file which will hold the content of all the other fragment files"
101 dest =
"commentChar",
103 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)"
108 action =
"store_true",
110 help =
"Switch to actually carry on with the merging procedure"
115 action =
"store_true",
117 help =
"Switch to remove our fragment file from the 'master' file"
124 help =
"Create the stamp file in the specified directory. If not specified"
125 +
" the directory of the source file is used."
129 action =
"store_true",
130 help =
"Do no create stamp files."
133 (options, args) = parser.parse_args()
136 options.doMerge =
not options.unMerge
141 options.mergedFileName = args[-1]
142 options.fragFileNames += args[:-1]
145 if not options.fragFileNames
or \
146 not options.mergedFileName :
147 str(parser.print_help()
or "")
148 print "*** ERROR ***",sys.argv
152 if options.stampDir
is None:
153 stampFileName =
lambda x: x +
".stamp"
155 stampFileName =
lambda x: os.path.join(options.stampDir,
160 logging.basicConfig(level = logging.INFO)
162 if "GAUDI_BUILD_LOCK" in os.environ:
168 sc =
mergeFiles( options.fragFileNames, options.mergedFileName,
170 doMerge = options.doMerge )
171 if not options.no_stamp:
172 for stamp
in map(stampFileName, options.fragFileNames):