OLD | NEW |
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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 '''Miscellaneous node types. | 6 '''Miscellaneous node types. |
7 ''' | 7 ''' |
8 | 8 |
9 import os.path | 9 import os.path |
| 10 import re |
10 import sys | 11 import sys |
11 | 12 |
12 from grit.node import base | 13 from grit.node import base |
13 from grit.node import message | 14 from grit.node import message |
14 | 15 |
15 from grit import exception | 16 from grit import exception |
16 from grit import constants | 17 from grit import constants |
17 from grit import util | 18 from grit import util |
18 | 19 |
19 import grit.format.rc_header | 20 import grit.format.rc_header |
20 | 21 |
21 | 22 |
| 23 def _ReadFirstIdsFromFile(filename, defines, src_root_dir): |
| 24 '''Read the starting resource id values from |filename|. We also |
| 25 expand variables of the form <(FOO) based on defines passed in on |
| 26 the command line.''' |
| 27 first_ids_dict = eval(open(filename).read()) |
| 28 |
| 29 def ReplaceVariable(matchobj): |
| 30 for key, value in defines.iteritems(): |
| 31 if matchobj.group(1) == key: |
| 32 value = os.path.abspath(value)[len(src_root_dir) + 1:] |
| 33 return value |
| 34 return '' |
| 35 |
| 36 renames = [] |
| 37 for grd_filename in first_ids_dict: |
| 38 new_grd_filename = re.sub(r'<\(([A-Za-z_]+)\)', ReplaceVariable, |
| 39 grd_filename) |
| 40 if new_grd_filename != grd_filename: |
| 41 new_grd_filename = new_grd_filename.replace('\\', '/') |
| 42 renames.append((grd_filename, new_grd_filename)) |
| 43 |
| 44 for grd_filename, new_grd_filename in renames: |
| 45 first_ids_dict[new_grd_filename] = first_ids_dict[grd_filename] |
| 46 del(first_ids_dict[grd_filename]) |
| 47 |
| 48 return first_ids_dict |
| 49 |
22 | 50 |
23 class IfNode(base.Node): | 51 class IfNode(base.Node): |
24 '''A node for conditional inclusion of resources. | 52 '''A node for conditional inclusion of resources. |
25 ''' | 53 ''' |
26 | 54 |
27 def _IsValidChild(self, child): | 55 def _IsValidChild(self, child): |
28 from grit.node import empty | 56 from grit.node import empty |
29 assert self.parent, '<if> node should never be root.' | 57 assert self.parent, '<if> node should never be root.' |
30 if isinstance(self.parent, empty.IncludesNode): | 58 if isinstance(self.parent, empty.IncludesNode): |
31 from grit.node import include | 59 from grit.node import include |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 else: | 288 else: |
261 return super(type(self), self).ItemFormatter(t) | 289 return super(type(self), self).ItemFormatter(t) |
262 | 290 |
263 def SetOutputContext(self, output_language, defines): | 291 def SetOutputContext(self, output_language, defines): |
264 self.output_language = output_language | 292 self.output_language = output_language |
265 self.defines = defines | 293 self.defines = defines |
266 | 294 |
267 def SetDefines(self, defines): | 295 def SetDefines(self, defines): |
268 self.defines = defines | 296 self.defines = defines |
269 | 297 |
270 def AssignFirstIds(self, filename_or_stream, first_id_filename): | 298 def AssignFirstIds(self, filename_or_stream, first_id_filename, defines): |
271 '''Assign first ids to each grouping node based on values from | 299 '''Assign first ids to each grouping node based on values from |
272 tools/grit/resource_ids.''' | 300 tools/grit/resource_ids.''' |
273 # If the input is a stream, then we're probably in a unit test and | 301 # If the input is a stream, then we're probably in a unit test and |
274 # should skip this step. | 302 # should skip this step. |
275 if type(filename_or_stream) not in (str, unicode): | 303 if type(filename_or_stream) not in (str, unicode): |
276 return | 304 return |
277 | 305 |
278 # By default, we use the the file resources_ids next to grit.py | 306 # By default, we use the the file resources_ids next to grit.py |
279 # to determine what ids to assign to resources. | 307 # to determine what ids to assign to resources. |
280 grit_root_dir = os.path.abspath(os.path.join(os.path.dirname( | 308 grit_root_dir = os.path.abspath(os.path.join(os.path.dirname( |
281 os.path.abspath(__file__)), '..', '..')) | 309 os.path.abspath(__file__)), '..', '..')) |
282 if not first_id_filename: | 310 if not first_id_filename: |
283 first_id_filename = os.path.join(grit_root_dir, 'resource_ids') | 311 first_id_filename = os.path.join(grit_root_dir, 'resource_ids') |
284 | 312 |
285 first_ids = None | 313 first_ids = None |
286 from grit.node import empty | 314 from grit.node import empty |
287 for node in self.inorder(): | 315 for node in self.inorder(): |
288 if isinstance(node, empty.GroupingNode): | 316 if isinstance(node, empty.GroupingNode): |
289 # The checkout base directory is 2 directories up from grit.py. | 317 # The checkout base directory is 2 directories up from grit.py. |
290 src_root_dir = os.path.dirname(os.path.dirname(grit_root_dir)) | 318 src_root_dir = os.path.dirname(os.path.dirname(grit_root_dir)) |
291 | 319 |
292 filename = os.path.abspath(filename_or_stream)[ | 320 filename = os.path.abspath(filename_or_stream)[ |
293 len(src_root_dir) + 1:] | 321 len(src_root_dir) + 1:] |
294 filename = filename.replace('\\', '/') | 322 filename = filename.replace('\\', '/') |
295 if not first_ids: | 323 if not first_ids: |
296 first_ids = eval(open(first_id_filename).read()) | 324 first_ids = _ReadFirstIdsFromFile(first_id_filename, defines, |
| 325 src_root_dir) |
297 | 326 |
298 if node.attrs['first_id'] != '': | 327 if node.attrs['first_id'] != '': |
299 raise Exception("Don't set the first_id attribute, update " | 328 raise Exception("Don't set the first_id attribute, update " |
300 "%s instead." % first_id_filename) | 329 "%s instead." % first_id_filename) |
301 | 330 |
302 try: | 331 try: |
303 id_list = first_ids[filename][node.name] | 332 id_list = first_ids[filename][node.name] |
304 except KeyError, e: | 333 except KeyError, e: |
305 print '-' * 78 | 334 print '-' * 78 |
306 print 'Resource id not set for %s (%s)!' % (filename, node.name) | 335 print 'Resource id not set for %s (%s)!' % (filename, node.name) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 by parameters of the same name. | 375 by parameters of the same name. |
347 ''' | 376 ''' |
348 node = IdentifierNode() | 377 node = IdentifierNode() |
349 node.StartParsing('identifier', parent) | 378 node.StartParsing('identifier', parent) |
350 node.HandleAttribute('name', name) | 379 node.HandleAttribute('name', name) |
351 node.HandleAttribute('id', id) | 380 node.HandleAttribute('id', id) |
352 node.HandleAttribute('comment', comment) | 381 node.HandleAttribute('comment', comment) |
353 node.EndParsing() | 382 node.EndParsing() |
354 return node | 383 return node |
355 Construct = staticmethod(Construct) | 384 Construct = staticmethod(Construct) |
OLD | NEW |