OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (C) 2011 Google Inc. All rights reserved. | 3 # Copyright (C) 2011 Google Inc. All rights reserved. |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 13 matching lines...) Expand all Loading... |
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 """Creates a grd file for packaging the inspector files.""" | 31 """Creates a grd file for packaging the inspector files.""" |
32 | 32 |
33 from __future__ import with_statement | 33 from __future__ import with_statement |
| 34 from os import path |
34 | 35 |
35 import errno | 36 import errno |
36 import os | 37 import os |
37 import shutil | 38 import shutil |
38 import sys | 39 import sys |
39 from xml.dom import minidom | 40 from xml.dom import minidom |
40 | 41 |
41 kDevToolsResourcePrefix = 'IDR_DEVTOOLS_' | 42 kDevToolsResourcePrefix = 'IDR_DEVTOOLS_' |
42 kGrdTemplate = '''<?xml version="1.0" encoding="UTF-8"?> | 43 kGrdTemplate = '''<?xml version="1.0" encoding="UTF-8"?> |
43 <grit latest_public_release="0" current_release="1"> | 44 <grit latest_public_release="0" current_release="1"> |
(...skipping 15 matching lines...) Expand all Loading... |
59 | 60 |
60 class ParsedArgs: | 61 class ParsedArgs: |
61 def __init__(self, source_files, relative_path_dirs, image_dirs, output_file
name): | 62 def __init__(self, source_files, relative_path_dirs, image_dirs, output_file
name): |
62 self.source_files = source_files | 63 self.source_files = source_files |
63 self.relative_path_dirs = relative_path_dirs | 64 self.relative_path_dirs = relative_path_dirs |
64 self.image_dirs = image_dirs | 65 self.image_dirs = image_dirs |
65 self.output_filename = output_filename | 66 self.output_filename = output_filename |
66 | 67 |
67 | 68 |
68 def parse_args(argv): | 69 def parse_args(argv): |
| 70 static_files_list_position = argv.index('--static_files_list') |
69 relative_path_dirs_position = argv.index('--relative_path_dirs') | 71 relative_path_dirs_position = argv.index('--relative_path_dirs') |
70 images_position = argv.index('--images') | 72 images_position = argv.index('--images') |
71 output_position = argv.index('--output') | 73 output_position = argv.index('--output') |
72 source_files = argv[:relative_path_dirs_position] | 74 static_files_list_path = argv[static_files_list_position + 1] |
| 75 source_files = argv[:static_files_list_position] |
| 76 with open(static_files_list_path, 'r') as static_list_file: |
| 77 source_files.extend([line.rstrip('\n') for line in static_list_file.read
lines()]) |
73 relative_path_dirs = argv[relative_path_dirs_position + 1:images_position] | 78 relative_path_dirs = argv[relative_path_dirs_position + 1:images_position] |
74 image_dirs = argv[images_position + 1:output_position] | 79 image_dirs = argv[images_position + 1:output_position] |
75 return ParsedArgs(source_files, relative_path_dirs, image_dirs, argv[output_
position + 1]) | 80 return ParsedArgs(source_files, relative_path_dirs, image_dirs, argv[output_
position + 1]) |
76 | 81 |
77 | 82 |
78 def make_name_from_filename(filename): | 83 def make_name_from_filename(filename): |
79 return (filename.replace('/', '_') | 84 return (filename.replace('/', '_') |
80 .replace('\\', '_') | 85 .replace('\\', '_') |
81 .replace('-', '_') | 86 .replace('-', '_') |
82 .replace('.', '_')).upper() | 87 .replace('.', '_')).upper() |
83 | 88 |
84 | 89 |
85 def add_file_to_grd(grd_doc, relative_filename): | 90 def add_file_to_grd(grd_doc, relative_filename): |
86 includes_node = grd_doc.getElementsByTagName('includes')[0] | 91 includes_node = grd_doc.getElementsByTagName('includes')[0] |
87 includes_node.appendChild(grd_doc.createTextNode('\n ')) | 92 includes_node.appendChild(grd_doc.createTextNode('\n ')) |
88 | 93 |
89 new_include_node = grd_doc.createElement('include') | 94 new_include_node = grd_doc.createElement('include') |
90 new_include_node.setAttribute('name', make_name_from_filename(relative_filen
ame)) | 95 new_include_node.setAttribute('name', make_name_from_filename(relative_filen
ame)) |
91 new_include_node.setAttribute('file', relative_filename) | 96 new_include_node.setAttribute('file', relative_filename) |
92 new_include_node.setAttribute('type', 'BINDATA') | 97 new_include_node.setAttribute('type', 'BINDATA') |
93 includes_node.appendChild(new_include_node) | 98 includes_node.appendChild(new_include_node) |
94 | 99 |
95 | 100 |
96 def build_relative_filename(relative_path_dirs, filename): | 101 def build_relative_filename(relative_path_dirs, filename): |
97 for relative_path_dir in relative_path_dirs: | 102 for relative_path_dir in relative_path_dirs: |
98 index = filename.find(relative_path_dir) | 103 index = filename.find(relative_path_dir) |
99 if index == 0: | 104 if index == 0: |
100 return filename[len(relative_path_dir) + 1:] | 105 return filename[len(relative_path_dir) + 1:] |
101 return os.path.basename(filename) | 106 return path.basename(filename) |
102 | 107 |
103 | 108 |
104 def main(argv): | 109 def main(argv): |
105 parsed_args = parse_args(argv[1:]) | 110 parsed_args = parse_args(argv[1:]) |
106 | 111 |
107 doc = minidom.parseString(kGrdTemplate) | 112 doc = minidom.parseString(kGrdTemplate) |
108 output_directory = os.path.dirname(parsed_args.output_filename) | 113 output_directory = path.dirname(parsed_args.output_filename) |
109 | 114 |
110 try: | 115 try: |
111 os.makedirs(os.path.join(output_directory, 'Images')) | 116 os.makedirs(path.join(output_directory, 'Images')) |
112 except OSError, e: | 117 except OSError, e: |
113 if e.errno != errno.EEXIST: | 118 if e.errno != errno.EEXIST: |
114 raise e | 119 raise e |
115 | 120 |
116 written_filenames = set() | 121 written_filenames = set() |
117 for filename in parsed_args.source_files: | 122 for filename in parsed_args.source_files: |
118 relative_filename = build_relative_filename(parsed_args.relative_path_di
rs, filename) | 123 relative_filename = build_relative_filename(parsed_args.relative_path_di
rs, filename) |
119 # Avoid writing duplicate relative filenames. | 124 # Avoid writing duplicate relative filenames. |
120 if relative_filename in written_filenames: | 125 if relative_filename in written_filenames: |
121 continue | 126 continue |
122 written_filenames.add(relative_filename) | 127 written_filenames.add(relative_filename) |
123 target_dir = os.path.join(output_directory, os.path.dirname(relative_fil
ename)) | 128 target_dir = path.join(output_directory, path.dirname(relative_filename)
) |
124 if not os.path.exists(target_dir): | 129 if not path.exists(target_dir): |
125 os.makedirs(target_dir) | 130 os.makedirs(target_dir) |
126 shutil.copy(filename, target_dir) | 131 shutil.copy(filename, target_dir) |
127 add_file_to_grd(doc, relative_filename) | 132 add_file_to_grd(doc, relative_filename) |
128 | 133 |
129 for dirname in parsed_args.image_dirs: | 134 for dirname in parsed_args.image_dirs: |
130 for filename in os.listdir(dirname): | 135 for filename in os.listdir(dirname): |
131 if not filename.endswith('.png') and not filename.endswith('.gif'): | 136 if not filename.endswith('.png') and not filename.endswith('.gif'): |
132 continue | 137 continue |
133 shutil.copy(os.path.join(dirname, filename), | 138 shutil.copy(path.join(dirname, filename), |
134 os.path.join(output_directory, 'Images')) | 139 path.join(output_directory, 'Images')) |
135 add_file_to_grd(doc, os.path.join('Images', filename)) | 140 add_file_to_grd(doc, path.join('Images', filename)) |
136 | 141 |
137 with open(parsed_args.output_filename, 'w') as output_file: | 142 with open(parsed_args.output_filename, 'w') as output_file: |
138 output_file.write(doc.toxml(encoding='UTF-8')) | 143 output_file.write(doc.toxml(encoding='UTF-8')) |
139 | 144 |
140 | 145 |
141 if __name__ == '__main__': | 146 if __name__ == '__main__': |
142 sys.exit(main(sys.argv)) | 147 sys.exit(main(sys.argv)) |
OLD | NEW |