-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
46 lines (36 loc) · 1.53 KB
/
compile.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
from werkzeug.utils import secure_filename
import os.path
import subs
def makedir(filename, referrer):
if not os.path.isdir('referrer_reqs'):
print("referrer_reqs directory not found...", end=' ')
print("creating referrer_reqs directory")
os.mkdir('referrer_reqs')
dir_path = 'referrer_reqs' + os.path.sep + referrer + os.path.sep
if not os.path.isdir(dir_path):
os.mkdir(dir_path)
filename = secure_filename(filename)
file_path = dir_path + os.path.sep + filename
if os.path.isfile(file_path):
print(f"deleting {referrer=} past request file")
os.unlink(file_path)
return file_path
def find_ques(file_path):
assert os.path.isfile(file_path), "file has to be present."
with open(file_path, "r") as file:
first_line = file.readline()
first_line = first_line.strip('#').strip('/**/').strip('//').strip()
if first_line.isdigit():
return (True, int(first_line))
return (False, "\033[31mERROR: \033[0mFirst line in the file is not question number.")
def process(file, referrer):
file_ext = file.filename[file.filename.rfind('.'):]
file_path = makedir(file.filename, referrer)
file.save(file_path)
find_ques_res = find_ques(file_path)
if not find_ques_res[0]:
print(f"{referrer}: first line is not question number...returning error message")
return (False, find_ques_res[1])
question_id = find_ques_res[1]
subs_res = subs.compilation(question_id, file_path, file_ext)
return subs_res