OLD | NEW |
---|---|
(Empty) | |
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 = { | |
24 'boringssl': ['third_party/boringssl/NOTICE'], | |
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/COPYING.txt'], | |
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])) | |
kjellander_webrtc
2016/04/28 07:38:24
I prefer __file__ instead of sys.argv[0] but I'm O
| |
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 webrtc_target_names = None | |
kjellander_webrtc
2016/04/28 07:38:24
I don't like the global variable here but I'll lea
| |
43 | |
44 | |
45 def GetWebRTCGypFilePaths(): | |
46 gyp_filepaths = [] | |
47 search_roots = [TALK_ROOT, WEBRTC_ROOT] | |
48 for search_root in search_roots: | |
49 for root, _, filenames in os.walk(search_root): | |
50 for filename in fnmatch.filter(filenames, '*.gyp*'): | |
51 gyp_filepaths.append(os.path.join(root, filename)) | |
52 return gyp_filepaths | |
53 | |
54 | |
55 def GetWebRTCTargetNames(): | |
56 gyp_filepaths = GetWebRTCGypFilePaths() | |
57 target_names = [] | |
58 for gyp_filepath in gyp_filepaths: | |
59 with open(gyp_filepath, 'r') as gyp_file: | |
60 for line in gyp_file: | |
61 match = re.search(r'\'target_name\'.*\'(\w+)\'', line) | |
62 if match: | |
63 target_name = match.group(1) | |
64 target_names.append(target_name) | |
65 return target_names | |
66 | |
67 | |
68 def IsWebRTCLib(lib_name): | |
69 alternate_lib_name = 'lib' + lib_name | |
70 return (lib_name in webrtc_target_names or | |
71 alternate_lib_name in webrtc_target_names) | |
72 | |
73 | |
74 def GetWebRTCPath(): | |
75 # WebRTC path is script path down three directories. | |
76 path = os.path.dirname(os.path.realpath(sys.argv[0])) | |
77 i = 3 | |
78 while i > 0: | |
79 path = os.path.dirname(path) | |
80 i -= 1 | |
81 return path | |
82 | |
83 | |
84 def GenerateLicenseText(static_lib_dir, output_dir): | |
85 # Get a list of libs from the files without their prefix and extension. | |
86 static_libs = [] | |
87 for static_lib in os.listdir(static_lib_dir): | |
88 # Skip non libraries. | |
89 if not (static_lib.endswith('.a') and static_lib.startswith('lib')): | |
90 continue | |
91 # Extract library name. | |
92 static_libs.append(static_lib[3:-2]) | |
93 | |
94 # Generate amalgamated list of libraries. Mostly this just collapses the | |
95 # various WebRTC libs names into just 'webrtc'. Will exit with error if a | |
96 # lib is unrecognized. | |
97 license_libs = set() | |
98 for static_lib in static_libs: | |
99 license_lib = 'webrtc' if IsWebRTCLib(static_lib) else static_lib | |
100 license_path = LIB_TO_LICENSES_DICT.get(license_lib) | |
101 if license_path is None: | |
102 print 'Missing license path for lib: %s' % license_lib | |
103 sys.exit(1) | |
104 license_libs.add(license_lib) | |
105 | |
106 # Put webrtc at the front of the list. | |
107 assert 'webrtc' in license_libs | |
108 license_libs.remove('webrtc') | |
109 license_libs = sorted(license_libs) | |
110 license_libs.insert(0, 'webrtc') | |
111 | |
112 # Generate HTML. | |
113 output_license_file = open(os.path.join(output_dir, 'LICENSE.html'), 'w+') | |
114 output_license_file.write('<!DOCTYPE html>\n') | |
115 output_license_file.write('<html>\n<head>\n') | |
116 output_license_file.write('<meta charset="UTF-8">\n') | |
117 output_license_file.write('<title>Licenses</title>\n') | |
118 style_tag = textwrap.dedent('''\ | |
119 <style> | |
120 body { margin: 0; font-family: sans-serif; } | |
121 pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; } | |
122 p { margin: 1em; white-space: nowrap; } | |
123 </style> | |
124 ''') | |
125 output_license_file.write(style_tag) | |
126 output_license_file.write('</head>\n') | |
127 | |
128 for license_lib in license_libs: | |
129 output_license_file.write('\n<p>\n%s<br/></p>\n' % license_lib) | |
130 output_license_file.write('<pre>\n') | |
131 for path in LIB_TO_LICENSES_DICT[license_lib]: | |
132 license_path = os.path.join(GetWebRTCPath(), path) | |
133 with open(license_path, 'r') as license_file: | |
134 license_text = cgi.escape(license_file.read(), quote=True) | |
135 output_license_file.write(license_text) | |
136 output_license_file.write('\n') | |
137 output_license_file.write('</pre>\n') | |
138 | |
139 output_license_file.write('</body>\n') | |
140 output_license_file.write('</html>') | |
141 output_license_file.close() | |
142 | |
143 | |
144 if __name__ == '__main__': | |
145 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.html') | |
146 parser.add_argument('static_lib_dir', | |
147 help='Directory with built static libraries.') | |
148 parser.add_argument('output_dir', | |
149 help='Directory to output LICENSE.html to.') | |
150 args = parser.parse_args() | |
151 webrtc_target_names = set(GetWebRTCTargetNames()) | |
152 GenerateLicenseText(args.static_lib_dir, args.output_dir) | |
OLD | NEW |