-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (40 loc) · 1.5 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
import os
import time
import subprocess
import sys
class PyReload:
def __init__(self, interval: float = 1) -> None:
self.interval = interval
self.last_time_modified = time.time()
if not (len(sys.argv) >= 2):
raise ValueError("You must provide file to run")
self.filename, self.args = sys.argv[1], sys.argv[2:]
self.current_subprocess: subprocess.Popen | None = None
if self.is_valid_input():
self.loop()
else:
raise FileNotFoundError(self.filename)
def is_valid_input(self) -> bool:
return os.path.isfile(self.filename)
def get_modified_time(self) -> float:
return os.stat(self.filename).st_mtime
def modified(self) -> bool:
return self.get_modified_time() != self.last_time_modified
def run_script(self) -> subprocess.Popen:
args = [sys.executable, self.filename] + self.args
print(f"Reloading: {self.filename}")
return subprocess.Popen(args)
def stop_current_subprocess(self) -> bool:
if self.current_subprocess != None:
self.current_subprocess.kill()
return True
return False
def loop(self):
while True:
time.sleep(self.interval)
if self.modified():
self.last_time_modified = self.get_modified_time()
self.stop_current_subprocess()
self.current_subprocess = self.run_script()
if __name__ == "__main__":
pyreload = PyReload()