def test_rename_no_overwrite(self): workdir = fileutil.abspath_expanduser_unicode( u"test_rename_no_overwrite") fileutil.make_dirs(workdir) source_path = os.path.join(workdir, "source") dest_path = os.path.join(workdir, "dest") # when neither file exists self.failUnlessRaises(OSError, fileutil.rename_no_overwrite, source_path, dest_path) # when only dest exists fileutil.write(dest_path, b"dest") self.failUnlessRaises(OSError, fileutil.rename_no_overwrite, source_path, dest_path) self.failUnlessEqual(fileutil.read(dest_path), b"dest") # when both exist fileutil.write(source_path, b"source") self.failUnlessRaises(OSError, fileutil.rename_no_overwrite, source_path, dest_path) self.failUnlessEqual(fileutil.read(source_path), b"source") self.failUnlessEqual(fileutil.read(dest_path), b"dest") # when only source exists os.remove(dest_path) fileutil.rename_no_overwrite(source_path, dest_path) self.failUnlessEqual(fileutil.read(dest_path), b"source") self.failIf(os.path.exists(source_path))
def _rename_deleted_file(self, abspath_u): self._log('renaming deleted file to backup: %s' % (abspath_u,)) try: fileutil.rename_no_overwrite(abspath_u, abspath_u + u'.backup') except OSError: self._log("Already gone: '%s'" % (abspath_u,)) return abspath_u
def _rename_conflicted_file(self, abspath_u, replacement_path_u): self._log("_rename_conflicted_file(%r, %r)" % (abspath_u, replacement_path_u)) conflict_path_u = self._get_conflicted_filename(abspath_u) if False: if os.path.isfile(replacement_path_u): print "%r exists" % (replacement_path_u,) if os.path.isfile(conflict_path_u): print "%r exists" % (conflict_path_u,) fileutil.rename_no_overwrite(replacement_path_u, conflict_path_u) return conflict_path_u