Advertisement
TheGhostOfInky

Untitled

Apr 22nd, 2023
311
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 1 0
  1. import os, winreg, re, subprocess, win32api
  2. from typing import Any
  3.  
  4.  
  5. def appdata_folder_version(path: str, reg: str) -> str | None:
  6.     pattern = re.compile(reg)
  7.     base_path = os.getenv("userprofile", "") + "\\AppData\\Local\\" + path
  8.     read_path = os.listdir(base_path)
  9.    
  10.     folders = [x for x in read_path if os.path.isdir(base_path + "\\" + x)]
  11.     matches: list[str] = []
  12.     for folder in folders:
  13.         match = pattern.match(folder)
  14.         if not match:
  15.             continue
  16.         ver: str = match.groups()[0]
  17.         matches.append(ver)
  18.    
  19.     if matches:
  20.         sorted_matches = sorted(
  21.             matches,
  22.             key=lambda x: tuple(int(v) for v in x.split("."))
  23.         )
  24.         return sorted_matches[-1]
  25.  
  26.  
  27. def parse_cli(cmd: str, reg: str) -> str | None:
  28.     pattern = re.compile(reg)
  29.     _, ret = subprocess.getstatusoutput(cmd)
  30.     match = pattern.match(ret)
  31.     if match:
  32.         return match.groups()[0]
  33.  
  34.  
  35. def get_file_version(path: str) -> str:
  36.     raw_version: dict[str, int] = win32api.GetFileVersionInfo(
  37.         path, "\\")  # type:ignore
  38.     ms1, ms2 = divmod(raw_version.get("FileVersionMS", 0), 0x10000)
  39.     ls1, ls2 = divmod(raw_version.get("FileVersionLS", 0), 0x10000)
  40.     return "%d.%d.%d.%d" % (ms1, ms2, ls1, ls2)
  41.  
  42.  
  43. reg_lm = winreg.HKEY_LOCAL_MACHINE
  44. reg_cu = winreg.HKEY_CURRENT_USER
  45.  
  46. reg_keys = {
  47.     "Discord.Discord": {
  48.         "regtype": reg_cu,
  49.         "regpath": "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Discord\\",
  50.         "key": "DisplayVersion",
  51.         "version": appdata_folder_version("Discord", r"^app-(\d*\.\d*\.\d*)$")
  52.     },
  53.     "Rustlang.Rustup": {
  54.         "regtype": reg_cu,
  55.         "regpath": "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Rustup\\",
  56.         "key": "DisplayVersion",
  57.         "version": parse_cli("rustup --version", r"^rustup (\d*\.\d*\.\d*) \(")
  58.     },
  59.     "OBSProject.OBSStudio": {
  60.         "regtype": reg_lm,
  61.         "regpath": "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OBS Studio\\",
  62.         "key": "DisplayVersion",
  63.         "version": get_file_version("C:\\Program Files\\obs-studio\\bin\\64bit\\obs64.exe")
  64.     },
  65.     "Ubisoft.Connect": {
  66.         "regtype": reg_lm,
  67.         "regpath": "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Uplay\\",
  68.         "key": "DisplayVersion",
  69.         "version": get_file_version("C:\\Program Files (x86)\\Ubisoft\\Ubisoft Game Launcher\\UbisoftGameLauncher.exe")
  70.     }
  71. }
  72.  
  73.  
  74. def read_reg(name: str, params: dict[str, Any]):
  75.     p_version = params["version"]
  76.     if not p_version:
  77.         p_version = input(f"Version for {name}:")
  78.  
  79.     key = winreg.OpenKeyEx(
  80.         params["regtype"], params["regpath"], 0, winreg.KEY_ALL_ACCESS)
  81.     val, ftype = winreg.QueryValueEx(key, params["key"])
  82.  
  83.     if p_version and  ftype == winreg.REG_SZ and val != p_version:
  84.         winreg.SetValueEx(
  85.             key,
  86.             params["key"],
  87.             0,
  88.             winreg.REG_SZ,
  89.             p_version
  90.         )
  91.         print(f"Changed {name} version to {p_version}")
  92.     winreg.CloseKey(key)
  93.  
  94.  
  95. for k, v in reg_keys.items():
  96.     read_reg(k,v)
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement