Improve platform detection.

This commit is contained in:
sfigato 2019-08-08 15:31:25 +02:00 committed by blallo
parent a369c4978d
commit f6e67ca977
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
2 changed files with 71 additions and 23 deletions

View File

@ -8,6 +8,7 @@ service. It exposes methods to login, logout and check in and out.
from datetime import datetime, timedelta from datetime import datetime, timedelta
import logging import logging
import os import os
import platform
import pkg_resources import pkg_resources
import shutil import shutil
import sys import sys
@ -15,11 +16,40 @@ import time
import typing as T import typing as T
from urllib.parse import urlparse 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: 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: 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 os.environ["PATH"] = os.environ["PATH"] + ":" + local_path
except FileNotFoundError: except FileNotFoundError:
print("Missing geckodriver executable in path", file=sys.stderr) print("Missing geckodriver executable in path", file=sys.stderr)

View File

@ -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( def assemble_driver_uri(
version: T.Optional[str] = None, platform: T.Optional[str] = None version: T.Optional[str] = None, platform: T.Optional[str] = None
) -> str: ) -> str:
@ -182,13 +198,8 @@ def assemble_driver_uri(
# TODO: use pkg_resources.get_platform() # TODO: use pkg_resources.get_platform()
if not version: if not version:
version = find_latest_version(GECKO_RELEASE_PATH) version = find_latest_version(GECKO_RELEASE_PATH)
if not platform: if platform is None:
s_platform = sys.platform platform = _identify_platform()
is_64bits = sys.maxsize > 2 ** 32
if is_64bits:
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:
@ -221,19 +232,32 @@ def download_driver_bin(uri: str, path: str) -> None:
os.remove(filepath) 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 Performs all the postintallation flow, donwloading in the
right place the geckodriver binary. 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(
target_path = pkg_resources.resource_filename("bot_z", "bin") "bot_z", os.path.join("bin", platform)
)
if not os.path.exists(target_path):
mkpath(target_path)
ensure_local_folder() ensure_local_folder()
version = os.environ.get("BOTZ_GECKO_VERSION") version = os.environ.get("BOTZ_GECKO_VERSION")
gecko_uri = assemble_driver_uri(version, platform) 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)
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: 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: if plat is None:
return None return None
PLATS = {
"win32": "win32",
"win-amd64": "win64",
"manylinux1-i686": "linux32",
"manylinux1-x86_64": "linux64",
"macosx": "macos",
}
try: try:
return PLATS[plat] return PLATS[plat]
except KeyError as e: except KeyError:
print("Allowed platforms are: {!r}".format(list(PLATS.keys()))) print("Allowed platforms are: {!r}".format(list(PLATS.keys())))
raise raise
@ -264,7 +281,8 @@ class CustomDevelopCommand(develop):
def run(self): def run(self):
print("POSTINSTALL") print("POSTINSTALL")
preinstall() platform = _identify_platform()
preinstall(platform, build_iface=False)
super().run() super().run()