Python tempfile.TMP_MAX Examples

The following are 8 code examples of tempfile.TMP_MAX(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tempfile , or try the search function .
Example #1
Source File: filesystems.py    From oss-ftp with MIT License 6 votes vote down vote up
def mkstemp(self, suffix='', prefix='', dir=None, mode='wb'):
        """A wrap around tempfile.mkstemp creating a file with a unique
        name.  Unlike mkstemp it returns an object with a file-like
        interface.
        """
        class FileWrapper:
            def __init__(self, fd, name):
                self.file = fd
                self.name = name

            def __getattr__(self, attr):
                return getattr(self.file, attr)

        text = 'b' not in mode
        # max number of tries to find out a unique file name
        tempfile.TMP_MAX = 50
        fd, name = tempfile.mkstemp(suffix, prefix, dir, text=text)
        file = os.fdopen(fd, mode)
        return FileWrapper(file, name)

    # --- Wrapper methods around os.* calls 
Example #2
Source File: filesystems.py    From oss-ftp with MIT License 6 votes vote down vote up
def mkstemp(self, suffix='', prefix='', dir=None, mode='wb'):
        """A wrap around tempfile.mkstemp creating a file with a unique
        name.  Unlike mkstemp it returns an object with a file-like
        interface.
        """
        class FileWrapper:
            def __init__(self, fd, name):
                self.file = fd
                self.name = name

            def __getattr__(self, attr):
                return getattr(self.file, attr)

        text = 'b' not in mode
        # max number of tries to find out a unique file name
        tempfile.TMP_MAX = 50
        fd, name = tempfile.mkstemp(suffix, prefix, dir, text=text)
        file = os.fdopen(fd, mode)
        return FileWrapper(file, name)

    # --- Wrapper methods around os.* calls 
Example #3
Source File: filesystems.py    From script-languages with MIT License 6 votes vote down vote up
def mkstemp(self, suffix='', prefix='', dir=None, mode='wb'):
        """A wrap around tempfile.mkstemp creating a file with a unique
        name.  Unlike mkstemp it returns an object with a file-like
        interface.
        """
        class FileWrapper:

            def __init__(self, fd, name):
                self.file = fd
                self.name = name

            def __getattr__(self, attr):
                return getattr(self.file, attr)

        text = 'b' not in mode
        # max number of tries to find out a unique file name
        tempfile.TMP_MAX = 50
        fd, name = tempfile.mkstemp(suffix, prefix, dir, text=text)
        file = os.fdopen(fd, mode)
        return FileWrapper(file, name)

    # --- Wrapper methods around os.* calls 
Example #4
Source File: ftpserver.py    From script-languages with MIT License 6 votes vote down vote up
def mkstemp(self, suffix='', prefix='', dir=None, mode='wb'):
        """A wrap around tempfile.mkstemp creating a file with a unique
        name.  Unlike mkstemp it returns an object with a file-like
        interface.
        """
        class FileWrapper:
            def __init__(self, fd, name):
                self.file = fd
                self.name = name
            def __getattr__(self, attr):
                return getattr(self.file, attr)

        text = not 'b' in mode
        # max number of tries to find out a unique file name
        tempfile.TMP_MAX = 50
        fd, name = tempfile.mkstemp(suffix, prefix, dir, text=text)
        file = os.fdopen(fd, mode)
        return FileWrapper(file, name)

    # --- Wrapper methods around os.* calls 
Example #5
Source File: temp.py    From owasp-pysec with Apache License 2.0 6 votes vote down vote up
def mkdtemp(dirpath, prefix='', suffix='', mode=0700):
    """Creates a directory in directory *dir* using *prefix* and *suffix* to
    name it:
            (dir)/<prefix><random_string><postfix>
    Returns absolute path of directory.
    """
    dirpath = os.path.abspath(dirpath)
    names = _get_candidate_names()
    mode = int(mode)
    if not fcheck.mode_check(mode):
        raise ValueError("wrong mode: %r" % oct(mode))
    for _ in xrange(TMP_MAX):
        name = names.next()
        fpath = os.path.abspath(os.path.join(dirpath, '%s%s%s'
                                % (prefix, name, suffix)))
        try:
            os.mkdir(fpath, mode)
            return fpath
        except OSError, ex:
            if ex.errno == errno.EEXIST:
                # try again
                continue
            raise 
Example #6
Source File: filesystems.py    From pyftpdlib with MIT License 6 votes vote down vote up
def mkstemp(self, suffix='', prefix='', dir=None, mode='wb'):
        """A wrap around tempfile.mkstemp creating a file with a unique
        name.  Unlike mkstemp it returns an object with a file-like
        interface.
        """
        class FileWrapper:

            def __init__(self, fd, name):
                self.file = fd
                self.name = name

            def __getattr__(self, attr):
                return getattr(self.file, attr)

        text = 'b' not in mode
        # max number of tries to find out a unique file name
        tempfile.TMP_MAX = 50
        fd, name = tempfile.mkstemp(suffix, prefix, dir, text=text)
        file = os.fdopen(fd, mode)
        return FileWrapper(file, name)

    # --- Wrapper methods around os.* calls 
Example #7
Source File: node.py    From ray with Apache License 2.0 5 votes vote down vote up
def _make_inc_temp(self, suffix="", prefix="", directory_name=None):
        """Return a incremental temporary file name. The file is not created.

        Args:
            suffix (str): The suffix of the temp file.
            prefix (str): The prefix of the temp file.
            directory_name (str) : The base directory of the temp file.

        Returns:
            A string of file name. If there existing a file having
                the same name, the returned name will look like
                "{directory_name}/{prefix}.{unique_index}{suffix}"
        """
        if directory_name is None:
            directory_name = ray.utils.get_ray_temp_dir()
        directory_name = os.path.expanduser(directory_name)
        index = self._incremental_dict[suffix, prefix, directory_name]
        # `tempfile.TMP_MAX` could be extremely large,
        # so using `range` in Python2.x should be avoided.
        while index < tempfile.TMP_MAX:
            if index == 0:
                filename = os.path.join(directory_name, prefix + suffix)
            else:
                filename = os.path.join(directory_name,
                                        prefix + "." + str(index) + suffix)
            index += 1
            if not os.path.exists(filename):
                # Save the index.
                self._incremental_dict[suffix, prefix, directory_name] = index
                return filename

        raise FileExistsError(errno.EEXIST,
                              "No usable temporary filename found") 
Example #8
Source File: temp.py    From owasp-pysec with Apache License 2.0 5 votes vote down vote up
def mkstemp(dirpath, prefix='', suffix='', unlink=1, mode=0600):
    """Creates a file in directory *dir* using *prefix* and *suffix* to
    name it:
            (dir)/<prefix><random_string><postfix>
    Returns a couple of files (pysec.io.fd.File):
            (Read_File, Write_File)
    If *unlink* is true registers a unlink function at exit.
    """
    dirpath = os.path.abspath(dirpath)
    mode = int(mode)
    if not fcheck.mode_check(mode):
        raise ValueError("wrong mode: %r" % oct(mode))
    names = _get_candidate_names()
    for _ in xrange(TMP_MAX):
        name = names.next()
        fpath = os.path.join(dirpath, '%s%s%s' % (prefix, name, suffix))
        if unlink:
            atexit.register(os.unlink, fpath)
        fdr = fdw = -1
        try:
            fdr = os.open(fpath, os.O_RDONLY | os.O_CREAT | os.O_EXCL, mode)
            fdw = os.open(fpath, os.O_WRONLY, mode)
            _set_cloexec(fdr)
            _set_cloexec(fdw)
            return fd.File(fdr), fd.File(fdw)
        except OSError, ex:
            if fdr != -1:
                os.close(fdr)
            if fdw != -1:
                os.close(fdw)
            if ex.errno == errno.EEXIST:
                continue
            else:
                try:
                    os.unlink(fpath)
                except IOError:
                    pass
            raise
        except: