| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Builds the complete main.html file from the basic components. | 6 """Builds the complete main.html file from the basic components. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 from HTMLParser import HTMLParser | 9 from HTMLParser import HTMLParser |
| 10 import argparse | 10 import argparse |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 def handle_starttag(self, tag, attrs): | 26 def handle_starttag(self, tag, attrs): |
| 27 for (name, value) in attrs: | 27 for (name, value) in attrs: |
| 28 if name == 'id': | 28 if name == 'id': |
| 29 if value in self.ids: | 29 if value in self.ids: |
| 30 error('Duplicate id: %s' % value) | 30 error('Duplicate id: %s' % value) |
| 31 self.ids.add(value) | 31 self.ids.add(value) |
| 32 | 32 |
| 33 | 33 |
| 34 class GenerateWebappHtml: | 34 class GenerateWebappHtml: |
| 35 def __init__(self, template_files, js_files, instrumented_js_files): | 35 def __init__(self, template_files, js_files, instrumented_js_files, |
| 36 template_rel_dir): |
| 36 | 37 |
| 37 self.js_files = js_files | 38 self.js_files = js_files |
| 38 self.instrumented_js_files = instrumented_js_files | 39 self.instrumented_js_files = instrumented_js_files |
| 39 | 40 self.template_rel_dir = template_rel_dir |
| 40 | 41 |
| 41 self.templates_expected = set() | 42 self.templates_expected = set() |
| 42 for template in template_files: | 43 for template in template_files: |
| 43 self.templates_expected.add(os.path.basename(template)) | 44 self.templates_expected.add(os.path.basename(template)) |
| 44 | 45 |
| 45 self.templates_found = set() | 46 self.templates_found = set() |
| 46 | 47 |
| 47 def includeJavascript(self, output): | 48 def includeJavascript(self, output): |
| 48 for js_path in sorted(self.js_files): | 49 for js_path in sorted(self.js_files): |
| 49 js_file = os.path.basename(js_path) | 50 js_file = os.path.basename(js_path) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 62 return True | 63 return True |
| 63 | 64 |
| 64 def validateTemplate(self, template_path): | 65 def validateTemplate(self, template_path): |
| 65 template = os.path.basename(template_path) | 66 template = os.path.basename(template_path) |
| 66 if template in self.templates_expected: | 67 if template in self.templates_expected: |
| 67 self.templates_found.add(template) | 68 self.templates_found.add(template) |
| 68 return True | 69 return True |
| 69 return False | 70 return False |
| 70 | 71 |
| 71 def processTemplate(self, output, template_file, indent): | 72 def processTemplate(self, output, template_file, indent): |
| 72 with open(template_file, 'r') as input_template: | 73 with open(os.path.join(self.template_rel_dir, template_file), 'r') as \ |
| 74 input_template: |
| 73 first_line = True | 75 first_line = True |
| 74 skip_header_comment = False | 76 skip_header_comment = False |
| 75 | 77 |
| 76 for line in input_template: | 78 for line in input_template: |
| 77 # If the first line is the start of a copyright notice, then | 79 # If the first line is the start of a copyright notice, then |
| 78 # skip over the entire comment. | 80 # skip over the entire comment. |
| 79 # This will remove the copyright info from the included files, | 81 # This will remove the copyright info from the included files, |
| 80 # but leave the one on the main template. | 82 # but leave the one on the main template. |
| 81 if first_line and re.match(r'<!--', line): | 83 if first_line and re.match(r'<!--', line): |
| 82 skip_header_comment = True | 84 skip_header_comment = True |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 parser.add_argument( | 123 parser.add_argument( |
| 122 '--exclude-js', | 124 '--exclude-js', |
| 123 nargs='*', | 125 nargs='*', |
| 124 default=[], | 126 default=[], |
| 125 help='The Javascript files to exclude from <--js> and <--instrumentedjs>') | 127 help='The Javascript files to exclude from <--js> and <--instrumentedjs>') |
| 126 parser.add_argument( | 128 parser.add_argument( |
| 127 '--instrument-js', | 129 '--instrument-js', |
| 128 nargs='*', | 130 nargs='*', |
| 129 default=[], | 131 default=[], |
| 130 help='Javascript to include and instrument for code coverage') | 132 help='Javascript to include and instrument for code coverage') |
| 133 parser.add_argument( |
| 134 '--dir-for-templates', |
| 135 default = ".", |
| 136 help='Directory template references in html are relative to') |
| 131 parser.add_argument('output_file') | 137 parser.add_argument('output_file') |
| 132 parser.add_argument('input_template') | 138 parser.add_argument('input_template') |
| 133 return parser.parse_args(sys.argv[1:]) | 139 return parser.parse_args(sys.argv[1:]) |
| 134 | 140 |
| 135 | 141 |
| 136 def main(): | 142 def main(): |
| 137 args = parseArgs() | 143 args = parseArgs() |
| 138 | 144 |
| 139 out_file = args.output_file | 145 out_file = args.output_file |
| 140 js_files = set(args.js) - set(args.exclude_js) | 146 js_files = set(args.js) - set(args.exclude_js) |
| 141 | 147 |
| 142 # Create the output directory if it does not exist. | 148 # Create the output directory if it does not exist. |
| 143 out_directory = os.path.dirname(out_file) | 149 out_directory = os.path.dirname(out_file) |
| 144 if not os.path.exists(out_directory): | 150 if not os.path.exists(out_directory): |
| 145 os.makedirs(out_directory) | 151 os.makedirs(out_directory) |
| 146 | 152 |
| 147 # Generate the main HTML file from the templates. | 153 # Generate the main HTML file from the templates. |
| 148 with open(out_file, 'w') as output: | 154 with open(out_file, 'w') as output: |
| 149 gen = GenerateWebappHtml(args.templates, js_files, args.instrument_js) | 155 gen = GenerateWebappHtml(args.templates, js_files, args.instrument_js, |
| 156 args.dir_for_templates) |
| 150 gen.processTemplate(output, args.input_template, 0) | 157 gen.processTemplate(output, args.input_template, 0) |
| 151 | 158 |
| 152 # Verify that all the expected templates were found. | 159 # Verify that all the expected templates were found. |
| 153 if not gen.verifyTemplateList(): | 160 if not gen.verifyTemplateList(): |
| 154 error('Extra templates specified') | 161 error('Extra templates specified') |
| 155 | 162 |
| 156 # Verify that the generated HTML file is valid. | 163 # Verify that the generated HTML file is valid. |
| 157 with open(out_file, 'r') as input_html: | 164 with open(out_file, 'r') as input_html: |
| 158 parser = HtmlChecker() | 165 parser = HtmlChecker() |
| 159 parser.feed(input_html.read()) | 166 parser.feed(input_html.read()) |
| 160 | 167 |
| 161 | 168 |
| 162 if __name__ == '__main__': | 169 if __name__ == '__main__': |
| 163 sys.exit(main()) | 170 sys.exit(main()) |
| OLD | NEW |