• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2# Copyright 2014 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"""Generates the contents of an Cronet LICENSE file for the third-party code.
7
8It makes use of src/tools/licenses.py and the README.chromium files on which
9it depends. Based on android_webview/tools/webview_licenses.py.
10"""
11
12import optparse
13import os
14import sys
15import textwrap
16
17REPOSITORY_ROOT = os.path.abspath(os.path.join(
18    os.path.dirname(__file__), '..', '..', '..'))
19
20sys.path.append(os.path.join(REPOSITORY_ROOT, 'tools'))
21import licenses
22
23def _ReadFile(path):
24  """Reads a file from disk.
25  Args:
26    path: The path of the file to read, relative to the root of the repository.
27  Returns:
28    The contents of the file as a string.
29  """
30  return open(os.path.join(REPOSITORY_ROOT, path), 'rb').read()
31
32
33def GenerateLicense():
34  """Generates the contents of an Cronet LICENSE file for the third-party code.
35  Returns:
36    The contents of the LICENSE file.
37  """
38  # TODO(mef): Generate list of third_party libraries using checkdeps.
39  third_party_dirs = [
40    "libevent",
41    "ashmem",
42    "zlib",
43    "modp_b64",
44    "openssl"
45  ]
46
47  # Start with Chromium's LICENSE file
48  content = [_ReadFile('LICENSE')]
49
50  # Add necessary third_party.
51  for directory in sorted(third_party_dirs):
52    metadata = licenses.ParseDir("third_party/" + directory, REPOSITORY_ROOT,
53                                 require_license_file=True)
54    content.append("-" * 20)
55    content.append(directory)
56    content.append("-" * 20)
57    license_file = metadata['License File']
58    if license_file and license_file != licenses.NOT_SHIPPED:
59      content.append(_ReadFile(license_file))
60
61  return '\n'.join(content)
62
63
64def main():
65  class FormatterWithNewLines(optparse.IndentedHelpFormatter):
66    def format_description(self, description):
67      paras = description.split('\n')
68      formatted_paras = [textwrap.fill(para, self.width) for para in paras]
69      return '\n'.join(formatted_paras) + '\n'
70
71  parser = optparse.OptionParser(formatter=FormatterWithNewLines(),
72                                 usage='%prog command [options]')
73  parser.description = (__doc__ +
74                       '\nCommands:\n' \
75                       '  license [filename]\n' \
76                       '    Generate Cronet LICENSE to filename or stdout.\n')
77  (_, args) = parser.parse_args()
78  if not args:
79    parser.print_help()
80    return 1
81
82  if args[0] == 'license':
83    if len(args) > 1:
84      print 'Saving license to %s' % args[1]
85      f = open(args[1], "w")
86      try:
87        f.write(GenerateLicense())
88      finally:
89        f.close()
90    else:
91      print GenerateLicense()
92    return 0
93
94  parser.print_help()
95  return 1
96
97
98if __name__ == '__main__':
99  sys.exit(main())
100