Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove wheeltools.py, import/require standalone wheeltools #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion delocate/cmd/delocate_addplat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from optparse import OptionParser, Option

from delocate import __version__
from delocate.wheeltools import add_platforms, WheelToolsError
from wheeltools import add_platforms, WheelToolsError


def main():
Expand Down
2 changes: 1 addition & 1 deletion delocate/delocating.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .tools import (set_install_name, zip2dir, dir2zip, validate_signature,
find_package_dirs, set_install_id, get_archs)
from .tmpdirs import TemporaryDirectory
from .wheeltools import rewrite_record, InWheel
from wheeltools import rewrite_record, InWheel

# Prefix for install_name_id of copied libraries
DLC_PREFIX = '/DLC/'
Expand Down
2 changes: 1 addition & 1 deletion delocate/fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .tools import (zip2dir, dir2zip, cmp_contents, lipo_fuse,
open_rw, chmod_perms)
from .tmpdirs import InTemporaryDirectory
from .wheeltools import rewrite_record
from wheeltools import rewrite_record


def _copyfile(in_fname, out_fname):
Expand Down
7 changes: 5 additions & 2 deletions delocate/tests/test_fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
open_readable)
from ..fuse import fuse_trees, fuse_wheels
from ..tmpdirs import InTemporaryDirectory
from ..wheeltools import rewrite_record
from wheeltools import rewrite_record

from .pytest_tools import (assert_true, assert_false, assert_raises,
assert_equal, assert_not_equal)


from .test_tools import LIB32, LIB64, LIB64A
from .test_wheelies import PURE_WHEEL
from .test_wheeltools import assert_record_equal


def assert_record_equal(record_orig, record_new):
assert_equal(sorted(record_orig.splitlines()), sorted(record_new.splitlines()))


def assert_same_tree(tree1, tree2):
Expand Down
44 changes: 42 additions & 2 deletions delocate/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@

from ..tmpdirs import InTemporaryDirectory
from ..tools import back_tick, set_install_name, zip2dir, dir2zip
from ..wheeltools import InWheel

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

from wheeltools import InWheel

from .scriptrunner import ScriptRunner

from .pytest_tools import (assert_true, assert_false, assert_equal, assert_raises,
Expand All @@ -27,7 +34,40 @@
STRAY_LIB_DEP, WHEEL_PATCH, WHEEL_PATCH_BAD,
_thin_lib, _thin_mod, _rename_module)
from .test_fuse import assert_same_tree
from .test_wheeltools import assert_winfo_similar


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 = wheelfile.dist_info_path + "/WHEEL"
return read_pkg_info_bytes(wheelfile.read(info_name))


def _filter_key(items, key):
return [(k, v) for k, v in items if k != key]


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(get_info(wf).items())
if drop_version:
w_info = _filter_key(w_info, "Wheel-Version")
exp_items = _filter_key(exp_items, "Wheel-Version")
assert_equal(len(exp_items), len(w_info))
# Extract some information from actual values
wheel_parts["pip_version"] = dict(w_info)["Generator"].split()[1]
for (key1, value1), (key2, value2) in zip(exp_items, w_info):
assert_equal(key1, key2)
value1 = value1.format(**wheel_parts)
assert_equal(value1, value2)


def _proc_lines(in_str):
Expand Down
174 changes: 2 additions & 172 deletions delocate/tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
""" Test tools module """
from __future__ import division, print_function

import os
from os.path import join as pjoin, dirname
import stat
import shutil

