def run(self, interactive=False): result = self.bundle.node.run( self.attributes['command'], may_fail=True, ) if self.attributes['expected_return_code'] is not None and \ not result.return_code == self.attributes['expected_return_code']: if not interactive: LOG.error("{}:{}: {}".format( self.bundle.node.name, self.id, red(_("FAILED")), )) raise ActionFailure(_( "wrong return code for action '{action}' in bundle '{bundle}': " "expected {ecode}, but was {rcode}" ).format( action=self.name, bundle=self.bundle.name, ecode=self.attributes['expected_return_code'], rcode=result.return_code, )) if self.attributes['expected_stderr'] is not None and \ result.stderr != self.attributes['expected_stderr']: LOG.error("{}:{}: {}".format( self.bundle.node.name, self.id, red(_("FAILED")), )) raise ActionFailure(_( "wrong stderr for action '{action}' in bundle '{bundle}'" ).format( action=self.name, bundle=self.bundle.name, )) if self.attributes['expected_stdout'] is not None and \ result.stdout != self.attributes['expected_stdout']: LOG.error("{}:{}: {}".format( self.bundle.node.name, self.id, red(_("FAILED")), )) raise ActionFailure(_( "wrong stdout for action '{action}' in bundle '{bundle}'" ).format( action=self.name, bundle=self.bundle.name, )) LOG.info("{}:{}: {}".format( self.bundle.node.name, self.id, green(_("OK")), )) return result
def test_encoding(self): content_old = ( "lineö1\n".encode("utf-8") ) content_new = ( "lineö1\n".encode("latin-1") ) self.assertEqual( files.diff(content_old, content_new, "/foo", encoding_hint="latin-1"), ( red("--- /foo") + "\n" + green("+++ <blockwart content>") + "\n" + "@@ -1 +1 @@\n" + red("-lineö1") + "\n" + green("+lineö1") + " (line encoded in latin-1)\n" ), )
def test_long_line(self): content_old = ( "line1\n" ) content_new = ( "line1" + 500 * "1" + "\n" ) self.assertEqual( files.diff(content_old, content_new, "/foo"), ( red("--- /foo") + "\n" + green("+++ <blockwart content>") + "\n" + "@@ -1 +1 @@\n" + red("-line1") + "\n" + green("+line111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111") + " (line truncated after 128 characters)\n" ), )
def ask(self, status): before = _("installed") if status.info['installed'] \ else _("not installed") after = green(_("installed")) if self.attributes['installed'] \ else red(_("not installed")) return "{} {} → {}\n".format( bold(_("status")), before, after, )
def test_diff(self): content_old = ( "line1\n" "line2\n" ) content_new = ( "line1\n" "line3\n" ) self.assertEqual( files.diff(content_old, content_new, "/foo"), ( red("--- /foo") + "\n" + green("+++ <blockwart content>") + "\n" + "@@ -1,2 +1,2 @@\n" " line1\n" + red("-line2") + "\n" + green("+line3") + "\n" ), )
def diff(content_old, content_new, filename, encoding_hint=None): output = "" LOG.debug("diffing {filename}: {len_before} B before, {len_after} B after".format( filename=filename, len_before=len(content_old), len_after=len(content_new), )) start = datetime.now() for line in unified_diff( content_old.splitlines(True), content_new.splitlines(True), fromfile=filename, tofile=_("<blockwart content>"), ): suffix = "" try: line = line.decode('UTF-8') except UnicodeDecodeError: if encoding_hint and encoding_hint.lower() != "utf-8": try: line = line.decode(encoding_hint) suffix += _(" (line encoded in {})").format(encoding_hint) except UnicodeDecodeError: line = line[0] suffix += _(" (line not encoded in UTF-8 or {})").format(encoding_hint) else: line = line[0] suffix += _(" (line not encoded in UTF-8)") line = line.rstrip("\n") if len(line) > DIFF_MAX_LINE_LENGTH: line = line[:DIFF_MAX_LINE_LENGTH] suffix += _(" (line truncated after {} characters)").format(DIFF_MAX_LINE_LENGTH) if line.startswith("+"): line = green(line) elif line.startswith("-"): line = red(line) output += line + suffix + "\n" duration = datetime.now() - start LOG.debug("diffing {file}: complete after {time}s".format( file=filename, time=duration.total_seconds(), )) return output