Skip to content

Commit 0796834

Browse files
committed
Worker can now request compiles and report status.
1 parent 56f606b commit 0796834

21 files changed

+972
-902
lines changed

ants/engine.py

-187
This file was deleted.

ants/play_one_game.cmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@echo off
2-
playgame.py -r 1 -j -o viewer -t %2 -m %1 "python bots/python/HunterBot.py" "python bots/python/LeftyBot.py" "python bots/python/HunterBot.py" "python bots/python/LeftyBot.py"
2+
playgame.py -r 1 -j -o viewer -t 1000 -m maps/random4.txt "python bots/python/HunterBot.py" "python bots/python/LeftyBot.py" "python bots/python/HunterBot.py" "python bots/python/LeftyBot.py"
33

ants/play_one_game.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
./playgame.py -r 1 -j -o viewer -t %2 -m %1 "python bots/python/HunterBot.py" "python bots/python/LeftyBot.py" "python bots/python/HunterBot.py" "python bots/python/LeftyBot.py"
1+
./playgame.py -r 1 -j -o viewer -t 1000 -m maps/random4.txt "python bots/python/HunterBot.py" "python bots/python/LeftyBot.py" "python bots/python/HunterBot.py" "python bots/python/LeftyBot.py"
22

ants/playgame.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import time
66
from optparse import OptionParser
77

8-
from engine import run_game
98
from ants import Ants
109

10+
# get engine from worker dir
11+
sys.path.append("../worker")
12+
from engine import run_game
13+
1114
def main(argv):
1215
usage ="Usage: %prog [options] map bot1 bot2\n\nYou must specify a map file."
1316
parser = OptionParser(usage=usage)
@@ -26,6 +29,9 @@ def main(argv):
2629
# it will also contain the bot input/output logs, if requested
2730
parser.add_option("-o", "--output_dir", dest="output_dir",
2831
help="Directory to dump replay files to.")
32+
parser.add_option("-j", "--output_json", dest="output_json",
33+
action="store_true", default=False,
34+
help="Return json result from engine.")
2935
parser.add_option("-I", "--log_input", dest="log_input",
3036
action="store_true", default=False,
3137
help="Log input streams sent to bots")
@@ -87,6 +93,7 @@ def main(argv):
8793
"map_file": opts.map,
8894
"turns": opts.turns,
8995
"output_dir": opts.output_dir,
96+
"output_json": opts.output_json,
9097
"log_input": opts.log_input,
9198
"log_output": opts.log_output,
9299
"serial": opts.serial,
@@ -102,7 +109,9 @@ def main(argv):
102109
(game.num_players, len(bots)))
103110
break
104111
print('playgame round %s' % round)
105-
run_game(game, bots, engine_options, round)
112+
result = run_game(game, bots, engine_options, round)
113+
if opts.output_json:
114+
print result
106115

107116
except Exception:
108117
traceback.print_exc()

manager/compile_anything.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ def compile_function(language, log):
311311

312312
def get_programming_languages():
313313
connection = MySQLdb.connect(host = server_info["db_host"],
314-
user = server_info["db_username"],
315-
passwd = server_info["db_password"],
316-
db = server_info["db_name"])
314+
user = server_info["db_username"],
315+
passwd = server_info["db_password"],
316+
db = server_info["db_name"])
317317
cursor = connection.cursor(MySQLdb.cursors.DictCursor)
318318
cursor.execute("SELECT * FROM language where language_id > 0")
319319
programming_languages = cursor.fetchall()
@@ -323,9 +323,10 @@ def get_programming_languages():
323323

