Functions | |
def | mergeFiles |
Variables | |
tuple | parser = OptionParser(usage="usage: %prog [options]") |
string | dest = "fragFileName" |
default = None, | |
string | help = "The path and name of the file one wants to merge into the 'master' one" |
string | dest = "mergedFileName" |
default = None, | |
string | help = "The path and name of the 'master' file which will hold the content of all the other fragment files" |
string | dest = "commentChar" |
string | default = "#" |
string | 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)" |
string | dest = "doMerge" |
string | action = "store_true" |
default = True, | |
string | help = "Switch to actually carry on with the merging procedure" |
string | dest = "unMerge" |
string | action = "store_true" |
default = False, | |
string | help = "Switch to remove our fragment file from the 'master' file" |
string | dest = "stampDir" |
string | action = "store" |
default = None, | |
string | help = "Create the stamp file in the specified directory. If not specified" |
int | sc = 1 |
string | stampFileName = ".stamp" |
tuple | stampFileName |
tuple | sc |
tuple | stamp = open( stampFileName, 'w' ) |
def fragments::merge_files::mergeFiles | ( | fragFileName, | ||
mergedFileName, | ||||
commentChar, | ||||
doMerge | ||||
) |
Definition at line 12 of file merge_files.py.
00012 : 00013 00014 isNewFile = not os.path.exists(mergedFileName) 00015 00016 # create an empty file if it does not exist 00017 # "append mode" ensures that, in case of two processes trying to 00018 # create the file, they do not truncate each other file 00019 if isNewFile: 00020 # check if the destination directory exists 00021 path_to_file = os.path.split(mergedFileName)[0] 00022 if not os.path.isdir(path_to_file): 00023 # if doesn't exist, create it 00024 os.makedirs(path_to_file) 00025 open(mergedFileName,'a') 00026 00027 mergedFile = open( mergedFileName, 'r+' ) 00028 00029 # locking file, gaining exclusive access to it 00030 lock = locker.lock( mergedFile ) 00031 try: 00032 00033 00034 startMark = "%s --Beg %s" % ( commentChar, 00035 os.path.basename(fragFileName) ) 00036 timeMark = "%s --Date inserted: %s" % ( commentChar, 00037 str(datetime.now()) ) 00038 endMark = "%s --End %s" % ( commentChar, 00039 os.path.basename(fragFileName) ) 00040 00041 newLines = [ ] 00042 skipBlock = False 00043 for line in mergedFile.readlines(): 00044 if line.startswith(startMark): 00045 skipBlock = True 00046 # remove all the empty lines occurring before the start mark 00047 while (len(newLines) > 0) and (newLines[-1].strip() == ''): 00048 newLines.pop() 00049 if not skipBlock: 00050 newLines.append(line) 00051 if line.startswith(endMark): 00052 skipBlock = False 00053 if skipBlock: 00054 print "WARNING: missing end mark ('%s')"%endMark 00055 00056 if doMerge: 00057 # I do not want to add 2 empty lines at the beginning of a file 00058 if not isNewFile: 00059 newLines.append('\n\n') 00060 newLines.append(startMark+'\n') 00061 newLines.append(timeMark+'\n') 00062 00063 for line in open( fragFileName, 'r' ).readlines(): 00064 newLines.append(line) 00065 00066 newLines.append(endMark+'\n') 00067 00068 mergedFile.seek(0) 00069 mergedFile.truncate(0) 00070 mergedFile.writelines(newLines) 00071 00072 finally: 00073 # unlock file 00074 locker.unlock( mergedFile ) 00075 00076 return 0 00077 if __name__ == "__main__":
tuple fragments::merge_files::parser = OptionParser(usage="usage: %prog [options]") [static] |
Definition at line 81 of file merge_files.py.
string fragments::merge_files::dest = "fragFileName" [static] |
Definition at line 85 of file merge_files.py.
fragments::merge_files::default = None, [static] |
Definition at line 86 of file merge_files.py.
string fragments::merge_files::help = "The path and name of the file one wants to merge into the 'master' one" [static] |
Definition at line 87 of file merge_files.py.
string fragments::merge_files::dest = "mergedFileName" [static] |
Definition at line 92 of file merge_files.py.
fragments::merge_files::default = None, [static] |
Definition at line 93 of file merge_files.py.
string fragments::merge_files::help = "The path and name of the 'master' file which will hold the content of all the other fragment files" [static] |
Definition at line 94 of file merge_files.py.
string fragments::merge_files::dest = "commentChar" [static] |
Definition at line 99 of file merge_files.py.
string fragments::merge_files::default = "#" [static] |
Definition at line 100 of file merge_files.py.
string fragments::merge_files::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)" [static] |
Definition at line 101 of file merge_files.py.
string fragments::merge_files::dest = "doMerge" [static] |
Definition at line 105 of file merge_files.py.
string fragments::merge_files::action = "store_true" [static] |
Definition at line 106 of file merge_files.py.
fragments::merge_files::default = True, [static] |
Definition at line 107 of file merge_files.py.
string fragments::merge_files::help = "Switch to actually carry on with the merging procedure" [static] |
Definition at line 108 of file merge_files.py.
string fragments::merge_files::dest = "unMerge" [static] |
Definition at line 112 of file merge_files.py.
string fragments::merge_files::action = "store_true" [static] |
Definition at line 113 of file merge_files.py.
fragments::merge_files::default = False, [static] |
Definition at line 114 of file merge_files.py.
string fragments::merge_files::help = "Switch to remove our fragment file from the 'master' file" [static] |
Definition at line 115 of file merge_files.py.
string fragments::merge_files::dest = "stampDir" [static] |
Definition at line 119 of file merge_files.py.
string fragments::merge_files::action = "store" [static] |
Definition at line 120 of file merge_files.py.
fragments::merge_files::default = None, [static] |
Definition at line 121 of file merge_files.py.
string fragments::merge_files::help = "Create the stamp file in the specified directory. If not specified" [static] |
Definition at line 122 of file merge_files.py.
int fragments::merge_files::sc = 1 [static] |
Definition at line 136 of file merge_files.py.
string fragments::merge_files::stampFileName = ".stamp" [static] |
Definition at line 145 of file merge_files.py.
tuple fragments::merge_files::stampFileName [static] |
Initial value:
os.path.join(options.stampDir,
os.path.basename(options.fragFileName)
+ ".stamp")
Definition at line 147 of file merge_files.py.
tuple fragments::merge_files::sc [static] |
Initial value:
mergeFiles( options.fragFileName, options.mergedFileName, options.commentChar, doMerge = options.doMerge )
Definition at line 151 of file merge_files.py.
tuple fragments::merge_files::stamp = open( stampFileName, 'w' ) [static] |
Definition at line 154 of file merge_files.py.