1 #!/usr/bin/env python 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 31 """Creates a grd file for packaging the inspector files.""" 32 33 from __future__ import with_statement 34 35 import errno 36 import os 37 import shutil 38 import sys 39 from xml.dom import minidom 40 41 kDevToolsResourcePrefix = 'IDR_DEVTOOLS_' 42 kGrdTemplate = '''<?xml version="1.0" encoding="UTF-8"?> 43 <grit latest_public_release="0" current_release="1"> 44 <outputs> 45 <output filename="grit/devtools_resources.h" type="rc_header"> 46 <emit emit_type='prepend'></emit> 47 </output> 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" /> 50 51 <output filename="devtools_resources.pak" type="data_package" /> 52 </outputs> 53 <release seq="1"> 54 <includes></includes> 55 </release> 56 </grit> 57 ''' 58 59 60 class ParsedArgs: 61 def __init__(self, source_files, image_dirs, output_filename): 62 self.source_files = source_files 63 self.image_dirs = image_dirs 64 self.output_filename = output_filename 65 66 67 def parse_args(argv): 68 images_position = argv.index('--images') 69 output_position = argv.index('--output') 70 source_files = argv[:images_position] 71 image_dirs = argv[images_position + 1:output_position] 72 return ParsedArgs(source_files, image_dirs, argv[output_position + 1]) 73 74 75 def make_name_from_filename(filename): 76 return (filename.replace('/', '_') 77 .replace('\\', '_') 78 .replace('.', '_')).upper() 79 80 81 def add_file_to_grd(grd_doc, filename): 82 includes_node = grd_doc.getElementsByTagName('includes')[0] 83 includes_node.appendChild(grd_doc.createTextNode('\n ')) 84 85 new_include_node = grd_doc.createElement('include') 86 new_include_node.setAttribute('name', make_name_from_filename(filename)) 87 new_include_node.setAttribute('file', filename) 88 new_include_node.setAttribute('type', 'BINDATA') 89 includes_node.appendChild(new_include_node) 90 91 92 def main(argv): 93 parsed_args = parse_args(argv[1:]) 94 95 doc = minidom.parseString(kGrdTemplate) 96 output_directory = os.path.dirname(parsed_args.output_filename) 97 98 try: 99 os.makedirs(os.path.join(output_directory, 'Images')) 100 except OSError, e: 101 if e.errno != errno.EEXIST: 102 raise e 103 104 for filename in parsed_args.source_files: 105 shutil.copy(filename, output_directory) 106 add_file_to_grd(doc, os.path.basename(filename)) 107 108 for dirname in parsed_args.image_dirs: 109 for filename in os.listdir(dirname): 110 if not filename.endswith('.png') and not filename.endswith('.gif'): 111 continue 112 shutil.copy(os.path.join(dirname, filename), 113 os.path.join(output_directory, 'Images')) 114 add_file_to_grd(doc, os.path.join('Images', filename)) 115 116 with open(parsed_args.output_filename, 'w') as output_file: 117 output_file.write(doc.toxml(encoding='UTF-8')) 118 119 120 if __name__ == '__main__': 121 sys.exit(main(sys.argv)) 122