diff sat/tools/utils.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 181735d1b062
children d62fceccff22
line wrap: on
line diff
--- a/sat/tools/utils.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/tools/utils.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SAT: a jabber client
@@ -36,7 +36,7 @@
 log = getLogger(__name__)
 
 
-NO_REPOS_DATA = u"repository data unknown"
+NO_REPOS_DATA = "repository data unknown"
 repos_cache_dict = None
 repos_cache = None
 
@@ -99,10 +99,10 @@
     @param with_time(bool): if True include the time
     @return(unicode): XEP-0082 formatted date and time
     """
-    template_date = u"%Y-%m-%d"
-    template_time = u"%H:%M:%SZ"
+    template_date = "%Y-%m-%d"
+    template_time = "%H:%M:%SZ"
     template = (
-        u"{}T{}".format(template_date, template_time) if with_time else template_date
+        "{}T{}".format(template_date, template_time) if with_time else template_date
     )
     return datetime.datetime.utcfromtimestamp(
         time.time() if timestamp is None else timestamp
@@ -119,9 +119,9 @@
     random.seed()
     if vocabulary is None:
         vocabulary = [
-            chr(i) for i in range(0x30, 0x3A) + range(0x41, 0x5B) + range(0x61, 0x7B)
+            chr(i) for i in list(range(0x30, 0x3A)) + list(range(0x41, 0x5B)) + list(range(0x61, 0x7B))
         ]
-    return u"".join([random.choice(vocabulary) for i in range(15)])
+    return "".join([random.choice(vocabulary) for i in range(15)])
 
 
 def getRepositoryData(module, as_string=True, is_path=False):
@@ -157,7 +157,7 @@
 
     if sys.platform == "android":
         #  FIXME: workaround to avoid trouble on android, need to be fixed properly
-        repos_cache = u"Cagou android build"
+        repos_cache = "Cagou android build"
         return repos_cache
 
     KEYS = ("node", "node_short", "branch", "date", "tag", "distance")
@@ -171,7 +171,7 @@
     try:
         hg_path = procutils.which("hg")[0]
     except IndexError:
-        log.warning(u"Can't find hg executable")
+        log.warning("Can't find hg executable")
         hg_path = None
         hg_data = {}
 
@@ -191,14 +191,15 @@
                     "{date|isodate}\n"
                     "{latesttag}\n"
                     "{latesttagdistance}",
-                ]
+                ],
+                text=True
             )
         except subprocess.CalledProcessError:
             hg_data = {}
         else:
-            hg_data = dict(zip(KEYS, hg_data_raw.split("\n")))
+            hg_data = dict(list(zip(KEYS, hg_data_raw.split("\n"))))
             try:
-                hg_data["modified"] = "+" in subprocess.check_output(["hg", "id", "-i"])
+                hg_data["modified"] = "+" in subprocess.check_output(["hg", "id", "-i"], text=True)
             except subprocess.CalledProcessError:
                 pass
     else:
@@ -206,7 +207,7 @@
 
     if not hg_data:
         # .hg/dirstate method
-        log.debug(u"trying dirstate method")
+        log.debug("trying dirstate method")
         if is_path:
             os.chdir(repos_root)
         else:
@@ -216,13 +217,13 @@
                 hg_data["node"] = hg_dirstate.read(20).encode("hex")
                 hg_data["node_short"] = hg_data["node"][:12]
         except IOError:
-            log.debug(u"Can't access repository data")
+            log.debug("Can't access repository data")
 
     # we restore original working dir
     os.chdir(ori_cwd)
 
     if not hg_data:
-        log.debug(u"Mercurial not available or working, trying package version")
+        log.debug("Mercurial not available or working, trying package version")
         try:
             import pkg_resources
 
@@ -234,7 +235,7 @@
             log.warning("can't retrieve package data")
         except ValueError:
             log.info(
-                u"no local version id in package: {pkg_version}".format(
+                "no local version id in package: {pkg_version}".format(
                     pkg_version=pkg_version
                 )
             )
@@ -259,21 +260,21 @@
         if not hg_data:
             repos_cache = NO_REPOS_DATA
         else:
-            strings = [u"rev", hg_data["node_short"]]
+            strings = ["rev", hg_data["node_short"]]
             try:
                 if hg_data["modified"]:
-                    strings.append(u"[M]")
+                    strings.append("[M]")
             except KeyError:
                 pass
             try:
-                strings.extend([u"({branch} {date})".format(**hg_data)])
+                strings.extend(["({branch} {date})".format(**hg_data)])
             except KeyError:
                 pass
             try:
-                strings.extend([u"+{distance}".format(**hg_data)])
+                strings.extend(["+{distance}".format(**hg_data)])
             except KeyError:
                 pass
-            repos_cache = u" ".join(strings)
+            repos_cache = " ".join(strings)
         return repos_cache
     else:
         return hg_data