def save_and_backup(self): """ Save file but keep backup first. The last step would shift a list of files below: weblib.dat.tmp (newly saved) weblib.dat (current weblib) weblib.dat.1 weblib.dat.2 weblib.dat.3 weblib.dat.4 weblib.dat.5 Note only weblib.dat.tmp is guarantine to exist before shift. """ self.lock.acquire() try: # first save to tmp filename tmp_pathname = self.pathname + '.tmp' self.save(tmp_pathname) # shift backup files files = [tmp_pathname, self.pathname] for i in range(self.BACKUP_COUNT): files.append('%s.%s' % (self.pathname, i+1)) fileutil.shift_files(files) finally: self.lock.release()
def save(self): today = str(datetime.date.today()) log.info('Saving config file: %s %s' % (self.config_path, today)) self.set('_system.updated', today) wpath = self.config_path + '.tmp' bpath = self.config_path + '.~' fp = file(wpath,'wb') try: self._serialize(fp) finally: fp.close() from minds.util import fileutil fileutil.shift_files([wpath, self.config_path, bpath])
def test_shift_files(self): A = testpath / 'a' B = testpath / 'b' C = testpath / 'c' if A.exists(): A.remove() if B.exists(): B.remove() if C.exists(): C.remove() # -- test 0 ------------------------------------------------------------------------ # ok if non of them exist fileutil.shift_files([A,B,C]) self.assert_(not A.exists()) self.assert_(not B.exists()) self.assert_(not C.exists()) # -- test 1 ------------------------------------------------------------------------ file(A,'w').write('1') # A->B (new file) fileutil.shift_files([A,B]) self.assert_(not A.exists()) self.assertEqual(file(B).read(), '1') # -- test 2 ------------------------------------------------------------------------ file(A,'w').write('2') # A->B (existing file) fileutil.shift_files([A,B]) self.assert_(not A.exists()) self.assertEqual(file(B).read(), '2') # -- test 3 ------------------------------------------------------------------------ file(A,'w').write('3') # A->B->C (new file) fileutil.shift_files([A,B,C]) self.assert_(not A.exists()) self.assertEqual(file(B).read(), '3') self.assertEqual(file(C).read(), '2') # -- test 4 ------------------------------------------------------------------------ file(A,'w').write('4') # A->B->C (existing file) fileutil.shift_files([A,B,C]) self.assert_(not A.exists()) self.assertEqual(file(B).read(), '4') self.assertEqual(file(C).read(), '3') # -- test 5 ------------------------------------------------------------------------ file(A,'w').write('5') # A->xBx->C (existing file) B.remove() fileutil.shift_files([A,B,C]) self.assert_(not A.exists()) self.assertEqual(file(B).read(), '5') self.assert_(not C.exists())