deploy-ng: Support getting whl files via pip

The samples/asteriods/wheels directory and requirements.txt work-around
can be removed when we can grab py3 whls from PyPI.
This commit is contained in:
Mitchell Stokes 2017-01-04 19:01:37 -08:00
parent db2cbd6e3a
commit c51afaf0cf
4 changed files with 50 additions and 28 deletions

View File

@ -1,5 +1,6 @@
import collections import collections
import os import os
import pip
import sys import sys
import zipfile import zipfile
@ -23,7 +24,7 @@ class Distribution(distutils.dist.Distribution):
self.files = [] self.files = []
self.exclude_paths = [] self.exclude_paths = []
self.exclude_modules = [] self.exclude_modules = []
self.wheels = [] self.deploy_platforms = []
distutils.dist.Distribution.__init__(self, attrs) distutils.dist.Distribution.__init__(self, attrs)
@ -42,34 +43,52 @@ def find_packages(whlfile):
class build(distutils.command.build.build): class build(distutils.command.build.build):
def run(self): def run(self):
distutils.command.build.build.run(self) distutils.command.build.build.run(self)
if not self.distribution.wheels: print(self.distribution.deploy_platforms)
platforms = {p3d.PandaSystem.get_platform(): None} if not self.distribution.deploy_platforms:
platforms = [p3d.PandaSystem.get_platform()]
use_wheels = False
else: else:
platforms = { platforms = self.distribution.deploy_platforms
whl.split('-')[-1].replace('.whl', ''): whl use_wheels = True
for whl in self.distribution.wheels print("Building platforms: {}".format(','.join(platforms)))
}
for platform, whl in platforms.items(): for platform in platforms:
builddir = os.path.join(self.build_base, platform) builddir = os.path.join(self.build_base, platform)
if os.path.exists(builddir): if not os.path.exists(builddir):
distutils.dir_util.remove_tree(builddir) distutils.dir_util.mkpath(builddir)
distutils.dir_util.mkpath(builddir)
whldir = os.path.join(self.build_base, '__whl_cache__') if use_wheels:
if os.path.exists(whldir): whldir = os.path.join(self.build_base, '__whl_cache__')
distutils.dir_util.remove_tree(whldir)
if whl is not None: pip.main(args=[
whlfile = zipfile.ZipFile(whl) 'download',
'-d', whldir,
'-r', 'requirements.txt',
'--only-binary', ':all:',
'--platform', platform,
])
wheelpaths = [os.path.join(whldir,i) for i in os.listdir(whldir) if platform in i]
p3dwhl = None
for whl in wheelpaths:
if 'panda3d-' in whl:
p3dwhlfn = whl
p3dwhl = zipfile.ZipFile(p3dwhlfn)
break
else:
raise RuntimeError("Missing panda3d wheel")
whlfiles = {whl: zipfile.ZipFile(whl) for whl in wheelpaths}
stub_path = 'panda3d_tools/deploy-stub' stub_path = 'panda3d_tools/deploy-stub'
if platform.startswith('win'): if platform.startswith('win'):
stub_path += '.exe' stub_path += '.exe'
stub_file = whlfile.open(stub_path) stub_file = p3dwhl.open(stub_path)
# Add whl files to the path so they are picked up by modulefinder # Add whl files to the path so they are picked up by modulefinder
sys.path.insert(0, whl) for whl in wheelpaths:
sys.path.insert(0, whl)
else: else:
dtool_path = p3d.Filename(p3d.ExecutionEnvironment.get_dtool_name()).to_os_specific() dtool_path = p3d.Filename(p3d.ExecutionEnvironment.get_dtool_name()).to_os_specific()
stub_path = os.path.join(os.path.dirname(dtool_path), '..', 'bin', 'deploy-stub') stub_path = os.path.join(os.path.dirname(dtool_path), '..', 'bin', 'deploy-stub')
@ -105,7 +124,10 @@ class build(distutils.command.build.build):
target_path = os.path.join(builddir, basename) target_path = os.path.join(builddir, basename)
if '.whl/' in source_path: if '.whl/' in source_path:
# This was found in a wheel, extract it # This was found in a wheel, extract it
wf = source_path.split('.whl/')[-1] whl, wf = source_path.split('.whl/')
whl += '.whl'
print(whl, source_path)
whlfile = whlfiles[whl]
print("copying {} -> {}".format(os.path.join(whl, wf), target_path)) print("copying {} -> {}".format(os.path.join(whl, wf), target_path))
with open(target_path, 'wb') as f: with open(target_path, 'wb') as f:
f.write(whlfile.read(wf)) f.write(whlfile.read(wf))
@ -114,11 +136,11 @@ class build(distutils.command.build.build):
distutils.file_util.copy_file(source_path, target_path) distutils.file_util.copy_file(source_path, target_path)
# Find Panda3D libs # Find Panda3D libs
libs = find_packages(whlfile if whl is not None else None) libs = find_packages(p3dwhl if use_wheels else None)
# Copy Panda3D files # Copy Panda3D files
etcdir = os.path.join(builddir, 'etc') etcdir = os.path.join(builddir, 'etc')
if whl is None: if not use_wheels:
# Libs # Libs
for lib in libs: for lib in libs:
target_path = os.path.join(builddir, os.path.basename(lib)) target_path = os.path.join(builddir, os.path.basename(lib))
@ -134,13 +156,13 @@ class build(distutils.command.build.build):
distutils.dir_util.mkpath(etcdir) distutils.dir_util.mkpath(etcdir)
# Combine prc files with libs and copy the whole list # Combine prc files with libs and copy the whole list
panda_files = libs + [i for i in whlfile.namelist() if i.endswith('.prc')] panda_files = libs + [i for i in p3dwhl.namelist() if i.endswith('.prc')]
for pf in panda_files: for pf in panda_files:
dstdir = etcdir if pf.endswith('.prc') else builddir dstdir = etcdir if pf.endswith('.prc') else builddir
target_path = os.path.join(dstdir, os.path.basename(pf)) target_path = os.path.join(dstdir, os.path.basename(pf))
print("copying {} -> {}".format(os.path.join(whl, pf), target_path)) print("copying {} -> {}".format(os.path.join(p3dwhlfn, pf), target_path))
with open(target_path, 'wb') as f: with open(target_path, 'wb') as f:
f.write(whlfile.read(pf)) f.write(p3dwhl.read(pf))
# Copy Game Files # Copy Game Files
ignore_copy_list = [ ignore_copy_list = [
@ -173,10 +195,6 @@ class build(distutils.command.build.build):
dst = builddir dst = builddir
distutils.file_util.copy_file(src, dst) distutils.file_util.copy_file(src, dst)
# Cleanup whl directory
if os.path.exists(whldir):
distutils.dir_util.remove_tree(whldir)
class bdist_panda3d(distutils.core.Command): class bdist_panda3d(distutils.core.Command):
user_options = [] user_options = []

View File

@ -0,0 +1,2 @@
-f ./wheels
panda3d

View File

@ -5,4 +5,5 @@ setup(
directories=['.'], directories=['.'],
exclude_paths=['build', 'setup.py'], exclude_paths=['build', 'setup.py'],
applications=[Application('main.py', 'asteroids')], applications=[Application('main.py', 'asteroids')],
deploy_platforms=['linux_x86_64'],
) )

View File

@ -0,0 +1 @@
Put whl files in this folder to get picked up by the build step