-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChocolateyUpdate.py
161 lines (144 loc) · 6.35 KB
/
ChocolateyUpdate.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# https://github.com/Technetium1
# Licensed under The Unlicense [unlicense.org]
from os import system
from os import remove
from os import rename
from shutil import which
from pathlib import Path
from ctypes import windll
import urllib3
import json
import certifi
import sys
version = "2.6"
def printascii():
system("cls")
system("title Tech\'s Chocolatey Updater V" + version)
print(r"""
.---..-- .- . .\\ ,-. .- . . .-. .- .-. . .. .---..--. . . . .-. .-. .. .---..--.-.
| | / | | ( ` / | |/ \/ / \| / \ | | \ / | | | )| \/ \ | | | )
| |- | |--| `-. | |--|| || | || |--| | |- Y | | |-' | ||--| | |- |-<
| | \ | | . ) \ | |\ /\ \ /| | | | | | | | | | /| | | | | \
' '-- `- ' ' `-' `- ' ' '-' `- `-' `--' ' ' `-- ' `--` ' '-' ' ' ' '--' '
""")
print("\n" + "v" + version + "\n")
def nopackagefile():
print(r"""
####################################################
######## ######## ######## ####### ######## ##
## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ##
###### ######## ######## ## ## ######## ##
## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ##
######## ## ## ## ## ####### ## ## ##
######## ERROR: NO PACKAGES FILE WAS FOUND! ########
This script needs a package list to work properly
Since it didn't exist the default was downloaded!
Edit out any unwanted programs and then rerun!
####################################################
""")
packagesurl = "https://raw.githubusercontent.com/Technetium1/ChocolateyUpdate/master/ChocolateyPackages.txt"
http = urllib3.PoolManager(ca_certs=certifi.where())
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
r = http.request(
"GET",
packagesurl,
timeout=urllib3.Timeout(connect=15.0, read=15.0),
retries=4,
redirect=False)
with open("ChocolateyPackages.txt", "wb") as chocopkgs:
chocopkgs.write(r.data)
system("pause")
raise SystemExit
def installpackages():
with open("ChocolateyPackages.txt", "r") as file:
updates = file.read().replace("\n", " ")
print("FOUND PACKAGES: " + updates)
up = "choco upgrade -y "
print("\n\n\n~~~~~~~~~~~~~~~~~~~~\nUPDATING CHOCOLATEY!\n~~~~~~~~~~~~~~~~~~~~\n\n\n")
system(up + "chocolatey")
print("\n\n\n~~~~~~~~~~~~~~~~~~\nUPDATING PACKAGES!\n~~~~~~~~~~~~~~~~~~\n\n\n")
system(up + updates)
print("\nSUCCESS!\n")
system("pause")
def checkforpackages():
if Path("ChocolateyPackages.txt").exists():
installpackages()
else:
nopackagefile()
def admincheck():
if windll.shell32.IsUserAnAdmin():
printascii()
stopcontrolledfolderaccess = "powershell.exe -Command Set-MpPreference -EnableControlledFolderAccess Disabled > nul 2>&1"
system(stopcontrolledfolderaccess)
if Path("OldChocolateyUpdate.exe").exists():
remove("OldChocolateyUpdate.exe")
if getattr(sys, "frozen", True):
selfupdate()
if which("choco") is not None:
checkforpackages()
else:
print("NO CHOCOLATEY INSTALLED!")
installchoco = "start /wait powershell.exe Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
system(installchoco)
print("\nPlease restart the program!\n")
system("pause")
raise SystemExit
else:
printascii()
print("ATTEMPTING TO GET ADMINISTRATOR PERMISSIONS!")
windll.shell32.ShellExecuteW(
None, "runas", sys.executable, sys.argv[0], None, 1)
def selfupdate():
updateurl = "https://api.github.com/repos/Technetium1/ChocolateyUpdate/releases/latest"
http = urllib3.PoolManager(ca_certs=certifi.where())
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
r = http.request(
"GET",
updateurl,
headers={"User-Agent": "curl", "Accept": "application/vnd.github.v3+json"},
timeout=urllib3.Timeout(connect=15.0, read=15.0),
retries=3,
redirect=False)
updateresult = json.loads(r.data.decode("utf-8"))
if updateresult.get("tag_name", None) is None:
print("\nFAILED SELF-UPDATE! GITHUB RATE LIMIT LIKELY EXCEEDED ON YOUR IP!\n")
print("\nCONTINUING WITHOUT SELF-UPDATE!\n")
system("pause")
print()
else:
currentversion = updateresult["tag_name"]
downloadlink = updateresult["assets"][0]["browser_download_url"]
if currentversion > version:
print("\nNewer version found on GitHub!\n")
elif currentversion < version:
print("\nVersion newer than what was found on GitHub!\n")
elif currentversion == version:
print("\nAlready at latest GitHub release!\n")
else:
print("\nSomething unexpected happened while updating! If this continues please report to https://github.com/Technetium1/ChocolateyUpdate\n")
system("pause")
raise SystemExit
if updateresult["assets"] and currentversion > version:
print("Downloading new release from: " + downloadlink)
if Path("NewChocolateyUpdate.exe").exists():
remove("NewChocolateyUpdate.exe")
if Path("OldChocolateyUpdate.exe").exists():
remove("OldChocolateyUpdate.exe")
r = http.request(
"GET",
downloadlink,
headers={"User-Agent": "curl"},
timeout=urllib3.Timeout(connect=15.0, read=120.0),
retries=2,
redirect=True)
with open("NewChocolateyUpdate.exe", "wb") as downloadedfile:
downloadedfile.write(r.data)
rename("ChocolateyUpdate.exe", "OldChocolateyUpdate.exe")
system("attrib +h OldChocolateyUpdate.exe")
rename("NewChocolateyUpdate.exe", "ChocolateyUpdate.exe")
print("Update completed! Restart ChocolateyUpdate to complete!")
system("pause")
raise SystemExit
admincheck()