summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-14 21:36:56 +0000
committerakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-14 21:36:56 +0000
commit88aad08a78ead47d9c54b4f87d3e52ed5de1070f (patch)
treeaa94db3424593edcdc55fadb3943ff90f94720ff /tools
parente689f18beeaf018bfeef7458029905d21801c403 (diff)
downloadchromium_src-88aad08a78ead47d9c54b4f87d3e52ed5de1070f.zip
chromium_src-88aad08a78ead47d9c54b4f87d3e52ed5de1070f.tar.gz
chromium_src-88aad08a78ead47d9c54b4f87d3e52ed5de1070f.tar.bz2
[grit] Avoid intermediate files for HTML inlining when building .pak files
This avoids problems where multiple invocations of grit try to write to the same intermediate file. BUG=69633 TEST=Manually Review URL: http://codereview.chromium.org/6245002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71491 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools')
-rwxr-xr-xtools/grit/grit/format/data_pack.py2
-rwxr-xr-xtools/grit/grit/format/html_inline.py66
-rw-r--r--tools/grit/grit/format/rc.py2
-rw-r--r--tools/grit/grit/node/include.py47
-rw-r--r--tools/grit/grit/node/message.py2
-rwxr-xr-xtools/grit/grit_info.py2
6 files changed, 88 insertions, 33 deletions
diff --git a/tools/grit/grit/format/data_pack.py b/tools/grit/grit/format/data_pack.py
index 664cbaec..20a8f73 100755
--- a/tools/grit/grit/format/data_pack.py
+++ b/tools/grit/grit/format/data_pack.py
@@ -29,7 +29,7 @@ class DataPack(interface.ItemFormatter):
nodes = DataPack.GetDataNodes(item)
data = {}
for node in nodes:
- id, value = node.GetDataPackPair(output_dir, lang)
+ id, value = node.GetDataPackPair(lang)
data[id] = value
return DataPack.WriteDataPack(data)
diff --git a/tools/grit/grit/format/html_inline.py b/tools/grit/grit/format/html_inline.py
index 9979bca..c5cb074 100755
--- a/tools/grit/grit/format/html_inline.py
+++ b/tools/grit/grit/format/html_inline.py
@@ -69,19 +69,30 @@ def SrcInlineAsDataURL(src_match, base_path, distribution, inlined_files):
prefix = src_match.string[src_match.start():src_match.start('filename')-1]
return "%s\"data:%s;base64,%s\"" % (prefix, mimetype, inline_data)
-def InlineFile(input_filename, output_filename, grd_node):
- """Inlines the resources in a specified file.
+
+class InlinedData:
+ """Helper class holding the results from DoInline().
+
+ Holds the inlined data and the set of filenames of all the inlined
+ files.
+ """
+ def __init__(self, inlined_data, inlined_files):
+ self.inlined_data = inlined_data
+ self.inlined_files = inlined_files
+
+def DoInline(input_filename, grd_node):
+ """Helper function that inlines the resources in a specified file.
Reads input_filename, finds all the src attributes and attempts to
- inline the files they are referring to, then writes the result
- to output_filename.
+ inline the files they are referring to, then returns the result and
+ the set of inlined files.
Args:
input_filename: name of file to read in
- output_filename: name of file to be written to
grd_node: html node from the grd file for this include tag
Returns:
- a set of filenames of all the inlined files
+ a tuple of the inlined data as a string and the set of filenames
+ of all the inlined files
"""
input_filepath = os.path.dirname(input_filename)
@@ -198,16 +209,45 @@ def InlineFile(input_filename, output_filename, grd_node):
flat_text = re.sub('<link rel="icon".+?href="(?P<filename>[^"\']*)"',
SrcReplace,
flat_text)
- if output_filename:
- out_file = open(output_filename, 'wb')
- out_file.writelines(flat_text)
- out_file.close()
- return inlined_files
+
+ return InlinedData(flat_text, inlined_files)
+
+
+def InlineToString(input_filename, grd_node):
+ """Inlines the resources in a specified file and returns it as a string.
+
+ Args:
+ input_filename: name of file to read in
+ grd_node: html node from the grd file for this include tag
+ Returns:
+ the inlined data as a string
+ """
+ return DoInline(input_filename, grd_node).inlined_data
+
+
+def InlineToFile(input_filename, output_filename, grd_node):
+ """Inlines the resources in a specified file and writes it.
+
+ Reads input_filename, finds all the src attributes and attempts to
+ inline the files they are referring to, then writes the result
+ to output_filename.
+
+ Args:
+ input_filename: name of file to read in
+ output_filename: name of file to be written to
+ grd_node: html node from the grd file for this include tag
+ Returns:
+ a set of filenames of all the inlined files
+ """
+ inlined_data = InlineToString(input_filename, grd_node)
+ out_file = open(output_filename, 'wb')
+ out_file.writelines(inlined_data)
+ out_file.close()
def GetResourceFilenames(filename):
"""For a grd file, returns a set of all the files that would be inline."""
- return InlineFile(filename, None, None)
+ return DoInline(filename, None).inlined_files
def main():
@@ -215,7 +255,7 @@ def main():
print "Flattens a HTML file by inlining its external resources.\n"
print "html_inline.py inputfile outputfile"
else:
- InlineFile(sys.argv[1], sys.argv[2], None)
+ InlineToFile(sys.argv[1], sys.argv[2], None)
if __name__ == '__main__':
main()
diff --git a/tools/grit/grit/format/rc.py b/tools/grit/grit/format/rc.py
index 13834b7..3e999c4 100644
--- a/tools/grit/grit/format/rc.py
+++ b/tools/grit/grit/format/rc.py
@@ -424,6 +424,8 @@ class RcInclude(interface.ItemFormatter):
# if needed (e.g. if it is an HTML file include).
filename = os.path.abspath(item.FileForLanguage(lang, output_dir))
if self.flatten_html:
+ # TODO(akalin): Flatten to a subdirectory of output_dir, lest we
+ # run into bugs like http://crbug.com/69633.
item.Flatten(output_dir)
# The flattened file is in the output dir.
filename = os.path.basename(filename)
diff --git a/tools/grit/grit/node/include.py b/tools/grit/grit/node/include.py
index 4f96cdf..cb64866 100644
--- a/tools/grit/grit/node/include.py
+++ b/tools/grit/grit/node/include.py
@@ -20,13 +20,23 @@ class IncludeNode(base.Node):
def __init__(self):
base.Node.__init__(self)
- # Keep track of whether we've flattened the file or not. We don't
- # want to flatten the same file multiple times.
- self._is_flattened = False
+ # Cache flattened data so that we don't flatten the same file
+ # multiple times.
+ self._flattened_data = None
+ # Also keep track of the last filename we flattened to, so we can
+ # avoid doing it more than once.
+ self._last_flat_filename = None
def _IsValidChild(self, child):
return False
+ def _GetFlattenedData(self):
+ if not self._flattened_data:
+ filename = self.FilenameToOpen()
+ self._flattened_data = (
+ grit.format.html_inline.InlineToString(filename, self))
+ return self._flattened_data
+
def MandatoryAttributes(self):
return ['name', 'type', 'file']
@@ -62,33 +72,36 @@ class IncludeNode(base.Node):
'''
return self.FilenameToOpen()
- def GetDataPackPair(self, output_dir, lang):
+ def GetDataPackPair(self, lang):
'''Returns a (id, string) pair that represents the resource id and raw
bytes of the data. This is used to generate the data pack data file.
'''
from grit.format import rc_header
id_map = rc_header.Item.tids_
id = id_map[self.GetTextualIds()[0]]
- filename = self.FilenameToOpen()
if self.attrs['flattenhtml'] == 'true':
- self.Flatten(output_dir)
- # The flattened file is in the output dir.
- filename = os.path.join(output_dir, os.path.split(filename)[1])
-
- file = open(filename, 'rb')
- data = file.read()
- file.close()
+ data = self._GetFlattenedData()
+ else:
+ filename = self.FilenameToOpen()
+ infile = open(filename, 'rb')
+ data = infile.read()
+ infile.close()
return id, data
def Flatten(self, output_dir):
- if self._is_flattened:
- return
-
filename = self.FilenameToOpen()
flat_filename = os.path.join(output_dir, os.path.split(filename)[1])
- grit.format.html_inline.InlineFile(filename, flat_filename, self)
- self._is_flattened = True
+
+ if self._last_flat_filename == flat_filename:
+ return
+
+ outfile = open(flat_filename, 'wb')
+ outfile.write(self._GetFlattenedData())
+ outfile.close()
+
+ self._last_flat_filename = flat_filename
+
def GetHtmlResourceFilenames(self):
"""Returns a set of all filenames inlined by this file."""
diff --git a/tools/grit/grit/node/message.py b/tools/grit/grit/node/message.py
index 2dfd0ec..a48a645 100644
--- a/tools/grit/grit/node/message.py
+++ b/tools/grit/grit/node/message.py
@@ -186,7 +186,7 @@ class MessageNode(base.ContentNode):
else:
return self.attrs['offset']
- def GetDataPackPair(self, output_dir, lang):
+ def GetDataPackPair(self, lang):
'''Returns a (id, string) pair that represents the string id and the string
in utf8. This is used to generate the data pack data file.
'''
diff --git a/tools/grit/grit_info.py b/tools/grit/grit_info.py
index 042f457..cf80954 100755
--- a/tools/grit/grit_info.py
+++ b/tools/grit/grit_info.py
@@ -109,7 +109,7 @@ def main(argv):
outputs = [posixpath.join(args[0], f) for f in Outputs(f, defines)]
print '\n'.join(outputs)
else:
- PringUsage()
+ PrintUsage()
return 1
return 0