summaryrefslogtreecommitdiffstats
path: root/base/update.py
blob: 5953cee1965b9d7dcb0bb5cea053a0755e3ab10b (plain)
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
#!/usr/bin/env python2

#   update.py
#   Copyright (C) 2011  Hamed Saleh and Mahrud Sayrafi

#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import MySQLdb

HOST = os.getenv("DB_HOST")
USER = os.getenv("DB_USERNAME")
PASS = os.getenv("DB_PASSWORD")
NAME = os.getenv("DB_NAME")

conn = MySQLdb.connection (host=HOST, user=USER, passwd=PASS, db=NAME)

strstat = [ 'Accepted!', 'Wrong answer', 'Time limit exceeded', 'Memory limit exceeded',
		'Run time error', 'Unexpected error', 'Signal #' ]

def status (query, SUBID, status, test = -1, time = -1, mem = -1, score = -1):
	if query == 'COMPILE':
		if status == -1:
			msg = 'compiling...'
		elif status == 0:
			msg = 'successfully compiled'
		elif status == 124:
			msg = 'compile time limit exceeded'
		else:
			msg = 'compilation error'

		conn.query ("""UPDATE `submitions` 
						SET status='{MSG}' WHERE id='{SUBID}'""".format (MSG = msg, SUBID = SUBID))

	elif query == 'RUN':
		if status == -1:
			if test == -1:
				msg = 'running...'
			else:
				msg = 'running on test ' + str (test + 1)
		else:
			msg = 'running on test ' + str (test + 1)

		conn.query ("UPDATE `submitions` SET status='{MSG}' WHERE id='{SUBID}'".format (MSG = msg, SUBID = SUBID))
		
		return status > 0

	elif query == 'END':
		if status == -1:
			status = 5;

		msg = strstat [min (6, status)]

		if status > 5:
			msg += str (status - 6)

		if status > 0:
			msg += ' on test ' + str (test + 1)

		conn.query ("""UPDATE `submitions` 
						SET status='{MSG}' WHERE id='{SUBID}'""".format (MSG = msg, SUBID = SUBID))