Fix compiler warning due to missing function prototype.
[svn.git] / tools / dev / check-license.py
blob0fb9c024edd9dd56033a41796e0fddc2e726c1d5
1 #!/usr/bin/env python
3 # check if a file has the proper license in it
5 # USAGE: check-license.py [-C] file1 file2 ... fileN
7 # A 'file' may in fact be a directory, in which case it is recursively
8 # searched.
10 # If the license cannot be found, then the filename is printed to stdout.
11 # Typical usage:
12 # $ check-license.py . > bad-files
14 # -C switch is used to change licenses.
15 # Typical usage:
16 # $ check-license.py -C file1 file2 ... fileN
19 import sys, os, re
21 OLD_LICENSE = '''\
22 \* ====================================================================
23 \* Copyright \(c\) (200[0-9]|200[0-9]-200[0-9]) CollabNet. All rights reserved.
25 \* This software is licensed as described in the file COPYING, which
26 \* you should have received as part of this distribution\. The terms
27 \* are also available at http://subversion.tigris.org/license-1\.html\.
28 \* If newer versions of this license are posted there, you may use a
29 \* newer version instead, at your option\.
31 \* This software consists of voluntary contributions made by many
32 \* individuals\. For exact contribution history, see the revision
33 \* history and logs, available at http://subversion.tigris.org/\.
34 \* ====================================================================
35 '''
37 SH_OLD_LICENSE = re.subn(r'(?m)^ \\\*', '#', OLD_LICENSE)[0]
39 # Remember not to do regexp quoting for NEW_LICENSE. Only OLD_LICENSE
40 # is used for matching; NEW_LICENSE is inserted as-is.
41 NEW_LICENSE = '''\
42 * ====================================================================
43 * Copyright (c) 2000-2005 CollabNet. All rights reserved.
45 * This software is licensed as described in the file COPYING, which
46 * you should have received as part of this distribution. The terms
47 * are also available at http://subversion.tigris.org/license-1.html.
48 * If newer versions of this license are posted there, you may use a
49 * newer version instead, at your option.
51 * This software consists of voluntary contributions made by many
52 * individuals. For exact contribution history, see the revision
53 * history and logs, available at http://subversion.tigris.org/.
54 * ====================================================================
55 '''
57 SH_NEW_LICENSE = re.subn(r'(?m)^ \*', '#', NEW_LICENSE)[0]
59 re_OLD = re.compile(OLD_LICENSE)
60 re_SH_OLD = re.compile(SH_OLD_LICENSE)
61 re_EXCLUDE = re.compile(
62 r'automatically generated by SWIG'
63 + r'|Generated from configure\.in'
64 + r'|placed into the public domain'
67 c_comment_suffices = ('.c', '.java', '.h', '.cpp', '.hw', '.pas')
69 # Yes, this is an empty tuple. No types that fit in this category uniformly
70 # have a copyright block.
71 # Possible types to add here:
72 # ('.bat', '.py', '.pl', '.in')
73 sh_comment_suffices = ()
75 def check_file(fname, old_re, new_lic):
76 s = open(fname).read()
77 if (not old_re.search(s)
78 and not re_EXCLUDE.search(s)):
79 print fname
81 def change_license(fname, old_re, new_lic):
82 s = open(fname).read()
83 m = old_re.search(s)
84 if not m:
85 print 'ERROR: missing old license:', fname
86 else:
87 s = s[:m.start()] + new_lic + s[m.end():]
88 open(fname, 'w').write(s)
89 print 'Changed:', fname
91 def visit(baton, dirname, dircontents):
92 file_func = baton
93 for i in dircontents:
94 # Don't recurse into certain directories
95 if i in ('.svn', '.libs'):
96 dircontents.remove(i)
97 continue
99 extension = os.path.splitext(i)[1]
100 fullname = os.path.join(dirname, i)
102 if os.path.isdir(fullname):
103 continue
105 if extension in c_comment_suffices:
106 file_func(fullname, re_OLD, NEW_LICENSE)
107 elif extension in sh_comment_suffices:
108 file_func(fullname, re_SH_OLD, SH_NEW_LICENSE)
110 def main():
111 file_func = check_file
112 if sys.argv[1] == '-C':
113 print 'Changing license text...'
114 del sys.argv[1]
115 file_func = change_license
117 for f in sys.argv[1:]:
118 if os.path.isdir(f):
119 baton = file_func
120 os.path.walk(f, visit, baton)
121 else:
122 baton = file_func
123 dir, i = os.path.split(f)
124 visit(baton, dir, i)
126 if __name__ == '__main__':
127 main()