| OLD | NEW |
| (Empty) | |
| 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 from os import path |
| 35 |
| 36 import errno |
| 37 import os |
| 38 import shlex |
| 39 import shutil |
| 40 import sys |
| 41 from xml.dom import minidom |
| 42 |
| 43 kDevToolsResourcePrefix = 'IDR_DEVTOOLS_' |
| 44 kGrdTemplate = '''<?xml version="1.0" encoding="UTF-8"?> |
| 45 <grit latest_public_release="0" current_release="1" |
| 46 output_all_resource_defines="false"> |
| 47 <outputs> |
| 48 <output filename="grit/devtools_resources.h" type="rc_header"> |
| 49 <emit emit_type='prepend'></emit> |
| 50 </output> |
| 51 <output filename="grit/devtools_resources_map.cc" type="resource_file_map_so
urce" /> |
| 52 <output filename="grit/devtools_resources_map.h" type="resource_map_header"
/> |
| 53 |
| 54 <output filename="devtools_resources.pak" type="data_package" /> |
| 55 </outputs> |
| 56 <release seq="1"> |
| 57 <includes></includes> |
| 58 </release> |
| 59 </grit> |
| 60 ''' |
| 61 |
| 62 |
| 63 class ParsedArgs: |
| 64 def __init__(self, source_files, relative_path_dirs, image_dirs, output_file
name): |
| 65 self.source_files = source_files |
| 66 self.relative_path_dirs = relative_path_dirs |
| 67 self.image_dirs = image_dirs |
| 68 self.output_filename = output_filename |
| 69 |
| 70 |
| 71 def parse_args(argv): |
| 72 # The arguments are of the format: |
| 73 # [ <source_files> ]* |
| 74 # [ (--static_files_list <file>) | (--static_files_args <file>) ] |
| 75 # --relative_path_dirs [ <directory> ]* |
| 76 # --images [ <image_dirs> ]* |
| 77 # --output <output_file> |
| 78 # |
| 79 # --static_files_list means the file contains newline-separated filenames |
| 80 # from GYP, and --static_files_args means the file looks like a shell |
| 81 # string from GN. |
| 82 relative_path_dirs_position = argv.index('--relative_path_dirs') |
| 83 images_position = argv.index('--images') |
| 84 output_position = argv.index('--output') |
| 85 |
| 86 if '--static_files_list' in argv: |
| 87 # This branch can be removed when GYP support is no longer necessary. |
| 88 static_files_list_position = argv.index('--static_files_list') |
| 89 static_files_list_path = argv[static_files_list_position + 1] |
| 90 source_files = argv[:static_files_list_position] |
| 91 with open(static_files_list_path, 'r') as static_list_file: |
| 92 source_files.extend([line.rstrip('\n') for line in static_list_file.
readlines()]) |
| 93 elif '--static_files_args' in argv: |
| 94 static_files_args_position = argv.index('--static_files_args') |
| 95 static_files_args_path = argv[static_files_args_position + 1] |
| 96 source_files = argv[:static_files_args_position] |
| 97 with open(static_files_args_path, 'r') as static_args_file: |
| 98 source_files.extend(shlex.split(static_args_file)) |
| 99 else: |
| 100 source_files = argv[:relative_path_dirs_position] |
| 101 |
| 102 relative_path_dirs = argv[relative_path_dirs_position + 1:images_position] |
| 103 image_dirs = argv[images_position + 1:output_position] |
| 104 return ParsedArgs(source_files, relative_path_dirs, image_dirs, argv[output_
position + 1]) |
| 105 |
| 106 |
| 107 def make_name_from_filename(filename): |
| 108 return (filename.replace('/', '_') |
| 109 .replace('\\', '_') |
| 110 .replace('-', '_') |
| 111 .replace('.', '_')).upper() |
| 112 |
| 113 |
| 114 def add_file_to_grd(grd_doc, relative_filename): |
| 115 includes_node = grd_doc.getElementsByTagName('includes')[0] |
| 116 includes_node.appendChild(grd_doc.createTextNode('\n ')) |
| 117 |
| 118 new_include_node = grd_doc.createElement('include') |
| 119 new_include_node.setAttribute('name', make_name_from_filename(relative_filen
ame)) |
| 120 new_include_node.setAttribute('file', relative_filename) |
| 121 new_include_node.setAttribute('type', 'BINDATA') |
| 122 includes_node.appendChild(new_include_node) |
| 123 |
| 124 |
| 125 def build_relative_filename(relative_path_dirs, filename): |
| 126 for relative_path_dir in relative_path_dirs: |
| 127 index = filename.find(relative_path_dir) |
| 128 if index == 0: |
| 129 return filename[len(relative_path_dir) + 1:] |
| 130 return path.basename(filename) |
| 131 |
| 132 |
| 133 def main(argv): |
| 134 parsed_args = parse_args(argv[1:]) |
| 135 |
| 136 doc = minidom.parseString(kGrdTemplate) |
| 137 output_directory = path.dirname(parsed_args.output_filename) |
| 138 |
| 139 try: |
| 140 os.makedirs(path.join(output_directory, 'Images')) |
| 141 except OSError, e: |
| 142 if e.errno != errno.EEXIST: |
| 143 raise e |
| 144 |
| 145 written_filenames = set() |
| 146 for filename in parsed_args.source_files: |
| 147 relative_filename = build_relative_filename(parsed_args.relative_path_di
rs, filename) |
| 148 # Avoid writing duplicate relative filenames. |
| 149 if relative_filename in written_filenames: |
| 150 continue |
| 151 written_filenames.add(relative_filename) |
| 152 target_dir = path.join(output_directory, path.dirname(relative_filename)
) |
| 153 if not path.exists(target_dir): |
| 154 os.makedirs(target_dir) |
| 155 shutil.copy(filename, target_dir) |
| 156 add_file_to_grd(doc, relative_filename) |
| 157 |
| 158 for dirname in parsed_args.image_dirs: |
| 159 for filename in os.listdir(dirname): |
| 160 if not filename.endswith('.png') and not filename.endswith('.gif') a
nd not filename.endswith('.svg'): |
| 161 continue |
| 162 shutil.copy(path.join(dirname, filename), |
| 163 path.join(output_directory, 'Images')) |
| 164 add_file_to_grd(doc, path.join('Images', filename)) |
| 165 |
| 166 with open(parsed_args.output_filename, 'w') as output_file: |
| 167 output_file.write(doc.toxml(encoding='UTF-8')) |
| 168 |
| 169 |
| 170 if __name__ == '__main__': |
| 171 sys.exit(main(sys.argv)) |
| OLD | NEW |