Example #1
0
def open_tempfile(mode="w",
                  buffering=-1,
                  suffix="",
                  prefix="",
                  dir="",
                  auto_remove=True,
                  create_dir=False):
    """Context manager that opens a temporary file."""
    if create_dir and not sos.path.isdir(dir):
        sos.mkdir(dir)
    fd, temp_name = tempfile.mkstemp \
        (suffix = suffix, prefix = prefix, dir = dir, text = "t" in mode)
    try:
        file = sos.fdopen(fd, mode, buffering)
    except:
        sos.close(fd)
        sos.remove(temp_name)
        raise
    else:
        try:
            try:
                yield (file, temp_name)
            except:
                if sos.path.exists(temp_name):
                    sos.remove(temp_name)
                raise
        finally:
            if not file.closed:
                file.close()
            if auto_remove and sos.path.exists(temp_name):
                sos.remove(temp_name)
Example #2
0
File: FCM.py Project: Tapyr/tapyr
def open_tempfile (mode = "w", buffering = -1, suffix = "", prefix = "", dir = "", auto_remove = True, create_dir = False) :
    """Context manager that opens a temporary file."""
    if create_dir and not sos.path.isdir (dir) :
        sos.mkdir (dir)
    fd, temp_name = tempfile.mkstemp \
        (suffix = suffix, prefix = prefix, dir = dir, text = "t" in mode)
    try :
        file = sos.fdopen (fd, mode, buffering)
    except :
        sos.close  (fd)
        sos.remove (temp_name)
        raise
    else :
        try :
            try :
                yield (file, temp_name)
            except :
                if sos.path.exists (temp_name) :
                    sos.remove (temp_name)
                raise
        finally :
            if not file.closed :
                file.close ()
            if auto_remove and sos.path.exists (temp_name) :
                sos.remove (temp_name)
Example #3
0
 def _close_lock(self):
     if self._lock_file:
         try:
             try:
                 sos.close(self._lock_file)
             finally:
                 sos.remove(self._lfile_name)
         finally:
             self._lock_file = self._lfile_name = None
Example #4
0
 def _close_lock (self) :
     if self._lock_file :
         try :
             try :
                 sos.close  (self._lock_file)
             finally :
                 sos.remove (self._lfile_name)
         finally :
             self._lock_file = self._lfile_name = None
Example #5
0
def lock_file (file_name) :
    """Context manager providing a lock file."""
    ln = Filename (file_name + ".lock", absolute = 1)
    try :
        ### Don't try to open a lock file in a read-only directory (don't
        ### need a lock there anyway!)
        can_lock = sos.access (ln.directory, sos.W_OK)
        if can_lock :
            lf = sos.open (ln.name, sos.O_CREAT | sos.O_EXCL)
    except sos.error as exc :
        if exc.args [0] != errno.EEXIST :
            raise
        raise Sync_Conflict (file_name)
    else :
        try :
            yield None
        finally :
            if can_lock :
                try :
                    sos.close  (lf)
                finally :
                    sos.remove (ln.name)