From d8f583530dc3b7933893156f45ab722006632225 Mon Sep 17 00:00:00 2001 From: Anderson Carlos Ferreira da Silva Date: Fri, 24 Mar 2023 13:39:52 +0100 Subject: [PATCH 1/6] detecting os and windows path --- chromedriver_binary/utils.py | 39 ++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/chromedriver_binary/utils.py b/chromedriver_binary/utils.py index 3d9a653..8ecb320 100644 --- a/chromedriver_binary/utils.py +++ b/chromedriver_binary/utils.py @@ -9,6 +9,8 @@ import subprocess import re import platform +import array +import ctypes try: from urllib.request import urlopen, URLError @@ -111,13 +113,46 @@ def get_chrome_major_version(): if sys.platform == "darwin": browser_executables.insert(0, "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") + get_version = lambda version: re.match(r'.*?((?P\d+)\.(\d+\.){2,3}\d+).*?', version).group('major') + for browser_executable in browser_executables: try: version = subprocess.check_output([browser_executable, '--version']) - return re.match(r'.*?((?P\d+)\.(\d+\.){2,3}\d+).*?', version.decode('utf-8')).group('major') + + return get_version(version.decode('utf-8')) + except Exception: - pass + if sys.platform.startswith('win'): + roots = list(filter(None, [os.getenv('LocalAppData'), os.getenv('ProgramFiles'), os.getenv('ProgramFiles(x86)'), os.getenv('ProgramW6432')])) + + for root in roots: + try: + # https://stackoverflow.com/questions/580924/how-to-access-a-files-properties-on-windows + document = ctypes.wstring_at(os.path.join(root, 'Google', 'Chrome', 'Application', browser_executable + '.exe')) + + buffer_size = ctypes.windll.version.GetFileVersionInfoSizeW(document, None) + buffer = ctypes.create_string_buffer(buffer_size) + + ctypes.windll.version.GetFileVersionInfoW(document, None, buffer_size, buffer) + + value_size = ctypes.c_uint(0) + value = ctypes.c_void_p(0) + ctypes.windll.version.VerQueryValueW(buffer, ctypes.wstring_at(r"\VarFileInfo\Translation"), ctypes.byref(value), ctypes.byref(value_size)) + + codepages = array.array('H', ctypes.string_at(value.value, value_size.value)) + + language = '{0:04x}{1:04x}'.format(*codepages[:2].tolist()) + + ctypes.windll.version.VerQueryValueW(buffer, ctypes.wstring_at('\\StringFileInfo\\' + language + '\\FileVersion'), ctypes.byref(value), ctypes.byref(value_size)) + + version = ctypes.wstring_at(value.value, value_size.value - 1) + + return get_version(version) + + except Exception: + pass + pass def check_version(binary, required_version): try: From 0bc86980a24446e64f7c85cc572cf99c3572fa76 Mon Sep 17 00:00:00 2001 From: Anderson Carlos Ferreira da Silva Date: Fri, 24 Mar 2023 15:59:57 +0100 Subject: [PATCH 2/6] adding support to Python 2 --- chromedriver_binary/utils.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/chromedriver_binary/utils.py b/chromedriver_binary/utils.py index 8ecb320..9af9bbd 100644 --- a/chromedriver_binary/utils.py +++ b/chromedriver_binary/utils.py @@ -123,30 +123,41 @@ def get_chrome_major_version(): except Exception: if sys.platform.startswith('win'): + get_info_size = ctypes.windll.version.GetFileVersionInfoSizeW + get_info = ctypes.windll.version.GetFileVersionInfoW + get_value = ctypes.windll.version.VerQueryValueW + get_string = ctypes.wstring_at + + if sys.version_info.major < 3: + get_info_size = ctypes.windll.version.GetFileVersionInfoSizeA + get_info = ctypes.windll.version.GetFileVersionInfoA + get_value = ctypes.windll.version.VerQueryValueA + get_string = ctypes.string_at + roots = list(filter(None, [os.getenv('LocalAppData'), os.getenv('ProgramFiles'), os.getenv('ProgramFiles(x86)'), os.getenv('ProgramW6432')])) - + for root in roots: try: # https://stackoverflow.com/questions/580924/how-to-access-a-files-properties-on-windows document = ctypes.wstring_at(os.path.join(root, 'Google', 'Chrome', 'Application', browser_executable + '.exe')) - buffer_size = ctypes.windll.version.GetFileVersionInfoSizeW(document, None) + buffer_size = get_info_size(document, None) buffer = ctypes.create_string_buffer(buffer_size) - ctypes.windll.version.GetFileVersionInfoW(document, None, buffer_size, buffer) + get_info(document, None, buffer_size, buffer) value_size = ctypes.c_uint(0) value = ctypes.c_void_p(0) - ctypes.windll.version.VerQueryValueW(buffer, ctypes.wstring_at(r"\VarFileInfo\Translation"), ctypes.byref(value), ctypes.byref(value_size)) + get_value(buffer, get_string(r"\VarFileInfo\Translation"), ctypes.byref(value), ctypes.byref(value_size)) codepages = array.array('H', ctypes.string_at(value.value, value_size.value)) language = '{0:04x}{1:04x}'.format(*codepages[:2].tolist()) - ctypes.windll.version.VerQueryValueW(buffer, ctypes.wstring_at('\\StringFileInfo\\' + language + '\\FileVersion'), ctypes.byref(value), ctypes.byref(value_size)) + get_value(buffer, get_string('\\StringFileInfo\\' + language + '\\FileVersion'), ctypes.byref(value), ctypes.byref(value_size)) - version = ctypes.wstring_at(value.value, value_size.value - 1) + version = get_string(value.value, value_size.value - 1) return get_version(version) From 84b2f54fd79c67ea01cd2177e504010d6cacb39d Mon Sep 17 00:00:00 2001 From: Anderson Carlos Ferreira da Silva Date: Sat, 1 Apr 2023 13:43:02 +0200 Subject: [PATCH 3/6] renaming major version function and adding support to cygwin --- chromedriver_binary/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chromedriver_binary/utils.py b/chromedriver_binary/utils.py index 9af9bbd..b316835 100644 --- a/chromedriver_binary/utils.py +++ b/chromedriver_binary/utils.py @@ -113,16 +113,16 @@ def get_chrome_major_version(): if sys.platform == "darwin": browser_executables.insert(0, "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") - get_version = lambda version: re.match(r'.*?((?P\d+)\.(\d+\.){2,3}\d+).*?', version).group('major') + get_major_version = lambda version: re.match(r'.*?((?P\d+)\.(\d+\.){2,3}\d+).*?', version).group('major') for browser_executable in browser_executables: try: version = subprocess.check_output([browser_executable, '--version']) - return get_version(version.decode('utf-8')) + return get_major_version(version.decode('utf-8')) except Exception: - if sys.platform.startswith('win'): + if sys.platform.startswith('win') or sys.platform.startswith('cygwin'): get_info_size = ctypes.windll.version.GetFileVersionInfoSizeW get_info = ctypes.windll.version.GetFileVersionInfoW get_value = ctypes.windll.version.VerQueryValueW @@ -159,7 +159,7 @@ def get_chrome_major_version(): version = get_string(value.value, value_size.value - 1) - return get_version(version) + return get_major_version(version) except Exception: pass From 4b3b03644565e54097f30a0322230a4282ae1cd1 Mon Sep 17 00:00:00 2001 From: Anderson Carlos Ferreira da Silva Date: Tue, 22 Aug 2023 06:42:05 +0000 Subject: [PATCH 4/6] adding second solution --- chromedriver_binary/utils.py | 37 ++++++------------------------------ 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/chromedriver_binary/utils.py b/chromedriver_binary/utils.py index b316835..2afd37f 100644 --- a/chromedriver_binary/utils.py +++ b/chromedriver_binary/utils.py @@ -122,44 +122,19 @@ def get_chrome_major_version(): return get_major_version(version.decode('utf-8')) except Exception: - if sys.platform.startswith('win') or sys.platform.startswith('cygwin'): - get_info_size = ctypes.windll.version.GetFileVersionInfoSizeW - get_info = ctypes.windll.version.GetFileVersionInfoW - get_value = ctypes.windll.version.VerQueryValueW - get_string = ctypes.wstring_at - - if sys.version_info.major < 3: - get_info_size = ctypes.windll.version.GetFileVersionInfoSizeA - get_info = ctypes.windll.version.GetFileVersionInfoA - get_value = ctypes.windll.version.VerQueryValueA - get_string = ctypes.string_at - + if sys.platform.startswith('win') or sys.platform.startswith('cygwin'): roots = list(filter(None, [os.getenv('LocalAppData'), os.getenv('ProgramFiles'), os.getenv('ProgramFiles(x86)'), os.getenv('ProgramW6432')])) for root in roots: try: # https://stackoverflow.com/questions/580924/how-to-access-a-files-properties-on-windows - document = ctypes.wstring_at(os.path.join(root, 'Google', 'Chrome', 'Application', browser_executable + '.exe')) + document = os.path.join(root, 'Google', 'Chrome', 'Application', browser_executable + '.exe') - buffer_size = get_info_size(document, None) - buffer = ctypes.create_string_buffer(buffer_size) - - get_info(document, None, buffer_size, buffer) - - value_size = ctypes.c_uint(0) - value = ctypes.c_void_p(0) - - get_value(buffer, get_string(r"\VarFileInfo\Translation"), ctypes.byref(value), ctypes.byref(value_size)) - - codepages = array.array('H', ctypes.string_at(value.value, value_size.value)) - - language = '{0:04x}{1:04x}'.format(*codepages[:2].tolist()) - - get_value(buffer, get_string('\\StringFileInfo\\' + language + '\\FileVersion'), ctypes.byref(value), ctypes.byref(value_size)) - - version = get_string(value.value, value_size.value - 1) + name = document.replace(os.path.sep, f'{os.path.sep}{os.path.sep}') + + version = subprocess.check_output(['wmic', 'datafile', 'where', f'name="{name}"', 'get', 'Version', '/value']) - return get_major_version(version) + return get_major_version(version.decode('utf-8').strip()) except Exception: pass From a55f8b2f8ce65c675cabaed4b84af0709ec15b33 Mon Sep 17 00:00:00 2001 From: Anderson Carlos Ferreira da Silva Date: Tue, 22 Aug 2023 06:50:40 +0000 Subject: [PATCH 5/6] adding first solution --- chromedriver_binary/utils.py | 38 ++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/chromedriver_binary/utils.py b/chromedriver_binary/utils.py index 2afd37f..40704e0 100644 --- a/chromedriver_binary/utils.py +++ b/chromedriver_binary/utils.py @@ -122,22 +122,30 @@ def get_chrome_major_version(): return get_major_version(version.decode('utf-8')) except Exception: - if sys.platform.startswith('win') or sys.platform.startswith('cygwin'): - roots = list(filter(None, [os.getenv('LocalAppData'), os.getenv('ProgramFiles'), os.getenv('ProgramFiles(x86)'), os.getenv('ProgramW6432')])) - - for root in roots: - try: - # https://stackoverflow.com/questions/580924/how-to-access-a-files-properties-on-windows - document = os.path.join(root, 'Google', 'Chrome', 'Application', browser_executable + '.exe') + if sys.platform.startswith('win') or sys.platform.startswith('cygwin'): + try: + import winreg + with winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon") as key: + version = winreg.QueryValueEx(key, "version")[0] + + return get_major_version(version) + + except Exception: + roots = list(filter(None, [os.getenv('LocalAppData'), os.getenv('ProgramFiles'), os.getenv('ProgramFiles(x86)'), os.getenv('ProgramW6432')])) + + for root in roots: + try: + # https://stackoverflow.com/questions/580924/how-to-access-a-files-properties-on-windows + document = os.path.join(root, 'Google', 'Chrome', 'Application', browser_executable + '.exe') + + name = document.replace(os.path.sep, f'{os.path.sep}{os.path.sep}') + + version = subprocess.check_output(['wmic', 'datafile', 'where', f'name="{name}"', 'get', 'Version', '/value']) + + return get_major_version(version.decode('utf-8').strip()) - name = document.replace(os.path.sep, f'{os.path.sep}{os.path.sep}') - - version = subprocess.check_output(['wmic', 'datafile', 'where', f'name="{name}"', 'get', 'Version', '/value']) - - return get_major_version(version.decode('utf-8').strip()) - - except Exception: - pass + except Exception: + pass pass def check_version(binary, required_version): From 1867ea8b9ef8f880d4c2b0f0110084b382d2926c Mon Sep 17 00:00:00 2001 From: Anderson Carlos Ferreira da Silva Date: Tue, 22 Aug 2023 15:56:36 +0900 Subject: [PATCH 6/6] adding space --- chromedriver_binary/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/chromedriver_binary/utils.py b/chromedriver_binary/utils.py index 40704e0..7acd7c6 100644 --- a/chromedriver_binary/utils.py +++ b/chromedriver_binary/utils.py @@ -148,6 +148,7 @@ def get_chrome_major_version(): pass pass + def check_version(binary, required_version): try: version = subprocess.check_output([binary, '-v'])