Gaudi Framework, version v23r5

Home   Generated: Wed Nov 28 2012
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
merge_files.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # @file: GaudiPolicy/cmt/fragments/merge_files.py
4 # @purpose: merge_files <fragment> file into a 'per-project' <merged> file
5 # @author: Sebastien Binet <binet@cern.ch>
6 
7 import os
8 import sys
9 from datetime import datetime
10 import locker
11 
12 def mergeFiles( fragFileNames, mergedFileName, commentChar, doMerge ):
13 
14  startMark = "%s --Beg " % commentChar
15  timeMark = "%s --Date inserted: %s" % (commentChar, datetime.now())
16  endMark = "%s --End " % commentChar
17  nameOffset = len(startMark)
18 
19  basenames = map(os.path.basename, fragFileNames)
20 
21  isNewFile = not os.path.exists(mergedFileName)
22 
23  # create an empty file if it does not exist
24  # "append mode" ensures that, in case of two processes trying to
25  # create the file, they do not truncate each other file
26  if isNewFile:
27  # check if the destination directory exists
28  path_to_file = os.path.split(mergedFileName)[0]
29  if path_to_file and not os.path.isdir(path_to_file):
30  # if doesn't exist, create it
31  os.makedirs(path_to_file)
32  open(mergedFileName,'a')
33 
34  mergedFile = open( mergedFileName, 'r+' )
35 
36  # locking file, gaining exclusive access to it
37  lock = locker.lock( mergedFile )
38  try:
39 
40  newLines = [ ]
41  skipBlock = ""
42  for line in mergedFile.readlines():
43  if line.startswith(startMark) and line[nameOffset:].strip() in basenames:
44  skipBlock = endMark + line[nameOffset:].strip()
45  # remove all the empty lines occurring before the start mark
46  while (len(newLines) > 0) and (newLines[-1].strip() == ''):
47  newLines.pop()
48  if not skipBlock:
49  newLines.append(line)
50  if line.startswith(skipBlock):
51  skipBlock = ""
52  if skipBlock:
53  print "WARNING: missing end mark ('%s')" % skipBlock
54 
55  if doMerge:
56  for f in fragFileNames:
57  # I do not want to add 2 empty lines at the beginning of a file
58  if newLines:
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':
66  newLines.append('\n')
67  newLines.append(endMark + bf + '\n')
68 
69  mergedFile.seek(0)
70  mergedFile.truncate(0)
71  mergedFile.writelines(newLines)
72 
73  finally:
74  # unlock file
75  locker.unlock( mergedFile )
76 
77  return 0
78 
79 if __name__ == "__main__":
80 
81  from optparse import OptionParser
82  parser = OptionParser(usage="usage: %prog [options]")
83  parser.add_option(
84  "-i",
85  "--input-file",
86  action = "append",
87  dest = "fragFileNames",
88  default = [],
89  help = "The path and name of the file one wants to merge into the 'master' one"
90  )
91  parser.add_option(
92  "-m",
93  "--merged-file",
94  dest = "mergedFileName",
95  default = None,
96  help = "The path and name of the 'master' file which will hold the content of all the other fragment files"
97  )
98  parser.add_option(
99  "-c",
100  "--comment-char",
101  dest = "commentChar",
102  default = "#",
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)"
104  )
105  parser.add_option(
106  "--do-merge",
107  dest = "doMerge",
108  action = "store_true",
109  default = True,
110  help = "Switch to actually carry on with the merging procedure"
111  )
112  parser.add_option(
113  "--un-merge",
114  dest = "unMerge",
115  action = "store_true",
116  default = False,
117  help = "Switch to remove our fragment file from the 'master' file"
118  )
119  parser.add_option(
120  "--stamp-dir",
121  dest = "stampDir",
122  action = "store",
123  default = None,
124  help = "Create the stamp file in the specified directory. If not specified"
125  +" the directory of the source file is used."
126  )
127  parser.add_option(
128  "--no-stamp",
129  action = "store_true",
130  help = "Do no create stamp files."
131  )
132 
133  (options, args) = parser.parse_args()
134 
135  # ensure consistency...
136  options.doMerge = not options.unMerge
137 
138  # allow command line syntax as
139  # merge_files.py [options] <fragment file1> [<fragment file2>...] <merged file>
140  if args:
141  options.mergedFileName = args[-1]
142  options.fragFileNames += args[:-1]
143 
144  sc = 1
145  if not options.fragFileNames or \
146  not options.mergedFileName :
147  str(parser.print_help() or "")
148  print "*** ERROR ***",sys.argv
149  sys.exit(sc)
150  pass
151 
152  if options.stampDir is None:
153  stampFileName = lambda x: x + ".stamp"
154  else:
155  stampFileName = lambda x: os.path.join(options.stampDir,
156  os.path.basename(x)
157  + ".stamp")
158  # Configure Python logging
159  import logging
160  logging.basicConfig(level = logging.INFO)
161 
162  if "GAUDI_BUILD_LOCK" in os.environ:
163  globalLock = locker.LockFile(os.environ["GAUDI_BUILD_LOCK"], temporary = True)
164  else:
165  globalLock = None
166 
167  if True: #try:
168  sc = mergeFiles( options.fragFileNames, options.mergedFileName,
169  options.commentChar,
170  doMerge = options.doMerge )
171  if not options.no_stamp:
172  for stamp in map(stampFileName, options.fragFileNames):
173  open(stamp, 'w')
174  #except IOError, err:
175  # print "ERROR:",err
176  #except Exception, err:
177  # print "ERROR:",err
178  #except:
179  # print "ERROR: unknown error !!"
180 
181  del globalLock
182 
183  sys.exit( sc )

Generated at Wed Nov 28 2012 12:17:17 for Gaudi Framework, version v23r5 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004