def compile_module(cb_path, src_path): cb_path = pathobj.os_stringify(cb_path).encode('utf-8') src_path = pathobj.os_stringify(src_path).encode('utf-8') app_dir = os.environ.get('LEVER_PATH') if app_dir is None: app_dir = '' compiler_path = os.path.join(app_dir, "compile.py") py_path = find_python_interpreter() status = os.spawnv(os.P_WAIT, py_path, [py_path, escape_arg(compiler_path), escape_arg(cb_path), escape_arg(src_path)]) if status != 0: raise OldError(u"module compile failed: %s %s" % (cb_path.decode('utf-8'), src_path.decode('utf-8')))
def spawnv(path, args): pathname = pathobj.to_path(path) path = pathobj.os_stringify(pathname).encode('utf-8') argv = [] for arg in args.contents: if isinstance(arg, pathobj.Path): argv.append(pathobj.os_stringify(arg).encode('utf-8')) else: argv.append(as_cstring(arg)) pid = os.spawnv(os.P_NOWAIT, path, argv) return Integer(pid)
def compile_module(cb_path, src_path): cb_path = pathobj.os_stringify(cb_path).encode('utf-8') src_path = pathobj.os_stringify(src_path).encode('utf-8') app_dir = os.environ.get('LEVER_PATH') if app_dir is None: app_dir = '' compiler_path = os.path.join(app_dir, "compile.py") pid = os.fork() if pid == 0: os.execv(compiler_path, [compiler_path, cb_path, src_path]) return pid, status = os.waitpid(pid, 0) if status != 0: raise OldError(u"module compile failed: %s %s" % (cb_path.decode('utf-8'), src_path.decode('utf-8')))
def link(path, new_path): path = pathobj.os_stringify(path).encode('utf-8') new_path = pathobj.os_stringify(new_path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_link(response.ec.uv_loop, req, path, new_path, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return null finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def build_path_args(path, args): pathname = pathobj.to_path(path) path = pathobj.os_stringify(pathname).encode('utf-8') if '\x00' in path: raise OldError(u"NUL byte in spawnv path string") argv = [] for arg in args.contents: if isinstance(arg, pathobj.Path): a = pathobj.os_stringify(arg).encode('utf-8') else: a = as_cstring(arg) if '\x00' in a: raise OldError(u"NUL byte in spawnv arg string") argv.append(rstring.assert_str0(a)) return rstring.assert_str0(path), argv
def moduleinfo(module_path): module_path = pathobj.abspath(module_path) module_name = module_path.getattr(u"basename") assert isinstance(module_name, String) s = pathobj.os_stringify(module_path).encode('utf-8') is_dir = False if os.path.isdir(s): w = os.path.join(s, "init") if os.path.exists(w + ".lc.cb") or os.path.exists(w + ".lc"): is_dir = True s = w else: module_path = pathobj.directory(module_path) cb_path = s + ".lc.cb" cb_present = os.path.exists(cb_path) cb_mtime = 0.0 lc_path = s + ".lc" lc_present = os.path.exists(lc_path) lc_mtime = 0.0 if cb_present: cb_mtime = os.path.getmtime(cb_path) if lc_present: lc_mtime = os.path.getmtime(lc_path) # This ignores outdated bytecode objects. if cb_present and lc_present: cb_present = not cb_mtime < lc_mtime return ModuleInfo( module_name, module_path, pathobj.os_parse(cb_path.decode('utf-8')), cb_present, cb_mtime, pathobj.os_parse(lc_path.decode('utf-8')), lc_present, lc_mtime, )
def scandir(path): path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) dirent = lltype.malloc(uv.dirent_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_scandir( response.ec.uv_loop, req, path, 0, # TODO: check if there are meaningful flags for this. uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) listing = [] while True: res = uv.fs_scandir_next(req, dirent) if res == uv.EOF: break elif res < 0: raise uv_callback.to_error(res) entry = Exnihilo() entry.setattr(u"path", from_cstring(rffi.charp2str(dirent.c_name))) if dirent.c_type in uv.dirent2name: entry.setattr(u"type", String(uv.dirent2name[dirent.c_type])) else: entry.setattr(u"type", Integer(rffi.r_long(dirent.c_type))) listing.append(entry) return List(listing) finally: uv.fs_req_cleanup(req) lltype.free(dirent, flavor='raw') lltype.free(req, flavor='raw')
def getctime(path): pathname = pathobj.to_path(path) path = pathobj.os_stringify(pathname).encode('utf-8') try: return Float(os.path.getctime(path)) except IOError as error: raise ioerror(pathname, error)
def loadit(self, module): if not self.cb_present: compile_module(self.cb_path, self.lc_path) self.cb_mtime = os.path.getmtime(pathobj.os_stringify(self.cb_path).encode('utf-8')) self.cb_present = True program = evaluator.loader.from_object(bon.open_file(self.cb_path)) return program.call([module])
def getctime(path): pathname = pathobj.to_path(path) path = pathobj.os_stringify(pathname).encode('utf-8') try: return Float(os.path.getctime(path)) except IOError as error: raise unwind(LIOError(pathname, error.errno))
def compile_module(cb_path, src_path): cb_path = pathobj.os_stringify(cb_path).encode('utf-8') src_path = pathobj.os_stringify(src_path).encode('utf-8') app_dir = os.environ.get('LEVER_PATH') if app_dir is None: app_dir = '' compiler_path = os.path.join(app_dir, "compile.py") py_path = find_python_interpreter() status = os.spawnv(os.P_WAIT, py_path, [ py_path, escape_arg(compiler_path), escape_arg(cb_path), escape_arg(src_path) ]) if status != 0: raise OldError(u"module compile failed: %s %s" % (cb_path.decode('utf-8'), src_path.decode('utf-8')))
def spawnv(path, args): pathname = pathobj.to_path(path) path = pathobj.os_stringify(pathname).encode('utf-8') argv = [] for arg in args.contents: if isinstance(arg, pathobj.Path): x = pathobj.os_stringify(arg).encode('utf-8') else: x = as_cstring(arg) assert '\x00' not in x argv.append(x) pid = os.fork() if pid == 0: assert '\x00' not in path os.execv(path, argv) return null return Integer(pid)
def loadit(self, module): if not self.cb_present: compile_module(self.cb_path, self.lc_path) self.cb_mtime = os.path.getmtime( pathobj.os_stringify(self.cb_path).encode('utf-8')) self.cb_present = True program = evaluator.loader.from_object(bon.open_file(self.cb_path)) return program.call([module])
def loadit(self, module, scope): if not self.cb_present: while scope.compile_file is null and scope.parent is not None: scope = scope.parent if scope.compile_file is null: raise OldError(u"Lever bytecode compiler stale or missing: " + self.lc_path.repr()) scope.compile_file.call([self.cb_path, self.lc_path]) self.cb_mtime = os.path.getmtime(pathobj.os_stringify(self.cb_path).encode('utf-8')) self.cb_present = True program = evaluator.loader.from_object(bon.open_file(self.cb_path), self.cb_path) return program.call([module])
def open_file(pathname): name = pathobj.os_stringify(pathname).encode('utf-8') try: fd = rfile.create_file(name, 'rb') try: return load(fd) finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode('utf-8') raise OldError(u"%s: %s" % (pathobj.stringify(pathname), message))
def write_file(pathname, data): name = pathobj.os_stringify(pathname).encode('utf-8') try: fd = rfile.create_file(name, "wb") try: dump(fd, data) finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode('utf-8') raise OldError(u"%s: %s" % (pathobj.stringify(pathname), message)) return null
def read_file(path): path = pathobj.os_stringify(path).encode('utf-8') fd = os.open(path, os.O_RDONLY, 0777) try: data = "" frame = os.read(fd, frame_size) while frame != "": data += frame frame = os.read(fd, frame_size) finally: os.close(fd) return data.decode('utf-8')
def loadit(self, module, scope): if not self.cb_present: while scope.compile_file is null and scope.parent is not None: scope = scope.parent if scope.compile_file is null: raise OldError(u"Lever bytecode compiler stale or missing: " + self.lc_path.repr()) scope.compile_file.call([self.cb_path, self.lc_path]) self.cb_mtime = os.path.getmtime(pathobj.os_stringify(self.cb_path).encode('utf-8')) self.cb_present = True program = evaluator.loader.from_object(bon.open_file(self.cb_path), self.cb_path) res = program.call([module]) return res
def write_file(pathname, data): name = pathobj.os_stringify(pathname).encode("utf-8") try: fd = rfile.create_file(name, "wb") try: dump(fd, data) finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode("utf-8") raise OldError(u"%s: %s" % (pathobj.stringify(pathname), message)) return null
def open_(argv): if len(argv) < 1: raise OldError(u"too few arguments to fs.open()") pathname = pathobj.to_path(argv[0]) path = pathobj.os_stringify(pathname).encode('utf-8') if len(argv) > 1: mode = as_cstring(argv[1]) mode += 'b' else: mode = 'rb' try: return File(rfile.create_file(path, 'rb')) except IOError as error: raise ioerror(pathname, error)
def realpath(path): path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_realpath(response.ec.uv_loop, req, path, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) # hmm? return from_cstring(rffi.charp2str(rffi.cast(rffi.CCHARP, req.c_ptr))) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def mkdtemp(path): path = pathobj.os_stringify(path).encode('utf-8') # TODO: XXXXXX the last six characters must be these. req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_mkdtemp(response.ec.uv_loop, req, path, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return from_cstring(rffi.charp2str(rffi.cast(rffi.CCHARP, req.c_path))) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def write_file(pathname, obj, config): name = pathobj.os_stringify(pathname).encode('utf-8') try: fd = rfile.create_file(name, "wb") try: # TODO: sort of defeats the purpose of # incremental encoder. fd.write(configured_stringify(obj, config).encode('utf-8')) fd.write('\n') finally: fd.close() except IOError as error: message = os.strerror(error.errno).decode('utf-8') raise OldError(u"%s: %s" % (pathobj.stringify(pathname), message)) return null
def open_(path, flags, mode): mode = 0664 if mode is None else mode.value path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_open(response.ec.uv_loop, req, path, flags.value, mode, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return File(rffi.r_long(req.c_result)) finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def normal_startup(argv): if len(argv) > 0: main_script = argv[0] else: main_script = pathobj.concat(core.get_ec().lever_path, pathobj.parse(u"app/main.lc")) main_script = space.String(pathobj.os_stringify(main_script)) module = module_resolution.start(main_script) try: main_func = module.getattr(u"main") except space.Unwinder as unwinder: pass # in this case main_func just isn't in the module. else: main_func.call([space.List(argv)]) return space.null
def mkdir(path, mode): mode = 0777 if mode is None else mode.value path = pathobj.os_stringify(path).encode('utf-8') req = lltype.malloc(uv.fs_ptr.TO, flavor='raw', zero=True) try: response = uv_callback.fs(req) response.wait( uv.fs_mkdir(response.ec.uv_loop, req, path, mode, uv_callback.fs.cb)) if req.c_result < 0: raise uv_callback.to_error(req.c_result) return null finally: uv.fs_req_cleanup(req) lltype.free(req, flavor='raw')
def which(program): if isinstance(program, String): if program.string.count(u"/") > 0: program = pathobj.to_path(program) if isinstance(program, pathobj.Path): path = pathobj.os_stringify(program).encode('utf-8') if is_exe(path): return pathobj.concat(pathobj.getcwd(), program) return null elif not isinstance(program, String): raise OldError(u"string or path expected to .which()") program = as_cstring(program) for path in os.environ.get("PATH").split(os.pathsep): path = path.strip('"') exe_file = os.path.join(path, program) if is_exe(exe_file): return from_cstring(exe_file) return null
def Watch_init(path): ec = core.get_ec() path = pathobj.os_stringify(path).encode('utf-8') handle = lltype.malloc(uv.fs_event_ptr.TO, flavor='raw', zero=True) res = uv.fs_event_init(ec.uv_loop, handle) if res < 0: lltype.free(handle, flavor='raw') raise uv_callback.to_error(res) self = Watch(handle) uv_callback.push(ec.uv__fs_event, self) res = uv.fs_event_start(self.fs_event, _fs_event_cb_, path, 0) if res < 0: uv_callback.drop(ec.uv__fs_event, self.fs_event) Handle_close(self) raise uv_callback.to_error(res) return self
def stat(path): pathname = pathobj.to_path(path) path = pathobj.os_stringify(pathname).encode('utf-8') try: s = os.stat(path) except IOError as error: raise ioerror(pathname, error) return Exnihilo({ u"st_mode": Integer(s.st_mode), u"st_ino": Integer(s.st_ino), u"st_dev": Integer(s.st_dev), u"st_nlink": Integer(s.st_nlink), u"st_uid": Integer(s.st_uid), u"st_gid": Integer(s.st_gid), u"st_size": Integer(s.st_size), u"st_atime": Float(s.st_atime), u"st_mtime": Float(s.st_mtime), u"st_ctime": Float(s.st_ctime), })
def read_file(argv): if len(argv) < 1: raise OldError(u"too few arguments to fs.read_file()") pathname = pathobj.to_path(argv[0]) path = pathobj.os_stringify(pathname).encode('utf-8') convert = from_cstring if len(argv) > 1: for ch in as_cstring(argv[1]): if ch == 'b': convert = to_uint8array else: raise OldError(u"unknown mode string action") try: fd = rfile.create_file(path, 'rb') try: return convert(fd.read()) finally: fd.close() except IOError as error: raise unwind(LIOError(pathname, error.errno))
def read_file(argv): if len(argv) < 1: raise OldError(u"too few arguments to fs.read_file()") pathname = pathobj.to_path(argv[0]) path = pathobj.os_stringify(pathname).encode('utf-8') convert = from_cstring if len(argv) > 1: for ch in as_cstring(argv[1]): if ch == 'b': convert = to_uint8array else: raise OldError(u"unknown mode string action") try: fd = rfile.create_file(path, 'rb') try: return convert(fd.read()) finally: fd.close() except IOError as error: raise ioerror(pathname, error)
def exists(path): pathname = pathobj.to_path(path) path = pathobj.os_stringify(pathname).encode('utf-8') return boolean(os.path.exists(path))
def Spawn_init(options): ec = core.get_ec() flags = 0 c_options = lltype.malloc(uv.process_options_ptr.TO, flavor='raw', zero=True) c_options.c_exit_cb = rffi.llhelper(uv.exit_cb, _on_exit_) c_args_c = 0 try: name = String(u"file") p = cast(options.getitem(name), pathobj.Path, u".file") p = pathobj.os_stringify(p).encode('utf-8') c_options.c_file = rffi.str2charp(p) name = String(u"args") a = cast(options.getitem(name), List, u".args") c_args_c = len(a.contents) c_options.c_args = lltype.malloc(rffi.CCHARPP.TO, c_args_c + 1, flavor='raw', zero=True) for i in range(c_args_c): s = cast(a.contents[i], String, u".args[i]") s = s.string.encode('utf-8') c_options.c_args[i] = rffi.str2charp(s) # TODO: the env variables are provided weirdly. # figure out what the actual rules are here. #c_options.c_env = CCHARPP name = String(u"cwd") if options.contains(name): c = cast(options.getitem(name), pathobj.Path, u".cwd") c = pathobj.os_stringify(c).encode('utf-8') c_options.c_cwd = rffi.str2charp(p) name = String(u"stdio") if options.contains(name): e = cast(options.getitem(name), List, u".stdio") c_options.c_stdio_count = rffi.cast(rffi.INT, len(e.contents)) c_options.c_stdio = lltype.malloc(rffi.CArrayPtr( uv.stdio_container_t).TO, c_options.c_stdio_count, flavor='raw', zero=True) for i in range(c_options.c_stdio_count): if e.contents[i] == null: c_options.c_stdio[i].c_flags = rffi.cast( uv.stdio_flags, uv.IGNORE) continue o = cast(e.contents[i], Dict, u".stdio[i]") set_stdio(c_options.c_stdio[i], o) name = String(u"uid") if options.contains(name): n = cast(options.getitem(name), Integer, u".uid") c_options.c_uid = rffi.cast(uv.id_t, n.value) flags |= uv.PROCESS_SETUID name = String(u"gid") if options.contains(name): n = cast(options.getitem(name), Integer, u".gid") c_options.c_gid = rffi.cast(uv.id_t, n.value) flags |= uv.PROCESS_SETGID name = String(u"windows_verbatim_arguments") if options.contains(name): if is_true(options.getitem(name)): flags |= uv.PROCESS_WINDOWS_VERBATIM_ARGUMENTS name = String(u"detached") if options.contains(name): if is_true(options.getitem(name)): flags |= uv.PROCESS_DETACHED name = String(u"windows_hide") if options.contains(name): if is_true(options.getitem(name)): flags |= uv.PROCESS_WINDOWS_HIDE c_options.c_flags = rffi.cast(rffi.UINT, flags) process = ec.handles.create(uv.process_ptr, uv.spawn, c_options) finally: if c_options.c_stdio: lltype.free(c_options.c_stdio, flavor='raw') if c_options.c_cwd: lltype.free(c_options.c_cwd, flavor='raw') if c_options.c_args: for i in range(c_args_c): if c_options.c_args[i]: lltype.free(c_options.c_args[i], flavor='raw') lltype.free(c_options.c_args, flavor='raw') if c_options.c_file: lltype.free(c_options.c_file, flavor='raw') lltype.free(c_options, flavor="raw") return Spawn(process)