-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_send2CSE.py
121 lines (101 loc) · 3.95 KB
/
auto_send2CSE.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
# By
# ████████╗██╗ ██╗ █████╗ ███╗ ██╗ ██╗ ██╗ ██████╗
# ╚══██╔══╝██║ ██║██╔══██╗████╗ ██║ ██║ ██║██╔═══██╗
# ██║ ██║ ██║███████║██╔██╗ ██║ ███████║██║ ██║
# ██║ ██║ ██║██╔══██║██║╚██╗██║ ██╔══██║██║ ██║
# ██║ ╚██████╔╝██║ ██║██║ ╚████║ ██║ ██║╚██████╔╝
# ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═════╝
# Email: ttuan.ho@outlook.com
import os, sys
import time
from datetime import datetime
import subprocess
from config.config import check_log_file_str, sending_py_file_str, submit_log_str, git_log_str, sending_ans_files_str
def sh(cmd):
subprocess.call(cmd, shell=True)
"""
Configuring data
"""
# zid
zid = "z5261243"
className = "cs2511"
# folder to store on the cse machine
examFolder = "exam-demo"
# git repo for back up
remote_repo_url = "https://github.com/ttuanho/exam-wrapper.git"
questions = {
"part2Q1A": False,
"part2Q1B": False,
"part2Q1C": False,
"part2Q2A": False,
"part2Q2B": False,
"part2Q2C": False,
"part2Q3": True,
"part3Q1": True,
"part3Q2": True,
"part3Q3" : True
}
# the seconds to resubmit periodically
submitInterval = 8
# the end time to submit
# Attributes: year, month, day, hour, minute, second, microsecond
deadline = datetime(2020,8,16,22,49,59).timestamp()
lastSubmitTime = None
lastSendTime = None
def createFiles():
for d, f in questions.items():
if (os.path.exists(f"./answers/{d}") == False):
sh(f"mkdir ./answers/{d}")
if (f == False and os.path.exists(f"./answers/{d}/{d}.txt") == False):
# sh(f"touch answers/{d}/{d}.txt")
sh(f"echo \"Created on `date`\" > answers/{d}/{d}.txt")
sh(f"touch {git_log_str}")
sh(f"touch {submit_log_str}")
sh(f"touch {check_log_file_str}")
sh(f"touch {sending_ans_files_str}")
sh(f"touch {sending_py_file_str}")
sh(f"scp -r config/ {zid}@cse.unsw.edu.au:~/{examFolder}")
def backup2CSE():
"""
Back up relevant files in the current dir to cse root/examFolder
"""
sh(f"scp -r answers/ {zid}@cse.unsw.edu.au:~/{examFolder}")
sh(f"scp -r *.py {zid}@cse.unsw.edu.au:~/{examFolder}")
def backupGit(msg):
"""
Git wrapper
"""
if (os.path.exists(".git") == False):
sh("git init")
sh("git add .")
sh("git commit -m \"intial commit\"")
sh(f"git remote add origin {remote_repo_url}")
sh("git push -f -u origin master")
else:
sh("git add .")
now_str = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
sh(f"git commit -m \"Bot@{now_str}:{msg}\"")
def pull_logs_back2Local():
sh(f"scp -r {zid}@cse.unsw.edu.au:~/{examFolder}/logs .")
# This func is not needed if running another process on cse
def submit_subshell():
sh(f"ssh -Y {zid}@cse.unsw.edu.au 'cd {examFolder} && python3 auto_submit.py && logout'")
def timer():
while (True):
global lastSendTime
now = time.time()
if (now < deadline and\
((lastSendTime == None) or\
(now - lastSendTime > submitInterval))):
backup2CSE()
backupGit(" committed changes")
submit_subshell()
pull_logs_back2Local()
lastSendTime = time.time()
if (time.time() > deadline):
pull_logs_back2Local()
print(f">> Time out: the script now breaks")
break
if __name__ == "__main__":
createFiles()
timer()