summaryrefslogtreecommitdiffstats
path: root/tools/grit
diff options
context:
space:
mode:
authortony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-13 20:33:58 +0000
committertony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-13 20:33:58 +0000
commiteb44eee2d7ea947e554abbbc8af3044e65c8c162 (patch)
treeb6eabab89d13fecab78eb9f64f1289bc802fc53b /tools/grit
parent335a82de3ca04230b393f1722e973a0f20e720b6 (diff)
downloadchromium_src-eb44eee2d7ea947e554abbbc8af3044e65c8c162.zip
chromium_src-eb44eee2d7ea947e554abbbc8af3044e65c8c162.tar.gz
chromium_src-eb44eee2d7ea947e554abbbc8af3044e65c8c162.tar.bz2
Assign resource ids from a central file.
This allows us to control resource id assignment across all grd files so we can avoid id collisions. BUG=48625 Review URL: http://codereview.chromium.org/2961002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52211 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
-rw-r--r--tools/grit/grit/format/rc_unittest.py4
-rw-r--r--tools/grit/grit/grd_reader.py16
-rw-r--r--tools/grit/grit/grd_reader_unittest.py50
-rw-r--r--tools/grit/grit/node/misc.py53
-rw-r--r--tools/grit/grit/tool/build.py13
-rwxr-xr-xtools/grit/grit_info.py3
-rw-r--r--tools/grit/resource_ids116
7 files changed, 245 insertions, 10 deletions
diff --git a/tools/grit/grit/format/rc_unittest.py b/tools/grit/grit/format/rc_unittest.py
index bf14dcb..36aac5b 100644
--- a/tools/grit/grit/format/rc_unittest.py
+++ b/tools/grit/grit/format/rc_unittest.py
@@ -1,5 +1,5 @@
#!/usr/bin/python2.4
-# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -189,7 +189,7 @@ END'''.strip())
<includes>
<include name="HTML_FILE1" flattenhtml="true" file="%s" type="BINDATA" />
</includes>''' % input_file), flexible_root = True)
- util.FixRootForUnittest(root, tempfile.gettempdir())
+ util.FixRootForUnittest(root, '.')
buf = StringIO.StringIO()
build.RcBuilder.ProcessNode(root, DummyOutput('rc_all', 'en', output_file),
diff --git a/tools/grit/grit/grd_reader.py b/tools/grit/grit/grd_reader.py
index 4f03fab1..308c1e7 100644
--- a/tools/grit/grit/grd_reader.py
+++ b/tools/grit/grit/grd_reader.py
@@ -1,5 +1,5 @@
#!/usr/bin/python2.4
-# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -14,6 +14,7 @@ import xml.sax.handler
from grit import exception
from grit.node import base
from grit.node import mapping
+from grit.node import misc
from grit import util
@@ -86,8 +87,8 @@ class GrdContentHandler(xml.sax.handler.ContentHandler):
pass
-def Parse(filename_or_stream, dir = None, flexible_root = False,
- stop_after=None, debug=False):
+def Parse(filename_or_stream, dir=None, flexible_root=False,
+ stop_after=None, debug=False, first_id_filename=None):
'''Parses a GRD file into a tree of nodes (from grit.node).
If flexible_root is False, the root node must be a <grit> element. Otherwise
@@ -103,12 +104,16 @@ def Parse(filename_or_stream, dir = None, flexible_root = False,
If 'debug' is true, lots of information about the parsing events will be
printed out during parsing of the file.
+ If first_id_filename is provided, then we use the provided path instead of
+ resources_id to gather the first id values for resources.
+
Args:
filename_or_stream: './bla.xml' (must be filename if dir is None)
dir: '.' or None (only if filename_or_stream is a filename)
flexible_root: True | False
stop_after: 'inputs'
debug: False
+ first_id_filename: None
Return:
Subclass of grit.node.base.Node
@@ -133,6 +138,11 @@ def Parse(filename_or_stream, dir = None, flexible_root = False,
dir = '.'
# Fix up the base_dir so it is relative to the input file.
handler.root.SetOwnDir(dir)
+
+ # Assign first ids to the nodes that don't have them.
+ if isinstance(handler.root, misc.GritNode) and first_id_filename != '':
+ handler.root.AssignFirstIds(filename_or_stream, first_id_filename)
+
return handler.root
diff --git a/tools/grit/grit/grd_reader_unittest.py b/tools/grit/grit/grd_reader_unittest.py
index 4612bbf..e1482e0 100644
--- a/tools/grit/grit/grd_reader_unittest.py
+++ b/tools/grit/grit/grd_reader_unittest.py
@@ -1,5 +1,5 @@
#!/usr/bin/python2.4
-# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -16,6 +16,7 @@ import StringIO
from grit import grd_reader
from grit import constants
from grit import util
+from grit.node import empty
class GrdReaderUnittest(unittest.TestCase):
@@ -97,6 +98,53 @@ class GrdReaderUnittest(unittest.TestCase):
'This is a very long line with no linebreaks yes yes it '
'stretches on and on and on!')
+ def testAssignFirstIds(self):
+ input = u'''<?xml version="1.0" encoding="UTF-8"?>
+<grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_dir=".">
+ <release seq="3">
+ <messages>
+ <message name="IDS_TEST" desc="test">
+ test
+ </message>
+ </messages>
+ </release>
+</grit>'''
+ pseudo_file = StringIO.StringIO(input)
+ root = grd_reader.Parse(pseudo_file, '.')
+ root.AssignFirstIds("../../chrome/app/generated_resources.grd", None)
+ messages_node = root.children[0].children[0]
+ self.failUnless(isinstance(messages_node, empty.MessagesNode))
+ self.failUnless(messages_node.attrs["first_id"] !=
+ empty.MessagesNode().DefaultAttributes()["first_id"])
+
+ def testAssignFirstIdsMultipleMessages(self):
+ """If there are multiple messages sections, the resource_ids file
+ needs to list multiple first_id values."""
+ input = u'''<?xml version="1.0" encoding="UTF-8"?>
+<grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_dir=".">
+ <release seq="3">
+ <messages>
+ <message name="IDS_TEST" desc="test">
+ test
+ </message>
+ </messages>
+ <messages>
+ <message name="IDS_TEST2" desc="test">
+ test2
+ </message>
+ </messages>
+ </release>
+</grit>'''
+ pseudo_file = StringIO.StringIO(input)
+ root = grd_reader.Parse(pseudo_file, '.')
+ root.AssignFirstIds("../../test.grd", "grit/test/data/resource_ids")
+ messages_node = root.children[0].children[0]
+ self.failUnless(isinstance(messages_node, empty.MessagesNode))
+ self.failUnless(messages_node.attrs["first_id"] == 100)
+ messages_node = root.children[0].children[1]
+ self.failUnless(isinstance(messages_node, empty.MessagesNode))
+ self.failUnless(messages_node.attrs["first_id"] == 10000)
+
if __name__ == '__main__':
unittest.main()
diff --git a/tools/grit/grit/node/misc.py b/tools/grit/grit/node/misc.py
index d41285e..62055a7 100644
--- a/tools/grit/grit/node/misc.py
+++ b/tools/grit/grit/node/misc.py
@@ -1,5 +1,5 @@
#!/usr/bin/python2.4
-# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -7,6 +7,7 @@
'''
import os.path
+import sys
from grit.node import base
from grit.node import message
@@ -237,6 +238,56 @@ class GritNode(base.Node):
def SetDefines(self, defines):
self.defines = defines
+ def AssignFirstIds(self, filename_or_stream, first_id_filename):
+ '''Assign first ids to each grouping node based on values from
+ tools/grit/resource_ids.'''
+ # If the input is a stream, then we're probably in a unit test and
+ # should skip this step.
+ if type(filename_or_stream) not in (str, unicode):
+ return
+
+ # By default, we use the the file resources_ids next to grit.py
+ # to determine what ids to assign to resources.
+ if not first_id_filename:
+ first_id_filename = os.path.join(os.path.dirname(
+ os.path.abspath(sys.argv[0])), 'resource_ids')
+
+ first_ids = None
+ from grit.node import empty
+ for node in self.inorder():
+ if isinstance(node, empty.GroupingNode):
+ # The checkout base directory is 2 directories up from grit.py.
+ src_root_dir = os.path.dirname(os.path.dirname(
+ os.path.dirname(os.path.abspath(sys.argv[0]))))
+
+ filename = os.path.abspath(filename_or_stream)[
+ len(src_root_dir) + 1:]
+ filename = filename.replace('\\', '/')
+ if not first_ids:
+ first_ids = eval(open(first_id_filename).read())
+
+ if node.attrs['first_id'] != '':
+ raise Exception("Don't set the first_id attribute, update "
+ "%s instead." % first_id_filename)
+
+ try:
+ id_list = first_ids[filename][node.name]
+ except KeyError, e:
+ print '-' * 78
+ print 'Resource id not set for %s (%s)!' % (filename, node.name)
+ print ('Please update %s to include an entry for %s. See the '
+ 'comments in resource_ids for information on why you need to '
+ 'update that file.' % (first_id_filename, filename))
+ print '-' * 78
+ raise e
+
+ try:
+ node.attrs['first_id'] = id_list.pop(0)
+ except IndexError, e:
+ raise Exception('Please update %s and add a first id for %s (%s).'
+ % (first_id_filename, filename, node.name))
+
+
class IdentifierNode(base.Node):
'''A node for specifying identifiers that should appear in the resource
header file, and be unique amongst all other resource identifiers, but don't
diff --git a/tools/grit/grit/tool/build.py b/tools/grit/grit/tool/build.py
index 89c2776..0a79769 100644
--- a/tools/grit/grit/tool/build.py
+++ b/tools/grit/grit/tool/build.py
@@ -56,6 +56,11 @@ Options:
-E NAME=VALUE Set environment variable NAME to VALUE (within grit).
+ -f FIRSTIDFILE Path to a python file that specifies the first id of
+ value to use for resources. Defaults to the file
+ resources_ids next to grit.py. Set to an empty string
+ if you don't want to use a first id file.
+
Conditional inclusion of resources only affects the output of files which
control which resources get linked into a binary, e.g. it affects .rc files
@@ -69,7 +74,8 @@ are exported to translation interchange files (e.g. XMB files), etc.
def Run(self, opts, args):
self.output_directory = '.'
- (own_opts, args) = getopt.getopt(args, 'o:D:E:')
+ first_id_filename = None
+ (own_opts, args) = getopt.getopt(args, 'o:D:E:f:')
for (key, val) in own_opts:
if key == '-o':
self.output_directory = val
@@ -79,6 +85,8 @@ are exported to translation interchange files (e.g. XMB files), etc.
elif key == '-E':
(env_name, env_value) = val.split('=')
os.environ[env_name] = env_value
+ elif key == '-f':
+ first_id_filename = val
if len(args):
print "This tool takes no tool-specific arguments."
@@ -90,7 +98,8 @@ are exported to translation interchange files (e.g. XMB files), etc.
self.VerboseOut('Output directory: %s (absolute path: %s)\n' %
(self.output_directory,
os.path.abspath(self.output_directory)))
- self.res = grd_reader.Parse(opts.input, debug=opts.extra_verbose)
+ self.res = grd_reader.Parse(opts.input, first_id_filename=first_id_filename,
+ debug=opts.extra_verbose)
self.res.SetDefines(self.defines)
self.res.RunGatherers(recursive = True)
self.Process()
diff --git a/tools/grit/grit_info.py b/tools/grit/grit_info.py
index 116ace0..c036aa0 100755
--- a/tools/grit/grit_info.py
+++ b/tools/grit/grit_info.py
@@ -62,7 +62,8 @@ def Inputs(filename):
# grit.
grit_root_dir = os.path.dirname(__file__)
for root, dirs, filenames in os.walk(grit_root_dir):
- grit_src = [os.path.join(root, f) for f in filenames if f.endswith('.py')]
+ grit_src = [os.path.join(root, f) for f in filenames
+ if f.endswith('.py') or f == 'resource_ids']
files.extend(grit_src)
return [f.replace('\\', '/') for f in files]
diff --git a/tools/grit/resource_ids b/tools/grit/resource_ids
new file mode 100644
index 0000000..a4db614
--- /dev/null
+++ b/tools/grit/resource_ids
@@ -0,0 +1,116 @@
+# Copyright (c) 2010 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# This file is used to assign starting resource ids for resources and strings
+# used by Chromium. This is done to ensure that resource ids are unique
+# across all the grd files. If you are adding a new grd file, please add
+# a new entry to this file.
+#
+# http://msdn.microsoft.com/en-us/library/t2zechd4(VS.71).aspx says that the
+# range for IDR_ is 1 to 28,671 and the range for IDS_ is 1 to 32,767 and
+# common convention starts practical use of IDs at 100 or 101.
+{
+ "chrome/browser/browser_resources.grd": {
+ "includes": [500],
+ },
+ "chrome/browser/resources/bookmark_manager_resources.grd": {
+ "includes": [1000],
+ },
+ "chrome/browser/resources/net_internals_resources.grd": {
+ "includes": [1500],
+ },
+ "chrome/browser/resources/shared_resources.grd": {
+ "includes": [2000],
+ },
+ "chrome/common/common_resources.grd": {
+ "includes": [2500],
+ },
+ "chrome/default_plugin/default_plugin_resources.grd": {
+ "includes": [3000],
+ },
+ "chrome/renderer/renderer_resources.grd": {
+ "includes": [3500],
+ },
+ "net/base/net_resources.grd": {
+ "includes": [4000],
+ },
+ "webkit/glue/webkit_resources.grd": {
+ "includes": [4500],
+ },
+ "webkit/tools/test_shell/test_shell_resources.grd": {
+ "includes": [5000],
+ },
+ "app/resources/app_resources.grd": {
+ "includes": [5500],
+ },
+ "chrome/app/theme/theme_resources.grd": {
+ "includes": [6000],
+ },
+ "chrome_frame/resources/chrome_frame_resources.grd": {
+ "includes": [6500],
+ },
+ # WebKit.grd can be in two different places depending on whether we are
+ # in a chromium checkout or a webkit-only checkout.
+ "third_party/WebKit/WebKit/chromium/WebKit.grd": {
+ "includes": [7000],
+ },
+ "WebKit.grd": {
+ "includes": [7000],
+ },
+
+ "app/resources/app_locale_settings.grd": {
+ "messages": [7500],
+ },
+ "chrome/app/resources/locale_settings.grd": {
+ "includes": [8000],
+ "messages": [8500],
+ },
+ # These each start with the same resource id because we only use one
+ # file for each build (cros, linux, mac, or win).
+ "chrome/app/resources/locale_settings_cros.grd": {
+ "messages": [9000],
+ },
+ "chrome/app/resources/locale_settings_linux.grd": {
+ "messages": [9000],
+ },
+ "chrome/app/resources/locale_settings_mac.grd": {
+ "messages": [9000],
+ },
+ "chrome/app/resources/locale_settings_win.grd": {
+ "messages": [9000],
+ },
+
+ "app/resources/app_strings.grd": {
+ "messages": [9500],
+ },
+ # Chromium strings and Google Chrome strings must start at the same id.
+ # We only use one file depending on whether we're building Chromium or
+ # Google Chrome.
+ "chrome/app/chromium_strings.grd": {
+ "messages": [10000],
+ },
+ "chrome/app/google_chrome_strings.grd": {
+ "messages": [10000],
+ },
+ # Leave lots of space for generated_resources since it has most of our
+ # strings.
+ "chrome/app/generated_resources.grd": {
+ "structures": [10500],
+ "messages": [11000],
+ },
+ "webkit/glue/inspector_strings.grd": {
+ "messages": [16000],
+ },
+ "webkit/glue/webkit_strings.grd": {
+ "messages": [16500],
+ },
+ "webkit/glue/devtools_strings.grd": {
+ "messages": [17000],
+ },
+
+ "chrome_frame/resources/chrome_frame_resources.grd": {
+ "includes": [17500],
+ "structures": [18000],
+ },
+}