import os
import re

def find_similar_versions(version, version_list):

    # 将version字符串转换为正则表达式模式
    version_pattern = re.sub(r'\d+', r'\\d+', re.escape(version))

    # 遍历版本列表
    for v in version_list:
        # 使用 re.fullmatch 检查匹配
        match = re.fullmatch(version_pattern, v)

        # 如果版本与给定 version 不同但格式相同,则打印出来
        if match and match.group() != version:
            # print(match.group())
            return match.group()



def notupdate_chrome(need_version = '101.0.4951.67',install_path = r"D:\Nextcloud\必要软件\101.0.4951.67_chrome_installerx64.exe", dirver_path=r"B:\datagrand\安装包\chromedriver.exe"):
    
    import subprocess
    if not os.path.exists(install_path):
        print("没有本地安装包,请放好安装包到【%s】,结束" % install_path)
        return
    else:
        print("有本地安装包,进入下一步检查版本")

    # 找到Chrome的安装路径
    chrome_path = ''
    chrome_path1 = os.getenv('LOCALAPPDATA') + '\\Google\\Chrome\\Application\\'
    chrome_path2 = 'C:\\Program Files\\Google\\Chrome\\Application\\'
    
    # C:\Program Files\Google\Chrome\Application\106.0.5249.103

    print(chrome_path)
    for path in [chrome_path1, chrome_path2]:
        if os.path.exists(os.path.join(path, 'chrome.exe')):
            chrome_path = path
            break
    else:
        print("没有安装,需要安装谷歌浏览器")
        os.system(install_path)
        return
    # 获取Chrome的版本号
    version_dirs = [d for d in os.listdir(chrome_path) if os.path.isdir(os.path.join(chrome_path, d))]
    version_dirs.sort(reverse=True)
    print(version_dirs, len(version_dirs))
    
    try:
        result = find_similar_versions(need_version, version_dirs)
        if result:
            print(result)
            version_dirs.remove(need_version)
    except:
        pass

    # # 执行卸载命令
    if version_dirs:
        if  version_dirs[-1] != need_version or len(version_dirs) > 3:
            
            uninstall_command2 = '"' + chrome_path + version_dirs[-1] + '\\Installer\\setup.exe' + '"' + ' --uninstall --force-uninstall'
            
            uninstall_command = '"' + chrome_path + version_dirs[-1] + '\\Installer\\setup.exe' + '"' + ' --uninstall --system-level --verbose-logging --force-uninstall'
            print('不满足版本号,卸载%s' % uninstall_command)
            print('不满足版本号,卸载%s' % uninstall_command2)

            # uninstall_command2

            os.system("taskkill /f /t /im chrome.exe")
            os.system("taskkill /f /t /im GoogleUpdate.exe")
            os.system("taskkill /f /t /im GoogleCrashHandler.exe")
            os.system("taskkill /f /t /im GoogleCrashHandler64.exe")

            subprocess.run(uninstall_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
            subprocess.run(uninstall_command2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

            os.system(install_path)

            if not os.path.exists(os.path.join(chrome_path, 'chromedriver.exe')):
                print("chromedriver不存在需要复制")
                import shutil
                shutil.copy(dirver_path, chrome_path)
                
            else:
                print("chromedriver.exe存在不需要复制")

        else:
            print("满足版本号")
    return chrome_path
# C:\Users\Administrator\AppData\Local\Google\Chrome\Application\

# 命令行	"C:\Program Files\Google\Chrome\Application\chrome.exe" --from-installer --flag-switches-begin --flag-switches-end --origin-trial-disabled-features=WebGPU
# 可执行文件路径	C:\Program Files\Google\Chrome\Application\chrome.exe
# 个人资料路径	C:\Users\Administrator\AppData\Local\Google\Chrome\User
for i in range(3):
    chrome_path = notupdate_chrome(need_version = '101.0.4951.67',install_path = r"B:\datagrand\安装包\101.0.4951.67_chrome_installerx64.exe", dirver_path = r"B:\datagrand\安装包\chromedriver.exe")
    if os.path.exists(os.path.join(chrome_path, 'chrome.exe')):
        break