Skip to content

Commit

Permalink
Merge branch 'apple_silicon_support'
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Johannes Aru <pauljohannes.aru@gmail.com>
  • Loading branch information
soraxas and paulpall committed Dec 4, 2023
2 parents 1d01827 + d2de787 commit 5f79ed8
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 51 deletions.
3 changes: 2 additions & 1 deletion echo360/binary_downloader/chromedriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ChromedriverDownloader(BinaryDownloader):
def __init__(self):
self._name = "chromedriver"
self._download_link_root = "https://chromedriver.storage.googleapis.com"
self._version = "2.38"
self._version = "114.0.5735.90"

def get_os_suffix(self):
self._os_linux_32 = "linux32"
Expand All @@ -14,6 +14,7 @@ def get_os_suffix(self):
self._os_windows_64 = "win32"
self._os_darwin_32 = "mac64"
self._os_darwin_64 = "mac64"
self._os_darwin_arm = "mac_arm64"
return super(ChromedriverDownloader, self).get_os_suffix()

def get_download_link(self):
Expand Down
4 changes: 4 additions & 0 deletions echo360/binary_downloader/downloader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os
import platform
import stat
import wget
import shutil
Expand All @@ -25,6 +26,9 @@ def get_os_suffix(self):
else:
return self._os_windows_32
elif "darwin" in sys.platform:
# detect if this is using arm processor (e.g. M1/M2 Mac)
if platform.processor() == "arm":
return self._os_darwin_arm
if arch == "64":
return self._os_darwin_64
else:
Expand Down
3 changes: 2 additions & 1 deletion echo360/binary_downloader/firefoxdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self):
self._download_link_root = (
"https://github.com/mozilla/geckodriver/releases/download"
)
self._version = "v0.30.0"
self._version = "v0.33.0"

def get_os_suffix(self):
self._os_linux_32 = "linux32"
Expand All @@ -16,6 +16,7 @@ def get_os_suffix(self):
self._os_windows_64 = "win64"
self._os_darwin_32 = "macos"
self._os_darwin_64 = "macos"
self._os_darwin_arm = "macos-aarch64"
return super(FirefoxDownloader, self).get_os_suffix()

def get_download_link(self):
Expand Down
163 changes: 114 additions & 49 deletions echo360/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from .course import EchoCloudCourse
from .echo_exceptions import EchoLoginError
from .utils import naive_versiontuple


from pick import pick
import selenium
Expand All @@ -19,6 +21,101 @@
_LOGGER = logging.getLogger(__name__)


def build_chrome_driver(
use_local_binary, selenium_version_ge_4100, setup_credential, user_agent, log_path
):
from selenium.webdriver.chrome.options import Options

opts = Options()
if not setup_credential:
opts.add_argument("--headless")
opts.add_argument("--window-size=1920x1080")
opts.add_argument("user-agent={}".format(user_agent))

kwargs = dict()
if selenium_version_ge_4100:
kwargs["options"] = opts
else:
kwargs["chrome_options"] = opts

if selenium_version_ge_4100:
from selenium.webdriver.chrome.service import Service

service = Service(**kwargs, log_file=log_path)
kwargs = dict(
service=service,
options=opts,
)
else:
if use_local_binary:
# newer selenium helps us to auto-download executable
from .binary_downloader.chromedriver import ChromedriverDownloader

kwargs["executable_path"] = ChromedriverDownloader().get_bin()
kwargs.update(
dict(
service_log_path=log_path,
chrome_options=opts,
)
)
return webdriver.Chrome(**kwargs)


def build_firefox_driver(
use_local_binary, selenium_version_ge_4100, setup_credential, user_agent, log_path
):
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", user_agent)
kwargs = dict()

if selenium_version_ge_4100:
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

option = Options()
option.profile = profile

service = Service(**kwargs, log_file=log_path)
kwargs = dict(
service=service,
options=option,
)
else:
if use_local_binary:
from .binary_downloader.firefoxdriver import FirefoxDownloader

