This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
compile.py
executable file
·79 lines (65 loc) · 1.97 KB
/
compile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3
import json
import os
import sys
import urllib.parse
import urllib.request
BASE_DIR = os.path.dirname(sys.argv[0])
CLOSURE_URL = 'http://closure-compiler.appspot.com/compile'
TARGET_JS = os.path.join(BASE_DIR, 'sample', 'chrome-nfc.js')
def print_errors(errors, js_files):
for error in errors:
if error['file'].lower().find('externs') >= 0:
filename = error['file']
else:
fileno = int(error['file'][6:])
filename = js_files[fileno]
if 'error' in error:
text = error['error']
else:
text = error['warning']
print(filename + ':' + str(error['lineno']) + ' ' + text)
print(error['line'])
print()
JS_FILES = [
'src/b64.js',
'src/mifare-classic.js',
'src/ndef.js',
'src/nfc.js',
'src/devmanager.js',
'src/scl3711.js',
'src/sha256.js',
'src/tag.js',
'src/tt2.js',
'src/usb.js',
'src/util.js'
]
def main():
print('Compiling JavaScript code.')
params = [
('compilation_level', 'WHITESPACE_ONLY'),
('formatting', 'pretty_print'),
('language', 'ECMASCRIPT5'),
('output_format', 'json'),
('output_info', 'statistics'),
('output_info', 'warnings'),
('output_info', 'errors'),
('output_info', 'compiled_code')
]
for js_file in JS_FILES:
params.append(('js_code', open(os.path.join(BASE_DIR, js_file)).read()))
params = bytes(urllib.parse.urlencode(params, encoding='utf8'), 'utf8')
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
print('Connecting', CLOSURE_URL)
out = urllib.request.urlopen(CLOSURE_URL, data=params)
result = json.loads(out.read().decode('utf8'))
if 'errors' in result and len(result['errors']):
print('Errors:')
print_errors(result['errors'], JS_FILES)
if 'warnings' in result and len(result['warnings']):
print('Warnings:')
print_errors(result['warnings'], JS_FILES)
print('Writing', TARGET_JS)
open(TARGET_JS, 'w').write(result['compiledCode'])
if __name__ == '__main__':
main()