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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 <output filename="devtools_resources.pak" type="data_package" /> | 51 <output filename="devtools_resources.pak" type="data_package" /> |
52 </outputs> | 52 </outputs> |
53 <release seq="1"> | 53 <release seq="1"> |
54 <includes></includes> | 54 <includes></includes> |
55 </release> | 55 </release> |
56 </grit> | 56 </grit> |
57 ''' | 57 ''' |
58 | 58 |
59 | 59 |
60 class ParsedArgs: | 60 class ParsedArgs: |
61 def __init__(self, source_files, relative_path_dir, image_dirs, output_filen
ame): | 61 def __init__(self, source_files, relative_path_dirs, image_dirs, output_file
name): |
62 self.source_files = source_files | 62 self.source_files = source_files |
63 self.relative_path_dir = relative_path_dir | 63 self.relative_path_dirs = relative_path_dirs |
64 self.image_dirs = image_dirs | 64 self.image_dirs = image_dirs |
65 self.output_filename = output_filename | 65 self.output_filename = output_filename |
66 | 66 |
67 | 67 |
68 def parse_args(argv): | 68 def parse_args(argv): |
69 relative_path_dir_position = argv.index('--relative_path_dir') | 69 relative_path_dirs_position = argv.index('--relative_path_dirs') |
70 images_position = argv.index('--images') | 70 images_position = argv.index('--images') |
71 output_position = argv.index('--output') | 71 output_position = argv.index('--output') |
72 source_files = argv[:relative_path_dir_position] | 72 source_files = argv[:relative_path_dirs_position] |
73 relative_path_dir = argv[relative_path_dir_position + 1] | 73 relative_path_dirs = argv[relative_path_dirs_position + 1:images_position] |
74 image_dirs = argv[images_position + 1:output_position] | 74 image_dirs = argv[images_position + 1:output_position] |
75 return ParsedArgs(source_files, relative_path_dir, image_dirs, argv[output_p
osition + 1]) | 75 return ParsedArgs(source_files, relative_path_dirs, image_dirs, argv[output_
position + 1]) |
76 | 76 |
77 | 77 |
78 def make_name_from_filename(filename): | 78 def make_name_from_filename(filename): |
79 return (filename.replace('/', '_') | 79 return (filename.replace('/', '_') |
80 .replace('\\', '_') | 80 .replace('\\', '_') |
| 81 .replace('-', '_') |
81 .replace('.', '_')).upper() | 82 .replace('.', '_')).upper() |
82 | 83 |
83 | 84 |
84 def add_file_to_grd(grd_doc, relative_filename): | 85 def add_file_to_grd(grd_doc, relative_filename): |
85 includes_node = grd_doc.getElementsByTagName('includes')[0] | 86 includes_node = grd_doc.getElementsByTagName('includes')[0] |
86 includes_node.appendChild(grd_doc.createTextNode('\n ')) | 87 includes_node.appendChild(grd_doc.createTextNode('\n ')) |
87 | 88 |
88 new_include_node = grd_doc.createElement('include') | 89 new_include_node = grd_doc.createElement('include') |
89 new_include_node.setAttribute('name', make_name_from_filename(relative_filen
ame)) | 90 new_include_node.setAttribute('name', make_name_from_filename(relative_filen
ame)) |
90 new_include_node.setAttribute('file', relative_filename) | 91 new_include_node.setAttribute('file', relative_filename) |
91 new_include_node.setAttribute('type', 'BINDATA') | 92 new_include_node.setAttribute('type', 'BINDATA') |
92 includes_node.appendChild(new_include_node) | 93 includes_node.appendChild(new_include_node) |
93 | 94 |
94 | 95 |
95 def build_relative_filename(relative_path_dir, filename): | 96 def build_relative_filename(relative_path_dirs, filename): |
96 if relative_path_dir: | 97 for relative_path_dir in relative_path_dirs: |
97 index = filename.find(relative_path_dir) | 98 index = filename.find(relative_path_dir) |
98 if index == 0: | 99 if index == 0: |
99 return filename[len(relative_path_dir) + 1:] | 100 return filename[len(relative_path_dir) + 1:] |
100 return os.path.basename(filename) | 101 return os.path.basename(filename) |
101 return filename | |
102 | 102 |
103 | 103 |
104 def main(argv): | 104 def main(argv): |
105 parsed_args = parse_args(argv[1:]) | 105 parsed_args = parse_args(argv[1:]) |
106 | 106 |
107 doc = minidom.parseString(kGrdTemplate) | 107 doc = minidom.parseString(kGrdTemplate) |
108 output_directory = os.path.dirname(parsed_args.output_filename) | 108 output_directory = os.path.dirname(parsed_args.output_filename) |
109 | 109 |
110 try: | 110 try: |
111 os.makedirs(os.path.join(output_directory, 'Images')) | 111 os.makedirs(os.path.join(output_directory, 'Images')) |
112 except OSError, e: | 112 except OSError, e: |
113 if e.errno != errno.EEXIST: | 113 if e.errno != errno.EEXIST: |
114 raise e | 114 raise e |
115 | 115 |
116 for filename in parsed_args.source_files: | 116 for filename in parsed_args.source_files: |
117 relative_filename = build_relative_filename(parsed_args.relative_path_di
r, filename) | 117 relative_filename = build_relative_filename(parsed_args.relative_path_di
rs, filename) |
118 target_dir = os.path.join(output_directory, os.path.dirname(relative_fil
ename)) | 118 target_dir = os.path.join(output_directory, os.path.dirname(relative_fil
ename)) |
119 if not os.path.exists(target_dir): | 119 if not os.path.exists(target_dir): |
120 os.makedirs(target_dir) | 120 os.makedirs(target_dir) |
121 shutil.copy(filename, target_dir) | 121 shutil.copy(filename, target_dir) |
122 add_file_to_grd(doc, relative_filename) | 122 add_file_to_grd(doc, relative_filename) |
123 | 123 |
124 for dirname in parsed_args.image_dirs: | 124 for dirname in parsed_args.image_dirs: |
125 for filename in os.listdir(dirname): | 125 for filename in os.listdir(dirname): |
126 if not filename.endswith('.png') and not filename.endswith('.gif'): | 126 if not filename.endswith('.png') and not filename.endswith('.gif'): |
127 continue | 127 continue |
128 shutil.copy(os.path.join(dirname, filename), | 128 shutil.copy(os.path.join(dirname, filename), |
129 os.path.join(output_directory, 'Images')) | 129 os.path.join(output_directory, 'Images')) |
130 add_file_to_grd(doc, os.path.join('Images', filename)) | 130 add_file_to_grd(doc, os.path.join('Images', filename)) |
131 | 131 |
132 with open(parsed_args.output_filename, 'w') as output_file: | 132 with open(parsed_args.output_filename, 'w') as output_file: |
133 output_file.write(doc.toxml(encoding='UTF-8')) | 133 output_file.write(doc.toxml(encoding='UTF-8')) |
134 | 134 |
135 | 135 |
136 if __name__ == '__main__': | 136 if __name__ == '__main__': |
137 sys.exit(main(sys.argv)) | 137 sys.exit(main(sys.argv)) |
OLD | NEW |