- add sources.
[platform/framework/web/crosswalk.git] / src / tools / licenses.py
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Utility for checking and processing licensing information in third_party
7 directories.
8
9 Usage: licenses.py <command>
10
11 Commands:
12   scan     scan third_party directories, verifying that we have licensing info
13   credits  generate about:credits on stdout
14
15 (You can also import this as a module.)
16 """
17
18 import cgi
19 import os
20 import sys
21
22 # Paths from the root of the tree to directories to skip.
23 PRUNE_PATHS = set([
24     # Same module occurs in crypto/third_party/nss and net/third_party/nss, so
25     # skip this one.
26     os.path.join('third_party','nss'),
27
28     # Placeholder directory only, not third-party code.
29     os.path.join('third_party','adobe'),
30
31     # Build files only, not third-party code.
32     os.path.join('third_party','widevine'),
33
34     # Only binaries, used during development.
35     os.path.join('third_party','valgrind'),
36
37     # Used for development and test, not in the shipping product.
38     os.path.join('third_party','bison'),
39     os.path.join('third_party','cygwin'),
40     os.path.join('third_party','gnu_binutils'),
41     os.path.join('third_party','gold'),
42     os.path.join('third_party','gperf'),
43     os.path.join('third_party','lighttpd'),
44     os.path.join('third_party','llvm'),
45     os.path.join('third_party','llvm-build'),
46     os.path.join('third_party','mingw-w64'),
47     os.path.join('third_party','nacl_sdk_binaries'),
48     os.path.join('third_party','pefile'),
49     os.path.join('third_party','perl'),
50     os.path.join('third_party','psyco_win32'),
51     os.path.join('third_party','pylib'),
52     os.path.join('third_party','python_26'),
53     os.path.join('third_party','pywebsocket'),
54     os.path.join('third_party','syzygy'),
55     os.path.join('tools','gn'),
56
57     # Chromium code in third_party.
58     os.path.join('third_party','fuzzymatch'),
59     os.path.join('tools', 'swarming_client'),
60
61     # Stuff pulled in from chrome-internal for official builds/tools.
62     os.path.join('third_party', 'clear_cache'),
63     os.path.join('third_party', 'gnu'),
64     os.path.join('third_party', 'googlemac'),
65     os.path.join('third_party', 'pcre'),
66     os.path.join('third_party', 'psutils'),
67     os.path.join('third_party', 'sawbuck'),
68
69     # Redistribution does not require attribution in documentation.
70     os.path.join('third_party','directxsdk'),
71     os.path.join('third_party','platformsdk_win2008_6_1'),
72     os.path.join('third_party','platformsdk_win7'),
73 ])
74
75 # Directories we don't scan through.
76 VCS_METADATA_DIRS = ('.svn', '.git')
77 PRUNE_DIRS = (VCS_METADATA_DIRS +
78               ('out', 'Debug', 'Release',  # build files
79                'layout_tests'))            # lots of subdirs
80
81 ADDITIONAL_PATHS = (
82     os.path.join('breakpad'),
83     os.path.join('chrome', 'common', 'extensions', 'docs', 'examples'),
84     os.path.join('chrome', 'test', 'chromeos', 'autotest'),
85     os.path.join('chrome', 'test', 'data'),
86     os.path.join('native_client'),
87     os.path.join('native_client_sdk'),
88     os.path.join('net', 'tools', 'spdyshark'),
89     os.path.join('ppapi'),
90     os.path.join('sdch', 'open-vcdiff'),
91     os.path.join('testing', 'gmock'),
92     os.path.join('testing', 'gtest'),
93     # The directory with the word list for Chinese and Japanese segmentation
94     # with different license terms than ICU.
95     os.path.join('third_party','icu','source','data','brkitr'),
96     os.path.join('tools', 'grit'),
97     os.path.join('tools', 'gyp'),
98     os.path.join('tools', 'page_cycler', 'acid3'),
99     os.path.join('url', 'third_party', 'mozilla'),
100     os.path.join('v8'),
101     # Fake directory so we can include the strongtalk license.
102     os.path.join('v8', 'strongtalk'),
103 )
104
105
106 # Directories where we check out directly from upstream, and therefore
107 # can't provide a README.chromium.  Please prefer a README.chromium
108 # wherever possible.
109 SPECIAL_CASES = {
110     os.path.join('native_client'): {
111         "Name": "native client",
112         "URL": "http://code.google.com/p/nativeclient",
113         "License": "BSD",
114     },
115     os.path.join('sdch', 'open-vcdiff'): {
116         "Name": "open-vcdiff",
117         "URL": "http://code.google.com/p/open-vcdiff",
118         "License": "Apache 2.0, MIT, GPL v2 and custom licenses",
119         "License Android Compatible": "yes",
120     },
121     os.path.join('testing', 'gmock'): {
122         "Name": "gmock",
123         "URL": "http://code.google.com/p/googlemock",
124         "License": "BSD",
125         "License File": "NOT_SHIPPED",
126     },
127     os.path.join('testing', 'gtest'): {
128         "Name": "gtest",
129         "URL": "http://code.google.com/p/googletest",
130         "License": "BSD",
131         "License File": "NOT_SHIPPED",
132     },
133     os.path.join('third_party', 'angle'): {
134         "Name": "Almost Native Graphics Layer Engine",
135         "URL": "http://code.google.com/p/angleproject/",
136         "License": "BSD",
137     },
138     os.path.join('third_party', 'cros_system_api'): {
139         "Name": "Chromium OS system API",
140         "URL": "http://www.chromium.org/chromium-os",
141         "License": "BSD",
142         # Absolute path here is resolved as relative to the source root.
143         "License File": "/LICENSE.chromium_os",
144     },
145     os.path.join('third_party', 'GTM'): {
146         "Name": "Google Toolbox for Mac",
147         "URL": "http://code.google.com/p/google-toolbox-for-mac/",
148         "License": "Apache 2.0",
149         "License File": "COPYING",
150     },
151     os.path.join('third_party', 'lss'): {
152         "Name": "linux-syscall-support",
153         "URL": "http://code.google.com/p/linux-syscall-support/",
154         "License": "BSD",
155         "License File": "/LICENSE",
156     },
157     os.path.join('third_party', 'ots'): {
158         "Name": "OTS (OpenType Sanitizer)",
159         "URL": "http://code.google.com/p/ots/",
160         "License": "BSD",
161     },
162     os.path.join('third_party', 'pdfsqueeze'): {
163         "Name": "pdfsqueeze",
164         "URL": "http://code.google.com/p/pdfsqueeze/",
165         "License": "Apache 2.0",
166         "License File": "COPYING",
167     },
168     os.path.join('third_party', 'ppapi'): {
169         "Name": "ppapi",
170         "URL": "http://code.google.com/p/ppapi/",
171     },
172     os.path.join('third_party', 'scons-2.0.1'): {
173         "Name": "scons-2.0.1",
174         "URL": "http://www.scons.org",
175         "License": "MIT",
176         "License File": "NOT_SHIPPED",
177     },
178     os.path.join('third_party', 'trace-viewer'): {
179         "Name": "trace-viewer",
180         "URL": "http://code.google.com/p/trace-viewer",
181         "License": "BSD",
182         "License File": "NOT_SHIPPED",
183     },
184     os.path.join('third_party', 'v8-i18n'): {
185         "Name": "Internationalization Library for v8",
186         "URL": "http://code.google.com/p/v8-i18n/",
187         "License": "Apache 2.0",
188     },
189     os.path.join('third_party', 'WebKit'): {
190         "Name": "WebKit",
191         "URL": "http://webkit.org/",
192         "License": "BSD and GPL v2",
193         # Absolute path here is resolved as relative to the source root.
194         "License File": "/webkit/LICENSE",
195     },
196     os.path.join('third_party', 'webpagereplay'): {
197         "Name": "webpagereplay",
198         "URL": "http://code.google.com/p/web-page-replay",
199         "License": "Apache 2.0",
200         "License File": "NOT_SHIPPED",
201     },
202     os.path.join('tools', 'grit'): {
203         "Name": "grit",
204         "URL": "http://code.google.com/p/grit-i18n",
205         "License": "BSD",
206         "License File": "NOT_SHIPPED",
207     },
208     os.path.join('tools', 'gyp'): {
209         "Name": "gyp",
210         "URL": "http://code.google.com/p/gyp",
211         "License": "BSD",
212         "License File": "NOT_SHIPPED",
213     },
214     os.path.join('v8'): {
215         "Name": "V8 JavaScript Engine",
216         "URL": "http://code.google.com/p/v8",
217         "License": "BSD",
218     },
219     os.path.join('v8', 'strongtalk'): {
220         "Name": "Strongtalk",
221         "URL": "http://www.strongtalk.org/",
222         "License": "BSD",
223         # Absolute path here is resolved as relative to the source root.
224         "License File": "/v8/LICENSE.strongtalk",
225     },
226 }
227
228 # Special value for 'License File' field used to indicate that the license file
229 # should not be used in about:credits.
230 NOT_SHIPPED = "NOT_SHIPPED"
231
232
233 class LicenseError(Exception):
234     """We raise this exception when a directory's licensing info isn't
235     fully filled out."""
236     pass
237
238 def AbsolutePath(path, filename, root):
239     """Convert a path in README.chromium to be absolute based on the source
240     root."""
241     if filename.startswith('/'):
242         # Absolute-looking paths are relative to the source root
243         # (which is the directory we're run from).
244         absolute_path = os.path.join(root, filename[1:])
245     else:
246         absolute_path = os.path.join(root, path, filename)
247     if os.path.exists(absolute_path):
248         return absolute_path
249     return None
250
251 def ParseDir(path, root, require_license_file=True):
252     """Examine a third_party/foo component and extract its metadata."""
253
254     # Parse metadata fields out of README.chromium.
255     # We examine "LICENSE" for the license file by default.
256     metadata = {
257         "License File": "LICENSE",  # Relative path to license text.
258         "Name": None,               # Short name (for header on about:credits).
259         "URL": None,                # Project home page.
260         "License": None,            # Software license.
261         }
262
263     # Relative path to a file containing some html we're required to place in
264     # about:credits.
265     optional_keys = ["Required Text", "License Android Compatible"]
266
267     if path in SPECIAL_CASES:
268         metadata.update(SPECIAL_CASES[path])
269     else:
270         # Try to find README.chromium.
271         readme_path = os.path.join(root, path, 'README.chromium')
272         if not os.path.exists(readme_path):
273             raise LicenseError("missing README.chromium or licenses.py "
274                                "SPECIAL_CASES entry")
275
276         for line in open(readme_path):
277             line = line.strip()
278             if not line:
279                 break
280             for key in metadata.keys() + optional_keys:
281                 field = key + ": "
282                 if line.startswith(field):
283                     metadata[key] = line[len(field):]
284
285     # Check that all expected metadata is present.
286     for key, value in metadata.iteritems():
287         if not value:
288             raise LicenseError("couldn't find '" + key + "' line "
289                                "in README.chromium or licences.py "
290                                "SPECIAL_CASES")
291
292     # Special-case modules that aren't in the shipping product, so don't need
293     # their license in about:credits.
294     if metadata["License File"] != NOT_SHIPPED:
295         # Check that the license file exists.
296         for filename in (metadata["License File"], "COPYING"):
297             license_path = AbsolutePath(path, filename, root)
298             if license_path is not None:
299                 break
300
301         if require_license_file and not license_path:
302             raise LicenseError("License file not found. "
303                                "Either add a file named LICENSE, "
304                                "import upstream's COPYING if available, "
305                                "or add a 'License File:' line to "
306                                "README.chromium with the appropriate path.")
307         metadata["License File"] = license_path
308
309     if "Required Text" in metadata:
310         required_path = AbsolutePath(path, metadata["Required Text"], root)
311         if required_path is not None:
312             metadata["Required Text"] = required_path
313         else:
314             raise LicenseError("Required text file listed but not found.")
315
316     return metadata
317
318
319 def ContainsFiles(path, root):
320     """Determines whether any files exist in a directory or in any of its
321     subdirectories."""
322     for _, dirs, files in os.walk(os.path.join(root, path)):
323         if files:
324             return True
325         for vcs_metadata in VCS_METADATA_DIRS:
326             if vcs_metadata in dirs:
327                 dirs.remove(vcs_metadata)
328     return False
329
330
331 def FilterDirsWithFiles(dirs_list, root):
332     # If a directory contains no files, assume it's a DEPS directory for a
333     # project not used by our current configuration and skip it.
334     return [x for x in dirs_list if ContainsFiles(x, root)]
335
336
337 def FindThirdPartyDirs(prune_paths, root):
338     """Find all third_party directories underneath the source root."""
339     third_party_dirs = []
340     for path, dirs, files in os.walk(root):
341         path = path[len(root)+1:]  # Pretty up the path.
342
343         if path in prune_paths:
344             dirs[:] = []
345             continue
346
347         # Prune out directories we want to skip.
348         # (Note that we loop over PRUNE_DIRS so we're not iterating over a
349         # list that we're simultaneously mutating.)
350         for skip in PRUNE_DIRS:
351             if skip in dirs:
352                 dirs.remove(skip)
353
354         if os.path.basename(path) == 'third_party':
355             # Add all subdirectories that are not marked for skipping.
356             for dir in dirs:
357                 dirpath = os.path.join(path, dir)
358                 if dirpath not in prune_paths:
359                     third_party_dirs.append(dirpath)
360
361             # Don't recurse into any subdirs from here.
362             dirs[:] = []
363             continue
364
365         # Don't recurse into paths in ADDITIONAL_PATHS, like we do with regular
366         # third_party/foo paths.
367         if path in ADDITIONAL_PATHS:
368             dirs[:] = []
369
370     for dir in ADDITIONAL_PATHS:
371         if dir not in prune_paths:
372             third_party_dirs.append(dir)
373
374     return third_party_dirs
375
376
377 def ScanThirdPartyDirs(root=None):
378     """Scan a list of directories and report on any problems we find."""
379     if root is None:
380       root = os.getcwd()
381     third_party_dirs = FindThirdPartyDirs(PRUNE_PATHS, root)
382     third_party_dirs = FilterDirsWithFiles(third_party_dirs, root)
383
384     errors = []
385     for path in sorted(third_party_dirs):
386         try:
387             metadata = ParseDir(path, root)
388         except LicenseError, e:
389             errors.append((path, e.args[0]))
390             continue
391
392     for path, error in sorted(errors):
393         print path + ": " + error
394
395     return len(errors) == 0
396
397
398 def GenerateCredits():
399     """Generate about:credits."""
400
401     if len(sys.argv) not in (2, 3):
402       print 'usage: licenses.py credits [output_file]'
403       return False
404
405     def EvaluateTemplate(template, env, escape=True):
406         """Expand a template with variables like {{foo}} using a
407         dictionary of expansions."""
408         for key, val in env.items():
409             if escape and not key.endswith("_unescaped"):
410                 val = cgi.escape(val)
411             template = template.replace('{{%s}}' % key, val)
412         return template
413
414     root = os.path.join(os.path.dirname(__file__), '..')
415     third_party_dirs = FindThirdPartyDirs(PRUNE_PATHS, root)
416
417     entry_template = open(os.path.join(root, 'chrome', 'browser', 'resources',
418                                        'about_credits_entry.tmpl'), 'rb').read()
419     entries = []
420     for path in sorted(third_party_dirs):
421         try:
422             metadata = ParseDir(path, root)
423         except LicenseError:
424             # TODO(phajdan.jr): Convert to fatal error (http://crbug.com/39240).
425             continue
426         if metadata['License File'] == NOT_SHIPPED:
427             continue
428         env = {
429             'name': metadata['Name'],
430             'url': metadata['URL'],
431             'license': open(metadata['License File'], 'rb').read(),
432             'license_unescaped': '',
433         }
434         if 'Required Text' in metadata:
435             required_text = open(metadata['Required Text'], 'rb').read()
436             env["license_unescaped"] = required_text
437         entries.append(EvaluateTemplate(entry_template, env))
438
439     file_template = open(os.path.join(root, 'chrome', 'browser', 'resources',
440                                       'about_credits.tmpl'), 'rb').read()
441     template_contents = "<!-- Generated by licenses.py; do not edit. -->"
442     template_contents += EvaluateTemplate(file_template,
443                                           {'entries': '\n'.join(entries)},
444                                           escape=False)
445
446     if len(sys.argv) == 3:
447       with open(sys.argv[2], 'w') as output_file:
448         output_file.write(template_contents)
449     elif len(sys.argv) == 2:
450       print template_contents
451
452     return True
453
454
455 def main():
456     command = 'help'
457     if len(sys.argv) > 1:
458         command = sys.argv[1]
459
460     if command == 'scan':
461         if not ScanThirdPartyDirs():
462             return 1
463     elif command == 'credits':
464         if not GenerateCredits():
465             return 1
466     else:
467         print __doc__
468         return 1
469
470
471 if __name__ == '__main__':
472   sys.exit(main())