def installPackages(self, logFile: io.IOBase = tempfile.TemporaryFile, outputLogFile: io.IOBase = tempfile.TemporaryFile): self._unlock() logFile, outputLogFile = logFile(), outputLogFile() oldStdError, oldStdOut = os.dup(sys.stderr.fileno()), os.dup( sys.stdout.fileno()) os.dup2(outputLogFile.fileno(), sys.stderr.fileno()) os.dup2(outputLogFile.fileno(), sys.stdout.fileno()) try: self.pkgManager.do_install(logFile.fileno()) except Exception as e: self.installError = (e, traceback.format_exc()) os.dup2(oldStdError, sys.stderr.fileno()) os.dup2(oldStdOut, sys.stdout.fileno()) self._lock() logFile.seek(0) outputLogFile.seek(0) self.updateCache() [package.update(install=True) for package in self.pkgList.values()] for line in (line.decode().replace("\n", "") for line in logFile.readlines()): matches = re.match(r'^pmerror:([^:]+):([^:]+):(.*)$', line) if matches: pkgName, _, error = matches[1], matches[2], matches[3] self.pkgList[pkgName].errors += [error] logFile.seek(0) self.logFile, self.outputLogFile = logFile, outputLogFile
def make_non_blocking(file_obj: io.IOBase): """make file object non-blocking Windows doesn't have the fcntl module, but someone on stack overflow supplied this code as an answer, and it works http://stackoverflow.com/a/34504971/2893090""" if USING_WINDOWS: LPDWORD = POINTER(DWORD) PIPE_NOWAIT = wintypes.DWORD(0x00000001) SetNamedPipeHandleState = windll.kernel32.SetNamedPipeHandleState SetNamedPipeHandleState.argtypes = [HANDLE, LPDWORD, LPDWORD, LPDWORD] SetNamedPipeHandleState.restype = BOOL h = msvcrt.get_osfhandle(file_obj.fileno()) # type: ignore res = windll.kernel32.SetNamedPipeHandleState(h, byref(PIPE_NOWAIT), None, None) if res == 0: raise ValueError(WinError()) else: # Set the file status flag (F_SETFL) on the pipes to be non-blocking # so we can attempt to read from a pipe with no new data without locking # the program up fcntl.fcntl(file_obj, fcntl.F_SETFL, os.O_NONBLOCK)
def _maybe_fd(self, filelike: io.IOBase) -> None: try: self.fd = filelike.fileno() except (AttributeError, OSError): self.fd = None
def read(source: io.IOBase, entry_start: int = None, entry_stop: int = None) -> ak.Array: """Read a MIDAS tape file and return the ragged array. :param source: open MIDAS file source :param entry_start: the first entry to include. If None, start at 0. :param entry_stop: the first entry to exclude. If None, stop at the end of the file. """ with contextlib.ExitStack() as stack: memory = stack.enter_context( mmap.mmap(source.fileno(), 0, flags=mmap.ACCESS_READ, prot=mmap.PROT_READ)) # We will be searching linearly, so advise the kernel memory.madvise(mmap.MADV_SEQUENTIAL) return load(memory)
def dup2(old_io: io.IOBase, new_fd: int): if old_io.fileno() == new_fd: return new_fd try: fcntl.fcntl(old_io, fcntl.F_GETFL) except OSError: print("cloud not close file descripter.") return errno.EBADF try: os.close(new_fd) except OSError: print("cloud not close file descripter.") return os.EX_IOERR try: new_fd = fcntl.fcntl(old_io, fcntl.F_DUPFD, new_fd) except OSError: print("cloud not duplicate file descripter.") return os.EX_IOERR return new_fd
def has_fileno(f: io.IOBase) -> bool: try: f.fileno() return True except io.UnsupportedOperation: return False