| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 import stringmanipulation | |
| 4 import filemanagement | |
| 5 import sys | |
| 6 | |
| 7 extensions = ['.h','.cc','.c','.cpp'] | |
| 8 | |
| 9 ignore_these = ['my_ignore_header.h'] | |
| 10 | |
| 11 if((len(sys.argv) != 2) and (len(sys.argv) != 3)): | |
| 12 print 'parameters are: directory [--commit]' | |
| 13 quit() | |
| 14 | |
| 15 directory = sys.argv[1]; | |
| 16 if(not filemanagement.pathexist(directory)): | |
| 17 print 'path ' + directory + ' does not exist' | |
| 18 quit() | |
| 19 | |
| 20 if((len(sys.argv) == 3) and (sys.argv[2] != '--commit')): | |
| 21 print 'parameters are: parent directory extension new extension [--commit]' | |
| 22 quit() | |
| 23 | |
| 24 commit = False | |
| 25 if(len(sys.argv) == 3): | |
| 26 commit = True | |
| 27 | |
| 28 files_to_fix = [] | |
| 29 for extension in extensions: | |
| 30 files_to_fix.extend(filemanagement.listallfilesinfolder(directory,\ | |
| 31 extension)) | |
| 32 | |
| 33 # Just steal the header from the template | |
| 34 def fileheaderasstring(): | |
| 35 template_file_name = 'license_template.txt' | |
| 36 if (not filemanagement.fileexist(template_file_name)): | |
| 37 print 'File ' + template_file_name + ' not found!' | |
| 38 quit() | |
| 39 template_file = open(template_file_name,'r') | |
| 40 return_string = '' | |
| 41 for line in template_file: | |
| 42 return_string += line | |
| 43 return return_string | |
| 44 | |
| 45 # Just steal the header from the template | |
| 46 def fileheaderasarray(): | |
| 47 template_file_name = 'license_template.txt' | |
| 48 if (not filemanagement.fileexist(template_file_name)): | |
| 49 print 'File ' + template_file_name + ' not found!' | |
| 50 quit() | |
| 51 template_file = open(template_file_name,'r') | |
| 52 return_value = [] | |
| 53 for line in template_file: | |
| 54 return_value.append(line) | |
| 55 return return_value | |
| 56 | |
| 57 | |
| 58 def findheader(path, file_name): | |
| 59 full_file_name = path + file_name | |
| 60 if (not filemanagement.fileexist(full_file_name)): | |
| 61 print 'File ' + file_name + ' not found!' | |
| 62 print 'Unexpected error!' | |
| 63 quit() | |
| 64 file_handle = open(full_file_name) | |
| 65 template_file_content = fileheaderasarray() | |
| 66 compare_content = [] | |
| 67 # load the same number of lines from file as the fileheader | |
| 68 for index in range(len(template_file_content)): | |
| 69 line = file_handle.readline() | |
| 70 if (line == ''): | |
| 71 return False | |
| 72 compare_content.append(line) | |
| 73 | |
| 74 while (True): | |
| 75 found = True | |
| 76 for index in range(len(template_file_content)): | |
| 77 line1 = template_file_content[index] | |
| 78 line2 = compare_content[index] | |
| 79 if(line1 != line2): | |
| 80 found = False | |
| 81 break | |
| 82 if (found): | |
| 83 return True | |
| 84 compare_content = compare_content[1:len(compare_content)] | |
| 85 line = file_handle.readline() | |
| 86 if (line == ''): | |
| 87 return False | |
| 88 compare_content.append(line) | |
| 89 return False | |
| 90 | |
| 91 # Used to store temporary result before flushing to real file when finished | |
| 92 def temporaryfilename(old_file_name): | |
| 93 return old_file_name + '.deleteme' | |
| 94 | |
| 95 def updatefile(path, old_file_name): | |
| 96 full_old_file_name = path + old_file_name | |
| 97 if (not filemanagement.fileexist(full_old_file_name)): | |
| 98 print 'File ' + full_old_file_name + ' is not found.' | |
| 99 print 'Should not happen! Ever!' | |
| 100 quit() | |
| 101 | |
| 102 full_temporary_file_name = path + temporaryfilename(old_file_name) | |
| 103 | |
| 104 # Make sure that the files are closed by putting them out of scope | |
| 105 old_file = open(full_old_file_name,'r') | |
| 106 temporary_file = open(full_temporary_file_name,'w') | |
| 107 | |
| 108 temporary_file.writelines(fileheaderasstring()) | |
| 109 remove_whitespaces = True | |
| 110 for line in old_file: | |
| 111 if (remove_whitespaces and (len(line.split()) == 0)): | |
| 112 continue | |
| 113 else: | |
| 114 remove_whitespaces = False | |
| 115 temporary_file.writelines(line) | |
| 116 old_file.close() | |
| 117 temporary_file.close() | |
| 118 | |
| 119 filemanagement.copyfile(full_old_file_name,full_temporary_file_name) | |
| 120 filemanagement.deletefile(full_temporary_file_name) | |
| 121 | |
| 122 | |
| 123 failed_files = [] | |
| 124 skipped_files = [] | |
| 125 for index in range(len(files_to_fix)): | |
| 126 if(commit): | |
| 127 print (100*index)/len(files_to_fix) | |
| 128 path_dir = files_to_fix[index][0] | |
| 129 filename = files_to_fix[index][1] | |
| 130 is_ignore = False | |
| 131 for ignore_names in ignore_these: | |
| 132 if(filename == ignore_names): | |
| 133 is_ignore = True | |
| 134 break | |
| 135 if(is_ignore): | |
| 136 continue | |
| 137 | |
| 138 # Let the word copyright be our sanity, i.e. make sure there is only one | |
| 139 # copy right occurance or report that there will be no change | |
| 140 if(filemanagement.findstringinfile(path_dir,filename,'Copyright') or | |
| 141 filemanagement.findstringinfile(path_dir,filename,'copyright') or | |
| 142 filemanagement.findstringinfile(path_dir,filename,'COPYRIGHT')): | |
| 143 if(findheader(path_dir,filename)): | |
| 144 skipped_files.append(path_dir + filename) | |
| 145 else: | |
| 146 failed_files.append(path_dir + filename) | |
| 147 continue | |
| 148 | |
| 149 if (not commit): | |
| 150 print 'File ' + path_dir + filename + ' will be updated' | |
| 151 continue | |
| 152 updatefile(path_dir,filename) | |
| 153 | |
| 154 tense = 'will be' | |
| 155 if (commit): | |
| 156 tense = 'has been' | |
| 157 if (len(skipped_files) > 0): | |
| 158 print str(len(skipped_files)) + ' file(s) ' + tense + ' skipped since they a
lready have the correct header' | |
| 159 | |
| 160 if (len(failed_files) > 0): | |
| 161 print 'Following files seem to have an invalid file header:' | |
| 162 for line in failed_files: | |
| 163 print line | |
| OLD | NEW |