def apply_patch(original, patchfile, reverse=False, fuzz=2): """Apply a patch with optional fuzz - uses the commandline 'patch' utility.""" result = tempfile.NamedTemporaryFile(delete=False) try: # We open the file again to avoid race-conditions with multithreaded reads with open(original.name) as fp: shutil.copyfileobj(fp, result) result.close() cmdline = ["patch", "--no-backup-if-mismatch", "--force", "--silent", "-r", "-"] if reverse: cmdline.append("--reverse") if fuzz != 2: cmdline.append("--fuzz=%d" % fuzz) cmdline += [result.name, patchfile.name] exitcode = subprocess.call(cmdline, stdout=_devnull, stderr=_devnull) if exitcode != 0: raise PatchApplyError("Failed to apply patch (exitcode %d)." % exitcode) # Hack - we can't keep the file open while patching ('patch' might rename/replace # the file), so create a new _TemporaryFileWrapper object for the existing path. return tempfile._TemporaryFileWrapper(file=open(result.name, 'r+b'), \ name=result.name, delete=True) except: os.unlink(result.name) raise
def apply_patch(original, patchfile, reverse=False, fuzz=2): """Apply a patch with optional fuzz - uses the commandline 'patch' utility.""" result = tempfile.NamedTemporaryFile(delete=False) try: # We open the file again to avoid race-conditions with multithreaded reads with open(original.name) as fp: shutil.copyfileobj(fp, result) result.close() cmdline = [ "patch", "--no-backup-if-mismatch", "--force", "--silent", "-r", "-" ] if reverse: cmdline.append("--reverse") if fuzz != 2: cmdline.append("--fuzz=%d" % fuzz) cmdline += [result.name, patchfile.name] exitcode = subprocess.call(cmdline, stdout=_devnull, stderr=_devnull) if exitcode != 0: raise PatchApplyError("Failed to apply patch (exitcode %d)." % exitcode) # Hack - we can't keep the file open while patching ('patch' might rename/replace # the file), so create a new _TemporaryFileWrapper object for the existing path. return tempfile._TemporaryFileWrapper(file=open(result.name, 'r+b'), \ name=result.name, delete=True) except: os.unlink(result.name) raise
def hardlink(path, suffix='', prefix='didjvu.', dir=None): new_path = name(suffix=suffix, prefix=prefix, dir=dir) os.link(path, new_path) return tempfile._TemporaryFileWrapper( open(new_path, 'r+b'), new_path )
def _tempfile(self): w, name = self.temp_maker() r = os.open(name, os.O_RDONLY) self._set_cloexec_flag(w) # TODO: bufsize? r = open(name, 'rU' if self.universal_newlines else 'rb', 0) r = tempfile._TemporaryFileWrapper(r, name) self._set_cloexec_flag(r.fileno()) return r, w
def _try_create_desired_file(mode, bufsize, dir_path, desired_name, delete): # try to match desired_name directly # most of this code is adapted from the tempfile source # pylint: disable=W0212 if 'b' in mode: flags = tempfile._bin_openflags else: flags = tempfile._text_openflags fd = os.open(os.path.join(dir_path, desired_name), flags, 0600) tempfile._set_cloexec(fd) file_name = os.path.abspath(os.path.join(dir_path, desired_name)) file_handle = os.fdopen(fd, mode, bufsize) return tempfile._TemporaryFileWrapper(file_handle, file_name, delete)
def open_concrete(self, delete: bool = True) -> IO[bytes]: if self._concrete_path and os.path.isfile(self._concrete_path): return open(self._concrete_path, "rb") # noqa: SIM115 file_path = os.path.join(self.parent_file.container_temp_root, self.path_in_container) os.makedirs(os.path.dirname(file_path), exist_ok=True) tf = open(file_path, "wb") # noqa: SIM115 self.extract(tf) tf.seek(0) self._concrete_path = tf.name return tempfile._TemporaryFileWrapper(tf, tf.name, delete=delete) # type: ignore
def __init__(self, mode='w+b', bufsize=-1, suffix=["shp", "dbf", "shx"], prefix=tmp.template, dir=None, delete=True): from collections import Counter super(NamedTemporaryFiles, self).__init__() # first check whether no invalid suffix is part suffix = list(suffix) for s in (s for s in suffix if not s): raise ValueError("Empty suffixes are not allowed!") # now check whether one suffix occurs multiple times for item in (item for item in Counter(suffix).items() if item[1] > 1): raise ValueError("Found suffix %s multiple (%i) times!" % item) suffix = ['.'+s for s in suffix] files = [0] * len(suffix) success = False for _ in xrange(tmp.TMP_MAX): f = tmp.NamedTemporaryFile( mode=mode, bufsize=bufsize, suffix=suffix[0], prefix=prefix, dir=dir, delete=delete) basename = os.path.splitext(f.name)[0] success = all(not os.path.exists(basename+s) for s in suffix[1:]) if success: break if not success: f.close() raise IOError("No usable temporary file name found") if 'b' in mode: flags = tmp._bin_openflags else: flags = tmp._text_openflags # Setting O_TEMPORARY in the flags causes the OS to delete # the file when it is closed. This is only supported by Windows. if os.name == 'nt' and delete: flags |= os.O_TEMPORARY files[0] = f for i, s in enumerate(suffix[1:], start=1): fd = os.open(basename+s, flags, 0600) tmp._set_cloexec(fd) f = os.fdopen(fd, mode, bufsize) files[i] = tmp._TemporaryFileWrapper(f, basename+s, delete=delete) self.name = basename self.update(zip([s[1:] for s in suffix], files))
def tmpfile(self, prefix=None, suffix=".tmp", fullname=None, delete=None): if delete is None: delete = self.delete if fullname is not None: path = self.pathname(fullname) f = open(path, "w+") if delete: f = _TemporaryFileWrapper(f, f.name) else: if prefix is None: prefix = self.name + "_" else: prefix = "{0}_{1}_".format(self.name, prefix) f = NamedTemporaryFile(prefix=prefix, suffix=suffix, dir=self.directory, delete=delete) self.memory.append(f) return f
def get_named_tempfile(delete): try: return tempfile.NamedTemporaryFile(delete=delete) except TypeError: if os.name == "nt": dir_ = tempfile.gettempdir() flags = tempfile._bin_openflags if delete: flags |= os.O_TEMPORARY (fd, name) = tempfile._mkstemp_inner(dir_, "tmp", "", flags) f = os.fdopen(fd, "w+b", -1) return tempfile._TemporaryFileWrapper(f, name) else: f = tempfile.NamedTemporaryFile() if not delete: def close(): f.close_called = True f.file.close() f.close = close return f
def SharedTemporaryFile( mode="w+b", buffering=-1, encoding=None, newline=None, suffix="", prefix=template, dir=None, delete=True, communicator=parallelComm, ): """Create a temporary file shared by all MPI ranks. The file is created as `NamedTemporaryFile` would do it. The name of the returned file-like object is accessible as its ``name`` attribute. The file will be automatically deleted when it is closed unless the `delete` argument is set to False. >>> from fipy.tools import SharedTemporaryFile, parallelComm >>> with SharedTemporaryFile(mode='w+', suffix=".tmp") as tmpFile: ... # write on processor 0 ... if parallelComm.procID == 0: ... _ = tmpFile.write("shared text") ... ... parallelComm.Barrier() ... ... # read on all processors ... _ = tmpFile.seek(0) ... txt = tmpFile.read() >>> print(txt) shared text Parameters ---------- prefix, suffix, dir : str As for mkstemp mode : str The mode argument to io.open (default "w+b") buffering : int The buffer size argument to io.open (default -1) encoding : str or None The encoding argument to io.open (default None) newline : str or None The newline argument to io.open (default None) delete : bool Whether the file is deleted on close (default True) communicator : ~fipy.tools.comms.commWrapper.CommWrapper MPI communicator describing ranks to share with. A duck-typed object with `procID` and `Nproc` attributes is sufficient. Returns ------- file-like object See Also -------- tempfile.NamedTemporaryFile, tempfile.mkstemp, io.open """ if communicator.procID == 0: file = NamedTemporaryFile( mode=mode, buffering=buffering, encoding=encoding, newline=newline, suffix=suffix, prefix=prefix, dir=dir, delete=delete, ) fname = file.name else: fname = None fname = communicator.bcast(fname) if communicator.procID != 0: file = _io.open(fname, mode, buffering=buffering, newline=newline, encoding=encoding) # let procID 0 handle delete file = _TemporaryFileWrapper(file, fname, delete=False) return file