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), )) content_old = force_text(content_old) content_new = force_text(content_new) start = datetime.now() for line in unified_diff( content_old.splitlines(True), content_new.splitlines(True), fromfile=filename, tofile=_("<bundlewrap content>"), ): suffix = "" line = force_text(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
def ask(self, status): if not status.info["exists"] and not self.attributes["delete"]: return _("Doesn't exist. Do you want to create it?") if status.info["exists"] and self.attributes["delete"]: return red(_("Will be deleted.")) if status.info["owner"] != self.attributes["owner"]: return "{} {} → {}".format(bold(_("owner")), status.info["owner"], self.attributes["owner"])
def test_encoding_unknown(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="ascii"), ( red("--- /foo") + "\n" + green("+++ <bundlewrap content>") + "\n" + "@@ -1 +1 @@\n" + red("-lineö1") + "\n" + green("+") + " (line not encoded in UTF-8 or ascii)\n" ), )
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("+++ <bundlewrap content>") + "\n" + "@@ -1 +1 @@\n" + red("-lineö1") + "\n" + green("+line�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("+++ <bundlewrap content>") + "\n" + "@@ -1 +1 @@\n" + red("-line1") + "\n" + green("+line111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111") + " (line truncated after 128 characters)\n" ), )
def ask(self, status): before = _("running") if status.info['running'] \ else _("not running") after = green(_("running")) if self.attributes['running'] \ else red(_("not running")) 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("+++ <bundlewrap content>") + "\n" + "@@ -1,2 +1,2 @@\n" " line1\n" + red("-line2") + "\n" + green("+line3") + "\n" ), )
def ask(self, status): before = status.info['version'] if status.info['version'] \ else _("not installed") target = green(self.attributes['version']) if self.attributes['version'] else \ green(_("installed")) after = target if self.attributes['installed'] \ else red(_("not installed")) return "{} {} → {}\n".format( bold(_("status")), before, after, )
def main(): io.activate() try: repo = Repository("") except NoSuchRepository: io.stderr("{} Not inside a bundlewrap repository".format(red("!"))) sys.exit(1) my_path = pathlib.Path(__file__).parent.absolute() relpath = my_path.relative_to(repo.path) if not str(relpath).startswith("collections/"): io.stderr( "{} Collection should be installed to <repo>/collections".format( yellow("!"))) sys.exit(1) install_dir(my_path / "hooks", repo.hooks_dir) install_dir(my_path / "libs", repo.libs_dir) install_dir(my_path / "items", repo.items_dir)
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=_("<bundlewrap 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
def ask(self, status): if not status.info['exists'] and not self.attributes['delete']: return _("Doesn't exist. Do you want to create it?") if status.info['exists'] and self.attributes['delete']: return red(_("Will be deleted.")) output = [] for attr, attr_pretty in ATTRS.items(): if self.attributes[attr] is None: continue if status.info[attr] != self.attributes[attr]: if attr in ('password_hash',): output.append("{} {}\n{}→ {}".format( bold(attr_pretty), status.info[attr], " " * (len(attr_pretty) - 1), self.attributes[attr], )) else: output.append("{} {} → {}".format( bold(attr_pretty), status.info[attr], self.attributes[attr], )) return "\n".join(output)
def test_ansi_clean(): assert red("test") != "test" assert len(red("test")) != len("test") assert ansi_clean(red("test")) == "test" assert ansi_clean(bold(red("test"))) == "test"