diff options
| author | Alex Grönholm <alex.gronholm@nextday.fi> | 2018-07-16 17:38:47 +0300 |
|---|---|---|
| committer | Alex Grönholm <alex.gronholm@nextday.fi> | 2018-07-17 15:02:50 +0300 |
| commit | 186ca2c82a7bdcc87b0a5584528507ad404ba790 (patch) | |
| tree | 45657a122e63a7d3da10e66c39f80f86e8cb68cf | |
| parent | 1a1ab7a6e569dbd3aed695e0298e564d7b19c5d0 (diff) | |
| download | wheel-git-186ca2c82a7bdcc87b0a5584528507ad404ba790.tar.gz | |
Converted the bdist_wheel command to use the WheelFile class
| -rw-r--r-- | wheel/archive.py | 77 | ||||
| -rw-r--r-- | wheel/bdist_wheel.py | 72 | ||||
| -rw-r--r-- | wheel/util.py | 10 |
3 files changed, 9 insertions, 150 deletions
diff --git a/wheel/archive.py b/wheel/archive.py deleted file mode 100644 index a11aa60..0000000 --- a/wheel/archive.py +++ /dev/null @@ -1,77 +0,0 @@ -""" -Archive tools for wheel. -""" - -import os -import os.path -import time -import zipfile -from distutils import log - - -def archive_wheelfile(base_name, base_dir): - """Archive all files under `base_dir` in a whl file and name it like - `base_name`. - """ - olddir = os.path.abspath(os.curdir) - base_name = os.path.abspath(base_name) - try: - os.chdir(base_dir) - return make_wheelfile_inner(base_name) - finally: - os.chdir(olddir) - - -def make_wheelfile_inner(base_name, base_dir='.'): - """Create a whl file from all the files under 'base_dir'. - - Places .dist-info at the end of the archive.""" - - zip_filename = base_name + ".whl" - - 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 ZipInfo objects. See - # issue #143. - timestamp = os.environ.get('SOURCE_DATE_EPOCH') - if timestamp is None: - date_time = None - else: - date_time = time.gmtime(int(timestamp))[0:6] - - score = {'WHEEL': 1, 'METADATA': 2, 'RECORD': 3} - - def writefile(path, date_time): - st = os.stat(path) - if date_time is None: - mtime = time.gmtime(st.st_mtime) - date_time = mtime[0:6] - zinfo = zipfile.ZipInfo(path, date_time) - zinfo.external_attr = st.st_mode << 16 - zinfo.compress_type = zipfile.ZIP_DEFLATED - with open(path, 'rb') as fp: - zip.writestr(zinfo, fp.read()) - log.info("adding '%s'" % path) - - with zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_DEFLATED, - allowZip64=True) as zip: - deferred = [] - for dirpath, dirnames, filenames in os.walk(base_dir): - # Sort the directory names so that `os.walk` will walk them in a - # defined order on the next iteration. - dirnames.sort() - for name in sorted(filenames): - path = os.path.normpath(os.path.join(dirpath, name)) - - if os.path.isfile(path): - if dirpath.endswith('.dist-info'): - deferred.append((score.get(name, 0), path)) - else: - writefile(path, date_time) - - deferred.sort() - for score, path in deferred: - writefile(path, date_time) - - return zip_filename diff --git a/wheel/bdist_wheel.py b/wheel/bdist_wheel.py index 8b08976..ec760d7 100644 --- a/wheel/bdist_wheel.py +++ b/wheel/bdist_wheel.py @@ -4,8 +4,6 @@ Create a wheel (.whl) distribution. A wheel is a built archive format. """ -import csv -import hashlib import os import shutil import sys @@ -19,10 +17,9 @@ from shutil import rmtree import pkg_resources from .pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag, get_platform -from .util import native, open_for_csv -from .archive import archive_wheelfile from .pkginfo import write_pkg_info from .metadata import pkginfo_to_metadata +from .wheelfile import WheelFile from . import pep425tags from . import __version__ as wheel_version @@ -180,18 +177,6 @@ class bdist_wheel(Command): assert tag in supported_tags, "would build wheel with unsupported tag {}".format(tag) return tag - def get_archive_basename(self): - """Return archive name without extension""" - - impl_tag, abi_tag, plat_tag = self.get_tag() - - archive_basename = "%s-%s-%s-%s" % ( - self.wheel_dist_name, - impl_tag, - abi_tag, - plat_tag) - return archive_basename - def run(self): build_scripts = self.reinitialize_command('build_scripts') build_scripts.executable = 'python' @@ -235,9 +220,8 @@ class bdist_wheel(Command): self.run_command('install') - archive_basename = self.get_archive_basename() - - pseudoinstall_root = os.path.join(self.dist_dir, archive_basename) + impl_tag, abi_tag, plat_tag = self.get_tag() + archive_basename = "{}-{}-{}-{}".format(self.wheel_dist_name, impl_tag, abi_tag, plat_tag) if not self.relative: archive_root = self.bdist_dir else: @@ -245,23 +229,23 @@ class bdist_wheel(Command): self.bdist_dir, self._ensure_relative(install.install_base)) - self.set_undefined_options( - 'install_egg_info', ('target', 'egginfo_dir')) + self.set_undefined_options('install_egg_info', ('target', 'egginfo_dir')) distinfo_dir = os.path.join(self.bdist_dir, '%s.dist-info' % self.wheel_dist_name) self.egg2dist(self.egginfo_dir, distinfo_dir) self.write_wheelfile(distinfo_dir) - self.write_record(self.bdist_dir, distinfo_dir) - # Make the archive if not os.path.exists(self.dist_dir): os.makedirs(self.dist_dir) - wheel_name = archive_wheelfile(pseudoinstall_root, archive_root) + + wheel_path = os.path.join(self.dist_dir, archive_basename + '.whl') + with WheelFile(wheel_path, 'w') as wf: + wf.write_files(archive_root) # Add to 'Distribution.dist_files' so that the "upload" command works getattr(self.distribution, 'dist_files', []).append( - ('bdist_wheel', get_python_version(), wheel_name)) + ('bdist_wheel', get_python_version(), wheel_path)) if not self.keep_temp: logger.info('removing %s', self.bdist_dir) @@ -361,41 +345,3 @@ class bdist_wheel(Command): shutil.copy(license, os.path.join(distinfo_path, license_filename)) adios(egginfo_path) - - def write_record(self, bdist_dir, distinfo_dir): - from .util import urlsafe_b64encode - - record_path = os.path.join(distinfo_dir, 'RECORD') - record_relpath = os.path.relpath(record_path, bdist_dir) - - def walk(): - for dir, dirs, files in os.walk(bdist_dir): - dirs.sort() - for f in sorted(files): - yield os.path.join(dir, f) - - def skip(path): - """Wheel hashes every possible file.""" - return (path == record_relpath) - - with open_for_csv(record_path, 'w+') as record_file: - writer = csv.writer(record_file) - for path in walk(): - relpath = os.path.relpath(path, bdist_dir) - if skip(relpath): - hash = '' - size = '' - else: - with open(path, 'rb') as f: - data = f.read() - digest = hashlib.sha256(data).digest() - hash = 'sha256=' + native(urlsafe_b64encode(digest)) - size = len(data) - - record_path = os.path.relpath(path, bdist_dir).replace(os.path.sep, '/') - - # On Python 2, re-encode the path as UTF-8 from the default file system encoding - if isinstance(record_path, bytes): - record_path = record_path.decode(sys.getfilesystemencoding()).encode('utf-8') - - writer.writerow((record_path, hash, size)) diff --git a/wheel/util.py b/wheel/util.py index db6dea6..be0aa9b 100644 --- a/wheel/util.py +++ b/wheel/util.py @@ -29,16 +29,6 @@ def urlsafe_b64decode(data): return base64.urlsafe_b64decode(data + pad) -def open_for_csv(name, mode): - if sys.version_info[0] < 3: - kwargs = {} - mode += 'b' - else: - kwargs = {'newline': '', 'encoding': 'utf-8'} - - return open(name, mode, **kwargs) - - def as_unicode(s): if isinstance(s, bytes): return s.decode('utf-8') |