324324
# Autodetects the language of the entry in the current working directory and
325325
# compiles it.
326-
def compile_anything():
326+
def compile_anything(programming_languages=None):
327327
log = Log()
328-
programming_languages = get_programming_languages()
328+
if programming_languages == None:
329+
programming_languages = get_programming_languages()
329330
detected_langs = [
330331
lang for lang in programming_languages \
331332
if os.path.exists(lang["main_code_file"])
@@ -366,7 +367,7 @@ def compile_anything():
366367
return log.out, log.err, language_id
367368

368369
if __name__ == '__main__':
369-
import cPickle
370+
import json
370371
out, err, language_id = compile_anything()
371-
print cPickle.dumps((out, err, language_id))
372+
print json.dumps((out, err, language_id))
372373

manager/submission_hash.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def hash_file_md5sum(filename):
3434
def hash_file_sha(filename):
3535
READ_SIZE = 4096 * 2500
3636
fhash = sha1()
37-
fhash.update(os.path.basename(filename))
38-
f = open(filename, 'r')
37+
#fhash.update(os.path.basename(filename))
38+
f = open(filename, 'rb')
3939
content = f.read(READ_SIZE)
4040
while len(content) != 0:
4141
fhash.update(content)

website/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
server_info.php
2+
*.log

website/api_compile_result.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
2+
// ini_set('error_reporting', E_ALL);
3+
// ini_set('display_errors', true);
24

35
require_once('api_functions.php');
46

7+
header("Content-type: application/json");
8+
59
$json_string = file_get_contents('php://input');
610
$json_hash = md5($json_string);
711
$compiledata = json_decode($json_string);
@@ -10,14 +14,18 @@
1014
if (mysql_query(sprintf($sql["update_submission_success"],
1115
$worker["worker_id"],
1216
$compiledata->submission_id))) {
13-
echo json_decode(array( "hash" => $json_hash ));
17+
echo json_encode(array( "hash" => $json_hash ));
18+
} else {
19+
api_log(sprintf("Error updating successful compile: %s", mysql_error()));
1420
}
1521
} else {
16-
if (mysql_query(sprintf($sql["update_submission_failure"],
22+
if (mysql_query(sprintf($sql["update_compile_result"],
1723
$compiledata->status_id,
1824
$worker["worker_id"],
1925
$compiledata->submission_id))) {
20-
echo json_decode(array( "hash" => $json_hash ));
26+
echo json_encode(array( "hash" => $json_hash ));
27+
} else {
28+
api_log(sprintf("Error updating errored compile: %s", mysql_error()));
2129
}
2230
}
2331

website/api_functions.php

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
require_once('memcache.php');
66
require_once('mysql_login.php');
7+
require_once('server_info.php');
78

89
if (!isset($_GET['api_key'])) {
910
header('HTTP/1.0 401 Unauthorized');
@@ -55,4 +56,11 @@ function valid_worker($api_key,$ip_address) {
5556
}
5657
}
5758

59+
function api_log($message) {
60+
global $server_info;
61+
$message = str_replace("\n", "", $message);
62+
$message = str_replace("\r", "", $message);
63+
$message = sprintf("%s - %s", date(DATE_ATOM), $message) . "\n";
64+
error_log($message, 3, $server_info["api_log"]);
65+
}
5866
?>

website/api_get_languages.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
// ini_set('error_reporting', E_ALL);
3+
// ini_set('display_errors', true);
4+
5+
require_once('api_functions.php');
6+
7+
header("Content-type: application/json");
8+
9+
// look for compile task first
10+
$result = mysql_query($sql["select_languages"]);
11+
$languages = array();
12+
while ($row = mysql_fetch_assoc($result)) {
13+
$languages[] = $row;
14+
}
15+
16+
echo json_encode($languages);
17+
18+
?>

website/api_get_submission.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020

2121
// set correct content headers based on compression type
2222
if (file_exists("entry.zip")) {
23-
header("Content-disposition: attachment; filename=$submission_id.zip");
23+
header("Content-disposition: attachment; filename=entry.zip");
2424
header("Content-type: application/zip");
2525
$file = getcwd() . "/entry.zip";
2626
} else if (file_exists("entry.tgz")) {
27-
header("Content-disposition: attachment; filename=$submission_id.tgz");
27+
header("Content-disposition: attachment; filename=entry.tgz");
2828
header("Content-type: application/x-compressed");
2929
$file = getcwd() . "/entry.tgz";
3030
} else if (file_exists("entry.tar.gz")) {
31-
header("Content-disposition: attachment; filename=$submission_id.tgz");
31+
header("Content-disposition: attachment; filename=entry.tgz");
3232
header("Content-type: application/x-compressed");
3333
$file = getcwd() . "/entry.tar.gz";
3434
} else {

0 commit comments

Comments
 (0)