kwargs["executable_path"] = FirefoxDownloader().get_bin()
kwargs.update(
dict(
service_log_path=log_path,
firefox_profile=profile,
)
)
return webdriver.Firefox(**kwargs)


def build_phantomjs_driver(
use_local_binary, selenium_version_ge_4100, setup_credential, user_agent, log_path
):
dcap = dict()
dcap.update(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 "
"(KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"
)
kwargs = {
"desired_capabilities": dcap,
"service_log_path": log_path,
}

if use_local_binary:
from .binary_downloader.phantomjs import PhantomjsDownloader

kwargs["executable_path"] = PhantomjsDownloader().get_bin()

return webdriver.PhantomJS(**kwargs)


class EchoDownloader(object):
def __init__(
self,
Expand Down Expand Up @@ -51,66 +148,34 @@ def __init__(
# self._driver = webdriver.PhantomJS()

if webdriver_to_use == "phantomjs":
selenium_major_version = int(selenium.__version__.split('.')[0])
selenium_major_version = int(selenium.__version__.split(".")[0])
if selenium_major_version >= 4:
print("============================================================")
print("WARNING: PhantomJS support had been removed in in selenium")
print(" version 4. If this app errors out later on, consider")
print(" installing earlier version of selenium.")
print(" e.g. pip3 install selenium==3.14")
print(" (see https://github.com/SeleniumHQ/selenium/blob/58122b261a5f5406da8e5252c9ab54c464da7aa8/py/CHANGES#L324)")
print(
" (see https://github.com/SeleniumHQ/selenium/blob/58122b261a5f5406da8e5252c9ab54c464da7aa8/py/CHANGES#L324)"
)
print("============================================================")

dcap = dict()
if use_local_binary:
if webdriver_to_use == "chrome":
from .binary_downloader.chromedriver import ChromedriverDownloader

get_bin = ChromedriverDownloader().get_bin
elif webdriver_to_use == "firefox":
from .binary_downloader.firefoxdriver import FirefoxDownloader

get_bin = FirefoxDownloader().get_bin
else:
from .binary_downloader.phantomjs import PhantomjsDownloader

get_bin = PhantomjsDownloader().get_bin
dcap.update(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 "
"(KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"
)
kwargs = {
"executable_path": get_bin(),
"desired_capabilities": dcap,
"service_log_path": log_path,
}
else:
kwargs = {}
if webdriver_to_use == "chrome":
from selenium.webdriver.chrome.options import Options

opts = Options()
if not setup_credential:
opts.add_argument("--headless")
opts.add_argument("--window-size=1920x1080")
opts.add_argument("user-agent={}".format(self._useragent))
kwargs["chrome_options"] = opts
self._driver = webdriver.Chrome(**kwargs)
driver_builder = build_chrome_driver
elif webdriver_to_use == "firefox":
# from selenium.webdriver.firefox.options import Options
# opts = Options()
# if not setup_credential:
# opts.add_argument("--headless")
# # opts.add_argument("--window-size=1920x1080")
# opts.add_argument("user-agent={}".format(self._useragent))
# kwargs['firefox_options'] = opts
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", self._useragent)
# driver = webdriver.Firefox(profile)
self._driver = webdriver.Firefox(profile, **kwargs)
driver_builder = build_firefox_driver
else:
self._driver = webdriver.PhantomJS(**kwargs)
driver_builder = build_phantomjs_driver

self._driver = driver_builder(
use_local_binary=use_local_binary,
selenium_version_ge_4100=(
naive_versiontuple(selenium.__version__) >= naive_versiontuple("4.10.0")
),
setup_credential=setup_credential,
user_agent=self._useragent,
log_path=log_path,
)

self.setup_credential = setup_credential
# Monkey Patch, set the course's driver to the one from .downloader
Expand Down
6 changes: 6 additions & 0 deletions echo360/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def naive_versiontuple(v):
"""
This only works for version tuple with the same number of parts.
Expects naive_versiontuple('xx.yy.zz') < naive_versiontuple('aa.bb.cc').
"""
return tuple(map(int, (v.split("."))))

0 comments on commit 5f79ed8

Please sign in to comment.