All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
merge_files Namespace Reference

Functions

def mergeFiles
 

Variables

tuple parser = OptionParser(usage="usage: %prog [options]")
 
string action = "append"
 
string dest = "fragFileNames"
 
list default = []
 
string help = "The path and name of the file one wants to merge into the 'master' one"
 
int sc = 1
 
string stampFileName = lambdax:x+".stamp"
 
tuple globalLock = locker.LockFile(os.environ["GAUDI_BUILD_LOCK"], temporary = True)
 

Function Documentation

def merge_files.mergeFiles (   fragFileNames,
  mergedFileName,
  commentChar,
  doMerge,
  ignoreMissing 
)

Definition at line 12 of file merge_files.py.

12 
13 def mergeFiles( fragFileNames, mergedFileName, commentChar, doMerge, ignoreMissing ):
14 
15  startMark = "%s --Beg " % commentChar
16  timeMark = "%s --Date inserted: %s" % (commentChar, datetime.now())
17  endMark = "%s --End " % commentChar
18  nameOffset = len(startMark)
19 
20  basenames = map(os.path.basename, fragFileNames)
21 
22  isNewFile = not os.path.exists(mergedFileName)
23 
24  # create an empty file if it does not exist
25  # "append mode" ensures that, in case of two processes trying to
26  # create the file, they do not truncate each other file
27  if isNewFile:
28  # check if the destination directory exists
29  path_to_file = os.path.split(mergedFileName)[0]
30  if path_to_file and not os.path.isdir(path_to_file):
31  # if doesn't exist, create it
32  os.makedirs(path_to_file)
33  open(mergedFileName,'a')
34 
35  mergedFile = open( mergedFileName, 'r+' )
36 
37  # locking file, gaining exclusive access to it
38  lock = locker.lock( mergedFile )
39  try:
40 
41  newLines = [ ]
42  skipBlock = ""
43  for line in mergedFile.readlines():
44  if line.startswith(startMark) and line[nameOffset:].strip() in basenames:
45  skipBlock = endMark + line[nameOffset:].strip()
46  # remove all the empty lines occurring before the start mark
47  while (len(newLines) > 0) and (newLines[-1].strip() == ''):
48  newLines.pop()
49  if not skipBlock:
50  newLines.append(line)
51  if line.startswith(skipBlock):
52  skipBlock = ""
53  if skipBlock:
54  print "WARNING: missing end mark ('%s')" % skipBlock
55 
56  if doMerge:
57  for f in fragFileNames:
58  if ignoreMissing and not os.path.exists(f):
59  print "WARNING: '%s' does not exist, I'm ignoring it" % f
60  continue
61  # I do not want to add 2 empty lines at the beginning of a file
62  if newLines:
63  newLines.append('\n\n')
64  bf = os.path.basename(f)
65  newLines.append(startMark + bf + '\n')
66  newLines.append(timeMark + '\n')
67  fileData = open(f, 'r').read()
68  newLines.append(fileData)
69  if fileData and fileData[-1] != '\n':
70  newLines.append('\n')
71  newLines.append(endMark + bf + '\n')
72 
73  mergedFile.seek(0)
74  mergedFile.truncate(0)
75  mergedFile.writelines(newLines)
76 
77  finally:
78  # unlock file
79  locker.unlock( mergedFile )
80 
81  return 0
def lock
Definition: locker.py:16
def mergeFiles
Definition: merge_files.py:12
struct GAUDI_API map
Parametrisation class for map-like implementation.
def unlock
Definition: locker.py:34

Variable Documentation

string merge_files.action = "append"

Definition at line 89 of file merge_files.py.

string merge_files.default = []

Definition at line 91 of file merge_files.py.

string merge_files.dest = "fragFileNames"

Definition at line 90 of file merge_files.py.

merge_files.globalLock = locker.LockFile(os.environ["GAUDI_BUILD_LOCK"], temporary = True)

Definition at line 172 of file merge_files.py.

string merge_files.help = "The path and name of the file one wants to merge into the 'master' one"

Definition at line 92 of file merge_files.py.

tuple merge_files.parser = OptionParser(usage="usage: %prog [options]")

Definition at line 85 of file merge_files.py.

tuple merge_files.sc = 1

Definition at line 153 of file merge_files.py.

tuple merge_files.stampFileName = lambdax:x+".stamp"

Definition at line 162 of file merge_files.py.