Esempio n. 1
0
 def _open_w(self, mode, bufsize, backup_name):
     """Open locked file for writing"""
     curr_key, tmp_name = self._get_key_save()
     result = None
     try:
         if curr_key == self.last_key:
             if curr_key:
                 if backup_name:
                     bak_name = Filename(backup_name, self.file_name).name
                     self.backup_name = bak_name
                     sos.rename(tmp_name, bak_name)
                 else:
                     self.backup_name = None
                     sos.remove(tmp_name)
             result = open(self.file_name, mode, bufsize)
             self.exists = 1
         else:
             self.changed_key = curr_key
             raise Sync_Conflict(self)
     finally:
         if not result:
             sos.rename(tmp_name, self.file_name)
         else:
             self._write_header(result)
     return result
Esempio n. 2
0
 def _open_w (self, mode, bufsize, backup_name) :
     """Open locked file for writing"""
     curr_key, tmp_name = self._get_key_save ()
     result = None
     try :
         if curr_key == self.last_key :
             if curr_key :
                 if backup_name :
                     bak_name = Filename (backup_name, self.file_name).name
                     self.backup_name = bak_name
                     sos.rename (tmp_name, bak_name)
                 else :
                     self.backup_name = None
                     sos.remove (tmp_name)
             result      = open (self.file_name, mode, bufsize)
             self.exists = 1
         else :
             self.changed_key = curr_key
             raise Sync_Conflict (self)
     finally :
         if not result :
             sos.rename         (tmp_name, self.file_name)
         else :
             self._write_header (result)
     return result
Esempio n. 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
Esempio n. 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
Esempio n. 5
0
 def _create_cache (self, name, map, minifier = None) :
     media_dir = self.media_dir
     if not sos.path.isdir (media_dir) :
         sos.mkdir_p (media_dir)
     else :
         for f in sos.listdir_ext (media_dir, name.lower ()) :
             sos.remove (f)
     for k, (href, fn, attr) in pyk.iteritems (map) :
         with open (fn, "wb") as file :
             if minifier is not None :
                 attr = minifier (attr)
             file.write (attr)
             if self.verbose :
                 print ("Wrote template media cache file", fn)
Esempio n. 6
0
 def _create_cache (self, name, map, minifier = None) :
     media_dir = self.media_dir
     if not sos.path.isdir (media_dir) :
         sos.mkdir_p (media_dir)
     else :
         for f in sos.listdir_ext (media_dir, name.lower ()) :
             sos.remove (f)
     for k, (href, fn, attr) in pyk.iteritems (map) :
         with open (fn, "wb") as file :
             if minifier is not None :
                 attr = minifier (attr)
             file.write (attr)
             if self.verbose :
                 print ("Wrote template media cache file", fn)
Esempio n. 7
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)
Esempio n. 8
0
File: FCM.py Progetto: 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)
Esempio n. 9
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)