| 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 def main(argv): |
| 30 |
| 31 if len(argv) < 3: |
| 32 print('usage: %s order.html input_file_1 input_file_2 ... ' |
| 33 'output_file' % argv[0]) |
| 34 return 1 |
| 35 |
| 36 output_file_name = argv.pop() |
| 37 |
| 38 full_paths = {} |
| 39 for full_path in argv[2:]: |
| 40 (dir_name, file_name) = os.path.split(full_path) |
| 41 if file_name.endswith('.js'): |
| 42 full_paths[file_name] = full_path |
| 43 |
| 44 extractor = OrderedJSFilesExtractor(argv[1]) |
| 45 output = StringIO() |
| 46 |
| 47 for input_file_name in extractor.ordered_js_files: |
| 48 if not input_file_name in full_paths: |
| 49 print('A full path to %s isn\'t specified in command line. ' |
| 50 'Probably, you have an obsolete script entry in %s' % |
| 51 (input_file_name, argv[1])) |
| 52 output.write('/* %s */\n\n' % input_file_name) |
| 53 input_file = open(full_paths[input_file_name], 'r') |
| 54 output.write(input_file.read()) |
| 55 output.write('\n') |
| 56 input_file.close() |
| 57 |
| 58 output_file = open(output_file_name, 'w') |
| 59 output_file.write(output.getvalue()) |
| 60 output_file.close() |
| 61 output.close() |
| 62 |
| 63 if __name__ == '__main__': |
| 64 sys.exit(main(sys.argv)) |
| OLD | NEW |