Skip to content

Commit a825b82

Browse files
author
Alexandru Stan
committed
copied worker folder from the other branch, hence merged them
2 parents 06a0128 + a6d5fce commit a825b82

32 files changed

+2924
-0
lines changed

planet_wars/worker/Makefile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2010 owners of the AI Challenge project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
# use this file except in compliance with the License. You may obtain a copy
5+
# of the License at http:#www.apache.org/licenses/LICENSE-2.0 . Unless
6+
# required by applicable law or agreed to in writing, software distributed
7+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
# specific language governing permissions and limitations under the License.
10+
#
11+
# Author: Jeff Cameron ([email protected])
12+
#
13+
# Makefile for the entire AI contest project.
14+
15+
CC=g++
16+
CPPFLAGS += -Wall -I.. -I../third_party/googletest/include
17+
VPATH = third_party/googletest/make:cpp_util:sandbox
18+
SUBDIRS = engine viz
19+
VM_IMG = http://csclub.uwaterloo.ca/~ssalbiz/test.img
20+
21+
all: test.img sample_bots/do_nothing_bot sample_bots/timeout_bot
22+
23+
clean:
24+
rm *.img
25+
26+
sample_bots/do_nothing_bot:
27+
g++ -o sample_bots/do_nothing_bot sample_bots/do_nothing_bot.cc
28+
29+
sample_bots/timeout_bot:
30+
g++ -o sample_bots/timeout_bot sample_bots/timeout_bot.cc
31+
32+
test.img:
33+
wget $(VM_IMG) -O test.img
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
import MySQLdb
3+
from server_info import server_info
4+
5+
n = int(sys.argv[1])
6+
connection = MySQLdb.connect(host = server_info["db_host"],
7+
user = server_info["db_username"],
8+
passwd = server_info["db_password"],
9+
db = server_info["db_name"])
10+
cursor = connection.cursor(MySQLdb.cursors.DictCursor)
11+
for i in range(1, n+1):
12+
query = "INSERT INTO maps (name,path,priority) VALUES ('Map " + str(i) + \
13+
"','map" + str(i) + ".txt',1)"
14+
print query
15+
cursor.execute(query)
16+
cursor.close()
17+
connection.close()
+321
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
# compile_anything.py
2+
# Author: Jeff Cameron ([email protected])
3+
#
4+
# Auto-detects the language of the entry based on the extension,
5+
# attempts to compile it, returning the stdout and stderr.
6+
# The auto-detection works by looking for the "main" code file of
7+
# the available languages. If the number of matching languages is 0 or
8+
# more than 1, it is an error, and an appropriate error message is returned.
9+
#
10+
# To add a new language you must add an entry to the "languages" dictionary in
11+
# the following format:
12+
# LanguageName : (extension, [NukeGlobs], [(Compile_globs, Compile_class)])
13+
#
14+
# For example the entry for Python is as follows:
15+
# "Python" : (".py", ["*.pyc"], [("*.py", ChmodCompiler("Python"))]).
16+
# This defines the extension as .py, removes all .pyc files, and runs all the
17+
# found .py files through the ChmodCompiler, which is a pseudo-compiler class
18+
# which only chmods the found files.
19+
#
20+
# If you want to run a real compiler then you need to define a set of flags to
21+
# send it. In this case you would either use TargetCompiler or ExternalCompiler.
22+
# The difference between the two is the TargetCompiler iterates over the found
23+
# files and creates object files from them, whereas the External doesn't.
24+
# If in doubt just stick to the ExternalCompiler.
25+
#
26+
# An example is from the C# Entry:
27+
# "C#" : (".exe", ["*.exe"],
28+
# [(["*.cs"], ExternalCompiler(comp_args["C#"][0]))])
29+
#
30+
# To make the dictionary more readable the flags have been split into a
31+
# separate "comp_args" dictionary. C#'s entry looks like so:
32+
# "C#" : [["gmcs", "-warn:0", "-out:%s.exe" % BOT]]
33+
# At runtime this all boils down to:
34+
# gmcs -warn:0 -out:MyBot.exe *.cs
35+
# (though the *.cs is actually replaced with the list of files found)
36+
37+
import os
38+
import re
39+
import glob
40+
import subprocess
41+
import errno
42+
import time
43+
import shutil
44+
import MySQLdb
45+
from server_info import server_info
46+
47+
BOT = "MyBot"
48+
SAFEPATH = re.compile('[a-zA-Z0-9_.$-]+$')
49+
50+
def safeglob(pattern):
51+
safepaths = []
52+
paths = glob.glob(pattern)
53+
for path in paths:
54+
if SAFEPATH.match(path):
55+
safepaths.append(path)
56+
return safepaths
57+
58+
def safeglob_multi(patterns):
59+
safepaths = []
60+
for pattern in patterns:
61+
safepaths.extend(safeglob(pattern))
62+
return safepaths
63+
64+
def nukeglob(pattern):
65+
paths = glob.glob(pattern)
66+
for path in paths:
67+
try:
68+
if os.path.isdir(path):
69+
shutil.rmtree(path)
70+
else:
71+
os.unlink(path)
72+
except OSError, e:
73+
if e.errno != errno.ENOENT:
74+
raise
75+
76+
class Log:
77+
def __init__(self):
78+
self.out = ""
79+
self.err = ""
80+
81+
def system(args, log):
82+
log.out += ' '.join(args) + "\n"
83+
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
84+
(out, err) = proc.communicate()
85+
log.out += out
86+
log.err += err
87+
return proc.returncode == 0
88+
89+
def check_path(path, log):
90+
if not os.path.exists(path):
91+
log.err += "\nFailure: output file " + str(path) + " was not created.\n"
92+
return False
93+
else:
94+
return True
95+
96+
class Compiler:
97+
def compile(self, globs, log):
98+
raise NotImplementedError
99+
100+
class ChmodCompiler(Compiler):
101+
def __init__(self, language):
102+
self.language = language
103+
104+
def compile(self, globs, log):
105+
for f in safeglob_multi(globs):
106+
try:
107+
os.chmod(f, 0644)
108+
except Exception, e:
109+
log.err += "Error chmoding %s - %s\n" % (f, e)
110+
log.out += self.language + " scripts do not need to be compiled.\n"
111+
return True
112+
113+
class ExternalCompiler(Compiler):
114+
def __init__(self, args, separate=False):
115+
self.args = args
116+
self.separate = separate
117+
118+
def compile(self, globs, log):
119+
files = safeglob_multi(globs)
120+
if self.separate:
121+
for file in files:
122+
if not system(self.args + [file], log):
123+
return False
124+
else:
125+
if not system(self.args + files, log):
126+
return False
127+
return True
128+
129+
# Compiles each file to its own output, based on the replacements dict.
130+
class TargetCompiler(Compiler):
131+
def __init__(self, args, replacements, outflag="-o"):
132+
self.args = args
133+
self.replacements = replacements
134+
self.outflag = outflag
135+
136+
def compile(self, globs, log):
137+
sources = safeglob_multi(globs)
138+
for source in sources:
139+
head, ext = os.path.splitext(source)
140+
if ext in self.replacements:
141+
target = head + self.replacements[ext]
142+
else:
143+
log.err += "Could not determine target for source file %s.\n" % source
144+
return False
145+
if not system(self.args + [self.outflag, target, source], log):
146+
return False
147+
return True
148+
149+
comp_args = {
150+
# lang : ([list of compilation arguments], ...)
151+
# If the compilation should output each source file to
152+
# its own object file, don't include the -o flags here,
153+
# and use the TargetCompiler in the languages dict.
154+
"C" : [["gcc", "-O3", "-funroll-loops", "-c"],
155+
["gcc", "-O2", "-lm", "-o", BOT]],
156+
"C#" : [["gmcs", "-warn:0", "-out:%s.exe" % BOT]],
157+
"C++" : [["g++", "-O3", "-funroll-loops", "-c"],
158+
["g++", "-O2", "-lm", "-o", BOT]],
159+
"Go" : [["/usr/local/bin/8g", "-o", "_go_.8"],
160+
["/usr/local/bin/8l", "-o", BOT, "_go_.8"]],
161+
"Groovy" : [["groovyc"],
162+
["jar", "cfe", BOT + ".jar", BOT]],
163+
"Haskell" : [["ghc", "--make", BOT + ".hs", "-O2", "-v0"]],
164+
"Java" : [["javac"],
165+
["jar", "cfe", BOT + ".jar", BOT]],
166+
"Lisp" : [['sbcl', '--end-runtime-options', '--no-sysinit',
167+
'--no-userinit', '--disable-debugger', '--load',
168+
BOT + '.lisp', '--eval', "(save-lisp-and-die \"" + BOT
169+
+ "\" :executable t :toplevel #'pwbot::main)"]],
170+
"OCaml" : [["ocamlbuild", BOT + ".native"]],
171+
"Scala" : [["scalac"]],
172+
}
173+
174+
targets = {
175+
# lang : { old_ext : new_ext, ... }
176+
"C" : { ".c" : ".o" },
177+
"C++" : { ".c" : ".o", ".cpp" : ".o", ".cc" : ".o" },
178+
}
179+
180+
languages = {
181+
# lang : (output extension, [nukeglobs], [(source glob, compiler), ...])
182+
# The compilers are run in the order given.
183+
# If the extension is "" it means the output file is just BOT
184+
# If a source glob is "" it means the source is part of the
185+
# compiler arguments.
186+
"C" : ("",
187+
["*.o", BOT],
188+
[(["*.c"], TargetCompiler(comp_args["C"][0], targets["C"])),
189+
(["*.o"], ExternalCompiler(comp_args["C"][1]))]),
190+
"C#" : (".exe",
191+
[BOT + ".exe"],
192+
[(["*.cs"], ExternalCompiler(comp_args["C#"][0]))]),
193+
"C++" : ("",
194+
["*.o", BOT],
195+
[(["*.c", "*.cpp", "*.cc"],
196+
TargetCompiler(comp_args["C++"][0], targets["C++"])),
197+
(["*.o"], ExternalCompiler(comp_args["C++"][1]))]),
198+
"Clojure" : (".clj",
199+
[],
200+
[(["*.clj"], ChmodCompiler("Clojure"))]),
201+
"CoffeeScript": (".coffee",
202+
[],
203+
[(["*.coffee"], ChmodCompiler("CoffeeScript"))]),
204+
"Go" : ("",
205+
["*.8", BOT],
206+
[(["*.go"], ExternalCompiler(comp_args["Go"][0])),
207+
([""], ExternalCompiler(comp_args["Go"][1]))]),
208+
"Groovy" : (".jar",
209+
["*.class, *.jar"],
210+
[(["*.groovy"], ExternalCompiler(comp_args["Groovy"][0])),
211+
(["*.class"], ExternalCompiler(comp_args["Groovy"][1]))]),
212+
"Haskell" : ("",
213+
[BOT],
214+
[([""], ExternalCompiler(comp_args["Haskell"][0]))]),
215+
"Java" : (".jar",
216+
["*.class, *.jar"],
217+
[(["*.java"], ExternalCompiler(comp_args["Java"][0])),
218+
(["*.class"], ExternalCompiler(comp_args["Java"][1]))]),
219+
"Javascript" : (".js",
220+
[],
221+
[(["*.js"], ChmodCompiler("Javascript"))]),
222+
"Lisp" : ("",
223+
[BOT],
224+
[([""], ExternalCompiler(comp_args["Lisp"][0]))]),
225+
"Lua" : (".lua",
226+
[],
227+
[(["*.lua"], ChmodCompiler("Lua"))]),
228+
"OCaml" : (".native",
229+
[BOT + ".native"],
230+
[([""], ExternalCompiler(comp_args["OCaml"][0]))]),
231+
"Perl" : (".pl",
232+
[],
233+
[(["*.pl"], ChmodCompiler("Perl"))]),
234+
"PHP" : (".php",
235+
[],
236+
[(["*.php"], ChmodCompiler("PHP"))]),
237+
"Python" : (".py",
238+
["*.pyc"],
239+
[(["*.py"], ChmodCompiler("Python"))]),
240+
"Ruby" : (".rb",
241+
[],
242+
[(["*.rb"], ChmodCompiler("Ruby"))]),
243+
"Scala" : (".class",
244+
["*.class, *.jar"],
245+
[(["*.scala"], ExternalCompiler(comp_args["Scala"][0]))]),
246+
"Scheme" : (".ss",
247+
[],
248+
[(["*.ss"], ChmodCompiler("Scheme"))]),
249+
}
250+
251+
252+
def compile_function(language, log):
253+
info = languages[language]
254+
extension = info[0]
255+
nukeglobs = info[1]
256+
compilers = info[2]
257+
258+
for glob in nukeglobs:
259+
nukeglob(glob)
260+
261+
for globs, compiler in compilers:
262+
if not compiler.compile(globs, log):
263+
return False
264+
265+
return check_path(BOT + extension, log)
266+
267+
def get_programming_languages():
268+
connection = MySQLdb.connect(host = server_info["db_host"],
269+
user = server_info["db_username"],
270+
passwd = server_info["db_password"],
271+
db = server_info["db_name"])
272+
cursor = connection.cursor(MySQLdb.cursors.DictCursor)
273+
cursor.execute("SELECT * FROM languages")
274+
programming_languages = cursor.fetchall()
275+
cursor.close()
276+
connection.close()
277+
return programming_languages
278+
279+
# Autodetects the language of the entry in the current working directory and
280+
# compiles it.
281+
def compile_anything():
282+
log = Log()
283+
programming_languages = get_programming_languages()
284+
detected_langs = [
285+
lang for lang in programming_languages \
286+
if os.path.exists(lang["main_code_file"])
287+
]
288+
# If no language was detected
289+
if len(detected_langs) == 0:
290+
log.err += "The auto-compile environment could not locate your main code\n"
291+
log.err += "file. This is probably because you accidentally changed the\n"
292+
log.err += "name of your main code file. You must include exactly one file"
293+
log.err += "\nwith one of the following names:\n"
294+
for lang in programming_languages:
295+
log.err += " * " + lang["main_code_file"] + " (" + lang["name"] + ")\n"
296+
log.err += "This is to help the auto-compile environment figure out which"
297+
log.err += "\nprogramming language you are using.\n"
298+
return log.out, log.err, "NULL"
299+
# If more than one language was detected
300+
if len(detected_langs) > 1:
301+
log.err = "The auto-compile environment found more than one main code "
302+
log.err += "file:\n"
303+
for lang in detected_langs:
304+
log.err += " * " + lang["main_code_file"] + " (" + lang["name"] + ")\n"
305+
log.err += "You must submit only one of these files so that the\n"
306+
log.err += "auto-compile environment can figure out which programming\n"
307+
log.err += "language you are trying to use.\n"
308+
return log.out, log.err, "NULL"
309+
# If we get this far, then we have successfully auto-detected the language
310+
# that this contestant is using.
311+
main_code_file = detected_langs[0]["main_code_file"]
312+
detected_lang = detected_langs[0]["name"]
313+
language_id = detected_langs[0]["language_id"]
314+
log.out += "Found " + main_code_file + ". Compiling this entry as " + \
315+
detected_lang + "\n"
316+
t1 = time.time()
317+
if compile_function(detected_lang, log):
318+
log.out += "Completed in %f seconds.\n" % (time.time() - t1)
319+
else:
320+
log.err += "Compilation failed.\n"
321+
return log.out, log.err, language_id

0 commit comments

Comments
 (0)