Created
December 6, 2010 08:21
-
-
Save hiromu/730019 to your computer and use it in GitHub Desktop.
Python for JOI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: UTF-8 | |
# JOI用スクリプト | |
# | |
# あるフォルダに以下のファイルを置く。 | |
# ソースコード(xxx.c もしくは xxx.cpp) | |
# 入力データ(ファイル名はそのままで) | |
# これ | |
# このスクリプトを実行(実行時引数にソースファイルを指定) | |
# 自動でコンパイル&実行 | |
# たぶんコンパイルエラーも表示 | |
# | |
# 現在のところC, C++に対応(拡張子で区別しているので、注意) | |
# 第二引数以降にコンパイルオプションを記述することもできる | |
import commands | |
import sys | |
import os.path | |
def compile(source): | |
filename = source.split('.') | |
output = filename[0] + '.out' | |
if filename[1] == 'cpp': | |
mes = commands.getoutput("g++ %s -o %s %s" % (source, output, options)) | |
if mes: | |
if mes.find('error') != -1: | |
print '### Compile error! ###' | |
print mes | |
sys.exit(1) | |
else: | |
print '### Compile infomation ###' | |
print mes | |
elif filename[1] == 'c': | |
mes = commands.getoutput("gcc %s -o %s %s" % (source, output, options)) | |
if mes: | |
if mes.find('error') != -1: | |
print '### Compile error! ###' | |
print mes | |
sys.exit(1) | |
else: | |
print '### Compile infomation ###' | |
print mes | |
mes = commands.getoutput("chmod 755 %s" % (output)) | |
if mes: | |
print '### Permission error! ###' | |
print mes | |
sys.exit(1) | |
return output | |
def run(exec_file): | |
ls = commands.getoutput('ls 20*') | |
if not ls: | |
print '### Input file not found! ###' | |
sys.exit(1) | |
inout = ls.split('.')[0][0:-3] | |
for i in range(1, 6): | |
mes = commands.getoutput("exec ./%s < %sin%d.txt > %sout%d.txt" % (exec_file, inout, i, inout, i)) | |
if mes: | |
print '### Runtime error! ###' | |
print mes | |
sys.exit(1) | |
if __name__ == '__main__': | |
source = sys.argv[1] | |
options = '' | |
if len(sys.argv) > 2: | |
for i in range(2, len(sys.argv)): | |
options += "%s " % (sys.argv[i]) | |
if not os.path.exists(source): | |
print '### Source file not found! ###' | |
sys.exit(1) | |
output = compile(source) | |
run(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment