-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
108 lines (92 loc) · 3.39 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
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
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from datetime import datetime, timedelta
import yaml
import subprocess
import os
from subprocess import PIPE, Popen
SKIP_CHECKS = []
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def load_exercise_order():
with open("exercises/exercises.yaml") as f:
exercises = yaml.safe_load(f)
return exercises
def flake_check(exercise, flake8_check=True):
flake8_cmd = "flake8 exercises/{exercise}.py".format(
exercise=exercise)
if flake8_check:
exit_code = subprocess.call(
flake8_cmd, shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT
)
if exit_code != 0:
p = Popen(["flake8",
"exercises/{exercise}.py".format(
exercise=exercise)],
stdin=PIPE,
stdout=PIPE)
output, err = p.communicate()
return output.decode("utf-8")
def check_exercises(flake8_check=False):
for exercise in exercises:
if exercise not in SKIP_CHECKS:
cmd = "python3 exercises/{exercise}.py".format(
exercise=exercise)
try:
flake_check(exercise, flake8_check)
exit_code = subprocess.call(cmd,
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT)
if exit_code != 0:
print(
"❌ exercises/{exercise}.py failed".format(
exercise=exercise)
)
return subprocess.check_output(cmd, shell=True)
else:
print(
"✅ exercises/{exercise}.py passed".format(
exercise=exercise)
)
SKIP_CHECKS.append(exercise)
except subprocess.CalledProcessError:
break
class ModificationWatcher(FileSystemEventHandler):
def __init__(self):
self.last_modified = datetime.now()
def on_modified(self, event):
# On Linux and inside the container, it will double report file changes
# so this prevents that from happening.
if datetime.now() - self.last_modified < timedelta(seconds=1):
return
else:
self.last_modified = datetime.now()
if os.getenv('EGGLINGS_FLAKE8'):
clear_screen()
check_exercises(flake8_check=True)
else:
clear_screen()
check_exercises(flake8_check=False)
if __name__ == "__main__":
exercises = load_exercise_order().get('exercises', [])
event_handler = ModificationWatcher()
observer = Observer()
observer.schedule(event_handler, path='exercises/', recursive=False)
observer.start()
try:
if os.getenv('EGGLINGS_FLAKE8'):
exercise_check = check_exercises(flake8_check=True)
else:
exercise_check = check_exercises(flake8_check=False)
if exercise_check:
print(exercise_check)
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
exit(0)
observer.join()