def create_linklet_json(rkt_file_name=""): # uses expander's extract to turn it into a linklet # gets the bytecodes and uses zo-expand to turn it into a json # and loads and instantiates it import os from pycket.util import os_get_env_var from rpython.rlib.rfile import create_popen_file PLTHOME = os_get_env_var("PLTHOME") EXPANDER_DIR = os.path.join( PLTHOME, os.path.join("racket", os.path.join("src", "expander"))) # FIXME: error check # prep_cmd = "raco make -v %s/bootstrap-run.rkt" % EXPANDER_DIR # pipe1 = create_popen_file(prep_cmd, "r") # pipe1.read() # pipe1.close() extract_cmd = "racket -t %s/bootstrap-run.rkt -- -c compiled/cache-src/ ++knot read - -s -x -t %s -o compiled/%s.sexp" % ( EXPANDER_DIR, rkt_file_name, rkt_file_name) pipe2 = create_popen_file(extract_cmd, "r") pipe2.read() pipe2.close() linklet_json_cmd = "racket linklet-extractor/linklet-sexp-to-json.rkt --output %s.linklet compiled/%s.sexp" % ( rkt_file_name, rkt_file_name) pipe3 = create_popen_file(linklet_json_cmd, "r") pipe3.read() pipe3.close()
def exec_(interp, cmd, r_output=None, r_return_var=None): space = interp.space if not cmd: raise ExitFunctionWithError('Cannot execute a blank command') if r_output is not None: if not space.is_array(r_output.deref_temp()): r_output.store(space.new_array_from_list([])) w_output = r_output.deref_unique() else: w_output = None try: pfile = create_popen_file(cmd, 'r') except OSError: raise ExitFunctionWithError('Unable to fork [%s]' % cmd, w_Null) last_line = '' while True: line = pfile.readline() if not line: break last_line = line.rstrip() if w_output: w_output.appenditem_inplace(space, space.newstr(last_line)) exitcode = pfile.close() if r_return_var is not None: r_return_var.store(space.wrap(exitcode)) return space.newstr(last_line)
def _expand_file_to_json(rkt_file, json_file, byte_flag=False): lib = _BE if byte_flag else _FN dbgprint("_expand_file_to_json", "", lib=lib, filename=rkt_file) from rpython.rlib.rfile import create_popen_file if not os.access(rkt_file, os.R_OK): raise ValueError("Cannot access file %s" % rkt_file) if not os.access(rkt_file, os.W_OK): # we guess that this means no permission to write the json file raise PermException(rkt_file) try: os.remove(json_file) except IOError: pass except OSError: pass cmd = "racket %s --output \"%s\" \"%s\" 2>&1" % (lib, json_file, rkt_file) if byte_flag: print "Transforming %s bytecode to %s" % (rkt_file, json_file) #cmd = "racket %s --output \"%s\" \"%s\"" % (lib, json_file, rkt_file) else: print "Expanding %s to %s" % (rkt_file, json_file) # print cmd pipe = create_popen_file(cmd, "r") out = pipe.read() err = os.WEXITSTATUS(pipe.close()) if err != 0: raise ExpandException("Racket produced an error and said '%s'" % out) return json_file
def systemstar_json_term(t): from rpython.rlib import rfile args = [shellquote(x.string_value()) for x in W_TermList(t)] f = rfile.create_popen_file(' '.join(args), 'r') s = f.read() f.close() return string_to_term(s)
def exec_(interp, cmd, r_output=None, r_return_var=None): space = interp.space if not cmd: raise ExitFunctionWithError("Cannot execute a blank command") if r_output is not None: if not space.is_array(r_output.deref_temp()): r_output.store(space.new_array_from_list([])) w_output = r_output.deref_unique() else: w_output = None try: pfile = create_popen_file(cmd, "r") except OSError: raise ExitFunctionWithError("Unable to fork [%s]" % cmd, w_Null) last_line = "" while True: line = pfile.readline() if not line: break last_line = line.rstrip() if w_output: w_output.appenditem_inplace(space, space.newstr(last_line)) exitcode = pfile.close() if r_return_var is not None: r_return_var.store(space.wrap(exitcode)) return space.newstr(last_line)
def test_pclose(self): retval = 32 printval = 42 cmd = "python -c 'import sys; print %s; sys.exit(%s)'" % (printval, retval) f = rfile.create_popen_file(cmd, "r") s = f.read() r = f.close() assert s == "%s\n" % printval assert os.WEXITSTATUS(r) == retval
def test_pclose(self): retval = 32 printval = 42 cmd = "python -c 'import sys; print %s; sys.exit(%s)'" % ( printval, retval) f = rfile.create_popen_file(cmd, "r") s = f.read() r = f.close() assert s == "%s\n" % printval assert os.WEXITSTATUS(r) == retval
def shell_exec(interp, cmd): try: r_pfile = create_popen_file(cmd, 'r') except OSError: interp.warn("Unable to execute '%s'" % cmd) return w_Null res = r_pfile.read(-1) if res: return interp.space.wrap(res) else: return w_Null
def expand_file_rpython(rkt_file): from rpython.rlib.rfile import create_popen_file cmd = "racket %s --stdout \"%s\" 2>&1" % (fn, rkt_file) if not os.access(rkt_file, os.R_OK): raise ValueError("Cannot access file %s" % rkt_file) pipe = create_popen_file(cmd, "r") out = pipe.read() err = os.WEXITSTATUS(pipe.close()) if err != 0: raise ExpandException("Racket produced an error and said '%s'" % out) return out
def expand_file_rpython(rkt_file, lib=_FN): from rpython.rlib.rfile import create_popen_file cmd = "racket %s --stdout \"%s\" 2>&1" % (lib, rkt_file) if not os.access(rkt_file, os.R_OK): raise ValueError("Cannot access file %s" % rkt_file) pipe = create_popen_file(cmd, "r") out = pipe.read() err = os.WEXITSTATUS(pipe.close()) if err != 0: raise ExpandException("Racket produced an error and said '%s'" % out) return out
def shell_exec(interp, cmd): try: r_pfile = create_popen_file(cmd, "r") except OSError: interp.warn("Unable to execute '%s'" % cmd) return w_Null res = r_pfile.read(-1) if res: return interp.space.wrap(res) else: return w_Null
def create_linklet_json(rkt_file_name=""): # uses expander's extract to turn it into a linklet # gets the bytecodes and uses zo-expand to turn it into a json # and loads and instantiates it import os from pycket.util import os_get_env_var from rpython.rlib.rfile import create_popen_file PLTHOME = os_get_env_var("PLTHOME") EXPANDER_DIR = os.path.join(PLTHOME, os.path.join("racket", os.path.join("src", "expander"))) # FIXME: error check prep_cmd = "raco make -v %s/bootstrap-run.rkt" % EXPANDER_DIR pipe1 = create_popen_file(prep_cmd, "r") pipe1.read() pipe1.close() extract_cmd = "racket -t %s/bootstrap-run.rkt -- -c compiled/cache-src/ ++knot read - -s -x -t %s -o compiled/%s.sexp" % (EXPANDER_DIR, rkt_file_name, rkt_file_name) pipe2 = create_popen_file(extract_cmd, "r") pipe2.read() pipe2.close() linklet_json_cmd = "racket linklet-extractor/linklet-sexp-to-json.rkt --output %s.linklet compiled/%s.sexp" % (rkt_file_name, rkt_file_name) pipe3 = create_popen_file(linklet_json_cmd, "r") pipe3.read() pipe3.close()
def execute(self, cmd): try: from rpython.rlib.rfile import create_popen_file except: return self.execute_fallback(cmd) pipe = create_popen_file(cmd, "r") result = pipe.read() err = "" exit_status = os.WEXITSTATUS(pipe.close()) if exit_status != 0: err = result result = "" return result, err
def _call_reader_rpython(self, modus, input): tmp_file = os.tmpnam() cmd = "racket -l racket/base -t %s -e '(do-read \"%s\")'" % ( "read.rkt", tmp_file) if not os.access("read.rkt", os.R_OK): raise Exception("Racket reader can not be accessed") pipe = create_popen_file(cmd, "w") input = "(%s %s)" % (modus, input) # NOTE: might go wrong when dealing with UNICODE pipe.write(input) pipe.write("\n\0\n") pipe.flush() err = os.WEXITSTATUS(pipe.close()) if err != 0: raise Exception("Reader produced an unexpected error") return tmp_file
def runlpeg(filename): changed = False if "lpeg" in listdir("."): chdir("./lpeg") # extremly unsafe. changed = True # i do this because the lpeg import by lua fails if i don't. no clue why. if filename in listdir("."): c_str = "lua "+filename file = create_popen_file(c_str, "r") bytecodestring = file.read() file.close() else: bytecodestring = None if changed: chdir("..") return bytecodestring
def expand_code_to_json(code, json_file, stdlib=True, mcons=False, wrap=True): from rpython.rlib.rfile import create_popen_file try: os.remove(json_file) except IOError: pass except OSError: pass cmd = "racket %s --output \"%s\" --stdin" % (_FN, json_file) # print cmd pipe = create_popen_file(cmd, "w") pipe.write("#lang s-exp pycket%s" % (" #:stdlib" if stdlib else "")) pipe.write(code) err = os.WEXITSTATUS(pipe.close()) if err != 0: raise ExpandException("Racket produced an error we failed to record") return json_file
def passthru(interp, cmd, r_return_var=None): space = interp.space if not cmd: raise ExitFunctionWithError('Cannot execute a blank command') try: pfile = create_popen_file(cmd, 'r') except OSError: raise ExitFunctionWithError('Unable to fork [%s]' % cmd, w_Null) last_line = '' while True: line = pfile.read() if not line: break interp.writestr(line, buffer=False) exitcode = pfile.close() if r_return_var is not None: r_return_var.store(space.wrap(exitcode)) return space.newstr(last_line)
def passthru(interp, cmd, r_return_var=None): space = interp.space if not cmd: raise ExitFunctionWithError("Cannot execute a blank command") try: pfile = create_popen_file(cmd, "r") except OSError: raise ExitFunctionWithError("Unable to fork [%s]" % cmd, w_Null) last_line = "" while True: line = pfile.read() if not line: break interp.writestr(line, buffer=False) exitcode = pfile.close() if r_return_var is not None: r_return_var.store(space.wrap(exitcode)) return space.newstr(last_line)
def file_to_AST_str(file_name_str, path=""): from config import USE_RPYTHON_POPEN out = "" c_str = command_str + option_str + " "+path+file_name_str # java call if USE_RPYTHON_POPEN: from rpython.rlib.rfile import create_popen_file file = create_popen_file(c_str, "r") out = file.read() file.close() else: p = Popen(c_str , shell=True, stderr=PIPE, stdin=PIPE, stdout=PIPE) w, r, e = (p.stdin, p.stdout, p.stderr) out = r.read() err_out = e.read() if err_out: print err_out r.close() w.close() e.close() return del_spaces(out)
def file_to_AST_str_no_print(file_name_str): from config import USE_RPYTHON_POPEN out = "" c_str = command_str + option_str + " "+file_name_str #print c_str if USE_RPYTHON_POPEN: # TODO: remove % s from rpython.rlib.rfile import create_popen_file file = create_popen_file(c_str, "r") out = file.read() file.close() return del_spaces(out), None else: p = Popen(c_str, shell=True, stderr=PIPE, stdin=PIPE, stdout=PIPE) w, r, e = (p.stdin, p.stdout, p.stderr) out = r.read() err_out = e.read() r.close() w.close() e.close() return del_spaces(out), err_out
def _expand_file_to_json(rkt_file, json_file): from rpython.rlib.rfile import create_popen_file if not os.access(rkt_file, os.R_OK): raise ValueError("Cannot access file %s" % rkt_file) if not os.access(rkt_file, os.W_OK): # we guess that this means no permission to write the json file raise PermException(rkt_file) try: os.remove(json_file) except IOError: pass except OSError: pass print "Expanding %s to %s" % (rkt_file, json_file) cmd = "racket %s --output \"%s\" \"%s\" 2>&1" % (fn, json_file, rkt_file) # print cmd pipe = create_popen_file(cmd, "r") out = pipe.read() err = os.WEXITSTATUS(pipe.close()) if err != 0: raise ExpandException("Racket produced an error and said '%s'" % out) return json_file
def _expand_file_to_json(rkt_file, json_file): from rpython.rlib.rfile import create_popen_file if not os.access(rkt_file, os.R_OK): raise ValueError("Cannot access file %s" % rkt_file) if not os.access(rkt_file, os.W_OK): # we guess that this means no permission to write the json file raise PermException(rkt_file) try: os.remove(json_file) except IOError: pass except OSError: pass print "Expanding %s to %s" % (rkt_file, json_file) cmd = 'racket %s --output "%s" "%s" 2>&1' % (fn, json_file, rkt_file) # print cmd pipe = create_popen_file(cmd, "r") out = pipe.read() err = os.WEXITSTATUS(pipe.close()) if err != 0: raise ExpandException("Racket produced an error and said '%s'" % out) return json_file
def f(): f = rfile.create_popen_file(cmd, "r") s = f.read() assert s == "%s\n" % printval return f.close()
def test_popen(self): f = rfile.create_popen_file("python -c 'print 42'", "r") s = f.read() f.close() assert s == '42\n'