From f6e67ca977fb8e0867f5f237e511e264732fb393 Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 8 Aug 2019 15:31:25 +0200 Subject: [PATCH] Improve platform detection. --- bot_z/operator.py | 36 ++++++++++++++++++++++++++--- setup.py | 58 +++++++++++++++++++++++++++++++---------------- 2 files changed, 71 insertions(+), 23 deletions(-) diff --git a/bot_z/operator.py b/bot_z/operator.py index 7a4571a..5743392 100644 --- a/bot_z/operator.py +++ b/bot_z/operator.py @@ -8,6 +8,7 @@ service. It exposes methods to login, logout and check in and out. from datetime import datetime, timedelta import logging import os +import platform import pkg_resources import shutil import sys @@ -15,11 +16,40 @@ import time import typing as T from urllib.parse import urlparse -geckoexe = shutil.which("geckodriver") + +def path_from_platform() -> T.Text: + res = None + _plat = sys.platform + _arch = platform.architecture() + if _plat == "darwin": + return "macos" + if _plat == "win32": + res = "win" + elif _plat.startswith("linux"): + res = "linux" + + if res is None: + raise RuntimeError("Platform unknown: {}".format(_plat)) + + if "64" in _arch[0]: + res += "64" + elif "32" in _arch[0]: + res += "32" + + return res + + +geckoname = "geckodriver" +geckoexe = shutil.which(geckoname) if geckoexe is None: - local_path = pkg_resources.resource_filename(__name__, "bin") + _plat_path = path_from_platform() + local_path = pkg_resources.resource_filename( + __name__, os.path.join("bin", _plat_path) + ) try: - os.stat(os.path.join(local_path, "geckodriver")) + if "win" in _plat_path: + geckoname = f"{geckoname}.exe" + os.stat(os.path.join(local_path, geckoname)) os.environ["PATH"] = os.environ["PATH"] + ":" + local_path except FileNotFoundError: print("Missing geckodriver executable in path", file=sys.stderr) diff --git a/setup.py b/setup.py index d64cae8..3592514 100644 --- a/setup.py +++ b/setup.py @@ -173,6 +173,22 @@ PLATFORM_MAP = { } +def _identify_platform(): + s_platform = sys.platform + if s_platform == "darwin": + return "macos" + is_64bits = sys.maxsize > 2 ** 32 + if "linux" in s_platform: + plat = "linux" + elif "win" in s_platform: + plat = "win" + if is_64bits: + platform = "{}64".format(plat) + else: + platform = "{}32".format(plat) + return platform + + def assemble_driver_uri( version: T.Optional[str] = None, platform: T.Optional[str] = None ) -> str: @@ -182,13 +198,8 @@ def assemble_driver_uri( # TODO: use pkg_resources.get_platform() if not version: version = find_latest_version(GECKO_RELEASE_PATH) - if not platform: - s_platform = sys.platform - is_64bits = sys.maxsize > 2 ** 32 - if is_64bits: - platform = "{}64".format(s_platform) - else: - platform = "{}64".format(s_platform) + if platform is None: + platform = _identify_platform() if "win" in platform: ext = "zip" else: @@ -221,19 +232,32 @@ def download_driver_bin(uri: str, path: str) -> None: os.remove(filepath) -def preinstall(platform: T.Optional[str] = None) -> None: +def preinstall(platform: T.Text, build_iface: bool = True) -> None: """ Performs all the postintallation flow, donwloading in the right place the geckodriver binary. """ - # 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", os.path.join("bin", platform) + ) + if not os.path.exists(target_path): + mkpath(target_path) ensure_local_folder() version = os.environ.get("BOTZ_GECKO_VERSION") gecko_uri = assemble_driver_uri(version, platform) print("[POSTINSTALL] gecko_uri: {}".format(gecko_uri)) download_driver_bin(gecko_uri, target_path) - build_web() + if build_iface: + build_web() + + +PLATS = { + "win32": "win32", + "win-amd64": "win64", + "manylinux1-i686": "linux32", + "manylinux1-x86_64": "linux64", + "macosx": "macos", +} def translate_platform_to_gecko_vers(plat: str) -> str: @@ -244,16 +268,9 @@ def translate_platform_to_gecko_vers(plat: str) -> str: if plat is None: return None - PLATS = { - "win32": "win32", - "win-amd64": "win64", - "manylinux1-i686": "linux32", - "manylinux1-x86_64": "linux64", - "macosx": "macos", - } try: return PLATS[plat] - except KeyError as e: + except KeyError: print("Allowed platforms are: {!r}".format(list(PLATS.keys()))) raise @@ -264,7 +281,8 @@ class CustomDevelopCommand(develop): def run(self): print("POSTINSTALL") - preinstall() + platform = _identify_platform() + preinstall(platform, build_iface=False) super().run()