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 |
27 # avoid doing it more than once. | 30 # avoid doing it more than once. |
28 self._last_flat_filename = None | 31 self._last_flat_filename = None |
29 | 32 |
30 def _IsValidChild(self, child): | 33 def _IsValidChild(self, child): |
31 return False | 34 return False |
32 | 35 |
33 def _GetFlattenedData(self, allow_external_script=False): | 36 def _GetFlattenedData(self, allow_external_script=False): |
34 if not self._flattened_data: | 37 if not self._flattened_data: |
35 filename = self.ToRealPath(self.GetInputPath()) | 38 filename = self.ToRealPath(self.GetInputPath()) |
36 self._flattened_data = ( | 39 self._flattened_data = ( |
37 grit.format.html_inline.InlineToString(filename, self, | 40 grit.format.html_inline.InlineToString(filename, self, |
38 allow_external_script=allow_external_script)) | 41 allow_external_script=allow_external_script)) |
39 return self._flattened_data | 42 return self._flattened_data |
40 | 43 |
44 def _GzipString(self, data): | |
flackr
2016/05/13 23:02:17
This helper function (or functions if you follow m
smaier
2016/05/19 14:17:56
Done.
| |
45 if sys.platform == 'linux2': | |
flackr
2016/05/13 23:02:16
nit: I'd prefer to see either two gzip functions (
smaier
2016/05/19 14:17:56
Done.
| |
46 # Make call to linux's gzip to get access to --rsyncable option. This | |
47 # option makes updates much smaller - if one line is changed in the | |
48 # resource, it won't have to push the entire compressed resource with | |
49 # the update. Instead, --rsyncable breaks the file into small chunks, | |
50 # so that one doesn't affect the other in compression, and then only | |
51 # that chunk will have to be updated. | |
52 gzip_proc = subprocess.Popen(['gzip', '--stdout', '--rsyncable', | |
53 '--best', '--no-name'], | |
54 stdin=subprocess.PIPE, | |
55 stdout=subprocess.PIPE, | |
56 stderr=subprocess.PIPE) | |
57 data, stderr = gzip_proc.communicate(data) | |
58 if gzip_proc.returncode != 0: | |
59 raise subprocess.CalledProcessError(gzip_proc.returncode, 'gzip', | |
60 stderr) | |
61 else: | |
62 # Only expecting linux gzip calls to work: windows doesn't ship with | |
flackr
2016/05/13 23:02:17
This comment needs updating, or to be removed.
smaier
2016/05/19 14:17:56
Done.
| |
63 # gzip, and OSX's gzip does not have an --rsyncable option built in. | |
64 # Therefore, fall back into the built in python function. | |
65 gzip_output = StringIO.StringIO() | |
66 with gzip.GzipFile('', 'wb', 9, gzip_output, 0) as gzip_file: | |
67 gzip_file.write(data) | |
68 data = gzip_output.getvalue() | |
69 gzip_output.close() | |
70 return data | |
71 | |
41 def MandatoryAttributes(self): | 72 def MandatoryAttributes(self): |
42 return ['name', 'type', 'file'] | 73 return ['name', 'type', 'file'] |
43 | 74 |
44 def DefaultAttributes(self): | 75 def DefaultAttributes(self): |
45 return {'translateable' : 'true', | 76 return {'translateable' : 'true', |
46 'generateid': 'true', | 77 'generateid': 'true', |
47 'filenameonly': 'false', | 78 'filenameonly': 'false', |
48 'mkoutput': 'false', | 79 'mkoutput': 'false', |
49 'flattenhtml': 'false', | 80 'flattenhtml': 'false', |
50 'allowexternalscript': 'false', | 81 'allowexternalscript': 'false', |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 from grit.format import rc_header | 114 from grit.format import rc_header |
84 id_map = rc_header.GetIds(self.GetRoot()) | 115 id_map = rc_header.GetIds(self.GetRoot()) |
85 id = id_map[self.GetTextualIds()[0]] | 116 id = id_map[self.GetTextualIds()[0]] |
86 if self.attrs['flattenhtml'] == 'true': | 117 if self.attrs['flattenhtml'] == 'true': |
87 allow_external_script = self.attrs['allowexternalscript'] == 'true' | 118 allow_external_script = self.attrs['allowexternalscript'] == 'true' |
88 data = self._GetFlattenedData(allow_external_script=allow_external_script) | 119 data = self._GetFlattenedData(allow_external_script=allow_external_script) |
89 else: | 120 else: |
90 filename = self.ToRealPath(self.GetInputPath()) | 121 filename = self.ToRealPath(self.GetInputPath()) |
91 data = util.ReadFile(filename, util.BINARY) | 122 data = util.ReadFile(filename, util.BINARY) |
92 | 123 |
124 if self.attrs['type'] == 'GZIPPABLE_BINDATA': | |
flackr
2016/05/13 23:02:17
I don't think this should be a new type. The type
smaier
2016/05/19 14:17:56
Done.
| |
125 data = self._GzipString(data) | |
flackr
2016/05/13 23:02:17
This new behavior should have a test. There's some
smaier
2016/05/19 14:17:56
Let me know if the unit tests written are sufficie
| |
126 | |
93 # Include does not care about the encoding, because it only returns binary | 127 # Include does not care about the encoding, because it only returns binary |
94 # data. | 128 # data. |
95 return id, data | 129 return id, data |
96 | 130 |
97 def Process(self, output_dir): | 131 def Process(self, output_dir): |
98 """Rewrite file references to be base64 encoded data URLs. The new file | 132 """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.""" | 133 will be written to output_dir and the name of the new file is returned.""" |
100 filename = self.ToRealPath(self.GetInputPath()) | 134 filename = self.ToRealPath(self.GetInputPath()) |
101 flat_filename = os.path.join(output_dir, | 135 flat_filename = os.path.join(output_dir, |
102 self.attrs['name'] + '_' + os.path.basename(filename)) | 136 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) | 177 node.StartParsing('include', parent) |
144 node.HandleAttribute('name', name) | 178 node.HandleAttribute('name', name) |
145 node.HandleAttribute('type', type) | 179 node.HandleAttribute('type', type) |
146 node.HandleAttribute('file', file) | 180 node.HandleAttribute('file', file) |
147 node.HandleAttribute('translateable', translateable) | 181 node.HandleAttribute('translateable', translateable) |
148 node.HandleAttribute('filenameonly', filenameonly) | 182 node.HandleAttribute('filenameonly', filenameonly) |
149 node.HandleAttribute('mkoutput', mkoutput) | 183 node.HandleAttribute('mkoutput', mkoutput) |
150 node.HandleAttribute('relativepath', relativepath) | 184 node.HandleAttribute('relativepath', relativepath) |
151 node.EndParsing() | 185 node.EndParsing() |
152 return node | 186 return node |
OLD | NEW |