Skip to content

Commit

Permalink
update r764
Browse files Browse the repository at this point in the history
  • Loading branch information
srz-zumix committed Dec 9, 2014
1 parent 480ed84 commit 4e1c413
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tools/paiza.io/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Makefile

.PHONY: sample

TOOLS=pypaiza.py iupaiza.py

WANDBOX_COMPILER?=gcc-head

fuse:
make -C ../fuse

help: $(TOOLS)
python iupaiza.py -h

sample: sample.cpp $(TOOLS) Makefile
python iupaiza.py sample.cpp

save: sample.cpp $(TOOLS) Makefile
python iupaiza.py sample.cpp

test: ../../test/iutest_syntax_tests.cpp Makefile
python iupaiza.py $< -c $(WANDBOX_COMPILER) -f"-DIUTEST_USE_MAIN" --expand_include --encoding utf-8-sig

153 changes: 153 additions & 0 deletions tools/paiza.io/iupaiza.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/env python
#
# iupaiza.py
#

import os
import sys
import re
import codecs
import argparse
import pypaiza

from argparse import ArgumentParser
from pypaiza import PaizaIO

IUTEST_FUSED_SRC = os.path.join(os.path.dirname(__file__), '../../fused-src/iutest.min.hpp')
IUTEST_INCLUDE_REGEX = re.compile(r'^\s*#\s*include\s*".*iutest\.hpp"')
EXPAND_INCLUDE_REGEX = re.compile(r'^\s*#\s*include\s*"(.*?)"')

#
# command line option
def parse_command_line():
parser = ArgumentParser()
parser.add_argument(
'-v'
, '--version'
, action='version'
, version=u'%(prog)s version 0.1'
)
parser.add_argument(
'--stdin'
, help = 'set stdin.'
)
parser.add_argument(
'-o'
, '--output'
, help = 'output source code.'
)
parser.add_argument(
'--encoding'
, help = 'set encoding.'
)
parser.add_argument(
'--expand_include'
, action='store_true'
, help = 'expand include file.'
)
parser.add_argument(
'code'
, metavar='CODE'
, help = 'Source code file'
, nargs='?'
)
if len(sys.argv) <= 1:
parser.error('invalid number arguments')
del sys.argv[0]
options = parser.parse_args(sys.argv)
return options

#
# file open
def file_open(path, mode, encoding):
if encoding:
file = codecs.open(path, mode, encoding)
else:
file = open(path, mode)
return file

#
# make code
def make_code(path, encoding, expand):
code = ''
file = file_open(path, 'r', encoding)
for line in file:
m = IUTEST_INCLUDE_REGEX.match(line)
if m:
f = codecs.open(IUTEST_FUSED_SRC, 'r', 'utf-8-sig')

code += '//==================================================================>>>> ' + line
code += f.read()
code += '//==================================================================<<<< ' + line
else:
if expand:
m = EXPAND_INCLUDE_REGEX.match(line)
if m:
include_path = os.path.join(os.path.dirname(path), m.group(1))
if os.path.exists(include_path):
code += make_code(include_path, encoding, expand)
code += '// '
code += line
file.close()
return code

#
# run paiza
def run_paiza(code, options):
paiza = PaizaIO()
paiza.code(code)
return paiza.run()

#
# show result
def show_result(r):
if 'error' in r:
print r['error']
sys.exit(1)
build_result = r['build_result']
if 'success' in build_result:
if 'stdout' in r:
print 'stdout:'
print r['stdout']
if 'stderr' in r:
print 'stderr:'
print r['stderr']
if 'time' in r:
print 'time:'
print r['time']
if 'memory' in r:
print 'memory:'
print r['memory']
if 'exit_code' in r:
return int(r['exit_code'])
else:
if 'build_stderr' in r:
print r['build_stderr']
if 'build_exit_code' in r:
return int(r['build_exit_code'])

return 1

#
# run
def run(options):
filepath = options.code
if not os.path.exists(filepath):
sys.exit(1)
code = make_code(filepath, options.encoding, options.expand_include)
if options.output:
f = file_open(options.output, 'w', options.encoding)
f.write(code)
f.close()
r = run_paiza(code, options)
b = show_result(r)
sys.exit(b)

#
#
def main():
options = parse_command_line()
run(options)

if __name__ == '__main__':
main()
84 changes: 84 additions & 0 deletions tools/paiza.io/pypaiza.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python
#
# pypaiza.py
#

import requests
import json;

#
#
#
class PaizaIO:
"""wandbox api class"""
api_url = 'http://api.paiza.io/'
parameter = { 'source_code':'', 'language':'cpp', 'api_key':'guest' }
session_id = ''

def create(self):
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
payload = json.dumps(self.parameter)
r = requests.post(self.api_url + 'runners/create/', data=payload, headers=headers)
r.raise_for_status()
result = r.json()
if 'error' in result:
print result['error']
print result
raise
elif 'id' in result:
self.session_id = result['id']
else:
print result
raise
return result

def get_status(self):
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
parameter = { 'id': self.session_id, 'api_key':self.parameter['api_key'] }
payload = json.dumps(parameter)
r = requests.get(self.api_url + 'runners/get_status/', data=payload, headers=headers)
r.raise_for_status()
return r.json()

def get_details(self):
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
parameter = { 'id': self.session_id, 'api_key':self.parameter['api_key'] }
payload = json.dumps(parameter)
r = requests.get(self.api_url + 'runners/get_details/', data=payload, headers=headers)
r.raise_for_status()
return r.json()

def is_completed(self, r):
if 'status' in r:
return r['status'] == 'completed'
return False

def wait_complete(self):
r = self.get_status()
while not self.is_completed(r):
r = self.get_status()

def run(self):
self.create()
self.wait_complete();
return self.get_details()

def language(language, str):
self.parameter.update({'language':str})
def source_code(source_code, str):
self.parameter.update({'source_code':str})
def code(self, str):
self.parameter.update({'source_code':str})
def input(self, str):
self.parameter.update({'input':str})
def stdin(self, str):
self.parameter.update({'input':str})

def dump(self):
print self.parameter

if __name__ == '__main__':
paiza = PaizaIO()
paiza.code('#include <iostream>\nint main() { int x = 0; std::cout << "hoge" << std::endl; }')
print paiza.run()

17 changes: 17 additions & 0 deletions tools/paiza.io/sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//#include "../../include/iutest.hpp"
#include <iostream>

int main(int argc, char** argv)
{
int x = 0;
std::cout << "hoge" << std::endl;

// IUTEST_INIT(&argc, argv);
// return IUTEST_RUN_ALL_TESTS();
return 0;
}

//IUTEST(Foo, Bar)
//{
// IUTEST_ASSERT_EQ(1, abs(-1));
//}

0 comments on commit 4e1c413

Please sign in to comment.