def testExclude(self): """Test ability to empty actual directory contents. Also ensure that the excludes argument can really be just an iterable. """ files = { 'keep': True, 'keepdir/foo': True, 'keepdir/bar': True, 'remove': False, 'removedir/foo': False, 'removedir/bar': False, } excludes = ['keep', 'keepdir', 'bogus'] # Perform exclusion of non-existent files. osutils.EmptyDir(self.tempdir, exclude=iter(excludes)) # Create files. for f in files.keys(): osutils.Touch(os.path.join(self.tempdir, f), makedirs=True) # Empty with excludes. osutils.EmptyDir(self.tempdir, exclude=iter(excludes)) # Verify that the results are what we expect. for f, expected in files.items(): f = os.path.join(self.tempdir, f) self.assertEqual(os.path.exists(f), expected, 'Unexpected: %s' % f) self.assertExists(os.path.join(self.tempdir, 'keepdir')) self.assertNotExists(os.path.join(self.tempdir, 'removedir'))
def testNonExistentDir(self): """Non-existent directory.""" # Ignore_missing=False with self.assertRaises(osutils.EmptyDirNonExistentException): osutils.EmptyDir(self.subdir) # Ignore missing=True osutils.EmptyDir(self.subdir, ignore_missing=True)
def testEmptyWithRootOwnedContents(self): """Test handling of root owned sub directories.""" # Root owned contents. osutils.SafeMakedirs(self.nestedfile, sudo=True) # Fails without sudo=True with self.assertRaises(OSError): osutils.EmptyDir(self.tempdir) self.assertExists(self.nestedfile) # Works with sudo=True osutils.EmptyDir(self.tempdir, sudo=True) self.assertExists(self.tempdir) self.assertNotExists(self.subdir)
def testEmptyWithContentsMaxFlags(self): """Test ability to empty actual directory contents.""" osutils.Touch(self.nestedfile, makedirs=True) osutils.Touch(self.topfile, makedirs=True) osutils.EmptyDir(self.tempdir, ignore_missing=True, sudo=True) self.assertExists(self.tempdir) self.assertNotExists(self.subdir) self.assertNotExists(self.topfile)
def Destroy(self): """Completely destroy this moblabvm setup. Cleans out workspace_dir.""" self.Stop() osutils.EmptyDir(self.workspace, ignore_missing=True, sudo=True) self._config = collections.defaultdict(str)
def Empty(path): """Helper wrapper for the dry-run checks""" if self.options.dry_run: logging.notice('would have emptied: %s', path) else: osutils.EmptyDir(path, ignore_missing=True, sudo=True)
def tearDown(self): osutils.EmptyDir(self.tempdir)
def testEmptyDir(self): """Empty an empty directory.""" osutils.EmptyDir(self.tempdir) osutils.EmptyDir(self.tempdir, ignore_missing=True, sudo=True)
def _sync(self, src, dest): logging.info('Syncing %s to %s', src, dest) # TODO: This would probably be more efficient with rsync. osutils.EmptyDir(dest) osutils.CopyDirContents(src, dest)