From dbca4946c65c914f10beabb77854e2d4ea65534e Mon Sep 17 00:00:00 2001 From: Asthowen Date: Thu, 18 Jul 2024 00:52:22 +0200 Subject: [PATCH] Abort if Chromedriver patch fails --- src/undetected_chromedriver/__init__.py | 4 +++- src/undetected_chromedriver/patcher.py | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/undetected_chromedriver/__init__.py b/src/undetected_chromedriver/__init__.py index 4e7fa1a..7d6f87a 100644 --- a/src/undetected_chromedriver/__init__.py +++ b/src/undetected_chromedriver/__init__.py @@ -256,7 +256,9 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): user_multi_procs=user_multi_procs, ) # self.patcher.auto(user_multiprocess = user_multi_num_procs) - self.patcher.auto() + if not self.patcher.auto(): + logging.error("Unable to patch chromedriver.") + sys.exit(1) # self.patcher = patcher if not options: diff --git a/src/undetected_chromedriver/patcher.py b/src/undetected_chromedriver/patcher.py index ad17e6f..82fa640 100644 --- a/src/undetected_chromedriver/patcher.py +++ b/src/undetected_chromedriver/patcher.py @@ -182,19 +182,19 @@ class Patcher(object): if not chromedriver_path or not os.path.isfile(chromedriver_path): logging.error("Chromedriver not installed!") - return + return False if not os.access(chromedriver_path, os.X_OK): logging.error( f"Execution permissions are not granted for the file {chromedriver_path}.", ) - return + return False destination_path = os.path.dirname(self.executable_path) if not os.access(destination_path, os.R_OK | os.W_OK | os.X_OK): logging.error( f"Read/write/execution permissions are not granted for the folder {destination_path}.", ) - return + return False try: process = os.popen(f'"{chromedriver_path}" --version') @@ -202,18 +202,22 @@ class Patcher(object): process.close() except Exception as e: logging.error(f"Failed to retrieve chromedriver version: {str(e)}") - return + return False version_path = os.path.join(destination_path, "version.txt") current_version = None - if os.path.isfile(version_path): - if not os.access(version_path, os.W_OK): - logging.error(f"Write permissions are not granted for the file {version_path}.") - return + if os.path.isfile(version_path) and os.path.isfile(self.executable_path): + if not os.access(version_path, os.R_OK | os.W_OK): + logging.error(f"Read/write permissions are not granted for the file {version_path}.") + return False with open(version_path, 'r') as f: current_version = f.read() if current_version == chromedriver_version: + if not os.access(self.executable_path, os.R_OK | os.W_OK | os.X_OK): + logging.error(f"Read/write/execution permissions are not granted for the file {self.executable_path}.") + return False + logging.info(f"The patched version of chromedriver in {destination_path} is the latest version on the system ({current_version}).") if self.is_binary_patched(): return True