Ideone APIを使ってコマンドラインからコードを公開(2)

前:http://d.hatena.ne.jp/nise_nabe/20101103/1288738359

前に書いたスクリプトを自分なりにキレイに書き直してみた.
https://github.com/nise-nabe/ideone-sender/blob/master/ideone

使い方は

ideone Main.java

とか

ideone Echo.java inputfile

とか.実際に使う場合はユーザ名とパスワードの設定を自分用に書き換えるのを忘れずに.

コード

#!/usr/bin/env python 
import sys
import os.path
from SOAPpy import WSDL

def fileext(filename):
	return os.path.splitext(filename)[1]

def langId(filename):
	exts = {
		'.cpp':1,	# 1 C++ (gcc-4.3.4)
		# 2 Pascal (gpc) (gpc 20070904)
		'.pl':3,	# 3 Perl (perl 5.12.1)
		'.py':4,	# 4 Python (python 2.6.4)
		# 5 Fortran (gfortran-4.3.4)
		'.ws':6,	# 6 Whitespace (wspace 0.3)
		# 7 Ada (gnat-4.3.2)
		# 8 Ocaml (ocamlopt 3.10.2)
		# 9 Intercal (c-intercal 28.0-r1)
		'.java':10,	# 10 Java (sun-jdk-1.6.0.17)
		'.c':11,	# 11 C (gcc-4.3.4)
		# 12 Brainf**k (bff-1.0.3.1)
		# 13 Assembler (nasm-2.07)
		# 14 CLIPS (clips 6.24)
		'.swi':15,	# 15 Prolog (swi) (swipl 5.6.64)
		# 16 Icon (iconc 9.4.3)
		'.rb':17,	# 17 Ruby (ruby-1.9.2)
		# 19 Pike (pike 7.6.86)
		'.hs':21,	# 21 Haskell (ghc-6.8.2)
		# 22 Pascal (fpc) (fpc 2.2.0)
		# 23 Smalltalk (gst 3.1)
		# 25 Nice (nicec 0.9.6)
		# 26 Lua (luac 5.1.4)
		# 27 C# (mono-2.8)
		# 28 Bash (bash 4.0.35)
		'.php':29,	# 29 PHP (php 5.2.11)
		# 30 Nemerle (ncc 0.9.3)
		# 32 Common Lisp (clisp) (clisp 2.47)
		# 33 Scheme (guile) (guile 1.8.5)
		# 34 C99 strict (gcc-4.3.4)
		# 35 JavaScript (rhino) (rhino-1.6.5)
		# 36 Erlang (erl-5.7.3)
		'.tcl':38,	# 38 Tcl (tclsh 8.5.7)
		# 39 Scala (scala-2.8.0.final)
		# 45 Assembler (gcc-4.3.4)
		# 54 Perl 6 (rakudo-2010.08)
		# 62 Text (text 6.10)
		# 101 Visual Basic .NET (mono-2.4.2.3)
		# 102 D (dmd) (dmd-2.042)
		# 104 AWK (gawk) (gawk-3.1.6)
		# 105 AWK (mawk) (mawk-1.3.3)
		# 106 COBOL 85 (tinycobol-0.65.9)
		# 107 Forth (gforth-0.7.0)
		# 108 Prolog (gnu) (gprolog-1.3.1)
		# 110 bc (bc-1.06.95)
		# 111 Clojure (clojure 1.1.0)
		# 112 JavaScript (spidermonkey) (spidermonkey-1.7)
		'.go':114,	# 114 Go (gc-2010-07-14)
		# 115 Unlambda (unlambda-2.0.0)
		# 116 Python 3 (python-3.1.2)
		# 117 R (R-2.11.1)
		# 118 COBOL (open-cobol-1.0)
		# 119 Oz (mozart-1.4.0)
		# 121 Groovy (groovy-1.7)
		# 122 Nimrod (nimrod-0.8.8)
		# 123 Factor (factor-0.93)
		# 124 F# (fsharp-2.0.0)
		# 125 Falcon (falcon-0.9.6.6)
	}
	return exts.get(fileext(filename), 62)

def submit(wsdlObject, srcfile, inputfile):
	srcstr = open(srcfile).read()
	inputstr = open(inputfile).read() if inputfile!='' else ''
	response = wsdlObject.createSubmission(user,passwd,srcstr,langId(srcfile),inputstr,True,True)
	return response.item[1][1]

def getStatus(wsdlObject, link):
	response = wsdlObject.getSubmissionStatus(user,passwd,link)
	return response.item[1][1], response.item[2][1]

def getResult(resultId):
	resulttype = {
		0:'not running',
		11:'compilation error',
		12:'runtime error',
		13:'time limit exceeded',
		15:'success',
		17:'memory lmit exceeded',
		19:'illegal system call',
		20:'internal error',
	}
	return resulttype.get(resultId, 'not expected return.')

def getOutput(wsdlObject, link):
	withSource, withInput, withOutput, withStderr, withCmpinfo = False,False,True,True,False
	response = wsdlObject.getSubmissionDetails(user,passwd,link, withSource, withInput, withOutput, withStderr, withCmpinfo)
	return response.item[11].value, response.item[12].value

if __name__ == '__main__':
	argv=sys.argv
	argc=len(argv)
	if argc<2:
		print 'usage: python %s sourcefile [inputfile]' % argv[0]
		quit()
	
	user = 'test' # TODO replace to your account
	passwd = 'test' # TODO replace to your password
	
	wsdlObject = WSDL.Proxy('http://ideone.com/api/1/service.wsdl')

	srcfilename = argv[1]
	inputfilename = argv[2] if argc>2 else ''
	link = submit(wsdlObject, srcfilename, inputfilename)
	
	# statusId == (wating for compilation <0, done 0, compilation 1, running 3)
	statusId = 1
	while statusId:
		print 'wating...'
		statusId, resultId = getStatus(wsdlObject, link)
	
	print getResult(resultId)
	
	url = 'http://ideone.com/'+link
	print 'url: ' + url

	# os.popen('echo '+url+' | xclip -sel clip')
	
	stdout, stderr = getOutput(wsdlObject, link)
	
	print "stdout:"
	print stdout
	print "stderr:"
	print stderr