4
4
import os
5
5
import json
6
6
import urllib
7
- import tempfile
8
- import logging
9
7
import logging .handlers
8
+ import logging
10
9
import shutil
11
- import hashlib
10
+ from hashlib import md5
12
11
import time
13
12
import stat
14
13
import platform
18
17
from optparse import OptionParser
19
18
20
19
from server_info import server_info
21
- from submission_hash import hash_file_sha
22
20
23
21
import compiler
24
22
from engine import run_game
28
26
log .setLevel (logging .INFO )
29
27
log_file = os .path .join (server_info ['logs_path' ], 'worker.log' )
30
28
handler = logging .handlers .RotatingFileHandler (log_file ,
31
- maxBytes = 1000000 ,
29
+ maxBytes = 10000000 ,
32
30
backupCount = 5 )
33
31
handler .setLevel (logging .INFO )
34
32
handler2 = logging .StreamHandler ()
@@ -125,20 +123,20 @@ def get_map(self, map_filename):
125
123
url = '%s/maps/%s' % (self .base_url , map_filename )
126
124
log .info ("Downloading map %s" % url )
127
125
data = urllib .urlopen (url ).read ()
128
- return data .read ()
126
+ log .debug (data )
127
+ return data
129
128
except Exception as ex :
130
129
log .error ("Get map error: %s" % ex )
131
130
return None
132
131
133
- # TODO: save failed posts locally and retry on worker startup
134
132
def post_result (self , method , result ):
135
133
# retry 10 times or until post is successful
136
134
retry = 1
137
135
for i in range (retry ):
138
136
url = self .get_url (method )
139
137
log .info (url )
140
138
json_data = json .dumps (result )
141
- hash = hashlib . md5 (json_data ).hexdigest ()
139
+ hash = md5 (json_data ).hexdigest ()
142
140
log .debug ("Posting result %s: %s" % (method , json_data ))
143
141
log .info ("Posting hash: %s" % hash )
144
142
response = urllib .urlopen (url , json .dumps (result ))
@@ -152,7 +150,7 @@ def post_result(self, method, result):
152
150
break
153
151
elif i < retry - 1 :
154
152
time .sleep (5 )
155
- except ValueError as ex :
153
+ except ValueError :
156
154
log .info ("Bad json from server during post result: %s" % data )
157
155
if i < retry - 1 :
158
156
time .sleep (5 )
@@ -201,7 +199,7 @@ def download_submission(self, submission_id):
201
199
if filename != None :
202
200
remote_hash = self .cloud .get_submission_hash (submission_id )
203
201
with open (filename , 'rb' ) as f :
204
- local_hash = hashlib . md5 (f .read ()).hexdigest ()
202
+ local_hash = md5 (f .read ()).hexdigest ()
205
203
if local_hash != remote_hash :
206
204
log .error ("After downloading submission %s to %s hash didn't match" %
207
205
(submission_id , download_dir ))
@@ -234,7 +232,7 @@ def unpack(self, submission_id):
234
232
for file_name , command in zip_files :
235
233
if os .path .exists (file_name ):
236
234
log .info ("unnzip status: %s" % os .system (command ))
237
- for dirpath , dirnames , filenames in os .walk (".." ):
235
+ for dirpath , _ , filenames in os .walk (".." ):
238
236
os .chmod (dirpath , 0755 )
239
237
for filename in filenames :
240
238
filename = os .path .join (dirpath , filename )
@@ -297,21 +295,16 @@ def report(status):
297
295
log .info ("Functional Test Failure" )
298
296
report (STATUS_TEST_ERROR )
299
297
return False
300
-
301
- def check_hash (self , submission_id ):
302
- try :
303
- for filename in os .listdir (os .path .join (server_info ["compiled_path" ], str (submission_id ))):
304
- if filename .endswith (".zip" ) or filename .endswith (".tgz" ):
305
- log .info ("%s: %s" % (filename , hash_file_sha1 (filename )))
306
- except :
307
- log .error ("Submission path not found." )
308
298
309
299
def get_map (self , map_filename ):
310
300
map_file = os .path .join (server_info ["maps_path" ], map_filename )
311
301
if not os .path .exists (map_file ):
312
302
data = self .cloud .get_map (map_filename )
313
303
if data == None :
314
304
raise Exception ("map" , "Could not download map from main server." )
305
+ map_dir = os .path .split (map_file )[0 ]
306
+ if not os .path .exists (map_dir ):
307
+ os .makedirs (map_dir )
315
308
f = open (map_file , 'w' )
316
309
f .write (data )
317
310
f .close ()
@@ -405,7 +398,6 @@ def game(self, task, report_status=False):
405
398
if report_status :
406
399
self .cloud .post_result ('api_game_result' , result )
407
400
except Exception as ex :
408
- import traceback
409
401
log .debug (traceback .format_exc ())
410
402
result = {"post_id" : self .post_id ,
411
403
"matchup_id" : matchup_id ,
@@ -418,13 +410,13 @@ def task(self, last=False):
418
410
task = self .cloud .get_task ()
419
411
if task :
420
412
try :
421
- log .info ("Recieved task: %s" % task )
413
+ log .info ("Received task: %s" % task )
422
414
if task ['task' ] == 'compile' :
423
415
submission_id = int (task ['submission_id' ])
424
416
try :
425
417
if not self .compile (submission_id , True ):
426
418
self .clean_download (submission_id )
427
- except Exception as e :
419
+ except Exception :
428
420
log .error (traceback .format_exc ())
429
421
self .clean_download (submission_id )
430
422
elif task ['task' ] == 'game' :
@@ -446,9 +438,6 @@ def main(argv):
446
438
parser .add_option ("-s" , "--submission_id" , dest = "submission_id" ,
447
439
type = "int" , default = 0 ,
448
440
help = "Submission id to use for hash, download and compile" )
449
- parser .add_option ("--hash" , dest = "hash" ,
450
- action = "store_true" , default = False ,
451
- help = "Display submission hash" )
452
441
parser .add_option ("-d" , "--download" , dest = "download" ,
453
442
action = "store_true" , default = False ,
454
443
help = "Download submission" )
@@ -465,7 +454,7 @@ def main(argv):
465
454
action = "store_true" , default = False ,
466
455
help = "Set the log level to debug" )
467
456
468
- (opts , args ) = parser .parse_args (argv )
457
+ (opts , _ ) = parser .parse_args (argv )
469
458
if opts .debug :
470
459
log .setLevel (logging .DEBUG )
471
460
worker = Worker (True )
0 commit comments