diff options
author | Barry Warsaw <barry@python.org> | 2015-06-11 13:55:54 -0400 |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2015-06-11 13:55:54 -0400 |
commit | fb609b4fab5ed1b8281be441ef94408d4e37ef6a (patch) | |
tree | d65e31327f34e4104acbd5ca8934c4d4f2e24aa9 | |
parent | f7e751759340246ef00f6fd31d6991c8150cc511 (diff) | |
download | wheel-fb609b4fab5ed1b8281be441ef94408d4e37ef6a.tar.gz |
We can't defer setting the date time; it has to be done when the ZipInfo
object is created, but the zipfile module API doesn't make this easy for us.
-rw-r--r-- | wheel/archive.py | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/wheel/archive.py b/wheel/archive.py index 71dcbd4..d3dc19c 100644 --- a/wheel/archive.py +++ b/wheel/archive.py @@ -2,6 +2,7 @@ Archive tools for wheel. """ +import os import time import logging import os.path @@ -32,6 +33,15 @@ def make_wheelfile_inner(base_name, base_dir='.'): log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir) + # Some applications need reproducible .whl files, but they can't do this + # without forcing the timestamp of the individual TarInfo objects. See + # issue #143. + timestamp = os.environ.get('WHEEL_FORCE_TIMESTAMP') + if timestamp is None: + date_time = None + else: + date_time = time.localtime(int(timestamp))[0:6] + # XXX support bz2, xz when available zip = zipfile.ZipFile(open(zip_filename, "wb+"), "w", compression=zipfile.ZIP_DEFLATED) @@ -39,8 +49,14 @@ def make_wheelfile_inner(base_name, base_dir='.'): score = {'WHEEL': 1, 'METADATA': 2, 'RECORD': 3} deferred = [] - def writefile(path): - zip.write(path, path) + def writefile(path, date_time): + if date_time is None: + st = os.stat(path) + mtime = time.localtime(st.st_mtime) + date_time = mtime[0:6] + zinfo = zipfile.ZipInfo(path, date_time) + with open(path, 'rb') as fp: + zip.writestr(zinfo, fp.read()) log.info("adding '%s'" % path) for dirpath, dirnames, filenames in os.walk(base_dir): @@ -51,19 +67,11 @@ def make_wheelfile_inner(base_name, base_dir='.'): if dirpath.endswith('.dist-info'): deferred.append((score.get(name, 0), path)) else: - writefile(path) + writefile(path, date_time) deferred.sort() for score, path in deferred: - writefile(path) - - # Before we close the zip file, see if the caller is forcing the timestamp - # of the individual TarInfo objects. See issue #143. - timestamp = os.environ.get('WHEEL_FORCE_TIMESTAMP') - if timestamp is not None: - date_time = time.localtime(int(timestamp))[0:6] - for info in zip.infolist(): - info.date_time = date_time + writefile(path, date_time) zip.close() |