Skip to content

Instantly share code, notes, and snippets.

@kikairoya
Last active December 31, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikairoya/7973391 to your computer and use it in GitHub Desktop.
Save kikairoya/7973391 to your computer and use it in GitHub Desktop.
script for using wandbox instead of local compiler
#!/usr/bin/python
from sys import argv, version_info
import json
from urllib2 import urlopen, Request
def print_usage():
print("usage: {} compiler {{options}} srcfile".format(argv[0]))
print(" {} --list-compilers".format(argv[0]))
print(" {} --list-options compiler".format(argv[0]))
print(" options are repetition of them:")
print(" some of shown by --list-options prefixed by '--'")
print(" '-f' {{whatever you wish for compiler option}}")
print(" '-F' {{whatever you wish for runtime option}}")
print(" if multiple sources are presented, they will be simply concatinated.")
return True
def get_compilers():
q = Request('http://melpon.org/wandbox/api/list.json', None, {'User-Agent':'Wandbox Command Line Interface/0.1'})
j = json.loads(urlopen(q).read())
s = []
r = {}
for c in j:
l = []
for o in c['switches']:
if 'options' in o:
for p in o['options']:
l.append(p['name'])
else:
l.append(o['name'])
r[c['name']] = l
s.append(c['name'])
return (s,r)
if len(argv) == 1: print_usage() and exit(1)
if argv[1] in ["-h", "--help", "-?"]: print_usage() and exit(0)
remote_compilers, remote_options = get_compilers()
if argv[1].startswith("--"):
if argv[1] == "--list-compilers":
for x in remote_compilers:
print(x)
exit(0)
if argv[1] == "--list-options":
if len(argv) < 3: print_usage() and exit(1)
if argv[2] not in remote_options :
print("compiler '{}' is not presented by wandbox".format(argv[2]))
exit(1)
for o in remote_options[argv[2]]:
print(o)
exit(0)
print("unknown option: {}".format(argv[1]))
exit(1)
if argv[1] not in remote_options:
print("compiler '{}' is not presented by wandbox".format(argv[1]))
exit(1)
next_raw = None
compiler = argv[1]
options = []
raw_cc_options = []
raw_run_options = []
srcfiles = []
for a in argv[2:]:
if next_raw == 'cc':
raw_cc_options.append(a)
next_raw = None
continue
if next_raw == 'run':
raw_run_options.append(a)
next_raw = None
continue
if a == "-f":
next_raw = 'cc'
continue
if a == "-F":
next_raw = 'run'
continue
if a.startswith("--"):
if a[2:] in remote_options[compiler]:
options.append(a[2:])
else:
print("option '{}' is not suitable for '{}', ignored".format(a[2:], compiler))
continue
if a.startswith("-"):
print("unknown option: {}".format(a))
exit(1)
srcfiles.append(a)
srctext=""
for f in srcfiles:
with open(f) as r:
srctext += r.read()
q = Request('http://melpon.org/wandbox/api/compile.json', json.dumps({
'code':srctext,
'options':','.join(options),
'compiler-option-raw':'\n'.join(raw_cc_options),
'runtime-option-raw':'\n'.join(raw_run_options),
'compiler':compiler,
'stdin':''
}), {'Content-type':'application/json','User-Agent':'Wandbox Command Line Interface/0.1'})
j = json.loads(urlopen(q).read())
print(j.get('compiler_message', ''))
print(j.get('program_message', ''))
if 'signal' in j: print(j['signal'])
exit(j['status'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment