bdist_wheel packaging

Leonardo Barcaroli 2019-01-21 09:32:41 +01:00
parent 952da1a102
commit 2dc861fbea
3 changed files with 85 additions and 30 deletions

View File

@ -0,0 +1 @@
wheel>=0.32.3

View File

@ -12,7 +12,7 @@ search = __version__ = '{current_version}'
replace = __version__ = '{new_version}' replace = __version__ = '{new_version}'
[bdist_wheel] [bdist_wheel]
universal = 1 universal = 0
[flake8] [flake8]
exclude = docs exclude = docs

112
setup.py
View File

@ -6,14 +6,17 @@
from collections import namedtuple from collections import namedtuple
from html.parser import HTMLParser from html.parser import HTMLParser
import os import os
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
import pkg_resources import pkg_resources
from setuptools import setup, find_packages from setuptools import setup, find_packages # noqa
from setuptools.command.develop import develop from setuptools.command.develop import develop # noqa
from setuptools.command.install import install from setuptools.command.install import install # noqa
from setuptools.command.bdist_egg import bdist_egg # noqa
import sys import sys
import tarfile import tarfile
import typing as T
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
from wheel.bdist_wheel import bdist_wheel # noqa
import zipfile import zipfile
@ -32,14 +35,14 @@ requirements = [
] ]
setup_requirements = [ setup_requirements = [
] ] # type: T.List[str]
test_requirements = [ test_requirements = [
] ] # type: T.List[str]
class GitTags(HTMLParser): class GitTags(HTMLParser):
tags = list() tags: T.List[str] = list()
take_next = 0 take_next = 0
def handle_starttag(self, tag, attrs): def handle_starttag(self, tag, attrs):
@ -63,7 +66,8 @@ def retrieve_page(url: str) -> bytes:
from and URI, handling the errors. from and URI, handling the errors.
""" """
try: try:
content = urlopen(url).read() with urlopen(url) as conn:
content = conn.read()
except HTTPError as e: except HTTPError as e:
print("Connection error: {!s}".format(e)) print("Connection error: {!s}".format(e))
raise raise
@ -101,26 +105,31 @@ def create_local_folder() -> None:
bin_path = pkg_resources.resource_filename(PKG_NAME, BIN_PATH) bin_path = pkg_resources.resource_filename(PKG_NAME, BIN_PATH)
def assemble_driver_uri() -> str: def assemble_driver_uri(
version: T.Optional[str]=None,
platform: T.Optional[str]=None
) -> str:
""" """
Selects the right geckodriver URI. Selects the right geckodriver URI.
""" """
# TODO: use pkg_resources.get_platform() # TODO: use pkg_resources.get_platform()
latest_vers = find_latest_version(GECKO_RELEASE_PATH) if not version:
platform = sys.platform version = find_latest_version(GECKO_RELEASE_PATH)
is_64bits = sys.maxsize > 2**32 if not platform:
if is_64bits: s_platform = sys.platform
full_platform = '{}64'.format(platform) is_64bits = sys.maxsize > 2**32
else: if is_64bits:
full_platform = '{}64'.format(platform) platform = '{}64'.format(s_platform)
else:
platform = '{}64'.format(s_platform)
if 'win' in platform: if 'win' in platform:
ext = 'zip' ext = 'zip'
else: else:
ext = 'tar.gz' ext = 'tar.gz'
return '{base}/releases/download/{vers}/geckodriver-{vers}-{platform}.{ext}'.format( return '{base}/releases/download/{vers}/geckodriver-{vers}-{platform}.{ext}'.format(
base=GECKO_RELEASE_PATH, base=GECKO_RELEASE_PATH,
vers=latest_vers, vers=version,
platform=full_platform, platform=platform,
ext=ext ext=ext
) )
@ -142,11 +151,13 @@ def download_driver_bin(uri: str, path: str) -> None:
elif name.endswith(".tar.gz"): elif name.endswith(".tar.gz"):
with tarfile.open(filepath, 'r') as r: with tarfile.open(filepath, 'r') as r:
r.extractall(path) r.extractall(path)
else:
raise RuntimeError("Unrecognized file extension: %s", name)
finally: finally:
os.remove(filepath) os.remove(filepath)
def postinstall() -> None: def postinstall(platform: T.Optional[str]=None) -> None:
""" """
Performs all the postintallation flow, donwloading in the Performs all the postintallation flow, donwloading in the
right place the geckodriver binary. right place the geckodriver binary.
@ -154,25 +165,66 @@ def postinstall() -> None:
# target_path = os.path.join(os.path.abspath(os.path.curdir), 'bot_z', 'bin') # target_path = os.path.join(os.path.abspath(os.path.curdir), 'bot_z', 'bin')
target_path = pkg_resources.resource_filename('bot_z', 'bin') target_path = pkg_resources.resource_filename('bot_z', 'bin')
pkg_resources.ensure_directory(os.path.join(target_path, 'target')) pkg_resources.ensure_directory(os.path.join(target_path, 'target'))
gecko_uri = assemble_driver_uri() version = os.environ.get('BOTZ_GECKO_VERSION')
gecko_uri = assemble_driver_uri(version, platform)
print("[POSTINSTALL] gecko_uri: {}".format(gecko_uri)) print("[POSTINSTALL] gecko_uri: {}".format(gecko_uri))
download_driver_bin(gecko_uri, target_path) download_driver_bin(gecko_uri, target_path)
def translate_platform_to_gecko_vers(plat: str) -> str:
"""
Map appropriately the platform provided on the command line
to the one used by PEP 513.
"""
PLATS = {
"win32": "win32",
"win-amd64": "win64",
"manylinux1-i686": "linux32",
"manylinux1-x86_64": "linux64",
"macosx": "macos",
}
try:
return PLATS[plat]
except KeyError as e:
print("Allowed platforms are: {!r}".format(list(PLATS.keys())))
raise
# From: https://stackoverflow.com/a/36902139 # From: https://stackoverflow.com/a/36902139
class PostDevelopCommand(develop): class CustomDevelopCommand(develop):
"""Post-installation for development mode.""" """Custom installation for development mode."""
def run(self): def run(self):
super().run() super().run()
print("POSTINSTALL") print("POSTINSTALL")
postinstall() postinstall()
class PostInstallCommand(install): class CustomInstallCommand(install):
"""Post-installation for installation mode.""" """Custom installation for installation mode."""
def run(self): def run(self):
super().run() super().run()
postinstall() opts = self.distribution.get_cmdline_options()
if 'bdist_wheel' in opts:
platform = translate_platform_to_gecko_vers(
opts['bdist_wheel'].get('plat-name')
)
postinstall(platform)
# From: https://stackoverflow.com/a/45150383
class CustomBDistWheel(bdist_wheel):
"""
Custom bdist_wheel command to ship the right binary
of geckodriver.
"""
def finalize_options(self):
super().finalize_options()
self.root_is_pure = False
def get_tag(self):
python, abi, plat = super().get_tag()
python, abi = 'py3', 'none'
return python, abi, plat
setup( setup(
@ -185,14 +237,16 @@ setup(
url='https://git.abbiamoundominio.org/blallo/BotZ', url='https://git.abbiamoundominio.org/blallo/BotZ',
packages=find_packages(include=['bot_z']), packages=find_packages(include=['bot_z']),
cmdclass={ cmdclass={
'develop': PostDevelopCommand, 'develop': CustomDevelopCommand,
'install': PostInstallCommand, 'install': CustomInstallCommand,
'bdist_wheel': CustomBDistWheel,
}, },
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'bot_z=bot_z.cli:main' 'bot_z=bot_z.cli:main'
] ]
}, },
package_data = {'bot_z': ['bot_z/bin/geckodriver']},
include_package_data=True, include_package_data=True,
install_requires=requirements, install_requires=requirements,
license="GLWTS Public Licence", license="GLWTS Public Licence",
@ -201,7 +255,7 @@ setup(
classifiers=[ classifiers=[
'Development Status :: 2 - Pre-Alpha', 'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Developers', 'Intended Audience :: Developers',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 'License :: GLWTS Public Licence',
'Natural Language :: English', 'Natural Language :: English',
'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.7',