Chromium Code Reviews| Index: tools/grit/grit/format/gzip_string.py |
| diff --git a/tools/grit/grit/format/gzip_string.py b/tools/grit/grit/format/gzip_string.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bd1e726a6efd9661f75d031abccf7c46ecaedbde |
| --- /dev/null |
| +++ b/tools/grit/grit/format/gzip_string.py |
| @@ -0,0 +1,40 @@ |
| +# Copyright (c) 2016 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. |
| +"""Provides gzip utilities for strings. |
| +""" |
| +import gzip |
| +import StringIO |
|
agrieve
2016/05/27 17:40:00
nit: cStringIO is probably faster.
smaier
2016/05/27 17:48:47
Done.
|
| +import subprocess |
| + |
| +def GzipStringRsyncable(data): |
| + # Make call to host system's gzip to get access to --rsyncable option. This |
| + # option makes updates much smaller - if one line is changed in the resource, |
| + # it won't have to push the entire compressed resource with the update. |
| + # Instead, --rsyncable breaks the file into small chunks, so that one doesn't |
| + # affect the other in compression, and then only that chunk will have to be |
| + # updated. |
| + gzip_proc = subprocess.Popen(['gzip', '--stdout', '--rsyncable', |
| + '--best', '--no-name'], |
| + stdin=subprocess.PIPE, |
| + stdout=subprocess.PIPE, |
| + stderr=subprocess.PIPE) |
| + data, stderr = gzip_proc.communicate(data) |
| + if gzip_proc.returncode != 0: |
| + raise subprocess.CalledProcessError(gzip_proc.returncode, 'gzip', |
| + stderr) |
| + return data |
| + |
| + |
| +def GzipString(data): |
| + # Allowing for option to use Python's gzip: Windows doesn't ship with gzip, |
| + # and OSX's gzip does not have an --rsyncable option built in. Although this |
| + # is not as good as --rsyncable, it is an option for the systems that do not |
| + # have --rsyncable. This function should be avoided for release builds. |
| + gzip_output = StringIO.StringIO() |
| + with gzip.GzipFile(mode='wb', compresslevel=9, fileobj=gzip_output, |
| + mtime=0) as gzip_file: |
| + gzip_file.write(data) |
| + data = gzip_output.getvalue() |
| + gzip_output.close() |
| + return data |