OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 import argparse | |
7 from HTMLParser import HTMLParser | |
8 import subprocess | |
9 import sys | |
10 import os | |
11 | |
12 DESCRIPTION = '''Extract the <script> tags from the specified html file and | |
13 run them through jscompile, along with any additional JS files specified.''' | |
14 FILES_HELP = '''A list of HTML or Javascript files. The Javascript files should | |
15 contain definitions of types or functions that are known to Chrome but not to | |
16 jscompile; they are added to the list of <script> tags found in each of the | |
17 HTML files.''' | |
18 STAMP_HELP = 'Timestamp file to update on success.' | |
19 | |
20 class ScriptTagParser(HTMLParser): | |
21 def __init__(self): | |
22 HTMLParser.__init__(self) | |
23 self.scripts = [] | |
24 | |
25 def handle_starttag(self, tag, attrs): | |
26 if tag == 'script': | |
27 for (name, value) in attrs: | |
28 if name == 'src': | |
29 self.scripts.append(value) | |
30 | |
31 | |
32 def parseHtml(html_file, js_proto_files): | |
33 src_dir = os.path.split(html_file)[0] | |
34 f = open(html_file, 'r') | |
35 html = f.read() | |
36 parser = ScriptTagParser(); | |
37 parser.feed(html) | |
38 scripts = [] | |
39 for script in parser.scripts: | |
40 # Ignore non-local scripts | |
41 if not '://' in script: | |
42 scripts.append(os.path.join(src_dir, script)) | |
43 | |
44 args = ['jscompile'] + scripts + js_proto_files | |
45 result = subprocess.call(args) | |
46 return result == 0 | |
47 | |
48 | |
49 def main(): | |
50 parser = argparse.ArgumentParser(description = DESCRIPTION) | |
51 parser.add_argument('files', nargs = '+', help = FILES_HELP) | |
52 parser.add_argument('--success-stamp', dest = 'success_stamp', | |
53 help = STAMP_HELP) | |
54 options = parser.parse_args() | |
55 | |
56 html = [] | |
57 js = [] | |
58 for file in options.files: | |
59 name, extension = os.path.splitext(file) | |
60 if extension == '.html': | |
61 html.append(file) | |
62 elif extension == '.js': | |
63 js.append(file) | |
64 else: | |
65 print >> sys.stderr, 'Unknown extension (' + extension + ') for ' + file | |
66 return 1 | |
67 | |
68 for html_file in html: | |
69 if not parseHtml(html_file, js): | |
70 return 1 | |
71 | |
72 if options.success_stamp: | |
73 with open(options.success_stamp, 'w'): | |
74 os.utime(options.success_stamp, None) | |
75 | |
76 return 0 | |
77 | |
78 if __name__ == '__main__': | |
79 sys.exit(main()) | |
OLD | NEW |