def runAptWithArgs(self, apt_config_filename, *args):
        """Run apt-ftparchive in subprocesses.

        :raise: AptFTPArchiveFailure if any of the apt-ftparchive
            commands failed.
        """
        self.log.debug("Filepath: %s" % apt_config_filename)

        stdout_handler = OutputLineHandler(self.log.debug, "a-f: ")
        stderr_handler = OutputLineHandler(self.log.info, "a-f: ")
        base_command = ["apt-ftparchive"] + list(args) + [apt_config_filename]
        spawner = CommandSpawner()

        returncodes = {}
        completion_handler = ReturnCodeReceiver()
        returncodes['all'] = completion_handler
        spawner.start(base_command,
                      stdout_handler=stdout_handler,
                      stderr_handler=stderr_handler,
                      completion_handler=completion_handler)

        spawner.complete()
        stdout_handler.finalize()
        stderr_handler.finalize()
        failures = sorted([(tag, receiver.returncode)
                           for tag, receiver in returncodes.iteritems()
                           if receiver.returncode != 0])
        if len(failures) > 0:
            by_arch = ["%s (returned %d)" % failure for failure in failures]
            raise AptFTPArchiveFailure("Failure(s) from apt-ftparchive: %s" %
                                       ", ".join(by_arch))
Exemple #2
0
def execute(logger, command, args=None):
    """Execute a shell command.

    :param logger: Output from the command will be logged here.
    :param command: Command to execute, as a string.
    :param args: Optional list of arguments for `command`.
    :raises LaunchpadScriptFailure: If the command returns failure.
    """
    command_line = [command]
    if args is not None:
        command_line += args
    description = ' '.join(command_line)

    logger.debug("Execute: %s", description)
    # Some of these commands can take a long time.  Use CommandSpawner
    # and friends to provide "live" log output.  Simpler ways of running
    # commands tend to save it all up and then dump it at the end, or
    # have trouble logging it as neat lines.
    stderr_logger = OutputLineHandler(logger.info)
    stdout_logger = OutputLineHandler(logger.debug)
    receiver = ReturnCodeReceiver()
    spawner = CommandSpawner()
    spawner.start(command_line,
                  completion_handler=receiver,
                  stderr_handler=stderr_logger,
                  stdout_handler=stdout_logger)
    spawner.complete()
    stdout_logger.finalize()
    stderr_logger.finalize()
    if receiver.returncode != 0:
        raise LaunchpadScriptFailure("Failure while running command: %s" %
                                     description)
    def runAptWithArgs(self, apt_config_filename, *args):
        """Run apt-ftparchive in subprocesses.

        :raise: AptFTPArchiveFailure if any of the apt-ftparchive
            commands failed.
        """
        self.log.debug("Filepath: %s" % apt_config_filename)

        stdout_handler = OutputLineHandler(self.log.debug, "a-f: ")
        stderr_handler = OutputLineHandler(self.log.info, "a-f: ")
        base_command = ["apt-ftparchive"] + list(args) + [apt_config_filename]
        spawner = CommandSpawner()

        returncodes = {}
        completion_handler = ReturnCodeReceiver()
        returncodes['all'] = completion_handler
        spawner.start(
            base_command, stdout_handler=stdout_handler,
            stderr_handler=stderr_handler,
            completion_handler=completion_handler)

        spawner.complete()
        stdout_handler.finalize()
        stderr_handler.finalize()
        failures = sorted([
            (tag, receiver.returncode)
            for tag, receiver in returncodes.iteritems()
                if receiver.returncode != 0])
        if len(failures) > 0:
            by_arch = ["%s (returned %d)" % failure for failure in failures]
            raise AptFTPArchiveFailure(
                "Failure(s) from apt-ftparchive: %s" % ", ".join(by_arch))
def execute(logger, command, args=None):
    """Execute a shell command.

    :param logger: Output from the command will be logged here.
    :param command: Command to execute, as a string.
    :param args: Optional list of arguments for `command`.
    :raises LaunchpadScriptFailure: If the command returns failure.
    """
    command_line = [command]
    if args is not None:
        command_line += args
    description = ' '.join(command_line)

    logger.debug("Execute: %s", description)
    # Some of these commands can take a long time.  Use CommandSpawner
    # and friends to provide "live" log output.  Simpler ways of running
    # commands tend to save it all up and then dump it at the end, or
    # have trouble logging it as neat lines.
    stderr_logger = OutputLineHandler(logger.info)
    stdout_logger = OutputLineHandler(logger.debug)
    receiver = ReturnCodeReceiver()
    spawner = CommandSpawner()
    spawner.start(
        command_line, completion_handler=receiver,
        stderr_handler=stderr_logger, stdout_handler=stdout_logger)
    spawner.complete()
    stdout_logger.finalize()
    stderr_logger.finalize()
    if receiver.returncode != 0:
        raise LaunchpadScriptFailure(
            "Failure while running command: %s" % description)
 def setUp(self):
     super(TestOutputLineHandler, self).setUp()
     self.handler = OutputLineHandler(FakeMethod())
