Instantly share code, notes, and snippets.

What would you like to do?
A simple tool for managing the license header of C/C++ files. Allows for removal, regeneration.
# Edit Header for C/C++
# Scan Files and Verify license for C/C++
# Emanuele Ruffaldi 2015-2016
# Usage:
# - select files using a combination of find/grep e.g.
# find . | grep -v buildosx | grep -v buildwin | grep -E ".*\.(cpp|hpp|h)$"
# - verify used headers or per-file
# python --filelist FILELIST --all --listlicenses
# python --filelist FILELIST --all --list
# - regenerate all
# python --filelist FILELIST --all --replace MYLICENSE
# - replace license
import argparse
import os,re
import sys
from collections import defaultdict
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def findfiles(args):
rem = re.compile(".*\.(cpp|hpp|c|h)$")
files = []
#build expression for the exclusion of paths and files
for x in args.paths:
if os.path.isfile(x):
matches = []
for root, dirnames, filenames in os.walk(x):
# TODO check path exclusion
for filename in filenames:
if rem.match(filename):
# TODO check file exclusion
files.append(os.path.join(root, filename))
return files
def extractlicense(filename,args):
lines = []
doublemode = False
hasmarker = False
for e,l in enumerate(open(filename,"r")):
l = l.strip()
if e == 0:
# first line
if not l.startswith("/*"):
if l.startswith("//"):
doublemode = True
elif l.startswith("#"):
return (False,False,e,[])
return (False,False,e,"OTHER")
l = l[2:].strip()
if l != "" and l != "*":
# stop on preprocessor
if l.startswith("#"):
elif doublemode:
if l.startswith("//"):
l = l[2:].strip()
if l == args.marker:
if l.startswith("*/"):
if not args.special:
if l.startswith("*"):
l = l[1:].strip()
if l == args.marker:
hasmarker = True
if not args.special:
return (doublemode,hasmarker,e,lines)
def main():
parser = argparse.ArgumentParser(description='List and modify licenses')
parser.add_argument('paths', metavar='paths', type=str, nargs='*',
help='list of files')
parser.add_argument('--filelist', help="takes files from this list instead from path")
parser.add_argument('--list', action='store_true',help="list file found with details")
parser.add_argument('--listlicenses', action='store_true',help="list licenses with related files")
parser.add_argument('--listnames', action='store_true',help="list filenames only")
#parser.add_argument('--exclude',type=str,nargs='*',help="relative paths to be excluded")
parser.add_argument('--marker', help="marker used at the end of the first block comment",default="--")
parser.add_argument('--all', help="select all files, otherwise only ones with marker present or empty license",action="store_true")
#parser.add_argument('--selecttopragma', help="removes license froms elected files",action="store_true")
parser.add_argument('--remove', help="Removes All licenses",action="store_true")
parser.add_argument('--test', help="Test Mode, does nothing",action="store_true")
parser.add_argument('--replace', help="Replace all licenses with new file")
parser.add_argument('--special', help="eat everything",action="store_true")
parser.add_argument('--new', help="Instead of replacing files produce a file with appended .new",action="store_true")
parser.add_argument('--regen', help="Extract and Regenerate the Licence",action="store_true")
args = parser.parse_args()
newlicense = None
if args.remove:
newlicense = []
elif args.replace:
newlicense = open(args.replace,"r").read().split("\n")
elif args.regen:
newlicense = []
files = []
if args.filelist:
files = [x.strip() for x in open(args.filelist,"r") if x.strip() != ""]
files = findfiles(args)
wfiles = [(x,extractlicense(x,args)) for x in files]
if not args.all:
# pick only marker or empty
wfiles = [x for x in wfiles if x[1][1] or len(x[1][3]) == 0]
if args.listnames:
for f,l in wfiles:
print f
elif args.list:
for f,l in wfiles:
(doublemode,hasmarker,e,lines) = l
print bcolors.OKGREEN,f,bcolors.ENDC
print "\tdoublemode",doublemode
print "\thasmarker",hasmarker
print "\tendline",e
print "\tlines",lines
elif args.listlicenses:
ll = defaultdict(list)
for f,l in wfiles:
(doublemode,hasmarker,e,lines) = l
for k,v in ll.iteritems():
print k
for y in v:
print "\t",y
elif newlicense is not None:
if len(newlicense) != 0:
a = ["/**"]
for x in newlicense:
a.append(" * " + x)
a.append(" * " +args.marker)
a.append(" */")
newlicenseblock = a
newlicenseblock = []
for f,l in wfiles:
doublemode,hasmarker,e,existing = l
ff = open(f,"r")
w ="\n")
pre = w[0:e]
post = w[e:]
if not args.regen and newlicense == existing:
print "same",f
if args.regen:
newlicenseblock = existing
tf = ( and f+".new" or f)
if not args.test:
print "updating",f,len(newlicenseblock),"post",len(post)
open(tf,"w").write("\n".join(newlicenseblock + post))
print "test updating",tf,"newlicenselines",len(newlicenseblock),"from",e,"bodylines",len(post)
print "no action"
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment