gzip_string.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 cStringIO
  7. import gzip
  8. import subprocess
  9. def GzipStringRsyncable(data):
  10. # Make call to host system's gzip to get access to --rsyncable option. This
  11. # option makes updates much smaller - if one line is changed in the resource,
  12. # it won't have to push the entire compressed resource with the update.
  13. # Instead, --rsyncable breaks the file into small chunks, so that one doesn't
  14. # affect the other in compression, and then only that chunk will have to be
  15. # updated.
  16. gzip_proc = subprocess.Popen(['gzip', '--stdout',
  17. '--best', '--no-name'],
  18. stdin=subprocess.PIPE,
  19. stdout=subprocess.PIPE,
  20. stderr=subprocess.PIPE)
  21. data, stderr = gzip_proc.communicate(data)
  22. if gzip_proc.returncode != 0:
  23. raise subprocess.CalledProcessError(gzip_proc.returncode, 'gzip',
  24. stderr)
  25. return data
  26. def GzipString(data):
  27. # Gzipping using Python's built in gzip: Windows doesn't ship with gzip, and
  28. # OSX's gzip does not have an --rsyncable option built in. Although this is
  29. # not preferable to --rsyncable, it is an option for the systems that do
  30. # not have --rsyncable. If used over GzipStringRsyncable, the primary
  31. # difference of this function's compression will be larger updates every time
  32. # a compressed resource is changed.
  33. gzip_output = cStringIO.StringIO()
  34. with gzip.GzipFile(mode='wb', compresslevel=9, fileobj=gzip_output,
  35. mtime=0) as gzip_file:
  36. gzip_file.write(data)
  37. data = gzip_output.getvalue()
  38. gzip_output.close()
  39. return data