Add environment checks. Add a topic to our refspec.
[metze/wireshark/wip.git] / tools / checklicenses.py
1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #    * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #    * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #    * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """Makes sure that all files contain proper licensing information."""
31
32
33 import optparse
34 import os.path
35 import subprocess
36 import sys
37
38
39 def PrintUsage():
40   print """Usage: python checklicenses.py [--root <root>] [tocheck]
41   --root   Specifies the repository root. This defaults to ".." relative
42            to the script file. This will be correct given the normal location
43            of the script in "<root>/tools".
44
45   --ignore-suppressions  Ignores path-specific license whitelist. Useful when
46                          trying to remove a suppression/whitelist entry.
47
48   tocheck  Specifies the directory, relative to root, to check. This defaults
49            to "." so it checks everything.
50
51 Examples:
52   python checklicenses.py
53   python checklicenses.py --root ~/chromium/src third_party"""
54
55
56 WHITELISTED_LICENSES = [
57     'Apache (v2.0)',
58     'Apache (v2.0) BSD (2 clause)',
59     'Apache (v2.0) GPL (v2)',
60     'Apple MIT',  # https://fedoraproject.org/wiki/Licensing/Apple_MIT_License
61     'APSL (v2)',
62     'APSL (v2) BSD (4 clause)',
63     'BSD',
64     'BSD (2 clause)',
65     'BSD (2 clause) GPL (v2 or later)',
66     'BSD (2 clause) MIT/X11 (BSD like)',
67     'BSD (3 clause)',
68     'BSD (3 clause) ISC',
69     'BSD (3 clause) LGPL (v2 or later)',
70     'BSD (3 clause) LGPL (v2.1 or later)',
71     'BSD (3 clause) MIT/X11 (BSD like)',
72     'BSD (4 clause)',
73     'BSD-like',
74
75     # TODO(phajdan.jr): Make licensecheck not print BSD-like twice.
76     'BSD-like MIT/X11 (BSD like)',
77
78     'BSL (v1.0)',
79     'GPL (v2 or later)',
80     'GPL (v2 or later) with Bison parser exception',
81     'GPL (v2 or later) with libtool exception',
82     'GPL (v3 or later) with Bison parser exception',
83     'GPL with Bison parser exception',
84     'ISC',
85     'ISC GPL (v2)',
86     'ISC GPL (v2 or later)',
87     'LGPL',
88     'LGPL (v2)',
89     'LGPL (v2 or later)',
90     'LGPL (v2.1)',
91     'LGPL (v3 or later)',
92
93     # TODO(phajdan.jr): Make licensecheck convert that comma to a dot.
94     'LGPL (v2,1 or later)',
95
96     'LGPL (v2.1 or later)',
97     'MPL (v1.0) LGPL (v2 or later)',
98     'MPL (v1.1)',
99     'MPL (v1.1) BSD-like',
100     'MPL (v1.1) BSD-like GPL (unversioned/unknown version)',
101     'MPL (v1.1,) BSD (3 clause) GPL (unversioned/unknown version) '
102         'LGPL (v2.1 or later)',
103     'MPL (v1.1) GPL (unversioned/unknown version)',
104     'MPL (v2.0)',
105
106     # TODO(phajdan.jr): Make licensecheck not print the comma after 1.1.
107     'MPL (v1.1,) GPL (unversioned/unknown version) LGPL (v2 or later)',
108     'MPL (v1.1,) GPL (unversioned/unknown version) LGPL (v2.1 or later)',
109
110     'MIT/X11 (BSD like)',
111     'Ms-PL',
112     'Public domain',
113     'Public domain BSD',
114     'Public domain BSD (3 clause)',
115     'Public domain BSD-like',
116     'Public domain GPL (v2 or later)',
117     'Public domain LGPL (v2.1 or later)',
118     'libpng',
119     'zlib/libpng',
120     'zlib/libpng GPL (v2 or later)',
121     'SGI Free Software License B',
122     'University of Illinois/NCSA Open Source License (BSD like)',
123 ]
124
125
126 PATH_SPECIFIC_WHITELISTED_LICENSES = {
127     'dtds': [
128         'UNKNOWN',
129     ],
130     'fix': [
131         'UNKNOWN',
132     ],
133     'wsutil/g711.c': [
134         'UNKNOWN',
135     ],
136     'wiretap/ascend.c': [
137         'GPL (v3 or later)',
138     ],
139     'wiretap/ascend.h': [
140         'GPL (v3 or later)',
141     ],
142     'packaging/macosx': [
143         'UNKNOWN',
144     ],
145     'svnversion.h': [
146         'UNKNOWN',
147     ],
148     'tools/lemon': [
149         'UNKNOWN',
150     ],
151     'epan/except.c': [
152         'UNKNOWN',
153     ],
154     'epan/except.h': [
155         'UNKNOWN',
156     ],
157     'plugins/mate/mate_grammar.c': [
158         'GPL (v2 or later) LGPL (v2 or later)', # licensecheck bug?
159     ],
160     'plugins/mate/mate_grammar.h': [
161         'UNKNOWN',
162     ],
163     'cmake/TestFileOffsetBits.c': [
164         'UNKNOWN',
165     ],
166     'cmake/TestWindowsFSeek.c': [
167         'UNKNOWN',
168     ],
169     'epan/dissectors/pidl/idl_types.h': [
170         # Special IDL license that appears to be compatible as far as I (not a
171         # lawyer) can tell. See
172         # https://www.wireshark.org/lists/wireshark-dev/201310/msg00234.html
173         'UNKNOWN',
174     ],
175     # The following tools are under incompatible licenses (mostly GPLv3 or
176     # GPLv3+), but this is OK since they are not actually linked into Wireshark
177     'tools/pidl': [
178         'UNKNOWN',
179     ],
180     'tools/html2text.py': [
181         'UNKNOWN',
182     ],
183 }
184
185
186 def check_licenses(options, args):
187   # Figure out which directory we have to check.
188   if len(args) == 0:
189     # No directory to check specified, use the repository root.
190     start_dir = options.base_directory
191   elif len(args) == 1:
192     # Directory specified. Start here. It's supposed to be relative to the
193     # base directory.
194     start_dir = os.path.abspath(os.path.join(options.base_directory, args[0]))
195   else:
196     # More than one argument, we don't handle this.
197     PrintUsage()
198     return 1
199
200   print "Using base directory:", options.base_directory
201   print "Checking:", start_dir
202   print
203
204   #licensecheck_path = os.path.abspath(os.path.join(options.base_directory,
205   #                                                 'third_party',
206   #                                                 'devscripts',
207   #                                                 'licensecheck.pl'))
208   licensecheck_path = 'licensecheck'
209
210   licensecheck = subprocess.Popen([licensecheck_path,
211                                    '-l', '100',
212                                    '-r', start_dir],
213                                   stdout=subprocess.PIPE,
214                                   stderr=subprocess.PIPE)
215   stdout, stderr = licensecheck.communicate()
216   if options.verbose:
217     print '----------- licensecheck stdout -----------'
218     print stdout
219     print '--------- end licensecheck stdout ---------'
220   if licensecheck.returncode != 0 or stderr:
221     print '----------- licensecheck stderr -----------'
222     print stderr
223     print '--------- end licensecheck stderr ---------'
224     print "\nFAILED\n"
225     return 1
226
227   success = True
228   for line in stdout.splitlines():
229     filename, license = line.split(':', 1)
230     filename = os.path.relpath(filename.strip(), options.base_directory)
231
232     # All files in the build output directory are generated one way or another.
233     # There's no need to check them.
234     if filename.startswith('out/') or filename.startswith('sconsbuild/'):
235       continue
236
237     # For now we're just interested in the license.
238     license = license.replace('*No copyright*', '').strip()
239
240     # Skip generated files.
241     if 'GENERATED FILE' in license:
242       continue
243
244     if license in WHITELISTED_LICENSES:
245       continue
246
247     if not options.ignore_suppressions:
248       found_path_specific = False
249       for prefix in PATH_SPECIFIC_WHITELISTED_LICENSES:
250         if (filename.startswith(prefix) and
251             license in PATH_SPECIFIC_WHITELISTED_LICENSES[prefix]):
252           found_path_specific = True
253           break
254       if found_path_specific:
255         continue
256
257     print "'%s' has non-whitelisted license '%s'" % (filename, license)
258     success = False
259
260   if success:
261     print "\nSUCCESS\n"
262     return 0
263   else:
264     print "\nFAILED\n"
265     print "Please read",
266     print "http://www.chromium.org/developers/adding-3rd-party-libraries"
267     print "for more info how to handle the failure."
268     print
269     print "Please respect OWNERS of checklicenses.py. Changes violating"
270     print "this requirement may be reverted."
271     return 1
272
273
274 def main():
275   default_root = os.path.abspath(
276       os.path.join(os.path.dirname(__file__), '..'))
277   option_parser = optparse.OptionParser()
278   option_parser.add_option('--root', default=default_root,
279                            dest='base_directory',
280                            help='Specifies the repository root. This defaults '
281                            'to "../.." relative to the script file, which '
282                            'will normally be the repository root.')
283   option_parser.add_option('-v', '--verbose', action='store_true',
284                            default=False, help='Print debug logging')
285   option_parser.add_option('--ignore-suppressions',
286                            action='store_true',
287                            default=False,
288                            help='Ignore path-specific license whitelist.')
289   options, args = option_parser.parse_args()
290   return check_licenses(options, args)
291
292
293 if '__main__' == __name__:
294   sys.exit(main())