Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Vendor in wheel
Browse files Browse the repository at this point in the history
Summary:
Upstream wheel refactored and deleted an API we depend on.
We would either have to copy in the specific code we need,
and potentially make modifications, or vendor in wheel. I
chose to vendor in wheel because it is cleaner, and we can
go back to upstream if they provide the API we need again.

See pypa/wheel#262.

Reviewed By: cooperlees

Differential Revision: D14146161

fbshipit-source-id: 5f6a3e49ce5c6d19964a3b067ec79ca6985d0d33
  • Loading branch information
terrelln authored and facebook-github-bot committed Feb 21, 2019
1 parent e80d9ed commit b8f3645
Show file tree
Hide file tree
Showing 23 changed files with 2,968 additions and 13 deletions.
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ def get_long_description():
author_email="[email protected]",
url="https://github.com/facebookincubator/xar",
license="BSD",
packages=["xar", "xar.commands"],
packages=[
"xar",
"xar.commands",
"xar.vendor",
"xar.vendor.wheel",
"xar.vendor.wheel.signatures",
],
install_requires=[
"pip>=10.0.1",
# Version 34.1 fixes a bug in the dependency resolution. If this is
# causing an problem for you, please open an issue, and we can evaluate
# a workaround. (grep setuptools>=34.1 to see issue)
# https://github.com/pypa/setuptools/commit/8c1f489f09434f42080397367b6491e75f64d838 # noqa: E501
"setuptools>=34.1",
"wheel<=0.31.1",
],
test_requires=["mock", "pytest"],
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion xar/py_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import zipimport

import pkg_resources
from wheel import install, paths, pkginfo
from xar import xar_util
from xar.compat import cache_from_source, native, source_from_cache
from xar.vendor.wheel import install, paths, pkginfo


logger = logging.getLogger("xar")
Expand Down
90 changes: 80 additions & 10 deletions xar/py_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import mock


THISDIR = os.path.dirname(__file__)
TESTWHEEL = os.path.join(THISDIR, "test-1.0-py2.py3-none-win32.whl")


class PyUtilTest(unittest.TestCase):
def test_environment_python_interpreter(self):
interpreter = py_util.environment_python_interpreter()
Expand Down Expand Up @@ -81,6 +85,15 @@ def test_does_sha256_match(self):
self.assertFalse(py_util.does_sha256_match(file, unexpected))
self.assertFalse(py_util.does_sha256_match(file, ""))

def _temppaths(self, root):
return {
"purelib": root,
"platlib": root,
"headers": os.path.join(root, "include/xar"),
"scripts": os.path.join(root, "bin"),
"data": root,
}

def test_wheel_copy_installation(self):
dist = mock.MagicMock()
dist.location = "/path/to/lib/xar"
Expand All @@ -105,16 +118,7 @@ def test_wheel_copy_installation(self):
with open(os.path.join(src, file), "wb") as f:
f.write(b"hello world")

def temppaths(root):
return {
"purelib": root,
"platlib": root,
"headers": os.path.join(root, "include/xar"),
"scripts": os.path.join(root, "bin"),
"data": root,
}

wheel.copy_installation(temppaths(src), temppaths(dst))
wheel.copy_installation(self._temppaths(src), self._temppaths(dst))

for file, hash, _ in wheel.records():
dst_file = os.path.join(dst, file)
Expand All @@ -127,3 +131,69 @@ def temppaths(root):

xar_util.safe_rmtree(src)
xar_util.safe_rmtree(dst)

def test_wheel_sys_install_paths(self):
print(TESTWHEEL)
wheel = py_util.Wheel(location=TESTWHEEL)
sys_paths = wheel.sys_install_paths()
for path in self._temppaths(""):
self.assertTrue(path in sys_paths)

def test_wheel_is_purelib(self):
print(TESTWHEEL)
wheel = py_util.Wheel(location=TESTWHEEL)
self.assertFalse(wheel.is_purelib())

def _check_install(self, paths):
def assertExists(*path):
self.assertTrue(os.path.exists(os.path.join(*path)))

assertExists(paths["platlib"], "hello.pyd")
assertExists(paths["platlib"], "hello", "hello.py")
assertExists(paths["platlib"], "hello", "__init__.py")
assertExists(paths["data"], "hello.dat")
assertExists(paths["headers"], "hello.dat")
assertExists(paths["scripts"], "hello.sh")
assertExists(paths["platlib"], "test-1.0.dist-info", "RECORD")

def test_wheel_install_archive(self):
print(TESTWHEEL)
self.assertTrue(py_util.Wheel.is_wheel_archive(TESTWHEEL))
wheel = py_util.Wheel(location=TESTWHEEL)
dst = tempfile.mkdtemp()
paths = self._temppaths(dst)

# Install without force
wheel.install_archive(paths, force=False)
self._check_install(paths)

# Reinstall with force
wheel.install_archive(paths, force=True)
self._check_install(paths)

xar_util.safe_rmtree(dst)

def test_wheel_install(self):
archived_wheel = py_util.Wheel(location=TESTWHEEL)
src = tempfile.mkdtemp()
dst = tempfile.mkdtemp()
src_paths = self._temppaths(src)
dst_paths = self._temppaths(dst)

# Install the archive to src
archived_wheel.install(None, src_paths)
self._check_install(src_paths)

# Copy the installation to dst
src_location = os.path.join(src, "test-1.0.dist-info")
self.assertFalse(py_util.Wheel.is_wheel_archive(src_location))
installed_wheel = py_util.Wheel(location=src_location)
installed_wheel.install(src_paths, dst_paths, force=False)
self._check_install(dst_paths)

# Reinstall copy to dst with force
installed_wheel.install(src_paths, dst_paths, force=True)
self._check_install(dst_paths)

xar_util.safe_rmtree(src)
xar_util.safe_rmtree(dst)
Binary file added xar/test-1.0-py2.py3-none-win32.whl
Binary file not shown.
Empty file added xar/vendor/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions xar/vendor/wheel/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"wheel" copyright (c) 2012-2014 Daniel Holth <[email protected]> and
contributors.

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions xar/vendor/wheel/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# __variables__ with double-quoted values will be available in setup.py:
__version__ = "0.31.1"
19 changes: 19 additions & 0 deletions xar/vendor/wheel/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Wheel command line tool (enable python -m wheel syntax)
"""

import sys


def main(): # needed for console script
if __package__ == '':
# To be able to run 'python wheel-0.9.whl/wheel':
import os.path
path = os.path.dirname(os.path.dirname(__file__))
sys.path[0:0] = [path]
import wheel.tool
sys.exit(wheel.tool.main())


if __name__ == "__main__":
sys.exit(main())
77 changes: 77 additions & 0 deletions xar/vendor/wheel/archive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
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
Loading

0 comments on commit b8f3645

Please sign in to comment.