diff extensions/revinfo.py @ 329:39674f294709

Ported the revinfo extension to Python3 and tested with Mercurial 5.0.1
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 10 Jul 2019 17:42:30 +0200
parents 80fa55e2fb16
children eb1e0f129963
line wrap: on
line diff
--- a/extensions/revinfo.py	Mon Jul 08 18:11:51 2019 +0200
+++ b/extensions/revinfo.py	Wed Jul 10 17:42:30 2019 +0200
@@ -37,7 +37,7 @@
     command = cmdutil.command(cmdtable)
 
 
-testedwith = "4.3.1 4.3.2 4.4.1 4.4.2 4.5.2 4.6.1 4.8.1 4.9 5.0.1"
+testedwith = b"4.3.1 4.3.2 4.4.1 4.4.2 4.5.2 4.6.1 4.8.1 4.9 5.0.1"
 
 
 def getversion():
@@ -52,26 +52,26 @@
     try:
         fn = __file__
     except NameError:
-        return "<unknown>"
+        return b"<unknown>"
     else:
         try:
             verdata = open(os.path.join(os.path.dirname(fn),
                                         "../VERSION"),
-                           "r").read()
-            return re.search("^(.*)", verdata,).group(1)
+                           "rb").read()
+            return re.search(b"^(.*)", verdata,).group(1)
         except OSError:
-            return "<not found>"
+            return b"<not found>"
 
 
 @command(
-    "revinfo",
+    b"revinfo",
     [
-        ("r", "rev", "",  _('revision to handle'), _('REV')),
-        ("", "amend", None, _('amend a given file with path information')),
-        ("p", "path", [], _('the configured default path'), _('SOURCE')),
-        ("d", "data", [], _("add an extra `KEY: VALUE' pair into the file"), _('KEY=VALUE')),
+        (b'r', b"rev", b'',  _(b"revision to handle"), _(b"REV")),
+        (b'', b"amend", None, _(b"amend a given file with path information")),
+        (b'p', b"path", [], _(b"the configured default path"), _(b"SOURCE")),
+        (b'd', b"data", [], _(b"add an extra `KEY: VALUE' pair into the file"), _(b"KEY=VALUE")),
     ],
-    _("hg revinfo [OPTION]... [DEST]"),
+    _(b"hg revinfo [OPTION]... [DEST]"),
     inferrepo=True)
 def revinfo(ui, repo, dest=None, **opts):
     """write a revision summary
@@ -96,49 +96,49 @@
 
     """
     opts = pycompat.byteskwargs(opts)
-    if opts.get("amend") and opts.get("rev"):
-        raise error.Abort(_("cannot use -r/--rev together with --amend"))
+    if opts.get(b"amend") and opts.get(b"rev"):
+        raise error.Abort(_(b"cannot use -r/--rev together with --amend"))
 
-    if opts.get("amend"):
+    if opts.get(b"amend"):
         if not dest:
-            raise error.Abort(_('need a destination file with --amend'))
+            raise error.Abort(_(b"need a destination file with --amend"))
         with open(dest, "rb") as rfile:
             msg = rfile.read()
     else:
-        ctx = scmutil.revsingle(repo, opts.get("rev"))
+        ctx = scmutil.revsingle(repo, opts.get(b"rev"))
         if not ctx:
             raise error.Abort(
-                _("no working directory: please specify a revision"))
+                _(b"no working directory: please specify a revision"))
 
         msg = archival.buildmetadata(ctx)
 
-    for kv in opts.get("data"):
-        kvparts = [i.strip() for i in kv.split("=", 1)]
+    for kv in opts.get(b"data"):
+        kvparts = [i.strip() for i in kv.split(b'=', 1)]
         if len(kvparts) == 2:
-            msg += "%s: %s\n" % tuple(kvparts)
+            msg += b"%s: %s\n" % tuple(kvparts)
         else:
             raise error.Abort(
-                _("given data `%s' is not a KEY=VALUE pair") % (kv, ))
+                _(b"given data `%s' is not a KEY=VALUE pair") % (kv, ))
 
-    paths = opts.get("path")
+    paths = opts.get(b"path")
     #
     # When not amending and no path options is given print the
     # "default" path
     #
-    if not opts.get("amend"):
+    if not opts.get(b"amend"):
         if not paths:
-            paths = ["default"]
+            paths = [b"default"]
     for canonicalpath in paths:
         if canonicalpath:
-            if canonicalpath == ".":
-                msg += "path: %s\n" % repo.root
+            if canonicalpath == b'.':
+                msg += b"path: %s\n" % repo.root
             else:
-                for name, path in sorted(ui.paths.iteritems()):
+                for name, path in sorted(ui.paths.items()):
                     if name == canonicalpath:
-                        msg += "path: %s\n" % util.hidepassword(path.loc)
+                        msg += b"path: %s\n" % util.hidepassword(path.loc)
                         break
                 else:
-                    msg += "path: %s\n" % canonicalpath
+                    msg += b"path: %s\n" % canonicalpath
     if dest:
         with open(dest, "wb") as rfile:
             rfile.write(msg)