tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
| 10 | |
| 11 | """Generates license HTML for a prebuilt version of WebRTC for iOS.""" |
| 12 | |
| 13 | import sys |
| 14 | |
| 15 | import argparse |
| 16 | import cgi |
| 17 | import fnmatch |
| 18 | import os |
| 19 | import re |
| 20 | import textwrap |
| 21 | |
| 22 | |
| 23 | LIB_TO_LICENSES_DICT = { |
tkchin | e76db89 | 2016-05-06 11:19:46 -0700 | [diff] [blame] | 24 | 'boringssl': ['third_party/boringssl/src/LICENSE'], |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 25 | 'expat': ['third_party/expat/files/COPYING'], |
| 26 | 'jsoncpp': ['third_party/jsoncpp/LICENSE'], |
| 27 | 'opus': ['third_party/opus/src/COPYING'], |
| 28 | 'protobuf_lite': ['third_party/protobuf/LICENSE'], |
| 29 | 'srtp': ['third_party/libsrtp/srtp/LICENSE'], |
| 30 | 'usrsctplib': ['third_party/usrsctp/LICENSE'], |
| 31 | 'webrtc': ['webrtc/LICENSE', 'webrtc/LICENSE_THIRD_PARTY'], |
| 32 | 'vpx': ['third_party/libvpx/source/libvpx/LICENSE'], |
| 33 | 'yuv': ['third_party/libyuv/LICENSE'], |
| 34 | } |
| 35 | |
| 36 | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 37 | CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir, |
| 38 | os.pardir)) |
| 39 | TALK_ROOT = os.path.join(CHECKOUT_ROOT, 'talk') |
| 40 | WEBRTC_ROOT = os.path.join(CHECKOUT_ROOT, 'webrtc') |
| 41 | |
| 42 | |
| 43 | def GetWebRTCGypFilePaths(): |
| 44 | gyp_filepaths = [] |
| 45 | search_roots = [TALK_ROOT, WEBRTC_ROOT] |
| 46 | for search_root in search_roots: |
| 47 | for root, _, filenames in os.walk(search_root): |
| 48 | for filename in fnmatch.filter(filenames, '*.gyp*'): |
| 49 | gyp_filepaths.append(os.path.join(root, filename)) |
| 50 | return gyp_filepaths |
| 51 | |
| 52 | |
| 53 | def GetWebRTCTargetNames(): |
| 54 | gyp_filepaths = GetWebRTCGypFilePaths() |
| 55 | target_names = [] |
| 56 | for gyp_filepath in gyp_filepaths: |
| 57 | with open(gyp_filepath, 'r') as gyp_file: |
| 58 | for line in gyp_file: |
| 59 | match = re.search(r'\'target_name\'.*\'(\w+)\'', line) |
| 60 | if match: |
| 61 | target_name = match.group(1) |
| 62 | target_names.append(target_name) |
| 63 | return target_names |
| 64 | |
| 65 | |
| 66 | class LicenseBuilder(object): |
| 67 | |
| 68 | def __init__(self): |
| 69 | self.webrtc_target_names = GetWebRTCTargetNames() |
| 70 | |
| 71 | def IsWebRTCLib(self, lib_name): |
| 72 | alternate_lib_name = 'lib' + lib_name |
| 73 | return (lib_name in self.webrtc_target_names or |
| 74 | alternate_lib_name in self.webrtc_target_names) |
| 75 | |
| 76 | def GenerateLicenseText(self, static_lib_dir, output_dir): |
| 77 | # Get a list of libs from the files without their prefix and extension. |
| 78 | static_libs = [] |
| 79 | for static_lib in os.listdir(static_lib_dir): |
| 80 | # Skip non libraries. |
| 81 | if not (static_lib.endswith('.a') and static_lib.startswith('lib')): |
| 82 | continue |
| 83 | # Extract library name. |
| 84 | static_libs.append(static_lib[3:-2]) |
| 85 | |
| 86 | # Generate amalgamated list of libraries. Mostly this just collapses the |
| 87 | # various WebRTC libs names into just 'webrtc'. Will exit with error if a |
| 88 | # lib is unrecognized. |
| 89 | license_libs = set() |
| 90 | for static_lib in static_libs: |
| 91 | license_lib = 'webrtc' if self.IsWebRTCLib(static_lib) else static_lib |
| 92 | license_path = LIB_TO_LICENSES_DICT.get(license_lib) |
| 93 | if license_path is None: |
| 94 | print 'Missing license path for lib: %s' % license_lib |
| 95 | return 1 |
| 96 | license_libs.add(license_lib) |
| 97 | |
| 98 | # Put webrtc at the front of the list. |
| 99 | assert 'webrtc' in license_libs |
| 100 | license_libs.remove('webrtc') |
| 101 | license_libs = sorted(license_libs) |
| 102 | license_libs.insert(0, 'webrtc') |
| 103 | |
| 104 | # Generate HTML. |
| 105 | output_license_file = open(os.path.join(output_dir, 'LICENSE.html'), 'w+') |
| 106 | output_license_file.write('<!DOCTYPE html>\n') |
| 107 | output_license_file.write('<html>\n<head>\n') |
| 108 | output_license_file.write('<meta charset="UTF-8">\n') |
| 109 | output_license_file.write('<title>Licenses</title>\n') |
| 110 | style_tag = textwrap.dedent('''\ |
| 111 | <style> |
| 112 | body { margin: 0; font-family: sans-serif; } |
| 113 | pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; } |
| 114 | p { margin: 1em; white-space: nowrap; } |
| 115 | </style> |
| 116 | ''') |
| 117 | output_license_file.write(style_tag) |
| 118 | output_license_file.write('</head>\n') |
| 119 | |
| 120 | for license_lib in license_libs: |
| 121 | output_license_file.write('<p>%s<br/></p>\n' % license_lib) |
| 122 | output_license_file.write('<pre>\n') |
| 123 | for path in LIB_TO_LICENSES_DICT[license_lib]: |
| 124 | license_path = os.path.join(CHECKOUT_ROOT, path) |
| 125 | with open(license_path, 'r') as license_file: |
| 126 | license_text = cgi.escape(license_file.read(), quote=True) |
| 127 | output_license_file.write(license_text) |
| 128 | output_license_file.write('\n') |
| 129 | output_license_file.write('</pre>\n') |
| 130 | |
| 131 | output_license_file.write('</body>\n') |
| 132 | output_license_file.write('</html>') |
| 133 | output_license_file.close() |
| 134 | return 0 |
| 135 | |
| 136 | |
| 137 | if __name__ == '__main__': |
| 138 | parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.html') |
| 139 | parser.add_argument('static_lib_dir', |
| 140 | help='Directory with built static libraries.') |
| 141 | parser.add_argument('output_dir', |
| 142 | help='Directory to output LICENSE.html to.') |
| 143 | args = parser.parse_args() |
| 144 | builder = LicenseBuilder() |
| 145 | sys.exit(builder.GenerateLicenseText(args.static_lib_dir, args.output_dir)) |