Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Generates the contents of an Cronet LICENSE file for the third-party code. | 6 """Generates the contents of an Cronet LICENSE file for the third-party code. |
| 7 | 7 |
| 8 It makes use of src/tools/licenses.py and the README.chromium files on which | 8 It makes use of src/tools/licenses.py and the README.chromium files on which |
| 9 it depends. Based on android_webview/tools/webview_licenses.py. | 9 it depends. Based on android_webview/tools/webview_licenses.py. |
| 10 """ | 10 """ |
| 11 | 11 |
| 12 import optparse | 12 import optparse |
| 13 import os | 13 import os |
| 14 import shutil | |
| 15 import subprocess | |
| 14 import sys | 16 import sys |
| 17 import tempfile | |
| 15 import textwrap | 18 import textwrap |
| 16 | 19 |
| 17 REPOSITORY_ROOT = os.path.abspath(os.path.join( | 20 REPOSITORY_ROOT = os.path.abspath(os.path.join( |
| 18 os.path.dirname(__file__), '..', '..', '..')) | 21 os.path.dirname(__file__), '..', '..', '..')) |
| 19 | 22 |
| 20 sys.path.append(os.path.join(REPOSITORY_ROOT, 'tools')) | 23 sys.path.append(os.path.join(REPOSITORY_ROOT, 'tools')) |
| 21 import licenses | 24 import licenses |
| 22 | 25 |
| 26 third_party_dirs = [ | |
| 27 'base/third_party/libevent', | |
| 28 'third_party/ashmem', | |
| 29 'third_party/boringssl', | |
| 30 'third_party/modp_b64', | |
| 31 'third_party/zlib', | |
| 32 ] | |
| 33 | |
| 34 | |
| 23 def _ReadFile(path): | 35 def _ReadFile(path): |
| 24 """Reads a file from disk. | 36 """Reads a file from disk. |
| 25 Args: | 37 Args: |
| 26 path: The path of the file to read, relative to the root of the repository. | 38 path: The path of the file to read, relative to the root of the repository. |
| 27 Returns: | 39 Returns: |
| 28 The contents of the file as a string. | 40 The contents of the file as a string. |
| 29 """ | 41 """ |
| 30 return open(os.path.join(REPOSITORY_ROOT, path), 'rb').read() | 42 return open(os.path.join(REPOSITORY_ROOT, path), 'rb').read() |
| 31 | 43 |
| 32 | 44 |
| 33 def GenerateLicense(): | 45 def GenerateLicense(): |
| 34 """Generates the contents of an Cronet LICENSE file for the third-party code. | 46 """Generates the contents of an Cronet LICENSE file for the third-party code. |
| 35 Returns: | 47 Returns: |
| 36 The contents of the LICENSE file. | 48 The contents of the LICENSE file. |
| 37 """ | 49 """ |
| 38 # TODO(mef): Generate list of third_party libraries using checkdeps. | |
| 39 third_party_dirs = [ | |
| 40 'base/third_party/libevent', | |
| 41 'third_party/ashmem', | |
| 42 'third_party/boringssl', | |
| 43 'third_party/modp_b64', | |
| 44 'third_party/zlib', | |
| 45 ] | |
| 46 | |
| 47 # Start with Chromium's LICENSE file | 50 # Start with Chromium's LICENSE file |
| 48 content = [_ReadFile('LICENSE')] | 51 content = [_ReadFile('LICENSE')] |
| 49 | 52 |
| 50 # Add necessary third_party. | 53 # Add necessary third_party. |
| 51 for directory in sorted(third_party_dirs): | 54 for directory in sorted(third_party_dirs): |
| 52 metadata = licenses.ParseDir(directory, REPOSITORY_ROOT, | 55 metadata = licenses.ParseDir(directory, REPOSITORY_ROOT, |
| 53 require_license_file=True) | 56 require_license_file=True) |
| 54 content.append('-' * 20) | 57 content.append('-' * 20) |
| 55 content.append(directory) | 58 content.append(directory.split("/")[-1]) |
| 56 content.append('-' * 20) | 59 content.append('-' * 20) |
| 57 license_file = metadata['License File'] | 60 license_file = metadata['License File'] |
| 58 if license_file and license_file != licenses.NOT_SHIPPED: | 61 if license_file and license_file != licenses.NOT_SHIPPED: |
| 59 content.append(_ReadFile(license_file)) | 62 content.append(_ReadFile(license_file)) |
| 60 | 63 |
| 61 return '\n'.join(content) | 64 return '\n'.join(content) |
| 62 | 65 |
| 63 | 66 |
| 67 def FindThirdPartyDeps(gn_out_dir): | |
| 68 # Generate gn project in temp directory and use it to find dependencies. | |
|
brettw
2016/05/23 20:05:33
Might be good to mention why we do the temp direct
mef
2016/05/24 19:39:17
Done.
| |
| 69 try: | |
| 70 tmp_dir = tempfile.mkdtemp(dir = gn_out_dir) | |
| 71 shutil.copy(gn_out_dir + "/args.gn", tmp_dir) | |
| 72 subprocess.check_output(["gn", "gen", tmp_dir]) | |
| 73 gn_deps = subprocess.check_output(["gn", "desc", tmp_dir, \ | |
| 74 "//net", "deps", "--as=buildfile", "--all"]) | |
| 75 finally: | |
| 76 if os.path.exists(tmp_dir): | |
| 77 shutil.rmtree(tmp_dir) | |
| 78 | |
| 79 third_party_deps = [] | |
| 80 for build_dep in gn_deps.split(): | |
| 81 if ("third_party" in build_dep and build_dep.endswith("/BUILD.gn")): | |
| 82 third_party_deps.append(build_dep.replace("/BUILD.gn", "")) | |
| 83 third_party_deps.sort() | |
| 84 return third_party_deps | |
| 85 | |
| 86 | |
| 64 def main(): | 87 def main(): |
| 65 class FormatterWithNewLines(optparse.IndentedHelpFormatter): | 88 class FormatterWithNewLines(optparse.IndentedHelpFormatter): |
| 66 def format_description(self, description): | 89 def format_description(self, description): |
| 67 paras = description.split('\n') | 90 paras = description.split('\n') |
| 68 formatted_paras = [textwrap.fill(para, self.width) for para in paras] | 91 formatted_paras = [textwrap.fill(para, self.width) for para in paras] |
| 69 return '\n'.join(formatted_paras) + '\n' | 92 return '\n'.join(formatted_paras) + '\n' |
| 70 | 93 |
| 71 parser = optparse.OptionParser(formatter=FormatterWithNewLines(), | 94 parser = optparse.OptionParser(formatter=FormatterWithNewLines(), |
| 72 usage='%prog command [options]') | 95 usage='%prog command [options]') |
| 96 parser.add_option('--gn', help='Use gn deps to find third party dependencies', | |
| 97 action='store_true') | |
| 73 parser.description = (__doc__ + | 98 parser.description = (__doc__ + |
| 74 '\nCommands:\n' \ | 99 '\nCommands:\n' \ |
| 75 ' license [filename]\n' \ | 100 ' license [filename]\n' \ |
| 76 ' Generate Cronet LICENSE to filename or stdout.\n') | 101 ' Generate Cronet LICENSE to filename or stdout.\n') |
| 77 (_, args) = parser.parse_args() | 102 (_, args) = parser.parse_args() |
| 103 | |
| 104 if _.gn: | |
| 105 global third_party_dirs | |
| 106 third_party_dirs = FindThirdPartyDeps(os.getcwd()) | |
| 107 | |
| 78 if not args: | 108 if not args: |
| 79 parser.print_help() | 109 parser.print_help() |
| 80 return 1 | 110 return 1 |
| 81 | 111 |
| 82 if args[0] == 'license': | 112 if args[0] == 'license': |
| 83 if len(args) > 1: | 113 if len(args) > 1: |
| 84 print 'Saving license to %s' % args[1] | 114 print 'Saving license to %s' % args[1] |
| 85 f = open(args[1], "w") | 115 f = open(args[1], "w") |
| 86 try: | 116 try: |
| 87 f.write(GenerateLicense()) | 117 f.write(GenerateLicense()) |
| 88 finally: | 118 finally: |
| 89 f.close() | 119 f.close() |
| 90 else: | 120 else: |
| 91 print GenerateLicense() | 121 print GenerateLicense() |
| 92 return 0 | 122 return 0 |
| 93 | 123 |
| 94 parser.print_help() | 124 parser.print_help() |
| 95 return 1 | 125 return 1 |
| 96 | 126 |
| 97 | 127 |
| 98 if __name__ == '__main__': | 128 if __name__ == '__main__': |
| 99 sys.exit(main()) | 129 sys.exit(main()) |
| OLD | NEW |