00001
00002
00003 import getopt
00004 import os
00005 import os.path
00006 import re
00007 import sys
00008
00009 extensions = dict()
00010 extensions['cpp'] = [".h", ".hpp", ".cpp", ".c", ".C", ".cc", ".hxx", ".cxx"]
00011 extensions['cuda'] = [".cu", ".cuh"]
00012 extensions['py'] = [".py"]
00013 extensions['cmake'] = [".find", ".cmake", ".txt"]
00014
00015 def Usage(progname):
00016 print >> sys.stderr, "Usage: " + os.path.basename(progname) + " [options] [filename]"
00017 print >> sys.stderr, " -C DIRECTORY target directory (can be specified multiple times)"
00018 print >> sys.stderr, " -l LICENSE specify a new license file"
00019 print >> sys.stderr, " -f insert the license header if none was found"
00020 print >> sys.stderr, " -o overwrite existing license"
00021 print >> sys.stderr, " -q don't print status messages"
00022 print >> sys.stderr, " -t filetype ('cmake', 'cpp', 'py' or 'cuda'). Determines the comment indicator and filename patterns. Default is 'c'"
00023 print >> sys.stderr, " --dry-run only print out which files would be changed (overrides -q)"
00024
00025 begin_license_block = dict()
00026 end_license_block = dict()
00027 begin_license_block['cpp'] = re.compile("// -- BEGIN LICENSE BLOCK --")
00028 end_license_block['cpp'] = re.compile("// -- END LICENSE BLOCK --")
00029
00030 begin_license_block['cuda'] = re.compile("// -- BEGIN LICENSE BLOCK --")
00031 end_license_block['cuda'] = re.compile("// -- END LICENSE BLOCK --")
00032
00033 begin_license_block['py'] = re.compile("# -- BEGIN LICENSE BLOCK --")
00034 end_license_block['py'] = re.compile("# -- END LICENSE BLOCK --")
00035
00036 begin_license_block['cmake'] = re.compile("# -- BEGIN LICENSE BLOCK --")
00037 end_license_block['cmake'] = re.compile("# -- END LICENSE BLOCK --")
00038
00039 extended_license_begin = dict()
00040 extended_license_end = dict()
00041 extended_license_begin['cpp'] = ["// -- BEGIN LICENSE BLOCK ----------------------------------------------\n"]
00042 extended_license_end['cpp'] = ["// -- END LICENSE BLOCK ------------------------------------------------\n","\n"]
00043
00044 extended_license_begin['cuda'] = ["// -- BEGIN LICENSE BLOCK ----------------------------------------------\n"]
00045 extended_license_end['cuda'] = ["// -- END LICENSE BLOCK ------------------------------------------------\n","\n"]
00046
00047 extended_license_begin['py'] = ["# -- BEGIN LICENSE BLOCK ----------------------------------------------\n"]
00048 extended_license_end['py'] = ["# -- END LICENSE BLOCK ------------------------------------------------\n","\n"]
00049
00050 extended_license_begin['cmake'] = ["# -- BEGIN LICENSE BLOCK ----------------------------------------------\n"]
00051 extended_license_end['cmake'] = ["# -- END LICENSE BLOCK ------------------------------------------------\n","\n"]
00052
00053
00054
00055 emacs_line = re.compile("-\\*-.*-\\*-")
00056
00057 def ProcessFile(filename, license_text, insert_if_missing, overwrite, quiet, dry_run, filetype):
00058 if not quiet:
00059 sys.stderr.write("Processing file " + filename + ": ")
00060
00061 infile = open(filename, "r")
00062 input_content = infile.readlines()
00063 infile.close()
00064
00065 output_content = []
00066 existing_license = []
00067
00068 license_block = False
00069 license_block_found = False
00070 for line in input_content:
00071 if not license_block:
00072 if begin_license_block[filetype].search(line):
00073 if not quiet:
00074 sys.stderr.write("found license block ... ")
00075 license_block = True
00076 license_block_found = True
00077
00078 output_content += [line]
00079 else:
00080 if end_license_block[filetype].search(line):
00081 license_block = False
00082 if len(existing_license) == 0 or overwrite:
00083 if not quiet:
00084 sys.stderr.write("exchanged")
00085 output_content += license_text
00086 else:
00087 if not quiet:
00088 sys.stderr.write("existing license kept")
00089 output_content += existing_license
00090 output_content += [line]
00091 elif license_block:
00092 existing_license += [line]
00093
00094 if not license_block_found:
00095 if not quiet:
00096 sys.stderr.write("no license block ... ")
00097 if insert_if_missing:
00098 if not quiet:
00099 sys.stderr.write("added")
00100 extended_license_text = extended_license_begin[filetype] + license_text + extended_license_end[filetype]
00101 if len(output_content)>0 and emacs_line.search(output_content[0]):
00102 extended_license_text.insert(0, "\n")
00103 output_content = [output_content[0]] + extended_license_text + output_content[1:]
00104 else:
00105 output_content = extended_license_text + output_content[:]
00106 elif not quiet:
00107 sys.stderr.write("ignored")
00108
00109 if not dry_run:
00110 outfile = open(filename, "w")
00111 outfile.writelines(output_content)
00112 outfile.close()
00113
00114 if not quiet:
00115 sys.stderr.write("\n")
00116
00117 def FindFiles(directories, filetype):
00118 file_list = []
00119
00120 for directory in directories:
00121
00122 for root, dirs, files in os.walk(directory):
00123 for f in files:
00124 abs_f = os.path.join(root,f)
00125
00126 if os.path.isdir(abs_f):
00127 file_list += FindFiles(os.readlink(abs_f))
00128 elif os.path.splitext(f)[1] in extensions[filetype]:
00129 file_list.append(abs_f)
00130
00131 return file_list
00132
00133 def Main(argv):
00134
00135 files = None
00136 directories = None
00137 license_file = None
00138 insert_if_missing = False
00139 quiet = False
00140 overwrite = False
00141 dry_run = False
00142 filetype = "cpp"
00143
00144
00145 try:
00146 opts, args = getopt.getopt(argv[1:], "C:l:t:foqh", ["directory=", "license=", "type=", "force", "overwrite", "quiet", "help", "dry-run"])
00147 except getopt.GetoptError:
00148 print "getopt error"
00149 sys.exit(1)
00150
00151 if len(opts) == 0:
00152 Usage(argv[0])
00153 return 0
00154
00155 for opt, arg in opts:
00156 if opt in ("-C", "--directory"):
00157 if directories == None:
00158 directories = [arg]
00159 else:
00160 directories += arg
00161
00162 elif opt in ("-l", "--license"):
00163 license_file = arg
00164
00165 elif opt in ("-f", "--force"):
00166 insert_if_missing = True
00167
00168 elif opt in ("-q", "--quiet"):
00169 quiet = True
00170
00171 elif opt in ("-o", "--overwrite"):
00172 overwrite = True
00173
00174 if opt in ("-t", "--type"):
00175 if arg in ["cmake", "cpp", "cuda", "py"]:
00176 filetype = arg
00177 else:
00178 print "Error: not a known filetype. Use 'cmake', 'cpp', 'cuda', or 'py'!"
00179 return 0
00180
00181 elif opt in ("-h", "--help"):
00182 Usage(argv[0])
00183 return 0
00184
00185 elif opt in ("--dry-run"):
00186 dry_run = True
00187 quiet = False
00188
00189
00190 if directories == None:
00191 directories = [os.path.join(os.environ["MCAHOME"],"libraries"),
00192 os.path.join(os.environ["MCAHOME"],"projects"),
00193 os.path.join(os.environ["MCAHOME"],"tools"),
00194 os.path.join(os.environ["MCAHOME"],"packages")]
00195
00196
00197 if len(args) > 0:
00198 files = args
00199 else:
00200 files = FindFiles(directories, filetype)
00201
00202
00203 license_text = []
00204 if license_file != None and os.path.exists(license_file):
00205 infile = open(license_file, "r")
00206 license_text = infile.readlines()
00207 infile.close()
00208
00209
00210 for filename in files:
00211 ProcessFile(filename, license_text, insert_if_missing, overwrite, quiet, dry_run, filetype)
00212
00213 return 0
00214
00215 if __name__ == "__main__":
00216 sys.exit(Main(sys.argv))