This repository was archived by the owner on Sep 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
148 lines (126 loc) · 4.62 KB
/
server.py
File metadata and controls
148 lines (126 loc) · 4.62 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import uuid
import zipfile
import sys
import platform
import shutil
import subprocess
from flask import Flask, request, jsonify
from config import *
from core.handler import Handler
from core.tester import Tester
def verify_token(data):
try:
try:
self_token = open(TOKEN_FILE_PATH, 'r').read().strip()
except OSError:
self_token = ''
if data.get('username') == 'token' and data.get('password') == self_token:
return True
return False
except KeyError:
return False
@app.route('/upload/<tag>/<pid>', methods=['POST'])
def server_upload(tag, pid):
result = {'status': 'reject'}
try:
if int(pid) < 0:
raise ValueError
if verify_token(request.authorization):
if tag == 'pretest':
target_dir = os.path.join(PRETEST_DIR, pid)
elif tag == 'data':
target_dir = os.path.join(DATA_DIR, pid)
else:
raise ValueError
if os.path.exists(target_dir):
shutil.rmtree(target_dir)
os.mkdir(target_dir)
source_path = os.path.join(TMP_DIR, str(uuid.uuid1()) + '.zip')
with open(source_path, 'wb') as f:
f.write(request.data)
source_zip = zipfile.ZipFile(source_path)
source_zip.extractall(target_dir)
source_zip.close()
result['status'] = 'received'
os.remove(source_path)
except Exception as e:
print(e)
return jsonify(result)
@app.route('/judge', methods=['POST'])
def server_judge():
result = {'status': 'reject'}
if request.is_json:
try:
if verify_token(request.authorization):
result.update(Handler(request.get_json()).run())
result['status'] = 'received'
except Exception as e:
print(e)
return jsonify(result)
@app.route('/test', methods=['POST'])
def server_test():
result = {'status': 'reject'}
if request.is_json:
try:
if verify_token(request.authorization):
result.update(Tester(request.get_json()).test())
result['status'] = 'received'
except Exception as e:
print(e)
return jsonify(result)
@app.route('/info', methods=['GET'])
def server_info():
result = {'status': 'received', 'error': 'not responding'}
try:
# System Information
result['system'] = ', '.join(platform.uname())
cpu_info = []
with open('/proc/cpuinfo') as f:
for line in f:
if line.strip():
if line.rstrip('\n').startswith('model name'):
model_name = line.rstrip('\n').split(':')[1]
cpu_info.append(model_name.strip())
result['cpu'] = ', '.join(cpu_info)
mem_info = []
with open('/proc/meminfo') as f:
for line in f:
if line.strip():
if line.rstrip('\n').startswith('MemTotal'):
mem_total = line.rstrip('\n').split(':')[1]
mem_info.append(mem_total.strip())
result['memory'] = ', '.join(mem_info)
result['cpp'] = os.popen('g++ --version').readline().strip()
result['java'] = ''
java_info_path = os.path.join(TMP_DIR, str(uuid.uuid1()))
if os.system('java -version 2> ' + java_info_path) == 0:
with open(java_info_path, 'r') as f:
java_info = []
for line in f:
if line.strip():
java_info.append(line.strip())
result['java'] = ', '.join(java_info)
result['python'] = os.popen('python3 --version').readline().strip()
if len(os.popen('ps aux | grep redis | grep -v grep').readlines()) == 0:
raise Exception('Redis is not running')
if len(os.popen('ps aux | grep celery | grep -v grep').readlines()) == 0:
raise Exception('Celery is not running')
result['status'] = 'ok'
result['error'] = ''
except Exception as e:
result['status'] = 'failure'
result['error'] = str(e)
return jsonify(result)
@app.route('/update_token/<token>', methods=['POST'])
def server_token_update(token):
result = {'status': 'reject'}
try:
if verify_token(request.authorization):
with open(TOKEN_FILE_PATH, 'w') as f:
f.write(token)
result['status'] = 'received'
except Exception as e:
print(e)
return jsonify(result)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4999, debug=False)