Skip to content

Add PHP, Java and Ruby #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions environments/php/pythia.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2013 The Pythia Authors.
# This file is part of Pythia.
#
# Pythia is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Pythia 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pythia. If not, see <http://www.gnu.org/licenses/>.

ENV_PHP := $(ENV_OUT_DIR)/php.sfs

environments: $(ENV_PHP)

$(ENV_PHP): $~/rootfs-config.sh $(MKROOTFS_DEPS)
@mkdir -p $(@D)
$(MKROOTFS) -o $@ $<

# vim:set ts=4 sw=4 noet:
24 changes: 24 additions & 0 deletions environments/php/rootfs-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2018 The Pythia Authors.
# This file is part of Pythia.
#
# Pythia is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Pythia 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pythia. If not, see <http://www.gnu.org/licenses/>.
#
# PHP 7.0
install_busybox

install_debs php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-readline

# Depending libraries
install_debs libc6 libedit2 libpcre3 libssl1.1 libxml2 mime-support tzdata ucf zlib1g \
libgcc1 libbsd0 libncurses5 libtinfo5 libmagic-mgc multiarch-support debconf \
libicu57 liblzma5 php-common coreutils libstdc++6 libselinux1
24 changes: 24 additions & 0 deletions environments/ruby/pythia.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2013 The Pythia Authors.
# This file is part of Pythia.
#
# Pythia is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Pythia 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pythia. If not, see <http://www.gnu.org/licenses/>.

ENV_RUBY := $(ENV_OUT_DIR)/ruby.sfs

environments: $(ENV_RUBY)

$(ENV_RUBY): $~/rootfs-config.sh $(MKROOTFS_DEPS)
@mkdir -p $(@D)
$(MKROOTFS) -o $@ $<

# vim:set ts=4 sw=4 noet:
22 changes: 22 additions & 0 deletions environments/ruby/rootfs-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2018 The Pythia Authors.
# This file is part of Pythia.
#
# Pythia is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Pythia 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pythia. If not, see <http://www.gnu.org/licenses/>.

# Ruby 2.3
install_debs ruby2.3 libruby2.3 rake ruby-did-you-mean ruby-minitest ruby-net-telnet \
ruby-power-assert ruby-test-unit rubygems-integration

# Base libraries
install_debs libc6 libgmp10 rubygems-integration libgcc1 libffi6 libgdbm3 libncurses5 libreadline7 \
libssl1.0.2 libtinfo5 libyaml-0-2 zlib1g ca-certificates dpkg readline-common debconf
10 changes: 10 additions & 0 deletions tasks/execute-java.task
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"environment": "java",
"taskfs": "execute-java.sfs",
"limits": {
"time": 90,
"memory": 64,
"disk": 50,
"output": 1024
}
}
70 changes: 70 additions & 0 deletions tasks/execute-java/Pre.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.io.*;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.Charset;

public class Pre
{
private static String ReadProc(InputStream is) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
return builder.toString();
}

public static void main(String[] args)
{
try{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(in);
String file = "Script.java";
OutputStreamWriter f = new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8"));
String str;

while((str = input.readLine()) != null) {

//System.out.println(str);
f.write(str.trim() + "\n");
}
f.close();

ProcessBuilder pb = new ProcessBuilder( "/usr/lib/jvm/java-8-openjdk-i386/bin/javac","-encoding", "UTF-8", file);
pb.directory(new File("/tmp/work"));
Process p = pb.start();

int a = p.waitFor();

if(a != 0) {
System.out.println("{\"stdout\": \"" + ReadProc(p.getInputStream()) + "\", \"stderr\": \"" + ReadProc(p.getErrorStream()) + "\", \"returncode\": \"" + a + "\"}");
return;
}

pb = new ProcessBuilder( "/usr/lib/jvm/java-8-openjdk-i386/bin/java", "Script");
p = pb.start();

a = p.waitFor();

System.out.println("{\"stdout\": \"" + ReadProc(p.getInputStream()) + "\", \"stderr\": \"" + ReadProc(p.getErrorStream()) + "\", \"returncode\": \"" + a + "\"}");

} catch (IOException|InterruptedException io) {
io.printStackTrace();
}
}
}
1 change: 1 addition & 0 deletions tasks/execute-java/control
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/task/execute-java.sh
7 changes: 7 additions & 0 deletions tasks/execute-java/execute-java.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
PATH=/usr/lib/jvm/java-8-openjdk-i386/bin:$PATH
mkdir /tmp/work
cp /task/Pre.java /tmp/work/Pre.java
cd /tmp/work
javac Pre.java
java Pre
10 changes: 10 additions & 0 deletions tasks/execute-php.task
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"environment": "php",
"taskfs": "execute-php.sfs",
"limits": {
"time": 60,
"memory": 32,
"disk": 50,
"output": 4096
}
}
1 change: 1 addition & 0 deletions tasks/execute-php/control
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/task/execute.php
32 changes: 32 additions & 0 deletions tasks/execute-php/execute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env php7.0
<?php
//Create and move to directory
if(!file_exists('/tmp')) {
mkdir('/tmp', 0666, true);
}

chdir('/tmp');

//Read input and create file
$f = fopen('php://stdin', 'r');
$filename = 'script.php';
$file = fopen($filename, 'a');
chmod($filename, 0777);

while($line=fgets($f)) {
fwrite($file, $line);
}
fclose($file);

//Send result <missing returncode>
$desc = array(STDIN, array('pipe', "w"), array('pipe','w'));
$process = proc_open('php7.0 -f ' .$filename, $desc, $pipes, '/tmp');
if(is_resource($process)) {
$output = stream_get_contents($pipes[1]);
$err = stream_get_contents($pipes[2]);
$return_val = proc_close($process);
echo ('{"stdin": "' . $output . '", "stderr": "' .$err . '", "returncode": "' .$return_val. '"}');
}


?>
10 changes: 10 additions & 0 deletions tasks/hello-php.task
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"environment": "php",
"taskfs": "hello-php.sfs",
"limits": {
"time": 60,
"memory": 32,
"disk": 50,
"output": 1024
}
}
1 change: 1 addition & 0 deletions tasks/hello-php/control
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/task/hello.php
4 changes: 4 additions & 0 deletions tasks/hello-php/hello.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env php7.0
<?php
echo "hello my PHP world!\n"
?>
10 changes: 10 additions & 0 deletions tasks/hello-ruby.task
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"environment": "ruby",
"taskfs": "hello-ruby.sfs",
"limits": {
"time": 60,
"memory": 32,
"disk": 50,
"output": 1024
}
}
1 change: 1 addition & 0 deletions tasks/hello-ruby/control
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/task/hello.sh
1 change: 1 addition & 0 deletions tasks/hello-ruby/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts 'Hello Ruby world!'
5 changes: 5 additions & 0 deletions tasks/hello-ruby/hello.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
mkdir /tmp/work
cp /task/hello.rb /tmp/work/hello.rb
cd /tmp/work
ruby hello.rb