@@ -339,7 +339,7 @@ class _Stream:
339
339
"""
340
340
341
341
def __init__ (self , name , mode , comptype , fileobj , bufsize ,
342
- compresslevel , preset ):
342
+ compresslevel , preset , reproducible ):
343
343
"""Construct a _Stream object.
344
344
"""
345
345
self ._extfileobj = True
@@ -374,7 +374,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
374
374
self .exception = zlib .error
375
375
self ._init_read_gz ()
376
376
else :
377
- self ._init_write_gz (compresslevel )
377
+ self ._init_write_gz (compresslevel , reproducible )
378
378
379
379
elif comptype == "bz2" :
380
380
try :
@@ -423,15 +423,19 @@ def __del__(self):
423
423
if hasattr (self , "closed" ) and not self .closed :
424
424
self .close ()
425
425
426
- def _init_write_gz (self , compresslevel ):
426
+ def _init_write_gz (self , compresslevel , reproducible ):
427
427
"""Initialize for writing with gzip compression.
428
428
"""
429
429
self .cmp = self .zlib .compressobj (compresslevel ,
430
430
self .zlib .DEFLATED ,
431
431
- self .zlib .MAX_WBITS ,
432
432
self .zlib .DEF_MEM_LEVEL ,
433
433
0 )
434
- timestamp = struct .pack ("<L" , int (time .time ()))
434
+ if reproducible :
435
+ timestamp = 0
436
+ else :
437
+ timestamp = int (time .time ())
438
+ timestamp = struct .pack ("<L" , timestamp )
435
439
self .__write (b"\037 \213 \010 \010 " + timestamp + b"\002 \377 " )
436
440
if self .name .endswith (".gz" ):
437
441
self .name = self .name [:- 3 ]
@@ -1726,7 +1730,7 @@ class TarFile(object):
1726
1730
def __init__ (self , name = None , mode = "r" , fileobj = None , format = None ,
1727
1731
tarinfo = None , dereference = None , ignore_zeros = None , encoding = None ,
1728
1732
errors = "surrogateescape" , pax_headers = None , debug = None ,
1729
- errorlevel = None , copybufsize = None , stream = False ):
1733
+ errorlevel = None , copybufsize = None , stream = False , reproducible = False ):
1730
1734
"""Open an (uncompressed) tar archive 'name'. 'mode' is either 'r' to
1731
1735
read from an existing archive, 'a' to append data to an existing
1732
1736
file or 'w' to create a new file overwriting an existing one. 'mode'
@@ -1932,8 +1936,9 @@ def not_compressed(comptype):
1932
1936
1933
1937
compresslevel = kwargs .pop ("compresslevel" , 6 )
1934
1938
preset = kwargs .pop ("preset" , None )
1939
+ reproducible = kwargs .pop ("reproducible" , False )
1935
1940
stream = _Stream (name , filemode , comptype , fileobj , bufsize ,
1936
- compresslevel , preset )
1941
+ compresslevel , preset , reproducible )
1937
1942
try :
1938
1943
t = cls (name , filemode , stream , ** kwargs )
1939
1944
except :
0 commit comments