def get_service(processor): """routes subdomains to the right service""" if not account.User.collection.find_one( {"key": request.args.get("key", "")}): return Response( "You must create an account and supply a valid API key to use this service", status=403) if processor not in processors: return Response( "Invalid compiler '{0}'. Valid compilers are: {1}".format( processor, ", ".join(processors)), status=400) elif request.files is None: return Response("No files recieved, should be 1", status=400) elif len(request.files) != 1: return Response("Should send 1 file, sent {0} instead".format( len(request.files)), status=400) elif request.files['data'] is None: return Response("Form key should be 'data'", status=400) in_dir = tempfile.mkdtemp() out_dir = tempfile.mkdtemp() comp_result = False try: untar_to_path(request.files['data'], in_dir) # Compile the code comp_result = (processors[processor])(in_dir, out_dir) stream = tar_paths(out_dir) finally: rmrf(in_dir) rmrf(out_dir) r = Response(stream, mimetype="application/gzip") if not comp_result: r.headers.add('warning', "Compilation reported a failure") return r
def extract_response(self, response, out_dir): if response.content is None: return "ERROR: No content recieved" # Create output dir if needed if not os.path.isdir(out_dir): try: os.mkdir(out_dir) except EnvironmentError as e: return "ERROR: Cannot create output folder '{0}': {1}".format(out_dir, str(e)) else: # Delete existing __precomp__ folder rmrf(os.path.join(out_dir, "__precomp__")) untar_stream_to_path(response.content, out_dir) warn = response.headers.get('warning', None) if warn: return "WARNING: '{0}'. Finished, new files in '{1}'".format(warn, out_dir) else: return "Compilation succeeded, new files in '{0}'".format(out_dir)
def get_service(processor): """routes subdomains to the right service""" if not account.User.collection.find_one({"key": request.args.get("key", "")}): return Response ("You must create an account and supply a valid API key to use this service", status=403) if processor not in processors: return Response ("Invalid compiler '{0}'. Valid compilers are: {1}".format(processor, ", ".join(processors)), status=400) elif request.files is None: return Response ("No files recieved, should be 1", status=400) elif len(request.files) != 1: return Response ("Should send 1 file, sent {0} instead".format(len(request.files)), status=400) elif request.files['data'] is None: return Response ("Form key should be 'data'", status=400) in_dir = tempfile.mkdtemp() out_dir = tempfile.mkdtemp() comp_result = False try: untar_to_path(request.files['data'], in_dir) # Compile the code comp_result = (processors[processor])(in_dir, out_dir) stream = tar_paths(out_dir) finally: rmrf(in_dir) rmrf(out_dir) r = Response(stream, mimetype="application/gzip") if not comp_result: r.headers.add('warning', "Compilation reported a failure") return r
def extract_response(self, response, out_dir): if response.content is None: return "ERROR: No content recieved" # Create output dir if needed if not os.path.isdir(out_dir): try: os.mkdir(out_dir) except EnvironmentError as e: return "ERROR: Cannot create output folder '{0}': {1}".format( out_dir, str(e)) else: # Delete existing __precomp__ folder rmrf(os.path.join(out_dir, "__precomp__")) untar_stream_to_path(response.content, out_dir) warn = response.headers.get('warning', None) if warn: return "WARNING: '{0}'. Finished, new files in '{1}'".format( warn, out_dir) else: return "Compilation succeeded, new files in '{0}'".format(out_dir)