Example #1
0
 def update_all(self, interactive=False):
     """Update all versions, prompting the user for each possible update"""
     io.activate()
     for name, table in self.toml.items():
         latest = self._latest_version(name)
         current = self._cached_version(name)
         now = datetime.datetime.now()
         if latest == current:
             io.stdout("{x} {name} is up to date ({current})".format(
                 x=green("✓"),
                 name=name,
                 current=current,
             ))
             continue
         if interactive:
             question = wrap_question(
                 name,
                 "{} → {}".format(red(current), green(latest)),
                 "Update {}".format(bold(name)),
                 prefix="{} versions".format(blue("?")))
             if not io.ask(question, True):
                 continue
         else:
             io.stdout("{x} updated {name}: {current} → {new}".format(
                 x=green("✓"),
                 name=bold(name),
                 current=red(current),
                 new=green(latest),
             ))
         table['version'] = latest
         table['version_date'] = now
         self.toml[name] = table
         self._save()
Example #2
0
 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,
     )
Example #3
0
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
Example #4
0
 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"
         ),
     )
Example #5
0
 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"
         ),
     )
Example #6
0
 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"
         ),
     )
Example #7
0
 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,
     )
Example #8
0
 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"
         ),
     )
Example #9
0
def install_dir(source, target):
    relpath = os.path.relpath(source, target)
    for _, dirs, files in os.walk(source, topdown=True):
        for directory in dirs:
            os.mkdir(target / directory)
        for file in files:
            try:
                os.symlink(
                    os.path.join(relpath, file),
                    os.path.join(target, file),
                )
            except FileExistsError:
                pass
            io.stdout("{} installed {}".format(
                green("✓"),
                bold(os.path.join(os.path.basename(target), file)),
            ))
Example #10
0
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