-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselfUpdate.py
More file actions
86 lines (75 loc) · 2.75 KB
/
selfUpdate.py
File metadata and controls
86 lines (75 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 1.0
import sys
import requests
import os
import time
import re
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)
def checkForUpdate(force: int):
localFile = os.path.abspath(__file__)
repoUrl = "https://raw.githubusercontent.com/ryder7223/Random/refs/heads/main/letsGoGambling.py"
print("Checking for updates...")
time.sleep(1)
# Read local version
try:
with open(localFile, "r", encoding="utf-8") as f:
firstLine = f.readline().strip()
localVersionMatch = re.match(r"#\s*([\d.]+)", firstLine)
if not localVersionMatch:
print("Unable to read local version number.")
time.sleep(1)
return
localVersion = localVersionMatch.group(1)
except Exception as e:
print(f"Error reading local version: {e}")
time.sleep(1)
return
# Get version from GitHub
try:
response = requests.get(repoUrl, timeout=10, verify=False)
if response.status_code != 200:
print(f"Failed to fetch version (HTTP {response.status_code}).")
time.sleep(1)
return
remoteText = response.text
remoteFirstLine = remoteText.splitlines()[0].strip()
remoteVersionMatch = re.match(r"#\s*([\d.]+)", remoteFirstLine)
if not remoteVersionMatch:
print("Unable to read version number.")
time.sleep(1)
return
remoteVersion = remoteVersionMatch.group(1)
except Exception:
print(f"Unable to fetch newest version, proceeding offline.")
time.sleep(1)
return
# Compare versions
def versionTuple(v: str):
return tuple(map(int, v.split('.')))
updateNeeded = False
if force == 1:
print("Updating...")
updateNeeded = True
elif versionTuple(remoteVersion) > versionTuple(localVersion):
print(f"New version available ({localVersion} → {remoteVersion}). Updating...")
updateNeeded = True
else:
print(f"Version {localVersion} is up to date.")
time.sleep(1)
if updateNeeded:
time.sleep(1)
try:
normalizedText = re.sub(r'\n{3,}', '\n\n', remoteText.replace('\r\n', '\n'))
with open(localFile, "w", encoding="utf-8") as f:
f.write(normalizedText)
print("Update complete. Restarting...")
time.sleep(1)
except Exception as e:
print(f"Failed to write updated file: {e}")
time.sleep(1)
return
# Restart script
os.execv(sys.executable, [sys.executable] + sys.argv)
checkForUpdate()