from ..tools import (back_tick, unique_by_index, ensure_writable, chmod_perms,
ensure_permissions, zip2dir, dir2zip, find_package_dirs,
cmp_contents, get_archs, lipo_fuse, replace_signature,
from ..tools import (back_tick, get_archs, lipo_fuse, replace_signature,
validate_signature, add_rpath)

from ..tmpdirs import InTemporaryDirectory

from .pytest_tools import assert_true, assert_false, assert_equal, assert_raises
from .pytest_tools import assert_equal, assert_raises

DATA_PATH = pjoin(dirname(__file__), 'data')
LIB32 = pjoin(DATA_PATH, 'liba32.dylib')
Expand All @@ -24,171 +19,6 @@
ARCH_32 = frozenset(['i386'])
ARCH_BOTH = ARCH_64 | ARCH_32

def test_back_tick():
cmd = 'python -c "print(\'Hello\')"'
assert_equal(back_tick(cmd), "Hello")
assert_equal(back_tick(cmd, ret_err=True), ("Hello", ""))
assert_equal(back_tick(cmd, True, False), (b"Hello", b""))
cmd = 'python -c "raise ValueError()"'
assert_raises(RuntimeError, back_tick, cmd)


def test_uniqe_by_index():
assert_equal(unique_by_index([1, 2, 3, 4]),
[1, 2, 3, 4])
assert_equal(unique_by_index([1, 2, 2, 4]),
[1, 2, 4])
assert_equal(unique_by_index([4, 2, 2, 1]),
[4, 2, 1])
def gen():
yield 4
yield 2
yield 2
yield 1
assert_equal(unique_by_index(gen()), [4, 2, 1])


def test_ensure_permissions():
# Test decorator to ensure permissions
with InTemporaryDirectory():
# Write, set zero permissions
sts = {}
for fname, contents in (('test.read', 'A line\n'),
('test.write', 'B line')):
with open(fname, 'wt') as fobj:
fobj.write(contents)
os.chmod(fname, 0)
sts[fname] = chmod_perms(fname)

def read_file(fname):
with open(fname, 'rt') as fobj:
contents = fobj.read()
return contents

fixed_read_file = ensure_permissions(stat.S_IRUSR)(read_file)
non_read_file = ensure_permissions(stat.S_IWUSR)(read_file)

def write_file(fname, contents):
with open(fname, 'wt') as fobj:
fobj.write(contents)

fixed_write_file = ensure_permissions(stat.S_IWUSR)(write_file)
non_write_file = ensure_permissions(stat.S_IRUSR)(write_file)

# Read fails with default, no permissions
assert_raises(IOError, read_file, 'test.read')
# Write fails with default, no permissions
assert_raises(IOError, write_file, 'test.write', 'continues')
# Read fails with wrong permissions
assert_raises(IOError, non_read_file, 'test.read')
# Write fails with wrong permissions
assert_raises(IOError, non_write_file, 'test.write', 'continues')
# Read succeeds with fixed function
assert_equal(fixed_read_file('test.read'), 'A line\n')
# Write fails, no permissions
assert_raises(IOError, non_write_file, 'test.write', 'continues')
# Write succeeds with fixed function
fixed_write_file('test.write', 'continues')
assert_equal(fixed_read_file('test.write'), 'continues')
# Permissions are as before
for fname, st in sts.items():
assert_equal(chmod_perms(fname), st)


def test_ensure_writable():
# Test ensure writable decorator
with InTemporaryDirectory():
with open('test.bin', 'wt') as fobj:
fobj.write('A line\n')
# Set to user rw, else r
os.chmod('test.bin', 0o644)
st = os.stat('test.bin')
@ensure_writable
def foo(fname):
pass
foo('test.bin')
assert_equal(os.stat('test.bin'), st)
# No-one can write
os.chmod('test.bin', 0o444)
st = os.stat('test.bin')
foo('test.bin')
assert_equal(os.stat('test.bin'), st)


def _write_file(filename, contents):
with open(filename, 'wt') as fobj:
fobj.write(contents)


def test_zip2():
# Test utilities to unzip and zip up
with InTemporaryDirectory():
os.mkdir('a_dir')
os.mkdir('zips')
_write_file(pjoin('a_dir', 'file1.txt'), 'File one')
s_dir = pjoin('a_dir', 's_dir')
os.mkdir(s_dir)
_write_file(pjoin(s_dir, 'file2.txt'), 'File two')
zip_fname = pjoin('zips', 'my.zip')
dir2zip('a_dir', zip_fname)
zip2dir(zip_fname, 'another_dir')
assert_equal(os.listdir('another_dir'), ['file1.txt', 's_dir'])
assert_equal(os.listdir(pjoin('another_dir', 's_dir')), ['file2.txt'])
# Try zipping from a subdirectory, with a different extension
dir2zip(s_dir, 'another.ext')
# Remove original tree just to be sure
shutil.rmtree('a_dir')
zip2dir('another.ext', 'third_dir')
assert_equal(os.listdir('third_dir'), ['file2.txt'])
# Check permissions kept in zip unzip cycle
os.mkdir('a_dir')
permissions = stat.S_IRUSR | stat.S_IWGRP | stat.S_IXGRP
fname = pjoin('a_dir', 'permitted_file')
_write_file(fname, 'Some script or something')
os.chmod(fname, permissions)
dir2zip('a_dir', 'test.zip')
zip2dir('test.zip', 'another_dir')
out_fname = pjoin('another_dir', 'permitted_file')
assert_equal(os.stat(out_fname).st_mode & 0o777, permissions)


def test_find_package_dirs():
# Test utility for finding package directories
with InTemporaryDirectory():
os.mkdir('to_test')
a_dir = pjoin('to_test', 'a_dir')
b_dir = pjoin('to_test', 'b_dir')
c_dir = pjoin('to_test', 'c_dir')
for dir in (a_dir, b_dir, c_dir):
os.mkdir(dir)
assert_equal(find_package_dirs('to_test'), set([]))
_write_file(pjoin(a_dir, '__init__.py'), "# a package")
assert_equal(find_package_dirs('to_test'), set([a_dir]))
_write_file(pjoin(c_dir, '__init__.py'), "# another package")
assert_equal(find_package_dirs('to_test'), set([a_dir, c_dir]))
# Not recursive
assert_equal(find_package_dirs('.'), set())
_write_file(pjoin('to_test', '__init__.py'), "# base package")
# Also - strips '.' for current directory
assert_equal(find_package_dirs('.'), set(['to_test']))


def test_cmp_contents():
# Binary compare of filenames
assert_true(cmp_contents(__file__, __file__))
with InTemporaryDirectory():
with open('first', 'wb') as fobj:
fobj.write(b'abc\x00\x10\x13\x10')
with open('second', 'wb') as fobj:
fobj.write(b'abc\x00\x10\x13\x11')
assert_false(cmp_contents('first', 'second'))
with open('third', 'wb') as fobj:
fobj.write(b'abc\x00\x10\x13\x10')
assert_true(cmp_contents('first', 'third'))
with open('fourth', 'wb') as fobj:
fobj.write(b'abc\x00\x10\x13\x10\x00')
assert_false(cmp_contents('first', 'fourth'))


def test_get_archs_fuse():
# Test routine to get architecture types from file
Expand Down
2 changes: 1 addition & 1 deletion delocate/tests/test_wheelies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
DLC_PREFIX)
from ..tools import (get_install_names, set_install_name, zip2dir,
dir2zip, back_tick, get_install_id, get_archs)
from ..wheeltools import InWheel
from wheeltools import InWheel

from ..tmpdirs import InTemporaryDirectory, InGivenDirectory

Expand Down
Loading