def testEmptyDir(self): """ Test if method can delete an empty dir. """ with namedTemporaryDir() as baseDir: dirty = os.path.join(baseDir, 'dirty') os.mkdir(dirty) fileUtils.cleanupdir(dirty) self.assertFalse(os.path.lexists(dirty))
def create_image_rollback(cls, task, image_dir): """ Remove empty image folder """ cls.log.info("create image rollback (image_dir=%s)", image_dir) if os.path.exists(image_dir): if not len(os.listdir(image_dir)): fileUtils.cleanupdir(image_dir) else: cls.log.error("create image rollback: Cannot remove dirty " "image (image_dir=%s)", image_dir)
def testDirWithUndeletableFiles(self): """ See that the method handles correctly a situation where it is given a dir it can't clean. """ baseDir = "/proc/misc" # This can't be deleted # Try and fail to clean it fileUtils.cleanupdir(baseDir, ignoreErrors=True) self.assertTrue(os.path.lexists(baseDir)) self.assertRaises(RuntimeError, fileUtils.cleanupdir, baseDir, False) self.assertTrue(os.path.lexists(baseDir))
def create_image_rollback(cls, task, image_dir): """ Remove empty image folder """ cls.log.info("create image rollback (image_dir=%s)", image_dir) if os.path.exists(image_dir): if not len(os.listdir(image_dir)): cls.log.info("Removing image directory %r", image_dir) fileUtils.cleanupdir(image_dir) else: cls.log.error("create image rollback: Cannot remove dirty " "image (image_dir=%s)", image_dir)
def testFullDir(self): """ Test if method can clean a dir it should be able to. """ with namedTemporaryDir() as baseDir: # Populate dir dirty = os.path.join(baseDir, 'dirty') os.mkdir(dirty) numOfFilesToCreate = 50 for i in range(numOfFilesToCreate): tempfile.mkstemp(dir=dirty) # clean it fileUtils.cleanupdir(dirty) self.assertFalse(os.path.lexists(dirty))
def createVolumeRollback(cls, taskObj, repoPath, sdUUID, imgUUID, volUUID, imageDir): cls.log.info("createVolumeRollback: repoPath=%s sdUUID=%s imgUUID=%s " "volUUID=%s imageDir=%s" % (repoPath, sdUUID, imgUUID, volUUID, imageDir)) vol = sdCache.produce(sdUUID).produceVolume(imgUUID, volUUID) pvol = vol.getParentVolume() # Remove volume vol.delete(postZero=False, force=True) if len(cls.getImageVolumes(repoPath, sdUUID, imgUUID)): # Don't remove the image folder itself return if not pvol or pvol.isShared(): # Remove image folder with all leftovers if os.path.exists(imageDir): fileUtils.cleanupdir(imageDir)
def createVolumeRollback(cls, taskObj, repoPath, sdUUID, imgUUID, volUUID, imageDir): cls.log.info("createVolumeRollback: repoPath=%s sdUUID=%s imgUUID=%s " "volUUID=%s imageDir=%s" % (repoPath, sdUUID, imgUUID, volUUID, imageDir)) vol = sdCache.produce(sdUUID).produceVolume(imgUUID, volUUID) pvol = vol.getParentVolume() # Remove volume vol.delete(postZero=False, force=True, discard=False) if len(cls.getImageVolumes(repoPath, sdUUID, imgUUID)): # Don't remove the image folder itself return if not pvol or pvol.isShared(): # Remove image folder with all leftovers if os.path.exists(imageDir): fileUtils.cleanupdir(imageDir)
def testNotExistingDir(self): """ See that method doesn't throw a fit if given a non existing dir """ fileUtils.cleanupdir(os.path.join("I", " DONT", "EXIST"))