-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull_service.py
298 lines (274 loc) · 14.1 KB
/
pull_service.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
from datetime import datetime
from pygit2 import Repository
import os
import pygit2
import stat
import shutil
import sys
import time
import json
import win32serviceutil # ServiceFramework and commandline helper
import win32service # Events
import win32ts
from win32con import MB_SERVICE_NOTIFICATION
import win32security
import ntsecuritycon as con
import os
import servicemanager # Simple setup and logging
TEMP_DIR = '.temp'
MB_SYSTEMMODAL = 0x00001000
class UpdaterService:
"""
A simple service that runs a loop every X minutes.
"""
def init_config(self) -> None:
self.args = {}
try:
if os.path.exists(os.path.join("C:\\", "ProgramData", "autoupdater", ".autopublish.config")):
with open(os.path.join("C:\\", "ProgramData", "autoupdater", ".autopublish.config"), 'r') as f:
self.args = json.load(f)
if os.path.exists(os.path.join(self.args.get("Path", ""), ".autopublish")):
with open(os.path.join(self.args.get("Path", ""), ".autopublish"), 'r') as f:
self.args.update(json.load(f))
except Exception:
pass
def stop(self):
"""Stop the service"""
self.running = False
def run(self):
"""Main service loop. This is where work is done!"""
self.running = True
self.init_config()
with open(os.path.join(self.args.get("Path", ""), "logs.txt"), 'a') as logs:
logs.write(f"{datetime.now().strftime('%d-%m-%Y %H:%M %p')} Starting service\n")
# Take ownership of git folder
try:
if os.path.exists(os.path.join(self.args.get("Path", ""), ".git")):
shutil.copytree(os.path.join(self.args["Path"], ".git"), os.path.join(self.args["Path"], "temp.git"), copy_function=shutil.copyfile)
rmtree(os.path.join(self.args["Path"], ".git"))
os.rename(os.path.join(self.args["Path"], "temp.git"), os.path.join(self.args["Path"], ".git"))
perms(os.path.join(self.args["Path"], ".git"))
except Exception as ex:
console_session = win32ts.WTSGetActiveConsoleSessionId()
raise ex
error("Error while copying git folder, update service will not start", console_session)
logs.write(str(ex) + "\n")
return
while self.running:
servicemanager.LogInfoMsg("Service running...")
try:
pull(self.args)
except Exception as ex:
with open(os.path.join(self.args.get("Path", ""), "logs.txt"), "a") as logs:
logs.write(str(ex) + "\n")
time.sleep(self.args.get('Wait', 1) * 60)
self.init_config()
class AppUpdaterServiceFramework(win32serviceutil.ServiceFramework):
_svc_name_ = 'AppUpdaterService'
_svc_display_name_ = 'App Updater Service'
_svc_description_ = 'Auto update application'
def SvcStop(self):
"""Stop the service"""
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.service_impl.stop()
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
def SvcDoRun(self):
"""Start the service; does not return until stopped"""
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
self.service_impl = UpdaterService()
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
# Run the service
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.service_impl.run()
def init():
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(AppUpdaterServiceFramework)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(AppUpdaterServiceFramework)
class RemoteCallbacks(pygit2.RemoteCallbacks):
def __init__(self, user, token):
self.user = user
self.token = token
def credentials(self, url, username_from_url, allowed_types):
return pygit2.UserPass(self.user, self.token)
def certificate_check(self, certificate, valid, host):
return True
def pull(info):
console_session = win32ts.WTSGetActiveConsoleSessionId()
with open(os.path.join(info.get("Path", ""), "logs.txt"), 'a') as logs:
# Setup Credentials
callbacks = None
if info.get("Username") and info.get("Password"):
callbacks = RemoteCallbacks(info["Username"], info["Password"])
# Get Changes
meta, path, repo = {}, info.get("Path", None), None
try:
# Is there a remote to pull from?
if not "URL" in info or "Path" not in info:
error(f"No remote URL or Path configured, Please run configure.exe", console_session)
return
# Path check
if not os.path.exists(path):
error(f"Path {path} does not exist, Please run configure.exe", console_session)
return
# Set path as current directory
os.chdir(path)
# Check if a temp folder already exists, which it shouldn't
if os.path.exists(f'../{TEMP_DIR}') and os.path.isdir(f'../{TEMP_DIR}'):
error(f"Hey! a folder named {TEMP_DIR} already exists in the parent directory. Please remove it and try again.", console_session)
return
# Git repo check, if not found, clone it
if not os.path.exists(os.path.join(path, ".git")):
try:
pygit2.clone_repository(info["URL"], os.path.join(path, "..", TEMP_DIR), callbacks=callbacks)
except pygit2.GitError as ex:
if "401" in str(ex):
error(f"Failed to clone the remote repo, check credentials.", console_session)
else:
error(f"Failed to clone the remote repo, reconfigure using config.exe", console_session)
return
shutil.copytree(os.path.join(path, "..", TEMP_DIR), os.path.join(path), dirs_exist_ok=True)
rmtree(os.path.join(path, "..", TEMP_DIR))
return
# Copy the .git folder to the temp folder
shutil.copytree(os.path.join(path, ".git"), os.path.join(path, "..", TEMP_DIR, ".git"), dirs_exist_ok=True)
# Open repo & checkout
repo = Repository(os.path.join(path, "..", TEMP_DIR, ".git"))
try:
git_checkout(repo, info.get("Branch", "main"))
except Exception as ex:
error(f"Failed to checkout, please reconfigure using config.exe", console_session)
if repo:
repo.free()
rmtree(os.path.join(path, "..", TEMP_DIR))
return
# Reset hard & pull
repo.reset(repo.head.target, pygit2.GIT_RESET_HARD)
try:
git_pull(repo, 'origin', info.get("Branch", "main"), callbacks=callbacks)
except pygit2.GitError as ex:
if "401" in str(ex):
error(f"Failed to get updates from the remote repo, check credentials.", console_session)
else:
error(f"Failed to get updates from the remote repo, check your internet connection and try again.", console_session)
if repo:
repo.free()
logs.write(str(ex) + "\n")
rmtree(os.path.join(path, "..", TEMP_DIR))
return
# Check if there are any updates in remote by checking the autopublish file
if os.path.exists(os.path.join(path, "..", TEMP_DIR, ".autopublish")):
with open(os.path.join(path, "..", TEMP_DIR, ".autopublish"), 'r') as f:
meta = json.load(f)
if repo:
repo.free()
rmtree(os.path.join(path, "..", TEMP_DIR))
except Exception as ex:
error(f"Failed to get updates into the parent {TEMP_DIR} folder, check permissions and try again.", console_session)
logs.write(str(ex) + "\n")
if repo:
repo.free()
return
# Check if there are changes
if info.get("Tag") == meta.get("Tag"):
logs.write(f"{datetime.now().strftime('%d-%m-%Y %H:%M %p')} Everything is on the version {meta.get('Tag')}!\n")
return
# Prompt user to update
#r = ctypes.windll.user32.MessageBoxW(0, meta.get("UpdateMessage", "Exciting new updates!"), f"Update to {meta.get('Tag', 'v1.0.0')}", 1 | MB_SYSTEMMODAL)
#r = win32ui.MessageBox(meta.get("UpdateMessage", "Exciting new updates!"), f"Update to {meta.get('Tag', 'v1.0.0')}", 1 | MB_SYSTEMMODAL | MB_SERVICE_NOTIFICATION)
r = win32ts.WTSSendMessage(win32ts.WTS_CURRENT_SERVER_HANDLE, console_session, f"Update to {meta.get('Tag', 'v1.0.0')}", meta.get("UpdateMessage", "Exciting new updates!"), 1 | MB_SYSTEMMODAL | MB_SERVICE_NOTIFICATION, 15, True)
if r == 1:
# Open repo & checkout
repo = Repository(os.path.join(path, ".git"))
try:
git_checkout(repo, info.get("Branch", "main"))
except Exception as ex:
error(f"Failed to checkout, please reconfigure using config.exe", console_session)
repo.free()
return
# Reset hard & pull
repo.reset(repo.head.target, pygit2.GIT_RESET_HARD)
try:
git_pull(repo, 'origin', info.get("Branch", "main"), callbacks=callbacks)
except Exception as ex:
error(f"Failed to get updates from the remote repo, check your internet connection and try again.", console_session)
repo.free()
logs.write(str(ex) + "\n")
return
if repo:
repo.free()
success(f"Updated to version {meta.get('Tag', 'v1.0.0')}", console_session)
else:
error(f"Denied an update to {meta.get('Tag', 'v1.0.0')} :(", console_session)
def rmtree(top):
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
filename = os.path.join(root, name)
os.chmod(filename, stat.S_IWUSR)
os.remove(filename)
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(top)
def perms(directory):
userx, domain, type = win32security.LookupAccountName("", "Everyone")
for dirpath, dirnames, filenames in os.walk(directory):
for FILENAME in filenames:
os.chmod(dirpath + '\\' + FILENAME, stat.S_IWUSR)
sd = win32security.GetFileSecurity(dirpath + '\\' + FILENAME, win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl() # instead of dacl = win32security.ACL()
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, userx)
sd.SetSecurityDescriptorDacl(1, dacl, 0)
win32security.SetFileSecurity(dirpath + '\\' + FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)
def error(msg, console_session):
win32ts.WTSSendMessage(win32ts.WTS_CURRENT_SERVER_HANDLE, console_session, "Error", msg, 0 | MB_SYSTEMMODAL | MB_SERVICE_NOTIFICATION, 0, False)
#win32ui.MessageBox(msg, "Error", 0 | MB_SYSTEMMODAL | MB_SERVICE_NOTIFICATION)
def success(msg, console_session):
win32ts.WTSSendMessage(win32ts.WTS_CURRENT_SERVER_HANDLE, console_session, "Yay", msg, 0 | MB_SYSTEMMODAL | MB_SERVICE_NOTIFICATION, 0, False)
#win32ui.MessageBox(msg, "Yay", 0 | MB_SYSTEMMODAL | MB_SERVICE_NOTIFICATION)
def git_pull(repo: Repository, remote_name='origin', branch='main', callbacks=None):
for remote in repo.remotes:
if remote.name == remote_name:
remote.fetch(callbacks=callbacks)
remote_master_id = repo.lookup_reference('refs/remotes/origin/%s' % (branch)).target
merge_result, _ = repo.merge_analysis(remote_master_id)
# Up to date, do nothing
if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE:
return
# We can just fastforward
elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD:
repo.checkout_tree(repo.get(remote_master_id))
try:
master_ref = repo.lookup_reference('refs/heads/%s' % (branch))
master_ref.set_target(remote_master_id)
except KeyError:
repo.create_branch(branch, repo.get(remote_master_id))
repo.head.set_target(remote_master_id)
elif merge_result & pygit2.GIT_MERGE_ANALYSIS_NORMAL:
repo.merge(remote_master_id)
if repo.index.conflicts is not None:
for conflict in repo.index.conflicts:
print('Conflicts found in:', conflict[0].path)
raise AssertionError('Conflicts, ahhhhh!!')
user = repo.default_signature
tree = repo.index.write_tree()
commit = repo.create_commit('HEAD',
user,
user,
'Merge!',
tree,
[repo.head.target, remote_master_id])
# We need to do this or git CLI will think we are still merging.
repo.state_cleanup()
else:
raise AssertionError('Unknown merge analysis result')
def git_checkout(repo, branch='main'):
branch = repo.lookup_branch(branch)
ref = repo.lookup_reference(branch.name)
repo.checkout(ref)
if __name__ == '__main__':
# UpdaterService().run()
init()