#!/usr/bin/env python # Copyright (c) 2009 Tim Freeman # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # (This is the standard MIT License, copied from # http://www.opensource.org/licenses/mit-license.php on 24 Apr 2007.) #desc Check that the given files incorporate copies of the LICENSE file. #desc If -fix is passed, modify files to make them have a license. # Tested on Linux. On a Windows or Mac, think more about the \r\n's # or the \r's at the ends of the lines, and perhaps redo the part # about preserving the execute bit. import sys from os import stat, chmod, rename from util import read_file import license_text from license_text import trim_trailing_spaces python_comment = "# " license = license_text.license(lambda x:x) license = python_comment + license.replace("\n", "\n" + python_comment) license = trim_trailing_spaces(license)+"\n" split_license = license.split("\n") assert "Copyright" in split_license[0] most_of_license = "\n".join(split_license[1:]) assert "Copyright" not in most_of_license def has_stale_license(name): return not has_license(name) and \ (most_of_license in trim_trailing_spaces(read_file(name))) def has_license(name): return license in trim_trailing_spaces(read_file(name)) def usage(): sys.stderr.write("Usage: python license_check.py [-fix] files...") def check_spelling(name): # Avoid saying Turing machine with a little t so we avoid # triggering when analyzing our own source. if "Turing machine".lower() in read_file(name): print "Bad capitalization for \"Turing machine\" in %r" % (name,) if len(sys.argv) == 0: usage() elif sys.argv[1] == "-fix": # -fix will add the license if it's missing, # and it will update the copyright data if it has gone stale. for name in sys.argv[2:]: check_spelling(name) if not has_license(name): copy_name = name + ".copy" first_line = True added_license = False out = file(copy_name, "w") f = file(name) if has_stale_license(name): print "Found stale copyright in %r" % (name,) found_a_line = False for line in f: if line.startswith("# Copyright (c) ") and \ line.endswith(" Tim Freeman\n"): line = split_license[0] + "\n" found_a_line = True out.write(line) if not found_a_line: print "Found no stale copyright in %r" % (name,) else: # Needs a fresh license. for line in f: if (line.startswith("#!") or "-*-" in line) and first_line: # Keep the #! line unchanged if it's the first line # of the file, so executable scripts can still find # their interpreter. pass else: if not added_license: added_license = True out.write(license) out.write(line) first_line = False out.close() # Preserve the execute bit. mode = stat(name).st_mode chmod(copy_name, mode) rename(copy_name, name) if not has_license(name): print "Failed to add a license to %r" % (name,) print "license is %s" % (license,) print "Text from %r is %s" % (name, read_file(name)) assert has_license(name), "Failed to add a license to %r" % (name,) else: failed = False for name in sys.argv[1:]: check_spelling(name) if not has_license(name): if has_stale_license(name): print "Stale copyright notice found in %r" % (name,) else: print "No license found in %r" % (name,) failed = True if failed: # Get Make to stop so I notice the problem. sys.exit(1)