Liviu Rau | 3da5d4a | 2022-02-23 11:07:12 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2011 Google Inc. All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | """Creates a grd file for packaging the inspector files.""" |
| 31 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 32 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 33 | import os |
| 34 | import shlex |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 35 | import sys |
| 36 | from xml.dom import minidom |
| 37 | |
| 38 | kDevToolsResourcePrefix = 'IDR_DEVTOOLS_' |
| 39 | kGrdTemplate = '''<?xml version="1.0" encoding="UTF-8"?> |
| 40 | <grit latest_public_release="0" current_release="1" |
| 41 | output_all_resource_defines="false"> |
| 42 | <outputs> |
| 43 | <output filename="grit/devtools_resources.h" type="rc_header"> |
| 44 | <emit emit_type='prepend'></emit> |
| 45 | </output> |
| 46 | <output filename="grit/devtools_resources_map.cc" type="resource_file_map_source" /> |
| 47 | <output filename="grit/devtools_resources_map.h" type="resource_map_header" /> |
| 48 | |
| 49 | <output filename="devtools_resources.pak" type="data_package" /> |
| 50 | </outputs> |
| 51 | <release seq="1"> |
| 52 | <includes> |
Jacques Chen | 4f6a876 | 2019-08-27 20:23:21 | [diff] [blame] | 53 | <include name="COMPRESSED_PROTOCOL_JSON" file="${protocol_file}" use_base_dir="false" compress="brotli" type="BINDATA" skip_in_resource_map = "true"/> |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 54 | </includes> |
| 55 | </release> |
| 56 | </grit> |
| 57 | ''' |
| 58 | |
| 59 | |
| 60 | class ParsedArgs: |
| 61 | |
Tim van der Lippe | 0fa2064 | 2021-04-23 15:26:04 | [diff] [blame] | 62 | def __init__(self, file_list, output_filename, compress): |
Rob Paveza | 4947355 | 2020-01-08 21:38:58 | [diff] [blame] | 63 | self.file_list = file_list |
Rob Paveza | 4112059 | 2020-01-13 17:17:02 | [diff] [blame] | 64 | file_list_file = open(file_list, 'r') |
Tim van der Lippe | 3edd9d7 | 2021-04-22 11:12:57 | [diff] [blame] | 65 | file_list_contents = file_list_file.read() |
Rob Paveza | 4947355 | 2020-01-08 21:38:58 | [diff] [blame] | 66 | self.source_files = shlex.split(file_list_contents) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 67 | self.output_filename = output_filename |
Bruce Dawson | 4239530 | 2021-01-26 08:57:36 | [diff] [blame] | 68 | self.compress = compress |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def parse_args(argv): |
| 72 | # The arguments are of the format: |
Rob Paveza | 4112059 | 2020-01-13 17:17:02 | [diff] [blame] | 73 | # --file_list <input_file_list> |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 74 | # --output <output_file> |
Bruce Dawson | 4239530 | 2021-01-26 08:57:36 | [diff] [blame] | 75 | # --compress |
Rob Paveza | 4112059 | 2020-01-13 17:17:02 | [diff] [blame] | 76 | file_list_position = argv.index('--file_list') |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | output_position = argv.index('--output') |
Rob Paveza | 4947355 | 2020-01-08 21:38:58 | [diff] [blame] | 78 | file_list = argv[file_list_position + 1] |
Bruce Dawson | 4239530 | 2021-01-26 08:57:36 | [diff] [blame] | 79 | compress = argv.count('--compress') > 0 |
Tim van der Lippe | 0fa2064 | 2021-04-23 15:26:04 | [diff] [blame] | 80 | return ParsedArgs(file_list, argv[output_position + 1], compress) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | |
| 82 | |
| 83 | def make_name_from_filename(filename): |
| 84 | return (filename.replace('/', '_').replace('\\', '_').replace('-', '_').replace('.', '_')).upper() |
| 85 | |
| 86 | |
Bruce Dawson | 4239530 | 2021-01-26 08:57:36 | [diff] [blame] | 87 | def add_file_to_grd(grd_doc, relative_filename, compress): |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | includes_node = grd_doc.getElementsByTagName('includes')[0] |
| 89 | includes_node.appendChild(grd_doc.createTextNode('\n ')) |
| 90 | |
Steven Bennetts | 457ff4a | 2019-11-15 20:44:18 | [diff] [blame] | 91 | ext = os.path.splitext(relative_filename)[1] |
Alex Rudenko | 8505e32 | 2021-05-25 06:01:17 | [diff] [blame] | 92 | new_include_node = grd_doc.createElement('include') |
Alex Rudenko | 7c94b5f | 2021-05-25 12:27:51 | [diff] [blame] | 93 | if compress and ext in ['.css', '.html', '.js', '.svg', '.json', '.md']: |
Alex Rudenko | 8505e32 | 2021-05-25 06:01:17 | [diff] [blame] | 94 | new_include_node.setAttribute('file', |
| 95 | relative_filename + '.compressed') |
| 96 | else: |
| 97 | new_include_node.setAttribute('file', relative_filename) |
| 98 | |
| 99 | new_include_node.setAttribute('name', make_name_from_filename(relative_filename)) |
| 100 | new_include_node.setAttribute('resource_path', relative_filename) |
| 101 | new_include_node.setAttribute('type', 'BINDATA') |
| 102 | new_include_node.setAttribute('compress', 'false') |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 103 | includes_node.appendChild(new_include_node) |
| 104 | |
| 105 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 106 | def main(argv): |
| 107 | parsed_args = parse_args(argv[1:]) |
| 108 | |
| 109 | doc = minidom.parseString(kGrdTemplate) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 110 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 111 | written_filenames = set() |
| 112 | for filename in parsed_args.source_files: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 113 | # Avoid writing duplicate relative filenames. |
Tim van der Lippe | 0fa2064 | 2021-04-23 15:26:04 | [diff] [blame] | 114 | if filename in written_filenames: |
| 115 | raise Exception("Duplicate file detected: %s" % filename) |
| 116 | written_filenames.add(filename) |
| 117 | add_file_to_grd(doc, filename, parsed_args.compress) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 118 | |
Dirk Pranke | 75443e83 | 2020-10-01 00:23:38 | [diff] [blame] | 119 | with open(parsed_args.output_filename, 'wb') as output_file: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 120 | output_file.write(doc.toxml(encoding='UTF-8')) |
| 121 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | |
| 123 | if __name__ == '__main__': |
| 124 | sys.exit(main(sys.argv)) |