Skip to content

Instantly share code, notes, and snippets.

@operatorequals
Last active May 3, 2019 11:21
Show Gist options
  • Save operatorequals/3cc9b8f0775b96bb6b910241e0da95ae to your computer and use it in GitHub Desktop.
Save operatorequals/3cc9b8f0775b96bb6b910241e0da95ae to your computer and use it in GitHub Desktop.
Batsh Online Compiler consumer
#!/usr/bin/env python3
'''
API consumer for the Batsh online compiler
# https://github.com/BYVoid/Batsh
'''
import argparse
import sys
import json
import requests
I_WANT_TO_BE_MITMd = False
class BatshCompilerException(Exception): pass
targets = ['bash', 'winbat']
url = 'https://batsh.org/compile'
#===
def compile_both_str(batsh_str, ignore = False, filename = '<inline>'):
output = {}
for target in targets:
output[target] = compile_str(target, batsh_str, ignore, filename)
return output
def compile_str(target, batsh_str, ignore = False, filename = '<inline>'):
if not target in targets: raise ValueError("target must be in", targets)
if batsh_str is None: raise ValueError("Batsh String must not be None")
post_data = {'code':batsh_str, 'target':target}
resp = requests.post(url, data = post_data,
verify = not I_WANT_TO_BE_MITMd
)
resp_obj = json.loads(resp.text)
if 'err' in resp_obj:
if not ignore:
msg = """
Batsh Syntax Error in file '%s':
%s""" % (filename,resp_obj['err'])
raise BatshCompilerException(msg)
return ''
return resp_obj['code']
def compile(target, batsh_file, ignore = False):
with open(batsh_file, 'r') as f:
batsh_str = f.read()
return compile_str(target, batsh_str, ignore, filename = batsh_file)
def main(args):
target = args.TARGET
batsh_file = args.BATSH_FILE
try:
compiled = compile(target, batsh_file)
print (compiled)
except BatshCompilerException as bce:
print (bce, file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("TARGET", help = 'The Target Command Line Environment', choices = targets)
parser.add_argument("BATSH_FILE", help = 'The file to compile', type=str)
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment