merge_files Namespace Reference

Functions

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

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 def mergeFiles( fragFileNames, mergedFileName, commentChar, doMerge, ignoreMissing ):
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  if ignoreMissing and not os.path.exists(f):
58  print "WARNING: '%s' does not exist, I'm ignoring it" % f
59  continue
60  # I do not want to add 2 empty lines at the beginning of a file
61  if newLines:
62  newLines.append('\n\n')
63  bf = os.path.basename(f)
64  newLines.append(startMark + bf + '\n')
65  newLines.append(timeMark + '\n')
66  fileData = open(f, 'r').read()
67  newLines.append(fileData)
68  if fileData and fileData[-1] != '\n':
69  newLines.append('\n')
70  newLines.append(endMark + bf + '\n')
71 
72  mergedFile.seek(0)
73  mergedFile.truncate(0)
74  mergedFile.writelines(newLines)
75 
76  finally:
77  # unlock file
78  locker.unlock( mergedFile )
79 
80  return 0
81 
def mergeFiles(fragFileNames, mergedFileName, commentChar, doMerge, ignoreMissing)
Definition: merge_files.py:12
struct GAUDI_API map
Parametrisation class for map-like implementation.
def lock(file)
Definition: locker.py:16
def unlock(file)
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.

tuple 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.