OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 """Provides gzip utilities for strings. |
| 5 """ |
| 6 import gzip |
| 7 import cStringIO |
| 8 import subprocess |
| 9 |
| 10 def GzipStringRsyncable(data): |
| 11 # Make call to host system's gzip to get access to --rsyncable option. This |
| 12 # option makes updates much smaller - if one line is changed in the resource, |
| 13 # it won't have to push the entire compressed resource with the update. |
| 14 # Instead, --rsyncable breaks the file into small chunks, so that one doesn't |
| 15 # affect the other in compression, and then only that chunk will have to be |
| 16 # updated. |
| 17 gzip_proc = subprocess.Popen(['gzip', '--stdout', '--rsyncable', |
| 18 '--best', '--no-name'], |
| 19 stdin=subprocess.PIPE, |
| 20 stdout=subprocess.PIPE, |
| 21 stderr=subprocess.PIPE) |
| 22 data, stderr = gzip_proc.communicate(data) |
| 23 if gzip_proc.returncode != 0: |
| 24 raise subprocess.CalledProcessError(gzip_proc.returncode, 'gzip', |
| 25 stderr) |
| 26 return data |
| 27 |
| 28 |
| 29 def GzipString(data): |
| 30 # Allowing for option to use Python's gzip: Windows doesn't ship with gzip, |
| 31 # and OSX's gzip does not have an --rsyncable option built in. Although this |
| 32 # is not as good as --rsyncable, it is an option for the systems that do not |
| 33 # have --rsyncable. This function should be avoided for release builds. |
| 34 gzip_output = cStringIO.StringIO() |
| 35 with gzip.GzipFile(mode='wb', compresslevel=9, fileobj=gzip_output, |
| 36 mtime=0) as gzip_file: |
| 37 gzip_file.write(data) |
| 38 data = gzip_output.getvalue() |
| 39 gzip_output.close() |
| 40 return data |
OLD | NEW |