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
10 # If the license cannot be found, then the filename is printed to stdout.
12 # $ check-license.py . > bad-files
14 # -C switch is used to change licenses.
16 # $ check-license.py -C file1 file2 ... fileN
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 \* ====================================================================
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.
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 * ====================================================================
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
)):
81 def change_license(fname
, old_re
, new_lic
):
82 s
= open(fname
).read()
85 print 'ERROR: missing old license:', fname
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
):
94 # Don't recurse into certain directories
95 if i
in ('.svn', '.libs'):
99 extension
= os
.path
.splitext(i
)[1]
100 fullname
= os
.path
.join(dirname
, i
)
102 if os
.path
.isdir(fullname
):
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
)
111 file_func
= check_file
112 if sys
.argv
[1] == '-C':
113 print 'Changing license text...'
115 file_func
= change_license
117 for f
in sys
.argv
[1:]:
120 os
.path
.walk(f
, visit
, baton
)
123 dir, i
= os
.path
.split(f
)
126 if __name__
== '__main__':