Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, winreg, re, subprocess, win32api
- from typing import Any
- def appdata_folder_version(path: str, reg: str) -> str | None:
- pattern = re.compile(reg)
- base_path = os.getenv("userprofile", "") + "\\AppData\\Local\\" + path
- read_path = os.listdir(base_path)
- folders = [x for x in read_path if os.path.isdir(base_path + "\\" + x)]
- matches: list[str] = []
- for folder in folders:
- match = pattern.match(folder)
- if not match:
- continue
- ver: str = match.groups()[0]
- matches.append(ver)
- if matches:
- sorted_matches = sorted(
- matches,
- key=lambda x: tuple(int(v) for v in x.split("."))
- )
- return sorted_matches[-1]
- def parse_cli(cmd: str, reg: str) -> str | None:
- pattern = re.compile(reg)
- _, ret = subprocess.getstatusoutput(cmd)
- match = pattern.match(ret)
- if match:
- return match.groups()[0]
- def get_file_version(path: str) -> str:
- raw_version: dict[str, int] = win32api.GetFileVersionInfo(
- path, "\\") # type:ignore
- ms1, ms2 = divmod(raw_version.get("FileVersionMS", 0), 0x10000)
- ls1, ls2 = divmod(raw_version.get("FileVersionLS", 0), 0x10000)
- return "%d.%d.%d.%d" % (ms1, ms2, ls1, ls2)
- reg_lm = winreg.HKEY_LOCAL_MACHINE
- reg_cu = winreg.HKEY_CURRENT_USER
- reg_keys = {
- "Discord.Discord": {
- "regtype": reg_cu,
- "regpath": "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Discord\\",
- "key": "DisplayVersion",
- "version": appdata_folder_version("Discord", r"^app-(\d*\.\d*\.\d*)$")
- },
- "Rustlang.Rustup": {
- "regtype": reg_cu,
- "regpath": "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Rustup\\",
- "key": "DisplayVersion",
- "version": parse_cli("rustup --version", r"^rustup (\d*\.\d*\.\d*) \(")
- },
- "OBSProject.OBSStudio": {
- "regtype": reg_lm,
- "regpath": "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OBS Studio\\",
- "key": "DisplayVersion",
- "version": get_file_version("C:\\Program Files\\obs-studio\\bin\\64bit\\obs64.exe")
- },
- "Ubisoft.Connect": {
- "regtype": reg_lm,
- "regpath": "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Uplay\\",
- "key": "DisplayVersion",
- "version": get_file_version("C:\\Program Files (x86)\\Ubisoft\\Ubisoft Game Launcher\\UbisoftGameLauncher.exe")
- }
- }
- def read_reg(name: str, params: dict[str, Any]):
- p_version = params["version"]
- if not p_version:
- p_version = input(f"Version for {name}:")
- key = winreg.OpenKeyEx(
- params["regtype"], params["regpath"], 0, winreg.KEY_ALL_ACCESS)
- val, ftype = winreg.QueryValueEx(key, params["key"])
- if p_version and ftype == winreg.REG_SZ and val != p_version:
- winreg.SetValueEx(
- key,
- params["key"],
- 0,
- winreg.REG_SZ,
- p_version
- )
- print(f"Changed {name} version to {p_version}")
- winreg.CloseKey(key)
- for k, v in reg_keys.items():
- read_reg(k,v)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement