Skip to content

Commit

Permalink
Refactor to new Wheel API
Browse files Browse the repository at this point in the history
Many wheel API changes.
  • Loading branch information
matthew-brett committed Sep 29, 2018
1 parent faa3ae3 commit 68355ee
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ language:
env:
global:
- VERSION="3.5.3"
- WHEEL_SPEC="wheel"

# Default OSX (xcode image) is 10.11 (xcode 7.3.1) as of 2017-05-02
# See: https://docs.travis-ci.com/user/osx-ci-environment/#OS-X-Version
Expand All @@ -13,6 +14,8 @@ matrix:
- env: VERSION="2.7.13"
- env: VERSION="3.4.4"
- env: VERSION="3.6.1"
# Wheel API changes
- env: WHEEL_SPEC="wheel==0.31.1"
# OS X 10.9
- osx_image: beta-xcode6.1
# Xcode 9.1 OS X 10.12
Expand All @@ -33,7 +36,7 @@ install:
- set -vx # echo commands
- source multibuild/osx_utils.sh
- get_macpython_environment $VERSION venv
- pip install wheel pytest
- pip install $WHEEL_SPEC pytest
- if [ -n "$BUILD_WHEELS" ]; then
pip install cython;
(cd wheel_makers && ./make_wheels.sh);
Expand Down
17 changes: 11 additions & 6 deletions delocate/tests/test_fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,31 @@ def assert_same_tree(tree1, tree2):
assert_equal(contents1, contents2)


def assert_listdir_equal(path, listing):
assert sorted(os.listdir(path)) == sorted(listing)


def test_fuse_trees():
# Test function to fuse two paths
with InTemporaryDirectory():
os.mkdir('tree1')
os.mkdir('tree2')
fuse_trees('tree1', 'tree2')
assert_equal(os.listdir('tree1'), [])
assert_listdir_equal('tree1', [])
with open(pjoin('tree2', 'afile.txt'), 'wt') as fobj:
fobj.write('Some text')
fuse_trees('tree1', 'tree2')
assert_equal(os.listdir('tree1'), ['afile.txt'])
assert_listdir_equal('tree1', ['afile.txt'])
assert_same_tree('tree1', 'tree2')
# Copy this test directory, show it turns up in output
shutil.copytree(dirname(__file__), pjoin('tree2', 'tests'))
fuse_trees('tree1', 'tree2')
assert_equal(os.listdir('tree1'), ['afile.txt', 'tests'])
assert_listdir_equal('tree1', ['afile.txt', 'tests'])
assert_same_tree('tree1', 'tree2')
# A library, not matched in to_tree
shutil.copy2(LIB64A, 'tree2')
fuse_trees('tree1', 'tree2')
assert_equal(os.listdir('tree1'), ['afile.txt', 'liba.a', 'tests'])
assert_listdir_equal('tree1', ['afile.txt', 'liba.a', 'tests'])
assert_same_tree('tree1', 'tree2')
# Run the same again; this tests that there is no error when the
# library is the same in both trees
Expand All @@ -76,8 +80,9 @@ def test_fuse_trees():
with open(pjoin('tree1', 'anotherfile.txt'), 'wt') as fobj:
fobj.write('Some more text')
fuse_trees('tree1', 'tree2')
assert_equal(os.listdir('tree1'),
['afile.txt', 'anotherfile.txt', 'liba.a', 'tests'])
assert_listdir_equal(
'tree1',
['afile.txt', 'anotherfile.txt', 'liba.a', 'tests'])


def test_fuse_wheels():
Expand Down
21 changes: 18 additions & 3 deletions delocate/tests/test_wheeltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from os.path import join as pjoin, exists, isfile, basename, realpath, splitext
import shutil

from wheel.install import WheelFile
try:
from wheel.install import WheelFile
except ImportError: # As of Wheel 0.32.0
from wheel.wheelfile import WheelFile

