Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 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 | """Creates a grd file for packaging the inspector files.""" |
| 31 | |
| 32 | from __future__ import with_statement |
| 33 | from os import path |
| 34 | |
| 35 | import errno |
| 36 | import os |
| 37 | import shlex |
| 38 | import shutil |
| 39 | import sys |
| 40 | from xml.dom import minidom |
| 41 | |
| 42 | kDevToolsResourcePrefix = 'IDR_DEVTOOLS_' |
| 43 | kGrdTemplate = '''<?xml version="1.0" encoding="UTF-8"?> |
| 44 | <grit latest_public_release="0" current_release="1" |
| 45 | output_all_resource_defines="false"> |
| 46 | <outputs> |
| 47 | <output filename="grit/devtools_resources.h" type="rc_header"> |
| 48 | <emit emit_type='prepend'></emit> |
| 49 | </output> |
| 50 | <output filename="grit/devtools_resources_map.cc" type="resource_file_map_source" /> |
| 51 | <output filename="grit/devtools_resources_map.h" type="resource_map_header" /> |
| 52 | |
| 53 | <output filename="devtools_resources.pak" type="data_package" /> |
| 54 | </outputs> |
| 55 | <release seq="1"> |
| 56 | <includes> |
Andrey Kosyakov | 6e36adc | 2018-11-20 00:25:34 +0000 | [diff] [blame] | 57 | <include name="COMPRESSED_PROTOCOL_JSON" file="${compressed_protocol_file}" use_base_dir="false" type="BINDATA" skip_in_resource_map = "true"/> |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 58 | </includes> |
| 59 | </release> |
| 60 | </grit> |
| 61 | ''' |
| 62 | |
| 63 | |
| 64 | class ParsedArgs: |
| 65 | |
| 66 | def __init__(self, source_files, relative_path_dirs, image_dirs, output_filename): |
| 67 | self.source_files = source_files |
| 68 | self.relative_path_dirs = relative_path_dirs |
| 69 | self.image_dirs = image_dirs |
| 70 | self.output_filename = output_filename |
| 71 | |
| 72 | |
| 73 | def parse_args(argv): |
| 74 | # The arguments are of the format: |
| 75 | # [ <source_files> ]* |
| 76 | # --relative_path_dirs [ <directory> ]* |
| 77 | # --images [ <image_dirs> ]* |
| 78 | # --output <output_file> |
| 79 | relative_path_dirs_position = argv.index('--relative_path_dirs') |
| 80 | images_position = argv.index('--images') |
| 81 | output_position = argv.index('--output') |
| 82 | source_files = argv[:relative_path_dirs_position] |
| 83 | relative_path_dirs = argv[relative_path_dirs_position + 1:images_position] |
| 84 | image_dirs = argv[images_position + 1:output_position] |
| 85 | return ParsedArgs(source_files, relative_path_dirs, image_dirs, argv[output_position + 1]) |
| 86 | |
| 87 | |
| 88 | def make_name_from_filename(filename): |
| 89 | return (filename.replace('/', '_').replace('\\', '_').replace('-', '_').replace('.', '_')).upper() |
| 90 | |
| 91 | |
| 92 | def add_file_to_grd(grd_doc, relative_filename): |
| 93 | includes_node = grd_doc.getElementsByTagName('includes')[0] |
| 94 | includes_node.appendChild(grd_doc.createTextNode('\n ')) |
| 95 | |
| 96 | new_include_node = grd_doc.createElement('include') |
| 97 | new_include_node.setAttribute('name', make_name_from_filename(relative_filename)) |
| 98 | new_include_node.setAttribute('file', relative_filename) |
| 99 | new_include_node.setAttribute('type', 'BINDATA') |
| 100 | includes_node.appendChild(new_include_node) |
| 101 | |
| 102 | |
| 103 | def build_relative_filename(relative_path_dirs, filename): |
| 104 | for relative_path_dir in relative_path_dirs: |
| 105 | index = filename.find(relative_path_dir) |
| 106 | if index == 0: |
| 107 | return filename[len(relative_path_dir) + 1:] |
| 108 | return path.basename(filename) |
| 109 | |
| 110 | |
| 111 | def main(argv): |
| 112 | parsed_args = parse_args(argv[1:]) |
| 113 | |
| 114 | doc = minidom.parseString(kGrdTemplate) |
| 115 | output_directory = path.dirname(parsed_args.output_filename) |
| 116 | |
| 117 | try: |
| 118 | os.makedirs(path.join(output_directory, 'Images')) |
| 119 | except OSError, e: |
| 120 | if e.errno != errno.EEXIST: |
| 121 | raise e |
| 122 | |
| 123 | written_filenames = set() |
| 124 | for filename in parsed_args.source_files: |
| 125 | relative_filename = build_relative_filename(parsed_args.relative_path_dirs, filename) |
| 126 | # Avoid writing duplicate relative filenames. |
| 127 | if relative_filename in written_filenames: |
| 128 | continue |
| 129 | written_filenames.add(relative_filename) |
| 130 | target_dir = path.join(output_directory, path.dirname(relative_filename)) |
| 131 | if not path.exists(target_dir): |
| 132 | os.makedirs(target_dir) |
| 133 | shutil.copy(filename, target_dir) |
| 134 | add_file_to_grd(doc, relative_filename) |
| 135 | |
| 136 | for dirname in parsed_args.image_dirs: |
| 137 | for filename in sorted(os.listdir(dirname)): |
| 138 | if not filename.endswith('.png') and not filename.endswith('.gif') and not filename.endswith('.svg'): |
| 139 | continue |
| 140 | shutil.copy(path.join(dirname, filename), path.join(output_directory, 'Images')) |
| 141 | add_file_to_grd(doc, path.join('Images', filename)) |
| 142 | |
| 143 | with open(parsed_args.output_filename, 'w') as output_file: |
| 144 | output_file.write(doc.toxml(encoding='UTF-8')) |
| 145 | |
| 146 | |
| 147 | if __name__ == '__main__': |
| 148 | sys.exit(main(sys.argv)) |