OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2009 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 # This script concatenates in place JS files in the order specified | |
7 # using <script> tags in a given 'order.html' file. | |
8 | |
9 from HTMLParser import HTMLParser | |
10 from cStringIO import StringIO | |
11 import os.path | |
12 import sys | |
13 | |
14 class OrderedJSFilesExtractor(HTMLParser): | |
15 | |
16 def __init__(self, order_html_name): | |
17 HTMLParser.__init__(self) | |
18 self.ordered_js_files = [] | |
19 order_html = open(order_html_name, 'r') | |
20 self.feed(order_html.read()) | |
21 | |
22 def handle_starttag(self, tag, attrs): | |
23 if tag == 'script': | |
24 attrs_dict = dict(attrs) | |
25 if ('type' in attrs_dict and attrs_dict['type'] == 'text/javascript' and | |
26 'src' in attrs_dict): | |
27 self.ordered_js_files.append(attrs_dict['src']) | |
28 | |
29 class PathExpander: | |
30 | |
31 def __init__(self, paths): | |
32 self.paths = paths; | |
33 | |
34 def expand(self, filename): | |
35 last_path = None | |
36 expanded_name = None | |
37 for path in self.paths: | |
38 fname = "%s/%s" % (path, filename) | |
39 if (os.access(fname, os.F_OK)): | |
40 if (last_path != None): | |
41 raise Exception('Ambiguous file %s: found in %s and %s' % | |
42 (filename, last_path, path)) | |
43 expanded_name = fname | |
44 last_path = path | |
45 return expanded_name | |
46 | |
47 def main(argv): | |
48 | |
49 if len(argv) < 3: | |
50 print('usage: %s order.html input_source_dir_1 input_source_dir_2 ... ' | |
51 'output_file' % argv[0]) | |
52 return 1 | |
53 | |
54 output_file_name = argv.pop() | |
55 input_order_file_name = argv[1] | |
56 extractor = OrderedJSFilesExtractor(input_order_file_name) | |
57 expander = PathExpander(argv[2:]) | |
58 output = StringIO() | |
59 | |
60 for input_file_name in extractor.ordered_js_files: | |
61 full_path = expander.expand(input_file_name) | |
62 if (full_path is None): | |
63 raise Exception('File %s referenced in %s not found on any source paths, ' | |
64 'check source tree for consistency' % | |
65 (input_file_name, input_order_file_name)) | |
66 output.write('/* %s */\n\n' % input_file_name) | |
67 input_file = open(full_path, 'r') | |
68 output.write(input_file.read()) | |
69 output.write('\n') | |
70 input_file.close() | |
71 | |
72 output_file = open(output_file_name, 'w') | |
73 output_file.write(output.getvalue()) | |
74 output_file.close() | |
75 output.close() | |
76 | |
77 # Touch output file directory to make sure that Xcode will copy | |
78 # modified resource files. | |
79 if sys.platform == 'darwin': | |
80 output_dir_name = os.path.dirname(output_file_name) | |
81 os.utime(output_dir_name, None) | |
82 | |
83 if __name__ == '__main__': | |
84 sys.exit(main(sys.argv)) | |
OLD | NEW |