From e6cc0a2b756f1462bcce2222437a6e159be48498 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 1 Feb 2017 17:18:08 +0100 Subject: Updated license script Added missing license headers Added pre-commit hook to check for missing licenses --- util/header-gpl3.txt | 2 +- util/license.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util/licensecheck.py | 59 ----------------------------------------- util/pre-commit.py | 19 ++++++++++++++ 4 files changed, 94 insertions(+), 60 deletions(-) create mode 100755 util/license.py delete mode 100755 util/licensecheck.py create mode 100755 util/pre-commit.py (limited to 'util') diff --git a/util/header-gpl3.txt b/util/header-gpl3.txt index 939e526..058a0f7 100644 --- a/util/header-gpl3.txt +++ b/util/header-gpl3.txt @@ -1,7 +1,7 @@ /** LICENSE ******************************************************************** ** ** smolbote: yet another qute browser - ** Copyright (C) $CURRENTYEAR$ $AUTHOR$ + ** Copyright (C) $CURRENTYEAR$ Xian Nox ** ** This program is free software: you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by diff --git a/util/license.py b/util/license.py new file mode 100755 index 0000000..0bbc6be --- /dev/null +++ b/util/license.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import argparse +import datetime +import sys + +def readLicense(licenseFile): + with open(licenseFile) as rawlicense: + licensetext = rawlicense.read() + + # fill in the year + licensetext = licensetext.replace('$CURRENTYEAR$', str(datetime.datetime.now().year)) + + return licensetext + +def hasLicense(sourceFile): + with open(sourceFile) as source: + if source.readline().startswith('/** LICENSE **'): + return True + else: + return False + +def addLicense(sourceFile, licenseText): + with open(sourceFile, 'r') as source: + origData = source.read() + with open(sourceFile, 'w') as source: + source.write(licenseText + origData) + +def lint(license, src, dryRun, verbose=False): + if verbose is True: + print("license={0}".format(license)) + print("source={0}".format(src)) + + # read license file + licensetext = readLicense(license) + if verbose is True: + print("-- license text --") + print(licensetext) + print("-- license text --") + + if len(src) is 0: + print("No files to process.") + return 0 + + # open source file and check for license + numFaults = 0 + for filename in src: + if hasLicense(filename) is True: + if verbose: + print("OK {0}".format(filename)) + else: + if dryRun is True: + if verbose: + print("fail {0}".format(filename)) + else: + addLicense(filename, licensetext) + print("fix {0}".format(filename)) + + numFaults += 1 + + if numFaults is not 0: + print("License check: number of faults: {0}".format(numFaults)) + + return numFaults + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='check source files for license') + parser.add_argument('-l', '--license', required=True, help="use this file as license header template") + parser.add_argument('-v', '--verbose', action='store_true', help="print additional information") + parser.add_argument('-c', '--dry-run', action='store_true', help="don't modify any files, only check for licenses") + parser.add_argument('src', nargs='*', help="list of files to check") + args = parser.parse_args() + + sys.exit(lint(args.license, args.src, args.dry_run, args.verbose)) diff --git a/util/licensecheck.py b/util/licensecheck.py deleted file mode 100755 index be6de9a..0000000 --- a/util/licensecheck.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python3 - -# TODO: username argument -# TODO: updating licenses - -import argparse -import datetime - -def getLicense(licenseFile): - with open(licenseFile) as rawlicense: - licensetext = rawlicense.read() - - # fill in name and year - licensetext = licensetext.replace('$AUTHOR$', 'Xian Nox') - licensetext = licensetext.replace('$CURRENTYEAR$', str(datetime.datetime.now().year)) - - return licensetext - -def hasLicense(sourceFile): - with open(sourceFile) as source: - if source.readline().startswith('/** LICENSE **'): - return True - else: - return False - -def fix(sourceFile, licenseText): - with open(sourceFile, 'r') as source: - origData = source.read() - with open(sourceFile, 'w') as source: - source.write(licenseText + origData) - -def main(license, src, verbose=False): - if verbose is True: - print("Using license header {0}".format(license)) - print("Going through {0}".format(src)) - - # read license file - licensetext = getLicense(license) - #print(licensetext) - - # open source file and check for license - for filename in src: - if hasLicense(filename): - print("{0} has license".format(filename)) - else: - fix(filename, licensetext) - print("{0} license added".format(filename)) - -if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Check source files for license') - parser.add_argument('-l', '--license', required=True) - parser.add_argument('-v', '--verbose', action='store_true') - parser.add_argument('src', nargs='*') - args = parser.parse_args() - - main(args.license, args.src, args.verbose) -else: - print('Do not use this as an import!') - sys.exit(-1) diff --git a/util/pre-commit.py b/util/pre-commit.py new file mode 100755 index 0000000..f8242f6 --- /dev/null +++ b/util/pre-commit.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +import os +import sys +import glob +import license + +# stash unstaged files before running test +os.system("git stash -q --keep-index") + +print("Running pre-commit hook in {0}".format(os.getcwd())) + +problems = 0 +problems += license.lint("util/header-gpl3.txt", glob.glob("src/**/*.cpp") + glob.glob("src/**/*.h"), True) + +# restore stash +os.system("git stash pop -q") + +sys.exit(problems) -- cgit v1.2.1