diff options
author | 2017-09-26 13:59:31 -0400 | |
---|---|---|
committer | 2017-09-27 10:01:06 -0400 | |
commit | 5b98fb43ecaa6a9343ba4aa649f74193f0cf0956 (patch) | |
tree | ee2cb58745f3588184197edb4b044b28e7dff9c9 | |
parent | ee33b634a40a39997e561130ab4918c68ba7eaa1 (diff) |
Make gzipped pkg_tar outputs repeatable
The current implementation allows the gzip module to use the current
time as a timestamp. Python2 unfortunately doesn't expose this in the
tarfile module so it's easy to overlook.
This patch separates the gzip process by creating a separate file
object manually and bypassing the gzip compression in the tarfile
module.
Fixes #1844
Change-Id: I19b265bf55314d38bdd53ccf20c49ecfe6d7618b
PiperOrigin-RevId: 170077036
-rw-r--r-- | tools/build_defs/pkg/archive.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/tools/build_defs/pkg/archive.py b/tools/build_defs/pkg/archive.py index cf83aa997f..9d5ddfa667 100644 --- a/tools/build_defs/pkg/archive.py +++ b/tools/build_defs/pkg/archive.py @@ -14,6 +14,7 @@ """Archive manipulation library for the Docker rules.""" # pylint: disable=g-import-not-at-top +import gzip import os try: from StringIO import StringIO @@ -105,16 +106,21 @@ class TarFileWriter(object): pass def __init__(self, name, compression=''): - if compression in ['tgz', 'gz']: - mode = 'w:gz' - elif compression in ['bzip2', 'bz2']: + if compression in ['bzip2', 'bz2']: mode = 'w:bz2' else: mode = 'w:' + self.gz = compression in ['tgz', 'gz'] # Support xz compression through xz... until we can use Py3 self.xz = compression in ['xz', 'lzma'] self.name = name - self.tar = tarfile.open(name=name, mode=mode) + self.fileobj = None + if self.gz: + # The Tarfile class doesn't allow us to specify gzip's mtime attribute. + # Instead, we manually re-implement gzopen from tarfile.py and set mtime. + self.fileobj = gzip.GzipFile( + filename=name, mode='w', compresslevel=9, mtime=0) + self.tar = tarfile.open(name=name, mode=mode, fileobj=self.fileobj) self.members = set([]) self.directories = set([]) @@ -383,6 +389,9 @@ class TarFileWriter(object): TarFileWriter.Error: if an error happens when compressing the output file. """ self.tar.close() + # Close the gzip file object if necessary. + if self.fileobj: + self.fileobj.close() if self.xz: # Support xz compression through xz... until we can use Py3 if subprocess.call('which xz', shell=True, stdout=subprocess.PIPE): |