Skip to content

Commit

Permalink
Added PHP support.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefano-maggiolo committed Nov 29, 2013
1 parent e1a1a78 commit 96d7118
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 4 deletions.
7 changes: 6 additions & 1 deletion cms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@
LANG_CPP = "cpp"
LANG_PASCAL = "pas"
LANG_PYTHON = "py"
LANG_PHP = "php"

LANGUAGE_NAMES = {
LANG_C: "C",
LANG_CPP: "C++",
LANG_PASCAL: "Pascal",
LANG_PYTHON: "Python",
LANG_PHP: "PHP",
}

LANGUAGES = [LANG_C, LANG_CPP, LANG_PASCAL, LANG_PYTHON]
LANGUAGES = [LANG_C, LANG_CPP, LANG_PASCAL, LANG_PYTHON, LANG_PHP]
DEFAULT_LANGUAGES = [LANG_C, LANG_CPP, LANG_PASCAL]

# A reference for extension-based automatic language detection.
Expand All @@ -67,6 +69,7 @@
".c++": LANG_CPP,
".pas": LANG_PASCAL,
".py": LANG_PYTHON,
".php": LANG_PHP,
}

# Our preferred source file and header file extension for each language.
Expand All @@ -75,12 +78,14 @@
LANG_CPP: ".cpp",
LANG_PASCAL: ".pas",
LANG_PYTHON: ".py",
LANG_PHP: ".php"
}
LANGUAGE_TO_HEADER_EXT_MAP = {
LANG_C: ".h",
LANG_CPP: ".h",
LANG_PASCAL: "lib.pas",
LANG_PYTHON: ".py",
LANG_PHP: ".php"
}


Expand Down
12 changes: 11 additions & 1 deletion cms/grading/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from sqlalchemy.orm import joinedload

from cms import LANG_C, LANG_CPP, LANG_PASCAL, LANG_PYTHON
from cms import LANG_C, LANG_CPP, LANG_PASCAL, LANG_PYTHON, LANG_PHP
from cms.db import Submission
from cms.grading.Sandbox import Sandbox

Expand Down Expand Up @@ -120,6 +120,11 @@ def get_compilation_command(language, source_filenames, executable_filename,
os.path.splitext(os.path.basename(source_filenames[0]))[0],
executable_filename,
)]
elif language == LANG_PHP:
command = ["/bin/sh", "-c"]
command += ["cp %s %s" % (source_filenames[0], executable_filename)]
else:
raise ValueError("Unknown language %s." % language)
return command


Expand All @@ -142,6 +147,11 @@ def get_evaluation_command(language, executable_filename):
command = ["/bin/sh", "-c"]
# Change "python2" to "python3" to use Python 3.
command += ["HOME=./ /usr/bin/python2 %s" % executable_filename]
elif language == LANG_PHP:
command = ["/bin/sh", "-c"]
command += ["/usr/bin/php5 %s" % executable_filename]
else:
raise ValueError("Unknown language %s." % language)
return command


Expand Down
5 changes: 3 additions & 2 deletions cmstestsuite/Tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import cmstestsuite.tasks.batch_fileio_managed as batch_fileio_managed
import cmstestsuite.tasks.communication as communication

from cms import LANGUAGES, LANG_C, LANG_CPP, LANG_PASCAL
from cms import LANGUAGES, LANG_C, LANG_CPP, LANG_PASCAL, LANG_PYTHON
from cmstestsuite.Test import Test, CheckOverallScore, CheckCompilationFail, \
CheckTimeout, CheckNonzeroReturn


ALL_LANGUAGES = tuple(LANGUAGES)
NON_INTERPRETED_LANGUAGES = (LANG_C, LANG_CPP, LANG_PASCAL)
COMPILED_LANGUAGES = (LANG_C, LANG_CPP, LANG_PASCAL, LANG_PYTHON)


ALL_TESTS = [
Expand Down Expand Up @@ -92,7 +93,7 @@

Test('compile-fail',
task=batch_fileio, filename='compile-fail.%l',
languages=ALL_LANGUAGES,
languages=COMPILED_LANGUAGES,
checks=[CheckCompilationFail()]),

# Various timeout conditions.
Expand Down
6 changes: 6 additions & 0 deletions cmstestsuite/code/correct-fileio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$fin = fopen("input.txt", "r");
$n = intval(fgets($fin));
$fout = fopen("output.txt", "w");
fwrite($fout, "correct $n\n");
?>
4 changes: 4 additions & 0 deletions cmstestsuite/code/correct-stdio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$n = intval(fgets(STDIN));
echo "correct $n\n";
?>
9 changes: 9 additions & 0 deletions cmstestsuite/code/half-correct-fileio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
$fin = fopen("input.txt", "r");
$n = intval(fgets($fin));
$fout = fopen("output.txt", "w");
if ($n % 2 == 0)
fwrite($fout, "correct 0\n");
else
fwrite($fout, "correct $n\n");
?>
7 changes: 7 additions & 0 deletions cmstestsuite/code/half-correct-stdio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
$n = intval(fgets(STDIN));
if ($n % 2 == 0)
echo "correct 0\n";
else
echo "correct $n\n";
?>
6 changes: 6 additions & 0 deletions cmstestsuite/code/incorrect-fileio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$fin = fopen("input.txt", "r");
$n = intval(fgets($fin));
$fout = fopen("output.txt", "w");
fwrite($fout, "incorrect $n\n");
?>
4 changes: 4 additions & 0 deletions cmstestsuite/code/incorrect-stdio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$n = intval(fgets(STDIN));
echo "incorrect $n\n";
?>
7 changes: 7 additions & 0 deletions cmstestsuite/code/nonzero-return-fileio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
$fin = fopen("input.txt", "r");
$n = intval(fgets($fin));
$fout = fopen("output.txt", "w");
fwrite($fout, "correct $n\n");
exit(1);
?>
5 changes: 5 additions & 0 deletions cmstestsuite/code/nonzero-return-stdio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$n = intval(fgets(STDIN));
echo "correct $n\n";
exit(1);
?>
6 changes: 6 additions & 0 deletions cmstestsuite/code/oom-heap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
// PHP arrays are extremely greedy; 1M elements are enough to cross the 64MB limit.
$n = range(0, 1024 * 1024);
$n[10000] = intval(fgets(STDIN));
echo "correct $n[10000]\n";
?>
5 changes: 5 additions & 0 deletions cmstestsuite/code/timeout-cputime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$n = intval(fgets(STDIN));
echo "correct $n\n";
while (true);
?>

0 comments on commit 96d7118

Please sign in to comment.