3 # Copyright (C) 2011 Google Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
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
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.
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.
31 """Creates a grd file for packaging the inspector files."""
33 from __future__ import with_statement
39 from xml.dom import minidom
41 kDevToolsResourcePrefix = 'IDR_DEVTOOLS_'
42 kGrdTemplate = '''<?xml version="1.0" encoding="UTF-8"?>
43 <grit latest_public_release="0" current_release="1">
45 <output filename="grit/devtools_resources.h" type="rc_header">
46 <emit emit_type='prepend'></emit>
48 <output filename="grit/devtools_resources_map.cc" type="resource_file_map_source" />
49 <output filename="grit/devtools_resources_map.h" type="resource_map_header" />
51 <output filename="devtools_resources.pak" type="data_package" />
61 def __init__(self, source_files, relative_path_dirs, image_dirs, output_filename):
62 self.source_files = source_files
63 self.relative_path_dirs = relative_path_dirs
64 self.image_dirs = image_dirs
65 self.output_filename = output_filename
69 relative_path_dirs_position = argv.index('--relative_path_dirs')
70 images_position = argv.index('--images')
71 output_position = argv.index('--output')
72 source_files = argv[:relative_path_dirs_position]
73 relative_path_dirs = argv[relative_path_dirs_position + 1:images_position]
74 image_dirs = argv[images_position + 1:output_position]
75 return ParsedArgs(source_files, relative_path_dirs, image_dirs, argv[output_position + 1])
78 def make_name_from_filename(filename):
79 return (filename.replace('/', '_')
82 .replace('.', '_')).upper()
85 def add_file_to_grd(grd_doc, relative_filename):
86 includes_node = grd_doc.getElementsByTagName('includes')[0]
87 includes_node.appendChild(grd_doc.createTextNode('\n '))
89 new_include_node = grd_doc.createElement('include')
90 new_include_node.setAttribute('name', make_name_from_filename(relative_filename))
91 new_include_node.setAttribute('file', relative_filename)
92 new_include_node.setAttribute('type', 'BINDATA')
93 includes_node.appendChild(new_include_node)
96 def build_relative_filename(relative_path_dirs, filename):
97 for relative_path_dir in relative_path_dirs:
98 index = filename.find(relative_path_dir)
100 return filename[len(relative_path_dir) + 1:]
101 return os.path.basename(filename)
105 parsed_args = parse_args(argv[1:])
107 doc = minidom.parseString(kGrdTemplate)
108 output_directory = os.path.dirname(parsed_args.output_filename)
111 os.makedirs(os.path.join(output_directory, 'Images'))
113 if e.errno != errno.EEXIST:
116 written_filenames = set()
117 for filename in parsed_args.source_files:
118 relative_filename = build_relative_filename(parsed_args.relative_path_dirs, filename)
119 # Avoid writing duplicate relative filenames.
120 if relative_filename in written_filenames:
122 written_filenames.add(relative_filename)
123 target_dir = os.path.join(output_directory, os.path.dirname(relative_filename))
124 if not os.path.exists(target_dir):
125 os.makedirs(target_dir)
126 shutil.copy(filename, target_dir)
127 add_file_to_grd(doc, relative_filename)
129 for dirname in parsed_args.image_dirs:
130 for filename in os.listdir(dirname):
131 if not filename.endswith('.png') and not filename.endswith('.gif'):
133 shutil.copy(os.path.join(dirname, filename),
134 os.path.join(output_directory, 'Images'))
135 add_file_to_grd(doc, os.path.join('Images', filename))
137 with open(parsed_args.output_filename, 'w') as output_file:
138 output_file.write(doc.toxml(encoding='UTF-8'))
141 if __name__ == '__main__':
142 sys.exit(main(sys.argv))