blob: be215c0181090f90383db3cc7f3c4bc736b238f1 [file] [log] [blame]
Liviu Rau3da5d4a2022-02-23 11:07:121#!/usr/bin/env vpython3
Blink Reformat4c46d092018-04-07 15:32:372#
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 Reformat4c46d092018-04-07 15:32:3732
Blink Reformat4c46d092018-04-07 15:32:3733import os
34import shlex
Blink Reformat4c46d092018-04-07 15:32:3735import sys
36from xml.dom import minidom
37
38kDevToolsResourcePrefix = 'IDR_DEVTOOLS_'
39kGrdTemplate = '''<?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 Chen4f6a8762019-08-27 20:23:2153 <include name="COMPRESSED_PROTOCOL_JSON" file="${protocol_file}" use_base_dir="false" compress="brotli" type="BINDATA" skip_in_resource_map = "true"/>
Blink Reformat4c46d092018-04-07 15:32:3754 </includes>
55 </release>
56</grit>
57'''
58
59
60class ParsedArgs:
61
Tim van der Lippe0fa20642021-04-23 15:26:0462 def __init__(self, file_list, output_filename, compress):
Rob Paveza49473552020-01-08 21:38:5863 self.file_list = file_list
Rob Paveza41120592020-01-13 17:17:0264 file_list_file = open(file_list, 'r')
Tim van der Lippe3edd9d72021-04-22 11:12:5765 file_list_contents = file_list_file.read()
Rob Paveza49473552020-01-08 21:38:5866 self.source_files = shlex.split(file_list_contents)
Blink Reformat4c46d092018-04-07 15:32:3767 self.output_filename = output_filename
Bruce Dawson42395302021-01-26 08:57:3668 self.compress = compress
Blink Reformat4c46d092018-04-07 15:32:3769
70
71def parse_args(argv):
72 # The arguments are of the format:
Rob Paveza41120592020-01-13 17:17:0273 # --file_list <input_file_list>
Blink Reformat4c46d092018-04-07 15:32:3774 # --output <output_file>
Bruce Dawson42395302021-01-26 08:57:3675 # --compress
Rob Paveza41120592020-01-13 17:17:0276 file_list_position = argv.index('--file_list')
Blink Reformat4c46d092018-04-07 15:32:3777 output_position = argv.index('--output')
Rob Paveza49473552020-01-08 21:38:5878 file_list = argv[file_list_position + 1]
Bruce Dawson42395302021-01-26 08:57:3679 compress = argv.count('--compress') > 0
Tim van der Lippe0fa20642021-04-23 15:26:0480 return ParsedArgs(file_list, argv[output_position + 1], compress)
Blink Reformat4c46d092018-04-07 15:32:3781
82
83def make_name_from_filename(filename):
84 return (filename.replace('/', '_').replace('\\', '_').replace('-', '_').replace('.', '_')).upper()
85
86
Bruce Dawson42395302021-01-26 08:57:3687def add_file_to_grd(grd_doc, relative_filename, compress):
Blink Reformat4c46d092018-04-07 15:32:3788 includes_node = grd_doc.getElementsByTagName('includes')[0]
89 includes_node.appendChild(grd_doc.createTextNode('\n '))
90
Steven Bennetts457ff4a2019-11-15 20:44:1891 ext = os.path.splitext(relative_filename)[1]
Alex Rudenko8505e322021-05-25 06:01:1792 new_include_node = grd_doc.createElement('include')
Alex Rudenko7c94b5f2021-05-25 12:27:5193 if compress and ext in ['.css', '.html', '.js', '.svg', '.json', '.md']:
Alex Rudenko8505e322021-05-25 06:01:1794 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 Reformat4c46d092018-04-07 15:32:37103 includes_node.appendChild(new_include_node)
104
105
Blink Reformat4c46d092018-04-07 15:32:37106def main(argv):
107 parsed_args = parse_args(argv[1:])
108
109 doc = minidom.parseString(kGrdTemplate)
Blink Reformat4c46d092018-04-07 15:32:37110
Blink Reformat4c46d092018-04-07 15:32:37111 written_filenames = set()
112 for filename in parsed_args.source_files:
Blink Reformat4c46d092018-04-07 15:32:37113 # Avoid writing duplicate relative filenames.
Tim van der Lippe0fa20642021-04-23 15:26:04114 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 Reformat4c46d092018-04-07 15:32:37118
Dirk Pranke75443e832020-10-01 00:23:38119 with open(parsed_args.output_filename, 'wb') as output_file:
Blink Reformat4c46d092018-04-07 15:32:37120 output_file.write(doc.toxml(encoding='UTF-8'))
121
Blink Reformat4c46d092018-04-07 15:32:37122
123if __name__ == '__main__':
124 sys.exit(main(sys.argv))