class TestOutputLineHandler(TestCase):
    """Unit tests for `OutputLineHandler`."""

    def setUp(self):
        super(TestOutputLineHandler, self).setUp()
        self.handler = OutputLineHandler(FakeMethod())

    def _getLines(self):
        """Get the lines that were passed to `handler`'s line processor."""
        return [
            line
            for (line, ) in self.handler.line_processor.extract_args()]

    def test_processes_line(self):
        self.handler("x\n")
        self.assertEqual(["x"], self._getLines())

    def test_buffers_partial_line(self):
        self.handler("x")
        self.assertEqual([], self._getLines())

    def test_splits_lines(self):
        self.handler("a\nb\n")
        self.assertEqual(["a", "b"], self._getLines())

    def test_ignores_empty_output(self):
        self.handler("")
        self.assertEqual([], self._getLines())

    def test_finalize_ignores_empty_output(self):
        self.handler("")
        self.handler.finalize()
        self.assertEqual([], self._getLines())

    def test_ignores_empty_line(self):
        self.handler("\n")
        self.assertEqual([], self._getLines())

    def test_joins_partial_lines(self):
        self.handler("h")
        self.handler("i\n")
        self.assertEqual(["hi"], self._getLines())

    def test_joins_lines_across_multiple_calls(self):
        self.handler("h")
        self.handler("i")
        self.handler("!\n")
        self.assertEqual(["hi!"], self._getLines())

    def test_joins_lines_across_empty_calls(self):
        self.handler("h")
        self.handler("")
        self.handler("i\n")
        self.assertEqual(["hi"], self._getLines())

    def test_clears_buffer_after_joining_lines(self):
        self.handler("hi")
        self.handler("!\n")
        self.assertEqual(["hi!"], self._getLines())
        self.handler("!\n")
        self.assertEqual(["hi!", "!"], self._getLines())

    def test_finalize_processes_remaining_partial_line(self):
        self.handler("foo")
        self.handler.finalize()
        self.assertEqual(["foo"], self._getLines())

    def test_finalize_is_idempotent(self):
        self.handler("foo")
        self.handler.finalize()
        self.handler.finalize()
        self.assertEqual(["foo"], self._getLines())

    def test_finalize_joins_partial_lines(self):
        self.handler("h")
        self.handler("i")
        self.handler.finalize()
        self.assertEqual(["hi"], self._getLines())

    def test_adds_prefix(self):
        self.handler.prefix = "->"
        self.handler("here\n")
        self.assertEqual(["->here"], self._getLines())

    def test_finalize_adds_prefix(self):
        self.handler.prefix = "->"
        self.handler("here")
        self.handler.finalize()
        self.assertEqual(["->here"], self._getLines())

    def test_empty_lines_are_ignored_despite_prefix(self):
        self.handler.prefix = "->"
        self.handler("\n")
        self.assertEqual([], self._getLines())
Exemple #7
0
 def setUp(self):
     super(TestOutputLineHandler, self).setUp()
     self.handler = OutputLineHandler(FakeMethod())
Exemple #8
0
class TestOutputLineHandler(TestCase):
    """Unit tests for `OutputLineHandler`."""
    def setUp(self):
        super(TestOutputLineHandler, self).setUp()
        self.handler = OutputLineHandler(FakeMethod())

    def _getLines(self):
        """Get the lines that were passed to `handler`'s line processor."""
        return [
            line for (line, ) in self.handler.line_processor.extract_args()
        ]

    def test_processes_line(self):
        self.handler("x\n")
        self.assertEqual(["x"], self._getLines())

    def test_buffers_partial_line(self):
        self.handler("x")
        self.assertEqual([], self._getLines())

    def test_splits_lines(self):
        self.handler("a\nb\n")
        self.assertEqual(["a", "b"], self._getLines())

    def test_ignores_empty_output(self):
        self.handler("")
        self.assertEqual([], self._getLines())

    def test_finalize_ignores_empty_output(self):
        self.handler("")
        self.handler.finalize()
        self.assertEqual([], self._getLines())

    def test_ignores_empty_line(self):
        self.handler("\n")
        self.assertEqual([], self._getLines())

    def test_joins_partial_lines(self):
        self.handler("h")
        self.handler("i\n")
        self.assertEqual(["hi"], self._getLines())

    def test_joins_lines_across_multiple_calls(self):
        self.handler("h")
        self.handler("i")
        self.handler("!\n")
        self.assertEqual(["hi!"], self._getLines())

    def test_joins_lines_across_empty_calls(self):
        self.handler("h")
        self.handler("")
        self.handler("i\n")
        self.assertEqual(["hi"], self._getLines())

    def test_clears_buffer_after_joining_lines(self):
        self.handler("hi")
        self.handler("!\n")
        self.assertEqual(["hi!"], self._getLines())
        self.handler("!\n")
        self.assertEqual(["hi!", "!"], self._getLines())

    def test_finalize_processes_remaining_partial_line(self):
        self.handler("foo")
        self.handler.finalize()
        self.assertEqual(["foo"], self._getLines())

    def test_finalize_is_idempotent(self):
        self.handler("foo")
        self.handler.finalize()
        self.handler.finalize()
        self.assertEqual(["foo"], self._getLines())

    def test_finalize_joins_partial_lines(self):
        self.handler("h")
        self.handler("i")
        self.handler.finalize()
        self.assertEqual(["hi"], self._getLines())

    def test_adds_prefix(self):
        self.handler.prefix = "->"
        self.handler("here\n")
        self.assertEqual(["->here"], self._getLines())

    def test_finalize_adds_prefix(self):
        self.handler.prefix = "->"
        self.handler("here")
        self.handler.finalize()
        self.assertEqual(["->here"], self._getLines())

    def test_empty_lines_are_ignored_despite_prefix(self):
        self.handler.prefix = "->"
        self.handler("\n")
        self.assertEqual([], self._getLines())
Exemple #9
0
 def test_integrates_with_outputlinehandler(self):
     spawner = self._makeSpawner()
     handler = OutputLineHandler(FakeMethod())
     spawner.start(["echo", "hello"], stdout_handler=handler)
     spawner.complete()
     self.assertEqual([("hello", )], handler.line_processor.extract_args())