Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: chrome/installer/util/prebuild/create_string_rc.py

Issue 8680018: Fix python scripts in src/chrome/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: copyright Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/net/PRESUBMIT.py ('k') | chrome/nacl/nacl_helper_bootstrap_munge_phdr.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/env python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """This script generates an rc file and header (setup_strings.{rc,h}) to be 6 """This script generates an rc file and header (setup_strings.{rc,h}) to be
7 included in setup.exe. The rc file includes translations for strings pulled 7 included in setup.exe. The rc file includes translations for strings pulled
8 from generated_resource.grd and the localized .xtb files. 8 from generated_resource.grd and the localized .xtb files.
9 9
10 The header file includes IDs for each string, but also has values to allow 10 The header file includes IDs for each string, but also has values to allow
11 getting a string based on a language offset. For example, the header file 11 getting a string based on a language offset. For example, the header file
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 'IDS_INSTALL_MULTI_INSTALLATION_EXISTS', 69 'IDS_INSTALL_MULTI_INSTALLATION_EXISTS',
70 'IDS_INSTALL_READY_MODE_REQUIRES_CHROME', 70 'IDS_INSTALL_READY_MODE_REQUIRES_CHROME',
71 'IDS_INSTALL_INCONSISTENT_UPDATE_POLICY', 71 'IDS_INSTALL_INCONSISTENT_UPDATE_POLICY',
72 'IDS_OEM_MAIN_SHORTCUT_NAME', 72 'IDS_OEM_MAIN_SHORTCUT_NAME',
73 'IDS_SHORTCUT_TOOLTIP', 73 'IDS_SHORTCUT_TOOLTIP',
74 ] 74 ]
75 75
76 # The ID of the first resource string. 76 # The ID of the first resource string.
77 kFirstResourceID = 1600 77 kFirstResourceID = 1600
78 78
79
79 class TranslationStruct: 80 class TranslationStruct:
80 """A helper struct that holds information about a single translation.""" 81 """A helper struct that holds information about a single translation."""
81 def __init__(self, resource_id_str, language, translation): 82 def __init__(self, resource_id_str, language, translation):
82 self.resource_id_str = resource_id_str 83 self.resource_id_str = resource_id_str
83 self.language = language 84 self.language = language
84 self.translation = translation 85 self.translation = translation
85 86
86 def __cmp__(self, other): 87 def __cmp__(self, other):
87 """Allow TranslationStructs to be sorted by id.""" 88 """Allow TranslationStructs to be sorted by id."""
88 id_result = cmp(self.resource_id_str, other.resource_id_str) 89 id_result = cmp(self.resource_id_str, other.resource_id_str)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 for i, string_id in enumerate(kStringIds): 146 for i, string_id in enumerate(kStringIds):
146 translated_string = translation_nodes.get(translation_ids[i], 147 translated_string = translation_nodes.get(translation_ids[i],
147 message_texts[i]) 148 message_texts[i])
148 translated_strings.append(TranslationStruct(string_id, 149 translated_strings.append(TranslationStruct(string_id,
149 language, 150 language,
150 translated_string)) 151 translated_string))
151 152
152 translated_strings.sort() 153 translated_strings.sort()
153 return translated_strings 154 return translated_strings
154 155
156
155 def WriteRCFile(translated_strings, out_filename): 157 def WriteRCFile(translated_strings, out_filename):
156 """Writes a resource (rc) file with all the language strings provided in 158 """Writes a resource (rc) file with all the language strings provided in
157 |translated_strings|.""" 159 |translated_strings|."""
158 kHeaderText = ( 160 kHeaderText = (
159 u'#include "%s.h"\n\n' 161 u'#include "%s.h"\n\n'
160 u'STRINGTABLE\n' 162 u'STRINGTABLE\n'
161 u'BEGIN\n' 163 u'BEGIN\n'
162 ) % os.path.basename(out_filename) 164 ) % os.path.basename(out_filename)
163 kFooterText = ( 165 kFooterText = (
164 u'END\n' 166 u'END\n'
165 ) 167 )
166 lines = [kHeaderText] 168 lines = [kHeaderText]
167 for translation_struct in translated_strings: 169 for translation_struct in translated_strings:
168 # Escape special characters for the rc file. 170 # Escape special characters for the rc file.
169 translation = (translation_struct.translation.replace('"', '""') 171 translation = (translation_struct.translation.replace('"', '""')
170 .replace('\t', '\\t') 172 .replace('\t', '\\t')
171 .replace('\n', '\\n')) 173 .replace('\n', '\\n'))
172 lines.append(u' %s "%s"\n' % (translation_struct.resource_id_str + '_' 174 lines.append(u' %s "%s"\n' % (translation_struct.resource_id_str + '_'
173 + translation_struct.language, 175 + translation_struct.language,
174 translation)) 176 translation))
175 lines.append(kFooterText) 177 lines.append(kFooterText)
176 outfile = open(out_filename + '.rc', 'wb') 178 outfile = open(out_filename + '.rc', 'wb')
177 outfile.write(''.join(lines).encode('utf-16')) 179 outfile.write(''.join(lines).encode('utf-16'))
178 outfile.close() 180 outfile.close()
179 181
182
180 def WriteHeaderFile(translated_strings, out_filename): 183 def WriteHeaderFile(translated_strings, out_filename):
181 """Writes a .h file with resource ids. This file can be included by the 184 """Writes a .h file with resource ids. This file can be included by the
182 executable to refer to identifiers.""" 185 executable to refer to identifiers."""
183 lines = [] 186 lines = []
184 do_languages_lines = ['#define DO_LANGUAGES'] 187 do_languages_lines = ['#define DO_LANGUAGES']
185 188
186 # Write the values for how the languages ids are offset. 189 # Write the values for how the languages ids are offset.
187 seen_languages = set() 190 seen_languages = set()
188 offset_id = 0 191 offset_id = 0
189 for translation_struct in translated_strings: 192 for translation_struct in translated_strings:
(...skipping 22 matching lines...) Expand all
212 translated_strings[0].language)) 215 translated_strings[0].language))
213 216
214 outfile = open(out_filename + '.h', 'wb') 217 outfile = open(out_filename + '.h', 'wb')
215 outfile.write('\n'.join(lines)) 218 outfile.write('\n'.join(lines))
216 outfile.write('\n#ifndef RC_INVOKED\n') 219 outfile.write('\n#ifndef RC_INVOKED\n')
217 outfile.write(' \\\n'.join(do_languages_lines)) 220 outfile.write(' \\\n'.join(do_languages_lines))
218 # .rc files must end in a new line 221 # .rc files must end in a new line
219 outfile.write('\n#endif // ndef RC_INVOKED\n') 222 outfile.write('\n#endif // ndef RC_INVOKED\n')
220 outfile.close() 223 outfile.close()
221 224
225
222 def main(argv): 226 def main(argv):
227 # TODO: Use optparse to parse command line flags.
228 if len(argv) < 2:
229 print 'Usage:\n %s <output_directory> [branding]' % argv[0]
230 return 1
223 branding = '' 231 branding = ''
224 if (len(sys.argv) > 2): 232 if (len(sys.argv) > 2):
225 branding = argv[2] 233 branding = argv[2]
226 translated_strings = CollectTranslatedStrings(branding) 234 translated_strings = CollectTranslatedStrings(branding)
227 kFilebase = os.path.join(argv[1], 'installer_util_strings') 235 kFilebase = os.path.join(argv[1], 'installer_util_strings')
228 WriteRCFile(translated_strings, kFilebase) 236 WriteRCFile(translated_strings, kFilebase)
229 WriteHeaderFile(translated_strings, kFilebase) 237 WriteHeaderFile(translated_strings, kFilebase)
238 return 0
239
230 240
231 if '__main__' == __name__: 241 if '__main__' == __name__:
232 if len(sys.argv) < 2: 242 sys.exit(main(sys.argv))
233 print 'Usage:\n %s <output_directory> [branding]' % sys.argv[0]
234 sys.exit(1)
235 # Use optparse to parse command line flags.
236 main(sys.argv)
OLDNEW
« no previous file with comments | « chrome/common/net/PRESUBMIT.py ('k') | chrome/nacl/nacl_helper_bootstrap_munge_phdr.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698