#!/usr/bin/env python3 # (C) 2019 The Johns Hopkins University Applied Physics Laboratory LLC import os import subprocess import re import sys COPYRIGHT = "2019 The Johns Hopkins University Applied Physics Laboratory LLC" def is_text(filename): msg = subprocess.Popen(["file", filename], stdout=subprocess.PIPE).communicate()[0].decode() return re.search('text|empty', msg) != None def main(): foundUncopyrighted = False p = subprocess.Popen(["git", "diff", "-z", "--cached", "--name-status"], stdout = subprocess.PIPE) lines = p.communicate()[0].strip().decode('utf8').split(u'\x00') for i in range(0, len(lines) - 1, 2): if lines[i].upper() == "A": filename = lines[i + 1] if is_text(filename): p = subprocess.Popen(["grep", COPYRIGHT, filename], stdout = subprocess.DEVNULL) exitCode = p.wait() if exitCode != 0: print("New file '" + lines[i + 1] + "' has no copyright header.") foundUncopyrighted = True if foundUncopyrighted: sys.stdin = open("/dev/tty", "r") answer = input("\nAbort commit to add copyright? [Yn] ").strip() if answer == "" or answer.upper() == "Y" or answer.upper() == "yes": return 1 return 0 if __name__ == "__main__": sys.exit(main())