from ..wheeltools import (rewrite_record, InWheel, InWheelCtx, WheelToolsError,
add_platforms)
add_platforms, _get_wheelinfo_name)
from ..tmpdirs import InTemporaryDirectory
from ..tools import zip2dir

Expand Down Expand Up @@ -91,11 +94,23 @@ def _filter_key(items, key):
return [(k, v) for k, v in items if k != key]


def get_info(wheelfile):
# Work round wheel API changes
try:
return wheelfile.parsed_wheel_info
except AttributeError:
pass
# Wheel 0.32.0
from wheel.pkginfo import read_pkg_info_bytes
info_name = _get_wheelinfo_name(wheelfile)
return read_pkg_info_bytes(wheelfile.read(info_name))


def assert_winfo_similar(whl_fname, exp_items, drop_version=True):
wf = WheelFile(whl_fname)
wheel_parts = wf.parsed_filename.groupdict()
# Info can contain duplicate keys (e.g. Tag)
w_info = sorted(wf.parsed_wheel_info.items())
w_info = sorted(get_info(wf).items())
if drop_version:
w_info = _filter_key(w_info, 'Wheel-Version')
exp_items = _filter_key(exp_items, 'Wheel-Version')
Expand Down
28 changes: 23 additions & 5 deletions delocate/wheeltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Tools that aren't specific to delocation
"""

import sys
import os
from os.path import (join as pjoin, abspath, relpath, exists, sep as psep,
splitext, basename, dirname)
Expand All @@ -11,17 +12,26 @@
import csv
from itertools import product

from wheel.util import urlsafe_b64encode, open_for_csv, native
from wheel.util import urlsafe_b64encode, native
from wheel.pkginfo import read_pkg_info, write_pkg_info
from wheel.install import WheelFile

try:
from wheel.install import WheelFile
except ImportError: # As of Wheel 0.32.0
from wheel.wheelfile import WheelFile
from .tmpdirs import InTemporaryDirectory
from .tools import unique_by_index, zip2dir, dir2zip

class WheelToolsError(Exception):
pass


def _open_for_csv(name, mode):
""" Deal with Python 2/3 open API differences """
if sys.version_info[0] < 3:
return open(name, mode + 'b')
return open(name, mode, newline='', encoding='utf-8')


def rewrite_record(bdist_dir):
""" Rewrite RECORD file with hashes for all files in `wheel_sdir`
Expand Down Expand Up @@ -53,7 +63,7 @@ def skip(path):
"""Wheel hashes every possible file."""
return (path == record_relpath)

with open_for_csv(record_path, 'w+') as record_file:
with _open_for_csv(record_path, 'w+') as record_file:
writer = csv.writer(record_file)
for path in walk():
relative_path = relpath(path, bdist_dir)
Expand Down Expand Up @@ -141,6 +151,14 @@ def __enter__(self):
return self


def _get_wheelinfo_name(wheelfile):
# Work round wheel API compatibility
try:
return wheelfile.wheelinfo_name
except AttributeError:
return wheelfile.dist_info_path + '/WHEEL'


def add_platforms(in_wheel, platforms, out_path=None, clobber=False):
""" Add platform tags `platforms` to `in_wheel` filename and WHEEL tags
Expand Down Expand Up @@ -171,7 +189,7 @@ def add_platforms(in_wheel, platforms, out_path=None, clobber=False):
in_wheel = abspath(in_wheel)
out_path = dirname(in_wheel) if out_path is None else abspath(out_path)
wf = WheelFile(in_wheel)
info_fname = wf.wheelinfo_name
info_fname = _get_wheelinfo_name(wf)
# Check what tags we have
in_fname_tags = wf.parsed_filename.groupdict()['plat'].split('.')
extra_fname_tags = [tag for tag in platforms if tag not in in_fname_tags]
Expand Down

0 comments on commit 68355ee

Please sign in to comment.