Esempio n. 1
0
    def tearDown(self):
        """Test case cleanup function called automatically after each test."""
        if self.command:
            # Check instance working directory prediction against reality
            if self.command.vm:
                estimate = self.command.working_dir_disk_space_required()
                actual = directory_size(self.command.vm.working_dir)
                if estimate < actual:
                    self.fail("Estimated {0} would be needed in working"
                              " directory, but VM actually used {1}".format(
                                  pretty_bytes(estimate),
                                  pretty_bytes(actual)))

            self.command.destroy()
            self.command = None

        super(CommandTestCase, self).tearDown()
Esempio n. 2
0
    def tearDown(self):
        """Test case cleanup function called automatically after each test."""
        if self.command:
            # Check instance working directory prediction against reality
            if self.command.vm:
                estimate = self.command.working_dir_disk_space_required()
                actual = directory_size(self.command.vm.working_dir)
                if estimate < actual:
                    self.fail("Estimated {0} would be needed in working"
                              " directory, but VM actually used {1}"
                              .format(pretty_bytes(estimate),
                                      pretty_bytes(actual)))

            self.command.destroy()
            self.command = None

        super(CommandTestCase, self).tearDown()
Esempio n. 3
0
    def destroy(self):
        """Clean up after ourselves.

        Deletes :attr:`self.working_dir` and its contents.
        """
        try:
            if hasattr(self, 'working_dir') and os.path.exists(
                    self.working_dir):
                logger.verbose("Removing working directory")
                total_size = directory_size(self.working_dir)
                logger.debug(
                    "Size of working directory '%s', prior to"
                    " removal, is %s", self.working_dir,
                    pretty_bytes(total_size))
                # Clean up
                shutil.rmtree(self.working_dir)
        except AttributeError:
            pass
Esempio n. 4
0
    def destroy(self):
        """Clean up after ourselves.

        Deletes :attr:`self.working_dir` and its contents.
        """
        try:
            if hasattr(self,
                       'working_dir') and os.path.exists(self.working_dir):
                logger.verbose("Removing working directory")
                total_size = directory_size(self.working_dir)
                logger.debug("Size of working directory '%s', prior to"
                             " removal, is %s",
                             self.working_dir,
                             pretty_bytes(total_size))
                # Clean up
                shutil.rmtree(self.working_dir)
        except AttributeError:
            pass
Esempio n. 5
0
    def check_disk_space(self,
                         required_size,
                         location,
                         label="File",
                         context=None,
                         force_check=False,
                         die=False):
        """Check if there is sufficient disk space available at a location.

        If there is insufficient space, warn the user before continuing.

        Caches space requirements per location, so it's safe to call
        repeatedly, as it will only re-check (and possibly re-prompt the user)
        if:

          1. a different location is requested
          2. or the required size changes
          3. or ``force_check`` is True.

        Args:
          required_size (int): Bytes required
          location (str): Path to check availability of.
          label (str): Descriptive label to display in user messages.
          context (str): Optional string for additional context to provide
            when prompting the user.
          force_check (bool): If True, re-check and re-prompt the user even
            if this location has previously been checked and its
            ``required_size`` has not changed.
          die (bool): If True, use :meth:`~COT.ui.UI.confirm_or_die` instead
            of :meth:`~COT.ui.UI.confirm`

        Returns:
          bool: Whether sufficient space is available (or if not,
            whether the user has opted to continue anyway).

        Raises:
          SystemExit: if disk space is insufficient and ``die`` is True and
            the user declines to continue.
        """
        dir_path = os.path.abspath(location)
        while dir_path and not os.path.isdir(dir_path):
            dir_path = os.path.dirname(dir_path)
        # The above will never fail to find something - in the worst case,
        # it may ascend all the way to the filesystem root, but stop there.

        if dir_path in self._cached_disk_requirements and not force_check:
            prev_req, prev_avail = self._cached_disk_requirements[dir_path]
            if required_size <= prev_req:
                return required_size <= prev_avail

        logger.verbose(
            "Checking requested disk space (%s) against"
            " available space in %s", pretty_bytes(required_size), dir_path)

        available = available_bytes_at_path(dir_path)
        self._cached_disk_requirements[dir_path] = (required_size, available)

        if required_size <= available:
            return True

        msg = ("{0} may require approximately {1} of disk space,"
               " but only {2} is available at {3}.".format(
                   label, pretty_bytes(required_size), pretty_bytes(available),
                   location))
        if context:
            msg += "\n({0})".format(context)
        msg += "\nOperation may fail. Continue anyway?"
        if die:
            self.ui.confirm_or_die(msg)
            return True
        else:
            return self.ui.confirm(msg)
Esempio n. 6
0
    def check_disk_space(self, required_size, location,
                         label="File", context=None,
                         force_check=False, die=False):
        """Check if there is sufficient disk space available at a location.

        If there is insufficient space, warn the user before continuing.

        Caches space requirements per location, so it's safe to call
        repeatedly, as it will only re-check (and possibly re-prompt the user)
        if:

          1. a different location is requested
          2. or the required size changes
          3. or ``force_check`` is True.

        Args:
          required_size (int): Bytes required
          location (str): Path to check availability of.
          label (str): Descriptive label to display in user messages.
          context (str): Optional string for additional context to provide
            when prompting the user.
          force_check (bool): If True, re-check and re-prompt the user even
            if this location has previously been checked and its
            ``required_size`` has not changed.
          die (bool): If True, use :meth:`~COT.ui.UI.confirm_or_die` instead
            of :meth:`~COT.ui.UI.confirm`

        Returns:
          bool: Whether sufficient space is available (or if not,
            whether the user has opted to continue anyway).

        Raises:
          SystemExit: if disk space is insufficient and ``die`` is True and
            the user declines to continue.
        """
        dir_path = os.path.abspath(location)
        while dir_path and not os.path.isdir(dir_path):
            dir_path = os.path.dirname(dir_path)
        # The above will never fail to find something - in the worst case,
        # it may ascend all the way to the filesystem root, but stop there.

        if dir_path in self._cached_disk_requirements and not force_check:
            prev_req, prev_avail = self._cached_disk_requirements[dir_path]
            if required_size <= prev_req:
                return required_size <= prev_avail

        logger.verbose("Checking requested disk space (%s) against"
                       " available space in %s", pretty_bytes(required_size),
                       dir_path)

        available = available_bytes_at_path(dir_path)
        self._cached_disk_requirements[dir_path] = (required_size, available)

        if required_size <= available:
            return True

        msg = ("{0} may require approximately {1} of disk space,"
               " but only {2} is available at {3}."
               .format(label, pretty_bytes(required_size),
                       pretty_bytes(available), location))
        if context:
            msg += "\n({0})".format(context)
        msg += "\nOperation may fail. Continue anyway?"
        if die:
            self.ui.confirm_or_die(msg)
            return True
        else:
            return self.ui.confirm(msg)