Example #1
0
  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'))
Example #2
0
  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)
Example #3
0
  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)
Example #4
0
  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)
Example #5
0
 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)
Example #6
0
 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)
Example #8
0
 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)