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.
6 """Utility for checking and processing licensing information in third_party
9 Usage: licenses.py <command>
12 scan scan third_party directories, verifying that we have licensing info
13 credits generate about:credits on stdout
15 (You can also import this as a module.)
22 # Paths from the root of the tree to directories to skip.
24 # Same module occurs in crypto/third_party/nss and net/third_party/nss, so
26 os.path.join('third_party','nss'),
28 # Placeholder directory only, not third-party code.
29 os.path.join('third_party','adobe'),
31 # Build files only, not third-party code.
32 os.path.join('third_party','widevine'),
34 # Only binaries, used during development.
35 os.path.join('third_party','valgrind'),
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'),
57 # Chromium code in third_party.
58 os.path.join('third_party','fuzzymatch'),
59 os.path.join('tools', 'swarming_client'),
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'),
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'),
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
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'),
101 # Fake directory so we can include the strongtalk license.
102 os.path.join('v8', 'strongtalk'),
106 # Directories where we check out directly from upstream, and therefore
107 # can't provide a README.chromium. Please prefer a README.chromium
110 os.path.join('native_client'): {
111 "Name": "native client",
112 "URL": "http://code.google.com/p/nativeclient",
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",
121 os.path.join('testing', 'gmock'): {
123 "URL": "http://code.google.com/p/googlemock",
125 "License File": "NOT_SHIPPED",
127 os.path.join('testing', 'gtest'): {
129 "URL": "http://code.google.com/p/googletest",
131 "License File": "NOT_SHIPPED",
133 os.path.join('third_party', 'angle'): {
134 "Name": "Almost Native Graphics Layer Engine",
135 "URL": "http://code.google.com/p/angleproject/",
138 os.path.join('third_party', 'cros_system_api'): {
139 "Name": "Chromium OS system API",
140 "URL": "http://www.chromium.org/chromium-os",
142 # Absolute path here is resolved as relative to the source root.
143 "License File": "/LICENSE.chromium_os",
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",
151 os.path.join('third_party', 'lss'): {
152 "Name": "linux-syscall-support",
153 "URL": "http://code.google.com/p/linux-syscall-support/",
155 "License File": "/LICENSE",
157 os.path.join('third_party', 'ots'): {
158 "Name": "OTS (OpenType Sanitizer)",
159 "URL": "http://code.google.com/p/ots/",
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",
168 os.path.join('third_party', 'ppapi'): {
170 "URL": "http://code.google.com/p/ppapi/",
172 os.path.join('third_party', 'scons-2.0.1'): {
173 "Name": "scons-2.0.1",
174 "URL": "http://www.scons.org",
176 "License File": "NOT_SHIPPED",
178 os.path.join('third_party', 'trace-viewer'): {
179 "Name": "trace-viewer",
180 "URL": "http://code.google.com/p/trace-viewer",
182 "License File": "NOT_SHIPPED",
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",
189 os.path.join('third_party', '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",
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",
202 os.path.join('tools', 'grit'): {
204 "URL": "http://code.google.com/p/grit-i18n",
206 "License File": "NOT_SHIPPED",
208 os.path.join('tools', 'gyp'): {
210 "URL": "http://code.google.com/p/gyp",
212 "License File": "NOT_SHIPPED",
214 os.path.join('v8'): {
215 "Name": "V8 JavaScript Engine",
216 "URL": "http://code.google.com/p/v8",
219 os.path.join('v8', 'strongtalk'): {
220 "Name": "Strongtalk",
221 "URL": "http://www.strongtalk.org/",
223 # Absolute path here is resolved as relative to the source root.
224 "License File": "/v8/LICENSE.strongtalk",
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"
233 class LicenseError(Exception):
234 """We raise this exception when a directory's licensing info isn't
238 def AbsolutePath(path, filename, root):
239 """Convert a path in README.chromium to be absolute based on the source
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:])
246 absolute_path = os.path.join(root, path, filename)
247 if os.path.exists(absolute_path):
251 def ParseDir(path, root, require_license_file=True):
252 """Examine a third_party/foo component and extract its metadata."""
254 # Parse metadata fields out of README.chromium.
255 # We examine "LICENSE" for the license file by default.
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.
263 # Relative path to a file containing some html we're required to place in
265 optional_keys = ["Required Text", "License Android Compatible"]
267 if path in SPECIAL_CASES:
268 metadata.update(SPECIAL_CASES[path])
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")
276 for line in open(readme_path):
280 for key in metadata.keys() + optional_keys:
282 if line.startswith(field):
283 metadata[key] = line[len(field):]
285 # Check that all expected metadata is present.
286 for key, value in metadata.iteritems():
288 raise LicenseError("couldn't find '" + key + "' line "
289 "in README.chromium or licences.py "
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:
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
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
314 raise LicenseError("Required text file listed but not found.")
319 def ContainsFiles(path, root):
320 """Determines whether any files exist in a directory or in any of its
322 for _, dirs, files in os.walk(os.path.join(root, path)):
325 for vcs_metadata in VCS_METADATA_DIRS:
326 if vcs_metadata in dirs:
327 dirs.remove(vcs_metadata)
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)]
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.
343 if path in prune_paths:
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:
354 if os.path.basename(path) == 'third_party':
355 # Add all subdirectories that are not marked for skipping.
357 dirpath = os.path.join(path, dir)
358 if dirpath not in prune_paths:
359 third_party_dirs.append(dirpath)
361 # Don't recurse into any subdirs from here.
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:
370 for dir in ADDITIONAL_PATHS:
371 if dir not in prune_paths:
372 third_party_dirs.append(dir)
374 return third_party_dirs
377 def ScanThirdPartyDirs(root=None):
378 """Scan a list of directories and report on any problems we find."""
381 third_party_dirs = FindThirdPartyDirs(PRUNE_PATHS, root)
382 third_party_dirs = FilterDirsWithFiles(third_party_dirs, root)
385 for path in sorted(third_party_dirs):
387 metadata = ParseDir(path, root)
388 except LicenseError, e:
389 errors.append((path, e.args[0]))
392 for path, error in sorted(errors):
393 print path + ": " + error
395 return len(errors) == 0
398 def GenerateCredits():
399 """Generate about:credits."""
401 if len(sys.argv) not in (2, 3):
402 print 'usage: licenses.py credits [output_file]'
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)
414 root = os.path.join(os.path.dirname(__file__), '..')
415 third_party_dirs = FindThirdPartyDirs(PRUNE_PATHS, root)
417 entry_template = open(os.path.join(root, 'chrome', 'browser', 'resources',
418 'about_credits_entry.tmpl'), 'rb').read()
420 for path in sorted(third_party_dirs):
422 metadata = ParseDir(path, root)
424 # TODO(phajdan.jr): Convert to fatal error (http://crbug.com/39240).
426 if metadata['License File'] == NOT_SHIPPED:
429 'name': metadata['Name'],
430 'url': metadata['URL'],
431 'license': open(metadata['License File'], 'rb').read(),
432 'license_unescaped': '',
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))
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)},
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
457 if len(sys.argv) > 1:
458 command = sys.argv[1]
460 if command == 'scan':
461 if not ScanThirdPartyDirs():
463 elif command == 'credits':
464 if not GenerateCredits():
471 if __name__ == '__main__':