Chromium Code Reviews| Index: tools_webrtc/libs/generate_licenses.py |
| diff --git a/tools_webrtc/ios/generate_licenses.py b/tools_webrtc/libs/generate_licenses.py |
| similarity index 45% |
| rename from tools_webrtc/ios/generate_licenses.py |
| rename to tools_webrtc/libs/generate_licenses.py |
| index b91314783f30407920966c549107e103f3cba337..731b4c09ee44c4fbd6e42a26cb12aaefe7b2fd3d 100755 |
| --- a/tools_webrtc/ios/generate_licenses.py |
| +++ b/tools_webrtc/libs/generate_licenses.py |
| @@ -1,4 +1,4 @@ |
| -#!/usr/bin/python |
| +#!/usr/bin/env python |
| # Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| # |
| @@ -8,125 +8,140 @@ |
| # in the file PATENTS. All contributing project authors may |
| # be found in the AUTHORS file in the root of the source tree. |
| -"""Generates license HTML for a prebuilt version of WebRTC for iOS.""" |
| +"""Generates license markdown for a prebuilt version of WebRTC.""" |
| import sys |
| import argparse |
| import cgi |
| +import json |
| +import logging |
| import os |
| import re |
| -import textwrap |
| import subprocess |
| LIB_TO_LICENSES_DICT = { |
| + 'android_tools': ['third_party/android_tools/LICENSE'], |
| 'boringssl': ['third_party/boringssl/src/LICENSE'], |
| 'expat': ['third_party/expat/files/COPYING'], |
| + 'ijar': ['third_party/ijar/LICENSE'], |
| 'jsoncpp': ['third_party/jsoncpp/LICENSE'], |
| + 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'], |
| + 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'], |
| + 'libevent': ['base/third_party/libevent/LICENSE'], |
| + 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'], |
| + 'libsrtp': ['third_party/libsrtp/LICENSE'], |
| + 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'], |
| + 'libyuv': ['third_party/libyuv/LICENSE'], |
| + 'openmax_dl': ['third_party/openmax_dl/LICENSE'], |
| 'opus': ['third_party/opus/src/COPYING'], |
| 'protobuf': ['third_party/protobuf/LICENSE'], |
| - 'libsrtp': ['third_party/libsrtp/LICENSE'], |
| 'usrsctp': ['third_party/usrsctp/LICENSE'], |
| 'webrtc': ['webrtc/LICENSE', 'webrtc/LICENSE_THIRD_PARTY'], |
| - 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'], |
| - 'libyuv': ['third_party/libyuv/LICENSE'], |
| + |
| + # Compile time dependencies, no license needed: |
| + 'yasm': [], |
| } |
| SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
| CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
| WEBRTC_ROOT = os.path.join(CHECKOUT_ROOT, 'webrtc') |
| +THIRD_PARTY_LIB_REGEX = r'^.*/third_party/([\w+]+).*$' |
| +def GetThirdPartyLibraries(buildfile_dir, target): |
| + def ParseLibrary(dep): |
| + """ |
| + Returns a regex match containing library name after third_party |
| -def GetThirdPartyLibraries(buildfile_dir, target_name): |
| - def ExtractLibName(string_list): |
| - # Sample input: |
| - # [" //third_party/usrsctp:usrsctp", " //webrtc:webrtc_common"] |
| - # Sample output: |
| - # ["usrsctp"] |
| - return re.sub(r'\(.*\)', '', string_list).strip().split( |
| - os.path.sep)[-1].split(':')[0] |
| - output = subprocess.check_output( |
| - ["gn", "desc", buildfile_dir, target_name, '--all']) .split(os.linesep) |
| - return [ExtractLibName(x) for x in output if re.search(r'third_party', x)] |
| + Input one of: |
| + //a/b/third_party/libname:c |
| + //a/b/third_party/libname:c(d) |
| + //a/b/third_party/libname/c:d(e) |
| + |
| + Outputs match with libname in group 1 or None if this is not a third_party |
| + dependency. |
| + """ |
| + return re.match(THIRD_PARTY_LIB_REGEX, dep) |
| + |
| + cmd = ["gn", "desc", '--all', '--format=json', os.path.abspath(buildfile_dir), |
| + target] |
| + logging.debug("Running: %r", cmd) |
| + output_json = subprocess.check_output(cmd) |
| + logging.debug("Output: %s", output_json) |
| + output = json.loads(output_json) |
| + |
| + libraries = set() |
| + for target in output.values(): |
| + third_party_matches = (ParseLibrary(dep) for dep in target['deps']) |
| + libraries |= set(match.group(1) for match in third_party_matches if match) |
| + return libraries |
| class LicenseBuilder(object): |
| - def __init__(self, buildfile_dirs, target_name): |
| + def __init__(self, buildfile_dirs, targets): |
| self.buildfile_dirs = buildfile_dirs |
| - self.target_name = target_name |
| + self.targets = targets |
| def GenerateLicenseText(self, output_dir): |
| # Get a list of third_party libs from gn. For fat libraries we must consider |
| # all architectures, hence the multiple buildfile directories. |
| - # The `sum` function flattens the 2d list. |
| - third_party_libs = sum([GetThirdPartyLibraries(buildfile, self.target_name) |
| - for buildfile in self.buildfile_dirs], []) |
| + third_party_libs = set() |
| + for buildfile in self.buildfile_dirs: |
| + for target in self.targets: |
| + third_party_libs |= GetThirdPartyLibraries(buildfile, target) |
| assert len(third_party_libs) > 0 |
| - # Generate amalgamated list of libraries. Will exit with error if a |
| - # lib is unrecognized. |
| - license_libs = set() |
| - for static_lib in third_party_libs: |
| - license_path = LIB_TO_LICENSES_DICT.get(static_lib) |
| - if static_lib == 'yasm': |
| - # yasm is a build-time dep only, and doesn't need a license. |
| - continue |
| - if license_path is None: |
| - print 'Missing license path for lib: %s' % static_lib |
| - return 1 |
| - license_libs.add(static_lib) |
| + missing_licenses = third_party_libs - set(LIB_TO_LICENSES_DICT.keys()) |
| + if missing_licenses: |
|
kjellander_webrtc
2017/09/01 08:44:52
What does this mean? Doesn't it mean that it will
sakal
2017/09/01 11:02:30
This exception is thrown if there are deps found i
|
| + error_msg = 'Missing licenses: %s' % ', '.join(missing_licenses) |
| + logging.error(error_msg) |
| + raise Exception(error_msg) |
| # Put webrtc at the front of the list. |
| - license_libs = sorted(license_libs) |
| + license_libs = sorted(third_party_libs) |
| license_libs.insert(0, 'webrtc') |
| - # Generate HTML. |
| - output_license_file = open(os.path.join(output_dir, 'LICENSE.html'), 'w+') |
| - output_license_file.write('<!DOCTYPE html>\n') |
| - output_license_file.write('<html>\n<head>\n') |
| - output_license_file.write('<meta charset="UTF-8">\n') |
| - output_license_file.write('<title>Licenses</title>\n') |
| - style_tag = textwrap.dedent('''\ |
| - <style> |
| - body { margin: 0; font-family: sans-serif; } |
| - pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; } |
| - p { margin: 1em; white-space: nowrap; } |
| - </style> |
| - ''') |
| - output_license_file.write(style_tag) |
| - output_license_file.write('</head>\n') |
| + logging.info("List of licenses: %s", ', '.join(license_libs)) |
| + # Generate markdown. |
| + output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+') |
| for license_lib in license_libs: |
| - output_license_file.write('<p>%s<br/></p>\n' % license_lib) |
| - output_license_file.write('<pre>\n') |
| + if len(LIB_TO_LICENSES_DICT[license_lib]) == 0: |
| + logging.info("Skipping compile time dependency: %s", license_lib) |
| + continue # Compile time dependency |
| + |
| + output_license_file.write('# %s\n' % license_lib) |
| + output_license_file.write('```\n') |
| for path in LIB_TO_LICENSES_DICT[license_lib]: |
| license_path = os.path.join(CHECKOUT_ROOT, path) |
| with open(license_path, 'r') as license_file: |
| license_text = cgi.escape(license_file.read(), quote=True) |
| output_license_file.write(license_text) |
| output_license_file.write('\n') |
| - output_license_file.write('</pre>\n') |
| + output_license_file.write('```\n\n') |
| - output_license_file.write('</body>\n') |
| - output_license_file.write('</html>') |
| output_license_file.close() |
| - return 0 |
| def main(): |
| - parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.html') |
| - parser.add_argument('target_name', |
| + parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md') |
| + parser.add_argument('--verbose', action='store_true', default=False, |
| + help='Debug logging.') |
| + parser.add_argument('--target', required=True, action='append', default=[], |
| help='Name of the GN target to generate a license for') |
| parser.add_argument('output_dir', |
| - help='Directory to output LICENSE.html to.') |
| + help='Directory to output LICENSE.md to.') |
| parser.add_argument('buildfile_dirs', nargs="+", |
| help='Directories containing gn generated ninja files') |
| args = parser.parse_args() |
| - builder = LicenseBuilder(args.buildfile_dirs, args.target_name) |
| - sys.exit(builder.GenerateLicenseText(args.output_dir)) |
| + |
| + logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) |
| + |
| + builder = LicenseBuilder(args.buildfile_dirs, args.target) |
| + builder.GenerateLicenseText(args.output_dir) |
| if __name__ == '__main__': |
| - main() |
| + sys.exit(main()) |