diff --git a/tests/test_brave_driver.py b/tests/test_brave_driver.py index acebd1bb..2c78d39f 100644 --- a/tests/test_brave_driver.py +++ b/tests/test_brave_driver.py @@ -4,8 +4,9 @@ from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager +from webdriver_manager.core.driver_cache import DriverCacheManager from webdriver_manager.core.logger import log -from webdriver_manager.core.utils import ChromeType, os_name, OSType +from webdriver_manager.core.os_manager import ChromeType, OSType, OperationSystemManager def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): @@ -16,8 +17,8 @@ def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): "ssl_disabled", ) driver_path = ChromeDriverManager( - version="87.0.4280.88", - path=custom_path, + driver_version="87.0.4280.88", + cache_manager=DriverCacheManager(custom_path), chrome_type=ChromeType.BRAVE, ).install() @@ -54,6 +55,7 @@ def test_brave_manager_with_wrong_version(): @pytest.mark.parametrize('os_type', ['win32', 'win64']) def test_can_get_brave_for_win(os_type): - path = ChromeDriverManager(version="83.0.4103.39", os_type=os_type, + path = ChromeDriverManager(driver_version="83.0.4103.39", + os_system_manager=OperationSystemManager(os_type), chrome_type=ChromeType.BRAVE).install() assert os.path.exists(path) diff --git a/tests/test_chrome_driver.py b/tests/test_chrome_driver.py index fee96b10..dbe5f91d 100644 --- a/tests/test_chrome_driver.py +++ b/tests/test_chrome_driver.py @@ -8,7 +8,10 @@ from webdriver_manager.core.constants import ROOT_FOLDER_NAME from selenium.webdriver.chrome.service import Service -os.environ.setdefault("WDM_LOCAL", "true") +from webdriver_manager.core.driver_cache import DriverCacheManager +from webdriver_manager.core.os_manager import OperationSystemManager + +os.environ.setdefault("WDM_LOCAL", "false") def test_chrome_manager_with_cache(delete_drivers_dir): @@ -36,7 +39,7 @@ def test_chrome_manager_with_project_root_local_folder(delete_drivers_dir): def test_driver_can_be_saved_to_custom_path(): custom_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "custom") - path = ChromeDriverManager(version="87.0.4280.88", path=custom_path).install() + path = ChromeDriverManager(driver_version="87.0.4280.88", cache_manager=DriverCacheManager(custom_path)).install() assert os.path.exists(path) assert custom_path in path @@ -60,14 +63,14 @@ def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): os.path.dirname(__file__)), "ssl_disabled", ) - driver_path = ChromeDriverManager(path=custom_path).install() + driver_path = ChromeDriverManager(cache_manager=DriverCacheManager(custom_path)).install() os.environ['WDM_SSL_VERIFY'] = '1' assert os.path.exists(driver_path) def test_chrome_manager_cached_driver_with_selenium(): custom_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "custom-cache") - manager = ChromeDriverManager(path=custom_path) + manager = ChromeDriverManager(cache_manager=DriverCacheManager(custom_path)) driver = webdriver.Chrome(service=Service(manager.install())) driver.get("http://automation-remarks.com") @@ -82,10 +85,10 @@ def test_chrome_manager_cached_driver_with_selenium(): with open(metadata_file, 'w') as outfile: json.dump(data, outfile) - ChromeDriverManager(path=custom_path).install() + ChromeDriverManager(cache_manager=DriverCacheManager(custom_path)).install() @pytest.mark.parametrize('os_type', ['win32', 'win64', 'mac64', 'mac64_m1']) def test_can_get_chrome_for_os(os_type): - path = ChromeDriverManager(os_type=os_type).install() + path = ChromeDriverManager(os_system_manager=OperationSystemManager(os_type)).install() assert os.path.exists(path) diff --git a/tests/test_chromium_driver.py b/tests/test_chromium_driver.py index d1f19ebb..db603283 100644 --- a/tests/test_chromium_driver.py +++ b/tests/test_chromium_driver.py @@ -3,7 +3,8 @@ import pytest from webdriver_manager.chrome import ChromeDriverManager -from webdriver_manager.core.utils import ChromeType +from webdriver_manager.core.driver_cache import DriverCacheManager +from webdriver_manager.core.os_manager import ChromeType, OperationSystemManager def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): @@ -13,8 +14,8 @@ def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): "ssl_disabled", ) driver_path = ChromeDriverManager( - version="87.0.4280.88", - path=custom_path, + driver_version="87.0.4280.88", + cache_manager=DriverCacheManager(custom_path), chrome_type=ChromeType.CHROMIUM, ).install() @@ -29,7 +30,7 @@ def test_chromium_manager_with_specific_version(): def test_driver_can_be_saved_to_custom_path(): custom_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "custom") - path = ChromeDriverManager(version="87.0.4280.88", path=custom_path, + path = ChromeDriverManager(driver_version="87.0.4280.88", cache_manager=DriverCacheManager(custom_path), chrome_type=ChromeType.CHROMIUM).install() assert os.path.exists(path) assert custom_path in path @@ -43,6 +44,7 @@ def test_chromium_manager_with_wrong_version(): @pytest.mark.parametrize('os_type', ['win32', 'win64']) def test_can_get_chromium_for_win(os_type): - path = ChromeDriverManager(version="83.0.4103.39", os_type=os_type, + path = ChromeDriverManager(driver_version="83.0.4103.39", + os_system_manager=OperationSystemManager(os_type), chrome_type=ChromeType.CHROMIUM).install() assert os.path.exists(path) diff --git a/tests/test_downloader.py b/tests/test_downloader.py index 2024f53c..fd5ed40f 100644 --- a/tests/test_downloader.py +++ b/tests/test_downloader.py @@ -4,12 +4,13 @@ from webdriver_manager.core.constants import DEFAULT_PROJECT_ROOT_CACHE_PATH from webdriver_manager.core.download_manager import WDMDownloadManager +from webdriver_manager.core.file_manager import FileManager from webdriver_manager.core.http import WDMHttpClient -from webdriver_manager.core.utils import ChromeType, FileManager +from webdriver_manager.core.os_manager import OperationSystemManager, ChromeType from webdriver_manager.drivers.chrome import ChromeDriver download_manager = WDMDownloadManager() -file_manager = FileManager() +file_manager = FileManager(OperationSystemManager()) def test_can_download_driver_as_zip_file(delete_drivers_dir): @@ -31,7 +32,9 @@ def test_can_download_driver_as_tar_gz(delete_drivers_dir): @pytest.mark.parametrize('version', ["2.26"]) def test_can_download_chrome_driver(delete_drivers_dir, version): - driver = ChromeDriver(name="chromedriver", version=version, os_type="win32", + driver = ChromeDriver(name="chromedriver", + driver_version=version, + os_system_manager=OperationSystemManager("win32"), url="http://chromedriver.storage.googleapis.com", latest_release_url="http://chromedriver.storage.googleapis.com/LATEST_RELEASE", chrome_type=ChromeType.GOOGLE, http_client=WDMHttpClient()) diff --git a/tests/test_edge_driver.py b/tests/test_edge_driver.py index 93434481..71c94a64 100644 --- a/tests/test_edge_driver.py +++ b/tests/test_edge_driver.py @@ -5,8 +5,9 @@ from selenium import webdriver from selenium.webdriver.edge.service import Service +from webdriver_manager.core.driver_cache import DriverCacheManager +from webdriver_manager.core.os_manager import PATTERN, ChromeType, OperationSystemManager from webdriver_manager.microsoft import EdgeChromiumDriverManager -from webdriver_manager.core.utils import PATTERN, ChromeType def test_edge_manager_with_selenium(): @@ -22,7 +23,7 @@ def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): os.path.dirname(os.path.dirname(__file__)), "ssl_disabled", ) - driver_path = EdgeChromiumDriverManager(path=custom_path).install() + driver_path = EdgeChromiumDriverManager(cache_manager=DriverCacheManager(custom_path)).install() os.environ['WDM_SSL_VERIFY'] = '1' assert os.path.exists(driver_path) @@ -31,7 +32,7 @@ def test_edge_manager_with_wrong_version(): with pytest.raises(ValueError) as ex: EdgeChromiumDriverManager( version="0.2", - os_type='win64', + os_system_manager=OperationSystemManager("win64") ).install() assert ( @@ -45,7 +46,7 @@ def test_edge_manager_with_wrong_version(): def test_edge_with_specific_version(os_type, specific_version): bin_path = EdgeChromiumDriverManager( version=specific_version, - os_type=os_type, + os_system_manager=OperationSystemManager(os_type), ).install() assert os.path.exists(bin_path) @@ -55,11 +56,11 @@ def test_edge_with_specific_version(os_type, specific_version): def test_can_get_edge_driver_from_cache(os_type, specific_version): EdgeChromiumDriverManager( version=specific_version, - os_type=os_type, + os_system_manager=OperationSystemManager(os_type), ).install() driver_path = EdgeChromiumDriverManager( version=specific_version, - os_type=os_type + os_system_manager=OperationSystemManager(os_type) ).install() assert os.path.exists(driver_path) diff --git a/tests/test_firefox_manager.py b/tests/test_firefox_manager.py index 35ab3e5f..d78a3d09 100644 --- a/tests/test_firefox_manager.py +++ b/tests/test_firefox_manager.py @@ -4,6 +4,8 @@ from selenium import webdriver from selenium.webdriver.firefox.service import Service +from webdriver_manager.core.driver_cache import DriverCacheManager +from webdriver_manager.core.os_manager import OperationSystemManager from webdriver_manager.firefox import GeckoDriverManager @@ -26,7 +28,7 @@ def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): os.path.dirname(os.path.dirname(__file__)), "ssl_disabled", ) - driver_path = GeckoDriverManager(path=custom_path).install() + driver_path = GeckoDriverManager(cache_manager=DriverCacheManager(custom_path)).install() os.environ['WDM_SSL_VERIFY'] = '1' assert os.path.exists(driver_path) @@ -46,7 +48,7 @@ def test_gecko_manager_with_correct_version_and_token(delete_drivers_dir): def test_can_download_ff_x64(delete_drivers_dir): - driver_path = GeckoDriverManager(os_type="win64").install() + driver_path = GeckoDriverManager(os_system_manager=OperationSystemManager("win64")).install() assert os.path.exists(driver_path) @@ -57,6 +59,6 @@ def test_can_download_ff_x64(delete_drivers_dir): 'mac64', 'mac64_m1']) def test_can_get_driver_from_cache(os_type): - GeckoDriverManager(os_type=os_type).install() - driver_path = GeckoDriverManager(os_type=os_type).install() + GeckoDriverManager(os_system_manager=OperationSystemManager(os_type)).install() + driver_path = GeckoDriverManager(os_system_manager=OperationSystemManager(os_type)).install() assert os.path.exists(driver_path) diff --git a/tests/test_ie_driver.py b/tests/test_ie_driver.py index 11c24bcb..5dd6b098 100644 --- a/tests/test_ie_driver.py +++ b/tests/test_ie_driver.py @@ -2,6 +2,8 @@ import pytest +from webdriver_manager.core.driver_cache import DriverCacheManager +from webdriver_manager.core.os_manager import OperationSystemManager from webdriver_manager.microsoft import IEDriverManager @@ -20,21 +22,21 @@ def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): os.path.dirname(os.path.dirname(__file__)), "ssl_disabled", ) - driver_path = IEDriverManager(path=custom_path).install() + driver_path = IEDriverManager(cache_manager=DriverCacheManager(custom_path)).install() os.environ['WDM_SSL_VERIFY'] = '1' assert os.path.exists(driver_path) @pytest.mark.parametrize('os_type', ['win32', 'win64']) def test_can_download_ie_driver_x64(os_type): - path = IEDriverManager(os_type=os_type).install() + path = IEDriverManager(os_system_manager=OperationSystemManager(os_type)).install() assert os.path.exists(path) @pytest.mark.parametrize('os_type', ['win32', 'win64']) def test_can_get_ie_driver_from_cache(os_type): - IEDriverManager(os_type=os_type).install() - driver_path = IEDriverManager(os_type=os_type).install() + IEDriverManager(os_system_manager=OperationSystemManager(os_type)).install() + driver_path = IEDriverManager(os_system_manager=OperationSystemManager(os_type)).install() assert os.path.exists(driver_path) diff --git a/tests/test_opera_manager.py b/tests/test_opera_manager.py index 846ecf4b..6527a80d 100755 --- a/tests/test_opera_manager.py +++ b/tests/test_opera_manager.py @@ -5,7 +5,8 @@ from selenium import webdriver from selenium.webdriver.chrome.service import Service -from webdriver_manager.core.utils import os_type as get_os_type +from webdriver_manager.core.driver_cache import DriverCacheManager +from webdriver_manager.core.os_manager import OperationSystemManager from webdriver_manager.opera import OperaDriverManager @@ -20,7 +21,7 @@ def test_driver_with_ssl_verify_disabled_can_be_downloaded(ssl_verify_enable): os.path.dirname(os.path.dirname(__file__)), "ssl_disabled", ) - driver_path = OperaDriverManager(path=custom_path).install() + driver_path = OperaDriverManager(cache_manager=DriverCacheManager(custom_path)).install() os.environ['WDM_SSL_VERIFY'] = '1' assert os.path.exists(driver_path) @@ -29,8 +30,8 @@ def test_operadriver_manager_with_selenium(): driver_path = OperaDriverManager().install() options = webdriver.ChromeOptions() options.add_experimental_option('w3c', True) - - if get_os_type() in ["win64", "win32"]: + os_type = OperationSystemManager().get_os_type() + if os_type in ["win64", "win32"]: paths = [f for f in glob.glob( f"C:/Users/{os.getlogin()}/AppData/Local/Programs/Opera/**", recursive=True @@ -39,11 +40,11 @@ def test_operadriver_manager_with_selenium(): if os.path.isfile(path) and path.endswith("opera.exe"): options.binary_location = path elif ( - (get_os_type() in ["linux64", "linux32"]) + (os_type in ["linux64", "linux32"]) and not os.path.exists('/usr/bin/opera') ): options.binary_location = "/usr/bin/opera" - elif get_os_type() in "mac64": + elif os_type in "mac64": options.binary_location = "/Applications/Opera.app/Contents/MacOS/Opera" web_service = Service(driver_path) web_service.start() @@ -64,7 +65,7 @@ def test_opera_driver_manager_with_wrong_version(): @pytest.mark.parametrize('path', ['.', None]) def test_opera_driver_manager_with_correct_version_and_token(path): - driver_path = OperaDriverManager(version="v.2.45", path=path).install() + driver_path = OperaDriverManager(version="v.2.45", cache_manager=DriverCacheManager(path)).install() assert os.path.exists(driver_path) @@ -73,6 +74,6 @@ def test_opera_driver_manager_with_correct_version_and_token(path): 'linux64', 'mac64']) def test_can_get_driver_from_cache(os_type): - OperaDriverManager(os_type=os_type).install() - driver_path = OperaDriverManager(os_type=os_type).install() + OperaDriverManager(os_system_manager=OperationSystemManager(os_type)).install() + driver_path = OperaDriverManager(os_system_manager=OperationSystemManager(os_type)).install() assert os.path.exists(driver_path) diff --git a/tests_negative/test_browsers_not_installed.py b/tests_negative/test_browsers_not_installed.py index bbe59b60..5afaef36 100644 --- a/tests_negative/test_browsers_not_installed.py +++ b/tests_negative/test_browsers_not_installed.py @@ -8,9 +8,9 @@ from selenium.webdriver.firefox.service import Service as FFService from webdriver_manager.chrome import ChromeDriverManager +from webdriver_manager.core.os_manager import OperationSystemManager, ChromeType, OSType from webdriver_manager.firefox import GeckoDriverManager from webdriver_manager.microsoft import EdgeChromiumDriverManager -from webdriver_manager.core.utils import OSType, os_name, ChromeType @pytest.mark.skip(reason="it is not stable on CI") @@ -19,7 +19,7 @@ def test_brave_not_installed(): OSType.LINUX: "/usr/bin/brave-browser", OSType.MAC: "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser", OSType.WIN: f"{os.getenv('LOCALAPPDATA')}\\BraveSoftware\\Brave-Browser\\Application\\brave.exe", - }[os_name()] + }[OperationSystemManager.get_os_name()] option = webdriver.ChromeOptions() option.binary_location = binary_location diff --git a/webdriver_manager/chrome.py b/webdriver_manager/chrome.py index f27c8073..44a76094 100644 --- a/webdriver_manager/chrome.py +++ b/webdriver_manager/chrome.py @@ -4,42 +4,39 @@ from webdriver_manager.core.download_manager import DownloadManager from webdriver_manager.core.driver_cache import DriverCacheManager from webdriver_manager.core.manager import DriverManager -from webdriver_manager.core.utils import ChromeType +from webdriver_manager.core.os_manager import OperationSystemManager, ChromeType from webdriver_manager.drivers.chrome import ChromeDriver class ChromeDriverManager(DriverManager): def __init__( self, - version: Optional[str] = None, - os_type: Optional[str] = None, - path: Optional[str] = None, + driver_version: Optional[str] = None, name: str = "chromedriver", url: str = "https://chromedriver.storage.googleapis.com", latest_release_url: str = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE", chrome_type: str = ChromeType.GOOGLE, - cache_valid_range: int = 1, download_manager: Optional[DownloadManager] = None, - cache_manager: Optional[DriverCacheManager] = None + cache_manager: Optional[DriverCacheManager] = None, + os_system_manager: Optional[OperationSystemManager] = None ): super().__init__( - path, - cache_valid_range=cache_valid_range, download_manager=download_manager, - cache_manager=cache_manager) + cache_manager=cache_manager + ) self.driver = ChromeDriver( name=name, - version=version, - os_type=os_type, + driver_version=driver_version, url=url, latest_release_url=latest_release_url, chrome_type=chrome_type, http_client=self.http_client, + os_system_manager=os_system_manager ) def install(self) -> str: - driver_path = self._get_driver_path(self.driver) + driver_path = self._get_driver_binary_path(self.driver) if all(test_os not in driver_path for test_os in ["mac_arm64", "mac_x64"]): os.chmod(driver_path, 0o755) return driver_path diff --git a/webdriver_manager/core/archive.py b/webdriver_manager/core/archive.py index 3645c281..80268a0c 100644 --- a/webdriver_manager/core/archive.py +++ b/webdriver_manager/core/archive.py @@ -21,8 +21,5 @@ def extract(self, member, path=None, pwd=None): class Archive(object): - def __init__(self, path: str, os_type: typing.Optional[str] = None): + def __init__(self, path: str): self.file_path = path - self.os_type: typing.Optional[str] = os_type - - diff --git a/webdriver_manager/core/download_manager.py b/webdriver_manager/core/download_manager.py index c6055f51..fa130897 100644 --- a/webdriver_manager/core/download_manager.py +++ b/webdriver_manager/core/download_manager.py @@ -1,9 +1,9 @@ import os from abc import ABC +from webdriver_manager.core.file_manager import File from webdriver_manager.core.http import WDMHttpClient from webdriver_manager.core.logger import log -from webdriver_manager.core.utils import File class DownloadManager(ABC): diff --git a/webdriver_manager/core/driver.py b/webdriver_manager/core/driver.py index 26461b8c..d4c1d36d 100644 --- a/webdriver_manager/core/driver.py +++ b/webdriver_manager/core/driver.py @@ -1,28 +1,27 @@ -from webdriver_manager.core import utils from webdriver_manager.core.logger import log from webdriver_manager.core.config import gh_token -from webdriver_manager.core.utils import get_browser_version_from_os +from webdriver_manager.core.os_manager import OperationSystemManager class Driver(object): def __init__( self, name, - version, - os_type, + driver_version, url, latest_release_url, - http_client): + http_client, + os_system_manager): self._name = name self._url = url - self._version = version - self._os_type = os_type - if os_type is None: - self._os_type = utils.os_type() + self._driver_version = driver_version self._latest_release_url = latest_release_url self._http_client = http_client self._browser_version = None self._driver_to_download_version = None + self._os_system_manager = os_system_manager + if not self._os_system_manager: + self._os_system_manager = OperationSystemManager() @property def auth_header(self): @@ -36,10 +35,10 @@ def get_name(self): return self._name def get_os_type(self): - return self._os_type + return self._os_system_manager.get_os_type() def get_driver_download_url(self): - return f"{self._url}/{self.get_driver_version_to_download()}/{self._name}_{self._os_type}.zip" + return f"{self._url}/{self.get_driver_version_to_download()}/{self._name}_{self.get_os_type()}.zip" def get_driver_version_to_download(self): """ @@ -48,7 +47,7 @@ def get_driver_version_to_download(self): Downloads determined browser version driver in all other ways as a bonus fallback for lazy users. """ if not self._driver_to_download_version: - self._driver_to_download_version = self._version if self._version not in (None, "latest") \ + self._driver_to_download_version = self._driver_version if self._driver_version not in (None, "latest") \ else self.get_latest_release_version() return self._driver_to_download_version @@ -65,7 +64,7 @@ def get_browser_version_from_os(self): Note: the fallback may have collisions in user cases when previous browser was not uninstalled properly. """ if self._browser_version is None: - self._browser_version = get_browser_version_from_os(self.get_browser_type()) + self._browser_version = self._os_system_manager.get_browser_version_from_os(self.get_browser_type()) return self._browser_version def get_browser_type(self): diff --git a/webdriver_manager/core/driver_cache.py b/webdriver_manager/core/driver_cache.py index 9325a67e..ef113a40 100644 --- a/webdriver_manager/core/driver_cache.py +++ b/webdriver_manager/core/driver_cache.py @@ -8,8 +8,10 @@ DEFAULT_USER_HOME_CACHE_PATH, ROOT_FOLDER_NAME, ) from webdriver_manager.core.driver import Driver +from webdriver_manager.core.file_manager import FileManager, File from webdriver_manager.core.logger import log -from webdriver_manager.core.utils import get_date_diff, File, FileManager +from webdriver_manager.core.os_manager import OperationSystemManager +from webdriver_manager.core.utils import get_date_diff class DriverCacheManager(object): @@ -21,8 +23,9 @@ def __init__(self, root_dir=None, valid_range=1, file_manager=None): log(f"xdist worker is: {xdist_worker_id}") self._root_dir = os.path.join(self._root_dir, xdist_worker_id) - if root_dir is not None: + if root_dir: self._root_dir = os.path.join(root_dir, ROOT_FOLDER_NAME, xdist_worker_id) + if is_wdm_local: self._root_dir = os.path.join(DEFAULT_PROJECT_ROOT_CACHE_PATH, xdist_worker_id) @@ -35,8 +38,9 @@ def __init__(self, root_dir=None, valid_range=1, file_manager=None): self._metadata_key = None self._driver_binary_path = None self._file_manager = file_manager + self._os_system_manager = OperationSystemManager() if not self._file_manager: - self._file_manager = FileManager() + self._file_manager = FileManager(self._os_system_manager) def save_archive_file(self, file: File, path): return self._file_manager.save_archive_file(file, path) @@ -86,18 +90,22 @@ def __save_metadata(self, driver: Driver, binary_path, date=None): with open(self._drivers_json_path, "w+") as outfile: json.dump(metadata, outfile, indent=4) + def get_os_type(self): + return self._os_system_manager.get_os_type() + def find_driver(self, driver: Driver): """Find driver by '{os_type}_{driver_name}_{driver_version}_{browser_version}'.""" - os_type = driver.get_os_type() + os_type = self.get_os_type() driver_name = driver.get_name() - browser_version = driver.get_browser_version_from_os() - driver_version = self.get_cache_key_driver_version(driver) browser_type = driver.get_browser_type() + browser_version = self._os_system_manager.get_browser_version_from_os(browser_type) + driver_version = self.get_cache_key_driver_version(driver) metadata = self.load_metadata_content() key = self.__get_metadata_key(driver) if key not in metadata: - log(f'There is no [{os_type}] {driver_name} "{driver_version}" for browser {browser_type} "{browser_version}" in cache') + log(f'There is no [{os_type}] {driver_name} "{driver_version}" for browser {browser_type} ' + f'"{browser_version}" in cache') return None driver_info = metadata[key] @@ -134,7 +142,8 @@ def __get_metadata_key(self, driver: Driver): def get_cache_key_driver_version(self, driver: Driver): if self._cache_key_driver_version is None: - self._cache_key_driver_version = "latest" if driver._version in (None, "latest") else driver._version + self._cache_key_driver_version = "latest" if driver._driver_version in ( + None, "latest") else driver._driver_version return self._cache_key_driver_version def __get_path(self, driver: Driver): diff --git a/webdriver_manager/core/file_manager.py b/webdriver_manager/core/file_manager.py new file mode 100644 index 00000000..a33d35c8 --- /dev/null +++ b/webdriver_manager/core/file_manager.py @@ -0,0 +1,94 @@ +import os +import re +import tarfile +import zipfile + +from webdriver_manager.core.archive import Archive, LinuxZipFileWithPermissions +from webdriver_manager.core.os_manager import OperationSystemManager + + +class File(object): + def __init__(self, stream, file_name): + self.content = stream.content + self.__stream = stream + self.file_name = file_name + self.__temp_name = "driver" + self.__regex_filename = r"""filename.+"(.+)"|filename.+''(.+)|filename=([\w.-]+)""" + + @property + def filename(self) -> str: + if self.file_name: + return self.file_name + try: + content = self.__stream.headers["content-disposition"] + + content_disposition_list = re.split(";", content) + filenames = [re.findall(self.__regex_filename, element) for element in content_disposition_list] + filename = next(filter(None, next(filter(None, next(filter(None, filenames)))))) + except KeyError: + filename = f"{self.__temp_name}.zip" + except (IndexError, StopIteration): + filename = f"{self.__temp_name}.exe" + + if '"' in filename: + filename = filename.replace('"', "") + + return filename + + +class FileManager(object): + + def __init__(self, os_system_manager: OperationSystemManager): + self._os_system_manager = os_system_manager + + def save_archive_file(self, file: File, directory: str): + os.makedirs(directory, exist_ok=True) + + archive_path = f"{directory}{os.sep}{file.filename}" + with open(archive_path, "wb") as code: + code.write(file.content) + if not os.path.exists(archive_path): + raise FileExistsError(f"No file has been saved on such path {archive_path}") + return Archive(archive_path) + + def unpack_archive(self, archive_file: Archive, target_dir): + file_path = archive_file.file_path + if file_path.endswith(".zip"): + return self.__extract_zip(archive_file, target_dir) + elif file_path.endswith(".tar.gz"): + return self.__extract_tar_file(archive_file, target_dir) + + def __extract_zip(self, archive_file, to_directory): + zip_class = (LinuxZipFileWithPermissions if self._os_system_manager.get_os_name() == "linux" else zipfile.ZipFile) + archive = zip_class(archive_file.file_path) + try: + archive.extractall(to_directory) + except Exception as e: + if e.args[0] not in [26, 13] and e.args[1] not in [ + "Text file busy", + "Permission denied", + ]: + raise e + file_names = [] + for n in archive.namelist(): + if "/" not in n: + file_names.append(n) + else: + file_path, file_name = n.split("/") + full_file_path = os.path.join(to_directory, file_path) + source = os.path.join(full_file_path, file_name) + destination = os.path.join(to_directory, file_name) + os.rename(source, destination) + file_names.append(file_name) + return sorted(file_names, key=lambda x: x.lower()) + return archive.namelist() + + def __extract_tar_file(self, archive_file, to_directory): + try: + tar = tarfile.open(archive_file.file_path, mode="r:gz") + except tarfile.ReadError: + tar = tarfile.open(archive_file.file_path, mode="r:bz2") + members = tar.getmembers() + tar.extractall(to_directory) + tar.close() + return [x.name for x in members] diff --git a/webdriver_manager/core/manager.py b/webdriver_manager/core/manager.py index 54098878..44b0dee1 100644 --- a/webdriver_manager/core/manager.py +++ b/webdriver_manager/core/manager.py @@ -6,14 +6,12 @@ class DriverManager(object): def __init__( self, - root_dir=None, - cache_valid_range=1, download_manager=None, cache_manager=None ): self._cache_manager = cache_manager if not self._cache_manager: - self._cache_manager = DriverCacheManager(root_dir, cache_valid_range) + self._cache_manager = DriverCacheManager() self._download_manager = download_manager if self._download_manager is None: @@ -27,7 +25,7 @@ def http_client(self): def install(self) -> str: raise NotImplementedError("Please Implement this method") - def _get_driver_path(self, driver): + def _get_driver_binary_path(self, driver): binary_path = self._cache_manager.find_driver(driver) if binary_path: return binary_path diff --git a/webdriver_manager/core/os_manager.py b/webdriver_manager/core/os_manager.py new file mode 100644 index 00000000..1d25cdbd --- /dev/null +++ b/webdriver_manager/core/os_manager.py @@ -0,0 +1,162 @@ +import platform +import sys + +from webdriver_manager.core.utils import linux_browser_apps_to_cmd, windows_browser_apps_to_cmd, \ + read_version_from_cmd + + +class ChromeType(object): + GOOGLE = "google-chrome" + CHROMIUM = "chromium" + BRAVE = "brave-browser" + MSEDGE = "edge" + + +class OSType(object): + LINUX = "linux" + MAC = "mac" + WIN = "win" + + +PATTERN = { + ChromeType.CHROMIUM: r"\d+\.\d+\.\d+", + ChromeType.GOOGLE: r"\d+\.\d+\.\d+(\.\d+)?", + ChromeType.MSEDGE: r"\d+\.\d+\.\d+", + "brave-browser": r"\d+\.\d+\.\d+(\.\d+)?", + "firefox": r"(\d+.\d+)", +} + + +class OperationSystemManager(object): + + def __init__(self, os_type=None): + self._os_type = os_type + + @staticmethod + def get_os_name(): + pl = sys.platform + if pl == "linux" or pl == "linux2": + return OSType.LINUX + elif pl == "darwin": + return OSType.MAC + elif pl == "win32": + return OSType.WIN + + @staticmethod + def get_os_architecture(): + if platform.machine().endswith("64"): + return 64 + else: + return 32 + + def get_os_type(self): + if self._os_type: + return self._os_type + return f"{self.get_os_name()}{self.get_os_architecture()}" + + @staticmethod + def is_arch(os_sys_type): + if '_m1' in os_sys_type: + return True + return platform.processor() != 'i386' + + @staticmethod + def is_mac_os(os_sys_type): + return OSType.MAC in os_sys_type + + def get_browser_version_from_os(self, browser_type=None): + """Return installed browser version.""" + cmd_mapping = { + ChromeType.GOOGLE: { + OSType.LINUX: linux_browser_apps_to_cmd( + "google-chrome", + "google-chrome-stable", + "google-chrome-beta", + "google-chrome-dev", + ), + OSType.MAC: r"/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version", + OSType.WIN: windows_browser_apps_to_cmd( + r'(Get-Item -Path "$env:PROGRAMFILES\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion', + r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Google\Chrome\BLBeacon").version', + r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome").version', + ), + }, + ChromeType.CHROMIUM: { + OSType.LINUX: linux_browser_apps_to_cmd("chromium", "chromium-browser"), + OSType.MAC: r"/Applications/Chromium.app/Contents/MacOS/Chromium --version", + OSType.WIN: windows_browser_apps_to_cmd( + r'(Get-Item -Path "$env:PROGRAMFILES\Chromium\Application\chrome.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Chromium\Application\chrome.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:LOCALAPPDATA\Chromium\Application\chrome.exe").VersionInfo.FileVersion', + r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Chromium\BLBeacon").version', + r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Chromium").version', + ), + }, + ChromeType.BRAVE: { + OSType.LINUX: linux_browser_apps_to_cmd( + "brave-browser", "brave-browser-beta", "brave-browser-nightly" + ), + OSType.MAC: r"/Applications/Brave\ Browser.app/Contents/MacOS/Brave\ Browser --version", + OSType.WIN: windows_browser_apps_to_cmd( + r'(Get-Item -Path "$env:PROGRAMFILES\BraveSoftware\Brave-Browser\Application\brave.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES (x86)\BraveSoftware\Brave-Browser\Application\brave.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:LOCALAPPDATA\BraveSoftware\Brave-Browser\Application\brave.exe").VersionInfo.FileVersion', + r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\BraveSoftware\Brave-Browser\BLBeacon").version', + r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\BraveSoftware Brave-Browser").version', + ), + }, + ChromeType.MSEDGE: { + OSType.LINUX: linux_browser_apps_to_cmd( + "microsoft-edge", + "microsoft-edge-stable", + "microsoft-edge-beta", + "microsoft-edge-dev", + ), + OSType.MAC: r"/Applications/Microsoft\ Edge.app/Contents/MacOS/Microsoft\ Edge --version", + OSType.WIN: windows_browser_apps_to_cmd( + # stable edge + r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Microsoft\Edge\BLBeacon").version', + r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Microsoft\EdgeUpdate\Clients\{56EB18F8-8008-4CBD-B6D2-8C97FE7E9062}").pv', + # beta edge + r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Beta\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Beta\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Beta\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Microsoft\Edge Beta\BLBeacon").version', + # dev edge + r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Dev\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Dev\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Dev\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Microsoft\Edge Dev\BLBeacon").version', + # canary edge + r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge SxS\Application\msedge.exe").VersionInfo.FileVersion', + r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Microsoft\Edge SxS\BLBeacon").version', + # highest edge + r"(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe').'(Default)').VersionInfo.ProductVersion", + r"[System.Diagnostics.FileVersionInfo]::GetVersionInfo((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe').'(Default)').ProductVersion", + r"Get-AppxPackage -Name *MicrosoftEdge.* | Foreach Version", + r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Edge").version', + ), + }, + "firefox": { + OSType.LINUX: linux_browser_apps_to_cmd("firefox"), + OSType.MAC: r"/Applications/Firefox.app/Contents/MacOS/firefox --version", + OSType.WIN: windows_browser_apps_to_cmd( + r'(Get-Item -Path "$env:PROGRAMFILES\Mozilla Firefox\firefox.exe").VersionInfo.FileVersion', + r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Mozilla Firefox\firefox.exe").VersionInfo.FileVersion', + r"(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe').'(Default)').VersionInfo.ProductVersion", + r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Mozilla\Mozilla Firefox").CurrentVersion', + ), + }, + } + + try: + cmd_mapping = cmd_mapping[browser_type][OperationSystemManager.get_os_name()] + pattern = PATTERN[browser_type] + version = read_version_from_cmd(cmd_mapping, pattern) + return version + except Exception: + return None diff --git a/webdriver_manager/core/utils.py b/webdriver_manager/core/utils.py index 7b495dbe..65d75b66 100644 --- a/webdriver_manager/core/utils.py +++ b/webdriver_manager/core/utils.py @@ -1,150 +1,10 @@ import datetime import os -import platform import re import subprocess -import sys -import tarfile -import zipfile -from webdriver_manager.core.archive import Archive, LinuxZipFileWithPermissions -class File(object): - def __init__(self, stream, file_name): - self.content = stream.content - self.__stream = stream - self.file_name = file_name - self.__temp_name = "driver" - self.__regex_filename = r"""filename.+"(.+)"|filename.+''(.+)|filename=([\w.-]+)""" - - @property - def filename(self) -> str: - if self.file_name: - return self.file_name - try: - content = self.__stream.headers["content-disposition"] - - content_disposition_list = re.split(";", content) - filenames = [re.findall(self.__regex_filename, element) for element in content_disposition_list] - filename = next(filter(None, next(filter(None, next(filter(None, filenames)))))) - except KeyError: - filename = f"{self.__temp_name}.zip" - except (IndexError, StopIteration): - filename = f"{self.__temp_name}.exe" - - if '"' in filename: - filename = filename.replace('"', "") - - return filename - - -class FileManager(object): - - def save_archive_file(self, file: File, directory: str): - os.makedirs(directory, exist_ok=True) - - archive_path = f"{directory}{os.sep}{file.filename}" - with open(archive_path, "wb") as code: - code.write(file.content) - if not os.path.exists(archive_path): - raise FileExistsError(f"No file has been saved on such path {archive_path}") - return Archive(archive_path, os_type=os_name()) - - def unpack_archive(self, archive_file: Archive, target_dir): - file_path = archive_file.file_path - if file_path.endswith(".zip"): - return self.__extract_zip(archive_file, target_dir) - elif file_path.endswith(".tar.gz"): - return self.__extract_tar_file(archive_file, target_dir) - - def __extract_zip(self, archive_file, to_directory): - zip_class = (LinuxZipFileWithPermissions if archive_file.os_type == "linux" else zipfile.ZipFile) - archive = zip_class(archive_file.file_path) - try: - archive.extractall(to_directory) - except Exception as e: - if e.args[0] not in [26, 13] and e.args[1] not in [ - "Text file busy", - "Permission denied", - ]: - raise e - file_names = [] - for n in archive.namelist(): - if "/" not in n: - file_names.append(n) - else: - file_path, file_name = n.split("/") - full_file_path = os.path.join(to_directory, file_path) - source = os.path.join(full_file_path, file_name) - destination = os.path.join(to_directory, file_name) - os.rename(source, destination) - file_names.append(file_name) - return sorted(file_names, key=lambda x: x.lower()) - return archive.namelist() - - def __extract_tar_file(self, archive_file, to_directory): - try: - tar = tarfile.open(archive_file.file_path, mode="r:gz") - except tarfile.ReadError: - tar = tarfile.open(archive_file.file_path, mode="r:bz2") - members = tar.getmembers() - tar.extractall(to_directory) - tar.close() - return [x.name for x in members] - - -class OSType(object): - LINUX = "linux" - MAC = "mac" - WIN = "win" - - -class ChromeType(object): - GOOGLE = "google-chrome" - CHROMIUM = "chromium" - BRAVE = "brave-browser" - MSEDGE = "edge" - - -PATTERN = { - ChromeType.CHROMIUM: r"\d+\.\d+\.\d+", - ChromeType.GOOGLE: r"\d+\.\d+\.\d+(\.\d+)?", - ChromeType.MSEDGE: r"\d+\.\d+\.\d+", - "brave-browser": r"\d+\.\d+\.\d+(\.\d+)?", - "firefox": r"(\d+.\d+)", -} - - -def os_name(): - pl = sys.platform - if pl == "linux" or pl == "linux2": - return OSType.LINUX - elif pl == "darwin": - return OSType.MAC - elif pl == "win32": - return OSType.WIN - - -def os_architecture(): - if platform.machine().endswith("64"): - return 64 - else: - return 32 - - -def os_type(): - return f"{os_name()}{os_architecture()}" - - -def is_arch(os_sys_type): - if '_m1' in os_sys_type: - return True - return platform.processor() != 'i386' - - -def is_mac_os(os_sys_type): - return OSType.MAC in os_sys_type def get_date_diff(date1, date2, date_format): @@ -178,104 +38,6 @@ def windows_browser_apps_to_cmd(*apps: str) -> str: return f'{powershell} -NoProfile "{script}"' -def get_browser_version_from_os(browser_type=None): - """Return installed browser version.""" - cmd_mapping = { - ChromeType.GOOGLE: { - OSType.LINUX: linux_browser_apps_to_cmd( - "google-chrome", - "google-chrome-stable", - "google-chrome-beta", - "google-chrome-dev", - ), - OSType.MAC: r"/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version", - OSType.WIN: windows_browser_apps_to_cmd( - r'(Get-Item -Path "$env:PROGRAMFILES\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion', - r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Google\Chrome\BLBeacon").version', - r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome").version', - ), - }, - ChromeType.CHROMIUM: { - OSType.LINUX: linux_browser_apps_to_cmd("chromium", "chromium-browser"), - OSType.MAC: r"/Applications/Chromium.app/Contents/MacOS/Chromium --version", - OSType.WIN: windows_browser_apps_to_cmd( - r'(Get-Item -Path "$env:PROGRAMFILES\Chromium\Application\chrome.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Chromium\Application\chrome.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:LOCALAPPDATA\Chromium\Application\chrome.exe").VersionInfo.FileVersion', - r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Chromium\BLBeacon").version', - r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Chromium").version', - ), - }, - ChromeType.BRAVE: { - OSType.LINUX: linux_browser_apps_to_cmd( - "brave-browser", "brave-browser-beta", "brave-browser-nightly" - ), - OSType.MAC: r"/Applications/Brave\ Browser.app/Contents/MacOS/Brave\ Browser --version", - OSType.WIN: windows_browser_apps_to_cmd( - r'(Get-Item -Path "$env:PROGRAMFILES\BraveSoftware\Brave-Browser\Application\brave.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES (x86)\BraveSoftware\Brave-Browser\Application\brave.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:LOCALAPPDATA\BraveSoftware\Brave-Browser\Application\brave.exe").VersionInfo.FileVersion', - r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\BraveSoftware\Brave-Browser\BLBeacon").version', - r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\BraveSoftware Brave-Browser").version', - ), - }, - ChromeType.MSEDGE: { - OSType.LINUX: linux_browser_apps_to_cmd( - "microsoft-edge", - "microsoft-edge-stable", - "microsoft-edge-beta", - "microsoft-edge-dev", - ), - OSType.MAC: r"/Applications/Microsoft\ Edge.app/Contents/MacOS/Microsoft\ Edge --version", - OSType.WIN: windows_browser_apps_to_cmd( - # stable edge - r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Microsoft\Edge\BLBeacon").version', - r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Microsoft\EdgeUpdate\Clients\{56EB18F8-8008-4CBD-B6D2-8C97FE7E9062}").pv', - # beta edge - r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Beta\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Beta\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Beta\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Microsoft\Edge Beta\BLBeacon").version', - # dev edge - r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Dev\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Dev\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Dev\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Microsoft\Edge Dev\BLBeacon").version', - # canary edge - r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge SxS\Application\msedge.exe").VersionInfo.FileVersion', - r'(Get-ItemProperty -Path Registry::"HKCU\SOFTWARE\Microsoft\Edge SxS\BLBeacon").version', - # highest edge - r"(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe').'(Default)').VersionInfo.ProductVersion", - r"[System.Diagnostics.FileVersionInfo]::GetVersionInfo((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe').'(Default)').ProductVersion", - r"Get-AppxPackage -Name *MicrosoftEdge.* | Foreach Version", - r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Edge").version', - ), - }, - "firefox": { - OSType.LINUX: linux_browser_apps_to_cmd("firefox"), - OSType.MAC: r"/Applications/Firefox.app/Contents/MacOS/firefox --version", - OSType.WIN: windows_browser_apps_to_cmd( - r'(Get-Item -Path "$env:PROGRAMFILES\Mozilla Firefox\firefox.exe").VersionInfo.FileVersion', - r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Mozilla Firefox\firefox.exe").VersionInfo.FileVersion', - r"(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe').'(Default)').VersionInfo.ProductVersion", - r'(Get-ItemProperty -Path Registry::"HKLM\SOFTWARE\Mozilla\Mozilla Firefox").CurrentVersion', - ), - }, - } - - try: - cmd_mapping = cmd_mapping[browser_type][os_name()] - pattern = PATTERN[browser_type] - version = read_version_from_cmd(cmd_mapping, pattern) - return version - except Exception: - return None - - def read_version_from_cmd(cmd, pattern): with subprocess.Popen( cmd, diff --git a/webdriver_manager/drivers/chrome.py b/webdriver_manager/drivers/chrome.py index a9631769..41de1b45 100644 --- a/webdriver_manager/drivers/chrome.py +++ b/webdriver_manager/drivers/chrome.py @@ -2,7 +2,7 @@ from webdriver_manager.core.driver import Driver from webdriver_manager.core.logger import log -from webdriver_manager.core.utils import ChromeType, is_arch, is_mac_os +from webdriver_manager.core.os_manager import OperationSystemManager, ChromeType class ChromeDriver(Driver): @@ -10,15 +10,20 @@ class ChromeDriver(Driver): def __init__( self, name, - version, - os_type, + driver_version, url, latest_release_url, http_client, - chrome_type=ChromeType.GOOGLE, + os_system_manager, + chrome_type=ChromeType.GOOGLE ): super(ChromeDriver, self).__init__( - name, version, os_type, url, latest_release_url, http_client + name, + driver_version, + url, + latest_release_url, + http_client, + os_system_manager ) self._browser_type = chrome_type self._os_type = self.get_os_type() @@ -28,10 +33,10 @@ def get_os_type(self): if "win" in os_type: return "win32" - if not is_mac_os(os_type): + if not OperationSystemManager.is_mac_os(os_type): return os_type - if is_arch(os_type): + if OperationSystemManager.is_arch(os_type): return "mac_arm64" return os_type @@ -68,7 +73,7 @@ def get_latest_release_version(self): latest_release_url = ( self._latest_release_url - if (self._version == "latest" or determined_browser_version is None) + if (self._driver_version == "latest" or determined_browser_version is None) else f"{self._latest_release_url}_{determined_browser_version}" ) resp = self._http_client.get(url=latest_release_url) diff --git a/webdriver_manager/drivers/edge.py b/webdriver_manager/drivers/edge.py index ba8777e2..2cee215f 100644 --- a/webdriver_manager/drivers/edge.py +++ b/webdriver_manager/drivers/edge.py @@ -1,6 +1,6 @@ from webdriver_manager.core.driver import Driver from webdriver_manager.core.logger import log -from webdriver_manager.core.utils import OSType, ChromeType +from webdriver_manager.core.os_manager import OSType, ChromeType class EdgeChromiumDriver(Driver): @@ -8,19 +8,19 @@ class EdgeChromiumDriver(Driver): def __init__( self, name, - version, - os_type, + driver_version, url, latest_release_url, - http_client + http_client, + os_system_manager ): super(EdgeChromiumDriver, self).__init__( name, - version, - os_type, + driver_version, url, latest_release_url, - http_client + http_client, + os_system_manager ) self._os_type = self.get_os_type() @@ -36,7 +36,7 @@ def get_latest_release_version(self) -> str: edge_driver_version_to_download = ( self.get_stable_release_version() - if (self._version == "latest" or determined_browser_version is None) + if (self._driver_version == "latest" or determined_browser_version is None) else determined_browser_version ) major_edge_version = edge_driver_version_to_download.split(".")[0] diff --git a/webdriver_manager/drivers/firefox.py b/webdriver_manager/drivers/firefox.py index 4f7920e0..2a5f73a3 100644 --- a/webdriver_manager/drivers/firefox.py +++ b/webdriver_manager/drivers/firefox.py @@ -1,26 +1,25 @@ from webdriver_manager.core.driver import Driver from webdriver_manager.core.logger import log -from webdriver_manager.core.utils import is_arch, is_mac_os class GeckoDriver(Driver): def __init__( self, name, - version, - os_type, + driver_version, url, latest_release_url, mozila_release_tag, http_client, + os_system_manager ): super(GeckoDriver, self).__init__( name, - version, - os_type, + driver_version, url, latest_release_url, http_client, + os_system_manager, ) self._mozila_release_tag = mozila_release_tag self._os_type = self.get_os_type() @@ -50,11 +49,11 @@ def get_driver_download_url(self): def get_os_type(self): os_type = super().get_os_type() - if not is_mac_os(os_type): + if not self._os_system_manager.is_mac_os(os_type): return os_type macos = 'macos' - if is_arch(os_type): + if self._os_system_manager.is_arch(os_type): return f"{macos}-aarch64" return macos diff --git a/webdriver_manager/drivers/ie.py b/webdriver_manager/drivers/ie.py index 64a87594..dc7c1c8d 100644 --- a/webdriver_manager/drivers/ie.py +++ b/webdriver_manager/drivers/ie.py @@ -7,22 +7,22 @@ class IEDriver(Driver): def __init__( self, name, - version, - os_type, + driver_version, url, latest_release_url, ie_release_tag, - http_client + http_client, + os_system_manager ): super(IEDriver, self).__init__( name, - version, - os_type, + driver_version, url, latest_release_url, - http_client + http_client, + os_system_manager ) - self.os_type = "x64" if os_type == "win64" else "Win32" + self.os_type = "x64" if self._os_system_manager.get_os_type() == "win64" else "Win32" self._ie_release_tag = ie_release_tag # todo: for 'browser_version' implement installed IE version detection # like chrome or firefox diff --git a/webdriver_manager/drivers/opera.py b/webdriver_manager/drivers/opera.py index 00949d66..993d38b4 100644 --- a/webdriver_manager/drivers/opera.py +++ b/webdriver_manager/drivers/opera.py @@ -6,14 +6,20 @@ class OperaDriver(Driver): def __init__( self, name, - version, - os_type, + driver_version, url, latest_release_url, opera_release_tag, - http_client): + http_client, + os_system_manager + ): super(OperaDriver, self).__init__( - name, version, os_type, url, latest_release_url, http_client + name, + driver_version, + url, + latest_release_url, + http_client, + os_system_manager ) self.opera_release_tag = opera_release_tag diff --git a/webdriver_manager/firefox.py b/webdriver_manager/firefox.py index a2c8cdca..392e254c 100644 --- a/webdriver_manager/firefox.py +++ b/webdriver_manager/firefox.py @@ -4,6 +4,7 @@ from webdriver_manager.core.download_manager import DownloadManager from webdriver_manager.core.driver_cache import DriverCacheManager from webdriver_manager.core.manager import DriverManager +from webdriver_manager.core.os_manager import OperationSystemManager from webdriver_manager.drivers.firefox import GeckoDriver @@ -11,34 +12,30 @@ class GeckoDriverManager(DriverManager): def __init__( self, version: Optional[str] = None, - os_type: Optional[str] = None, - path: Optional[str] = None, name: str = "geckodriver", url: str = "https://github.com/mozilla/geckodriver/releases/download", latest_release_url: str = "https://api.github.com/repos/mozilla/geckodriver/releases/latest", mozila_release_tag: str = "https://api.github.com/repos/mozilla/geckodriver/releases/tags/{0}", - cache_valid_range: int = 1, download_manager: Optional[DownloadManager] = None, - cache_manager: Optional[DriverCacheManager] = None + cache_manager: Optional[DriverCacheManager] = None, + os_system_manager: Optional[OperationSystemManager] = None ): super(GeckoDriverManager, self).__init__( - path, - cache_valid_range, download_manager=download_manager, cache_manager=cache_manager ) self.driver = GeckoDriver( - version=version, - os_type=os_type, + driver_version=version, name=name, url=url, latest_release_url=latest_release_url, mozila_release_tag=mozila_release_tag, http_client=self.http_client, + os_system_manager=os_system_manager ) def install(self) -> str: - driver_path = self._get_driver_path(self.driver) + driver_path = self._get_driver_binary_path(self.driver) os.chmod(driver_path, 0o755) return driver_path diff --git a/webdriver_manager/microsoft.py b/webdriver_manager/microsoft.py index 69da5b76..7e170a88 100644 --- a/webdriver_manager/microsoft.py +++ b/webdriver_manager/microsoft.py @@ -1,9 +1,9 @@ import os from typing import Optional -from webdriver_manager.core import utils from webdriver_manager.core.download_manager import DownloadManager from webdriver_manager.core.driver_cache import DriverCacheManager +from webdriver_manager.core.os_manager import OperationSystemManager from webdriver_manager.drivers.edge import EdgeChromiumDriver from webdriver_manager.drivers.ie import IEDriver from webdriver_manager.core.manager import DriverManager @@ -13,64 +13,59 @@ class IEDriverManager(DriverManager): def __init__( self, version: Optional[str] = None, - os_type: Optional[str] = None, - path: Optional[str] = None, name: str = "IEDriverServer", url: str = "https://github.com/seleniumhq/selenium/releases/download", latest_release_url: str = "https://api.github.com/repos/seleniumhq/selenium/releases", ie_release_tag: str = "https://api.github.com/repos/seleniumhq/selenium/releases/tags/selenium-{0}", - cache_valid_range: int = 1, download_manager: Optional[DownloadManager] = None, - cache_manager: Optional[DriverCacheManager] = None + cache_manager: Optional[DriverCacheManager] = None, + os_system_manager: Optional[OperationSystemManager] = None ): super().__init__( - path, - cache_valid_range, download_manager=download_manager, - cache_manager=cache_manager) + cache_manager=cache_manager + ) self.driver = IEDriver( - version=version, - os_type=os_type, + driver_version=version, name=name, url=url, latest_release_url=latest_release_url, ie_release_tag=ie_release_tag, http_client=self.http_client, + os_system_manager=os_system_manager ) def install(self) -> str: - return self._get_driver_path(self.driver) + return self._get_driver_binary_path(self.driver) class EdgeChromiumDriverManager(DriverManager): def __init__( self, version: Optional[str] = None, - os_type: str = utils.os_type(), - path: Optional[str] = None, name: str = "edgedriver", url: str = "https://msedgedriver.azureedge.net", latest_release_url: str = "https://msedgedriver.azureedge.net/LATEST_RELEASE", - cache_valid_range: int = 1, download_manager: Optional[DownloadManager] = None, - cache_manager: Optional[DriverCacheManager] = None + cache_manager: Optional[DriverCacheManager] = None, + os_system_manager: Optional[OperationSystemManager] = None ): - super().__init__(path, - cache_valid_range, - download_manager=download_manager, - cache_manager=cache_manager) + super().__init__( + download_manager=download_manager, + cache_manager=cache_manager + ) self.driver = EdgeChromiumDriver( - version=version, - os_type=os_type, + driver_version=version, name=name, url=url, latest_release_url=latest_release_url, http_client=self.http_client, + os_system_manager=os_system_manager ) def install(self) -> str: - driver_path = self._get_driver_path(self.driver) + driver_path = self._get_driver_binary_path(self.driver) os.chmod(driver_path, 0o755) return driver_path diff --git a/webdriver_manager/opera.py b/webdriver_manager/opera.py index 0014ef0d..214c6f60 100755 --- a/webdriver_manager/opera.py +++ b/webdriver_manager/opera.py @@ -4,6 +4,7 @@ from webdriver_manager.core.download_manager import DownloadManager from webdriver_manager.core.driver_cache import DriverCacheManager from webdriver_manager.core.manager import DriverManager +from webdriver_manager.core.os_manager import OperationSystemManager from webdriver_manager.drivers.opera import OperaDriver @@ -11,8 +12,6 @@ class OperaDriverManager(DriverManager): def __init__( self, version: Optional[str] = None, - os_type: Optional[str] = None, - path: Optional[str] = None, name: str = "operadriver", url: str = "https://github.com/operasoftware/operachromiumdriver/" "releases/", @@ -20,27 +19,27 @@ def __init__( "operasoftware/operachromiumdriver/releases/latest", opera_release_tag: str = "https://api.github.com/repos/" "operasoftware/operachromiumdriver/releases/tags/{0}", - cache_valid_range: int = 1, download_manager: Optional[DownloadManager] = None, - cache_manager: Optional[DriverCacheManager] = None + cache_manager: Optional[DriverCacheManager] = None, + os_system_manager: Optional[OperationSystemManager] = None ): - super().__init__(path, - cache_valid_range, - download_manager=download_manager, - cache_manager=cache_manager) + super().__init__( + download_manager=download_manager, + cache_manager=cache_manager + ) self.driver = OperaDriver( name=name, - version=version, - os_type=os_type, + driver_version=version, url=url, latest_release_url=latest_release_url, opera_release_tag=opera_release_tag, http_client=self.http_client, + os_system_manager=os_system_manager ) def install(self) -> str: - driver_path = self._get_driver_path(self.driver) + driver_path = self._get_driver_binary_path(self.driver) if not os.path.isfile(driver_path): for name in os.listdir(driver_path): if "sha512_sum" in name: