This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
61 lines (47 loc) · 1.7 KB
/
main.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
from config import git_local_path, target_program, pull_wait
from time import sleep
import subprocess as sp
import git
git_local_path += "/"
target_program += ".py"
def git_pull_change(path):
repo = git.Repo(path)
current = repo.head.commit
repo.remotes.origin.pull()
if current == repo.head.commit:
print("Repo not changed. Sleep mode activated.")
return False
else:
print("Repo changed! Activated.")
return True
def target_start(target):
# Thanks! https://stackoverflow.com/questions/5611576/how-do-i-spawn-a-separate-python-process
return sp.Popen(["python3", git_local_path + target]).returncode # assumes target in root of git directory
def target_stop(target):
return sp.run(["pkill", "-f", target]).returncode
def target_restart(target):
stopped = target_stop(target)
print(f"stopped={stopped}")
# stop_count = 0
while stopped != 0: # naive assumption that the program will close gracefully without returning errors
print("Can't stop")
stopped = target_stop(target)
# stop_count += 1
# if stop_count > 100: # a branchless no no, but then again this is not assembly so meh.
# pass # you might like to add an action here
started = target_start(target)
print(f"started={started}")
# start_count = 0
while started is not None:
print("Can't start")
started = target_start(target)
# start_count += 1
# if start_count > 100:
# pass # you might like to add an action here
target_start(target_program)
while True:
if git_pull_change(git_local_path):
target_restart(target_program)
else:
pass
sleep(pull_wait)