OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """Handling of the <include> element. | 6 """Handling of the <include> element. |
7 """ | 7 """ |
8 | 8 |
9 import gzip | |
9 import os | 10 import os |
11 import StringIO | |
12 import subprocess | |
13 import sys | |
10 | 14 |
15 from grit import util | |
11 import grit.format.html_inline | 16 import grit.format.html_inline |
17 import grit.format.rc | |
12 import grit.format.rc_header | 18 import grit.format.rc_header |
13 import grit.format.rc | |
14 | |
15 from grit.node import base | 19 from grit.node import base |
16 from grit import util | |
17 | 20 |
18 class IncludeNode(base.Node): | 21 class IncludeNode(base.Node): |
19 """An <include> element.""" | 22 """An <include> element.""" |
20 def __init__(self): | 23 def __init__(self): |
21 super(IncludeNode, self).__init__() | 24 super(IncludeNode, self).__init__() |
22 | 25 |
23 # Cache flattened data so that we don't flatten the same file | 26 # Cache flattened data so that we don't flatten the same file |
24 # multiple times. | 27 # multiple times. |
25 self._flattened_data = None | 28 self._flattened_data = None |
26 # Also keep track of the last filename we flattened to, so we can | 29 # Also keep track of the last filename we flattened to, so we can |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 from grit.format import rc_header | 86 from grit.format import rc_header |
84 id_map = rc_header.GetIds(self.GetRoot()) | 87 id_map = rc_header.GetIds(self.GetRoot()) |
85 id = id_map[self.GetTextualIds()[0]] | 88 id = id_map[self.GetTextualIds()[0]] |
86 if self.attrs['flattenhtml'] == 'true': | 89 if self.attrs['flattenhtml'] == 'true': |
87 allow_external_script = self.attrs['allowexternalscript'] == 'true' | 90 allow_external_script = self.attrs['allowexternalscript'] == 'true' |
88 data = self._GetFlattenedData(allow_external_script=allow_external_script) | 91 data = self._GetFlattenedData(allow_external_script=allow_external_script) |
89 else: | 92 else: |
90 filename = self.ToRealPath(self.GetInputPath()) | 93 filename = self.ToRealPath(self.GetInputPath()) |
91 data = util.ReadFile(filename, util.BINARY) | 94 data = util.ReadFile(filename, util.BINARY) |
92 | 95 |
96 if self.attrs['type'] == 'GZIPPABLE_BINDATA': | |
97 raw_data = data | |
98 try: | |
agrieve
2016/05/11 18:29:52
nit: extract this into a private helper function.
smaier
2016/05/13 15:27:25
Done.
| |
99 # Make call to OS's gzip to get access to --rsyncable option | |
agrieve
2016/05/11 18:29:52
nit: end with a period.
smaier
2016/05/13 15:27:25
Done.
| |
100 gzip_proc = subprocess.Popen(['gzip', '--stdout', '--rsyncable', | |
101 '--best', '--no-name'], | |
102 stdin=subprocess.PIPE, | |
103 stdout=subprocess.PIPE, | |
104 stderr=subprocess.PIPE) | |
105 data, stderr = gzip_proc.communicate(raw_data) | |
106 if gzip_proc.returncode != 0: | |
107 raise subprocess.CalledProcessError(gzip_proc.returncode, 'gzip', | |
108 stderr) | |
109 except Exception: | |
110 if sys.platform == 'linux2': | |
111 # Only expecting linux gzip calls to work: windows doesn't ship with | |
112 # gzip, and OSX's gzip does not have an --rsyncable option built in. | |
113 raise | |
agrieve
2016/05/11 18:29:52
nit: might be a good idea to store whether this wo
flackr
2016/05/11 18:54:33
I'm a little uncomfortable with maybe using the pl
smaier
2016/05/13 15:27:25
I've decided to move away from the fallback entire
| |
114 else: | |
115 # On all non-linux platforms, fall back to python's gzip. | |
116 gzip_output = StringIO.StringIO() | |
117 gzip.GzipFile('', 'wb', 9, gzip_output, 0).write(raw_data) | |
agrieve
2016/05/11 18:29:52
nit: I think GzipFile probably wants to be closed.
smaier
2016/05/13 15:27:25
Done.
| |
118 data = gzip_output.getvalue() | |
119 gzip_output.close() | |
120 | |
93 # Include does not care about the encoding, because it only returns binary | 121 # Include does not care about the encoding, because it only returns binary |
94 # data. | 122 # data. |
95 return id, data | 123 return id, data |
96 | 124 |
97 def Process(self, output_dir): | 125 def Process(self, output_dir): |
98 """Rewrite file references to be base64 encoded data URLs. The new file | 126 """Rewrite file references to be base64 encoded data URLs. The new file |
99 will be written to output_dir and the name of the new file is returned.""" | 127 will be written to output_dir and the name of the new file is returned.""" |
100 filename = self.ToRealPath(self.GetInputPath()) | 128 filename = self.ToRealPath(self.GetInputPath()) |
101 flat_filename = os.path.join(output_dir, | 129 flat_filename = os.path.join(output_dir, |
102 self.attrs['name'] + '_' + os.path.basename(filename)) | 130 self.attrs['name'] + '_' + os.path.basename(filename)) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 node.StartParsing('include', parent) | 171 node.StartParsing('include', parent) |
144 node.HandleAttribute('name', name) | 172 node.HandleAttribute('name', name) |
145 node.HandleAttribute('type', type) | 173 node.HandleAttribute('type', type) |
146 node.HandleAttribute('file', file) | 174 node.HandleAttribute('file', file) |
147 node.HandleAttribute('translateable', translateable) | 175 node.HandleAttribute('translateable', translateable) |
148 node.HandleAttribute('filenameonly', filenameonly) | 176 node.HandleAttribute('filenameonly', filenameonly) |
149 node.HandleAttribute('mkoutput', mkoutput) | 177 node.HandleAttribute('mkoutput', mkoutput) |
150 node.HandleAttribute('relativepath', relativepath) | 178 node.HandleAttribute('relativepath', relativepath) |
151 node.EndParsing() | 179 node.EndParsing() |
152 return node | 180 return node |
OLD | NEW |