changeset 8125:5ddd6b930dd0

py3: add safe_bytes in places where we will need it and where it doesn't do any harm
author Mads Kiilerich <mads@kiilerich.com>
date Wed, 08 Jan 2020 03:08:09 +0100
parents a553bc3a3d0e
children 9584eb51ae52
files kallithea/controllers/compare.py kallithea/controllers/pullrequests.py kallithea/lib/auth.py kallithea/lib/hooks.py kallithea/lib/middleware/simplehg.py kallithea/lib/utils.py kallithea/lib/vcs/backends/git/inmemory.py kallithea/lib/vcs/backends/git/repository.py kallithea/lib/vcs/backends/hg/changeset.py kallithea/lib/vcs/backends/hg/inmemory.py kallithea/lib/vcs/backends/hg/repository.py kallithea/model/db.py kallithea/model/scm.py kallithea/tests/other/test_libs.py
diffstat 14 files changed, 44 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/compare.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/controllers/compare.py	Wed Jan 08 03:08:09 2020 +0100
@@ -43,7 +43,7 @@
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.base import BaseRepoController, render
 from kallithea.lib.graphmod import graph_data
-from kallithea.lib.utils2 import ascii_bytes, ascii_str, safe_int, safe_str
+from kallithea.lib.utils2 import ascii_bytes, ascii_str, safe_bytes, safe_int, safe_str
 from kallithea.model.db import Repository
 
 
@@ -98,8 +98,8 @@
             # case two independent repos
             if org_repo != other_repo:
                 hgrepo = mercurial.unionrepo.makeunionrepository(other_repo.baseui,
-                                                       other_repo.path,
-                                                       org_repo.path)
+                                                       safe_bytes(other_repo.path),
+                                                       safe_bytes(org_repo.path))
                 # all ancestors of other_rev will be in other_repo and
                 # rev numbers from hgrepo can be used in other_repo - org_rev ancestors cannot
 
--- a/kallithea/controllers/pullrequests.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/controllers/pullrequests.py	Wed Jan 08 03:08:09 2020 +0100
@@ -534,8 +534,8 @@
                             # valid revision numbers are 100% org_scm compatible
                             # - both for avail_revs and for revset results
                             hgrepo = mercurial.unionrepo.makeunionrepository(org_scm_instance.baseui,
-                                                                   org_scm_instance.path,
-                                                                   other_scm_instance.path)
+                                                                   safe_bytes(org_scm_instance.path),
+                                                                   safe_bytes(other_scm_instance.path))
                         else:
                             hgrepo = org_scm_instance._repo
                         show = set(hgrepo.revs('::%ld & !::parents(%s) & !::%s',
--- a/kallithea/lib/auth.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/auth.py	Wed Jan 08 03:08:09 2020 +0100
@@ -111,7 +111,7 @@
     """
 
     if is_windows:
-        return hashlib.sha256(password).hexdigest() == hashed
+        return hashlib.sha256(safe_bytes(password)).hexdigest() == hashed
     elif is_unix:
         import bcrypt
         try:
--- a/kallithea/lib/hooks.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/hooks.py	Wed Jan 08 03:08:09 2020 +0100
@@ -33,7 +33,7 @@
 from kallithea.lib import helpers as h
 from kallithea.lib.exceptions import UserCreationError
 from kallithea.lib.utils import action_logger, make_ui, setup_cache_regions
-from kallithea.lib.utils2 import ascii_str, get_hook_environment, safe_str, safe_unicode
+from kallithea.lib.utils2 import ascii_str, get_hook_environment, safe_bytes, safe_str, safe_unicode
 from kallithea.lib.vcs.backends.base import EmptyChangeset
 from kallithea.model.db import Repository, User
 
@@ -74,7 +74,7 @@
            'Last revision is now r%s:%s\n') % (
         size_hg_f, size_root_f, size_total_f, last_cs.rev(), ascii_str(last_cs.hex())[:12]
     )
-    ui.status(msg)
+    ui.status(safe_bytes(msg))
 
 
 def log_pull_action(ui, repo, **kwargs):
@@ -364,7 +364,7 @@
                 if scm_repo.is_empty():
                     scm_repo._repo.refs.set_symbolic_ref(
                         b'HEAD',
-                        b'refs/heads/%s' % push_ref['name'])
+                        b'refs/heads/%s' % safe_bytes(push_ref['name']))
 
                 # build exclude list without the ref
                 cmd = ['for-each-ref', '--format=%(refname)', 'refs/heads/*']
@@ -399,5 +399,5 @@
 def rejectpush(ui, **kwargs):
     """Mercurial hook to be installed as pretxnopen and prepushkey for read-only repos"""
     ex = get_hook_environment()
-    ui.warn((b"Push access to %r denied\n") % safe_str(ex.repository))
+    ui.warn(safe_bytes("Push access to %r denied\n" % safe_str(ex.repository)))
     return 1
--- a/kallithea/lib/middleware/simplehg.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/middleware/simplehg.py	Wed Jan 08 03:08:09 2020 +0100
@@ -36,7 +36,7 @@
 
 from kallithea.lib.base import BaseVCSController, get_path_info
 from kallithea.lib.utils import make_ui
-from kallithea.lib.utils2 import safe_str, safe_unicode
+from kallithea.lib.utils2 import safe_bytes, safe_str, safe_unicode
 
 
 log = logging.getLogger(__name__)
@@ -140,7 +140,7 @@
         str_repo_name = safe_str(parsed_request.repo_name)
         repo_path = os.path.join(safe_str(self.basepath), str_repo_name)
         baseui = make_ui(repo_path=repo_path)
-        hgweb_app = mercurial.hgweb.hgweb(repo_path, name=str_repo_name, baseui=baseui)
+        hgweb_app = mercurial.hgweb.hgweb(safe_bytes(repo_path), name=str_repo_name, baseui=baseui)
 
         def wrapper_app(environ, start_response):
             environ['REPO_NAME'] = str_repo_name # used by mercurial.hgweb.hgweb
--- a/kallithea/lib/utils.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/utils.py	Wed Jan 08 03:08:09 2020 +0100
@@ -358,7 +358,7 @@
         if os.path.isfile(hgrc_path):
             log.debug('reading hgrc from %s', hgrc_path)
             cfg = mercurial.config.config()
-            cfg.read(hgrc_path)
+            cfg.read(safe_bytes(hgrc_path))
             for section in ui_sections:
                 for k, v in cfg.items(section):
                     log.debug('config from file: [%s] %s=%s', section, k, v)
--- a/kallithea/lib/vcs/backends/git/inmemory.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/vcs/backends/git/inmemory.py	Wed Jan 08 03:08:09 2020 +0100
@@ -100,7 +100,7 @@
             for tree in new_trees:
                 object_store.add_object(tree)
         for node in self.removed:
-            paths = node.path.split(b'/')
+            paths = safe_bytes(node.path).split(b'/')
             tree = commit_tree
             trees = [tree]
             # Traverse deep into the forest...
@@ -147,7 +147,7 @@
         object_store.add_object(commit)
 
         # Update vcs repository object & recreate dulwich repo
-        ref = b'refs/heads/%s' % branch
+        ref = b'refs/heads/%s' % safe_bytes(branch)
         repo.refs[ref] = commit.id
         self.repository.revisions.append(ascii_str(commit.id))
         # invalidate parsed refs after commit
--- a/kallithea/lib/vcs/backends/git/repository.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/vcs/backends/git/repository.py	Wed Jan 08 03:08:09 2020 +0100
@@ -29,7 +29,7 @@
 from kallithea.lib.vcs.conf import settings
 from kallithea.lib.vcs.exceptions import (
     BranchDoesNotExistError, ChangesetDoesNotExistError, EmptyRepositoryError, RepositoryError, TagAlreadyExistError, TagDoesNotExistError)
-from kallithea.lib.vcs.utils import ascii_str, date_fromtimestamp, makedate, safe_str, safe_unicode
+from kallithea.lib.vcs.utils import ascii_str, date_fromtimestamp, makedate, safe_bytes, safe_str, safe_unicode
 from kallithea.lib.vcs.utils.lazy import LazyProperty
 from kallithea.lib.vcs.utils.paths import abspath, get_user_home
 
@@ -168,7 +168,7 @@
             url = url[url.find('+') + 1:]
 
         handlers = []
-        url_obj = mercurial.util.url(url)
+        url_obj = mercurial.util.url(safe_bytes(url))
         test_uri, authinfo = url_obj.authinfo()
         if not test_uri.endswith('info/refs'):
             test_uri = test_uri.rstrip('/') + '/info/refs'
@@ -282,7 +282,7 @@
                     raise ChangesetDoesNotExistError(msg)
 
             # get by branch/tag name
-            _ref_revision = self._parsed_refs.get(revision)
+            _ref_revision = self._parsed_refs.get(safe_bytes(revision))
             if _ref_revision:  # and _ref_revision[1] in [b'H', b'RH', b'T']:
                 return ascii_str(_ref_revision[0])
 
@@ -407,7 +407,7 @@
         changeset = self.get_changeset(revision)
         message = message or "Added tag %s for commit %s" % (name,
             changeset.raw_id)
-        self._repo.refs[b"refs/tags/%s" % name] = changeset._commit.id
+        self._repo.refs[b"refs/tags/%s" % safe_bytes(name)] = changeset._commit.id
 
         self._parsed_refs = self._get_parsed_refs()
         self.tags = self._get_tags()
--- a/kallithea/lib/vcs/backends/hg/changeset.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/vcs/backends/hg/changeset.py	Wed Jan 08 03:08:09 2020 +0100
@@ -10,7 +10,7 @@
 from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError, ChangesetError, ImproperArchiveTypeError, NodeDoesNotExistError, VCSError
 from kallithea.lib.vcs.nodes import (
     AddedFileNodesGenerator, ChangedFileNodesGenerator, DirNode, FileNode, NodeKind, RemovedFileNodesGenerator, RootNode, SubModuleNode)
-from kallithea.lib.vcs.utils import ascii_bytes, ascii_str, date_fromtimestamp, safe_str, safe_unicode
+from kallithea.lib.vcs.utils import ascii_bytes, ascii_str, date_fromtimestamp, safe_bytes, safe_str, safe_unicode
 from kallithea.lib.vcs.utils.lazy import LazyProperty
 from kallithea.lib.vcs.utils.paths import get_dirs_for_path
 
@@ -219,7 +219,7 @@
         if self._get_kind(path) != NodeKind.FILE:
             raise ChangesetError("File does not exist for revision %s at "
                 " '%s'" % (self.raw_id, path))
-        return self._ctx.filectx(path)
+        return self._ctx.filectx(safe_bytes(path))
 
     def _extract_submodules(self):
         """
@@ -318,7 +318,7 @@
             raise VCSError("Prefix cannot be empty")
 
         mercurial.archival.archive(self.repository._repo, stream, ascii_bytes(self.raw_id),
-                         kind, prefix=prefix, subrepos=subrepos)
+                         safe_bytes(kind), prefix=safe_bytes(prefix), subrepos=subrepos)
 
     def get_nodes(self, path):
         """
--- a/kallithea/lib/vcs/backends/hg/inmemory.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/vcs/backends/hg/inmemory.py	Wed Jan 08 03:08:09 2020 +0100
@@ -38,7 +38,7 @@
 
         if branch is None:
             branch = MercurialRepository.DEFAULT_BRANCH_NAME
-        kwargs[b'branch'] = branch
+        kwargs[b'branch'] = safe_bytes(branch)
 
         def filectxfn(_repo, memctx, bytes_path):
             """
@@ -78,15 +78,15 @@
                 parents[i] = parent._ctx.node()
 
         if date and isinstance(date, datetime.datetime):
-            date = date.strftime('%a, %d %b %Y %H:%M:%S')
+            date = safe_bytes(date.strftime('%a, %d %b %Y %H:%M:%S'))
 
         commit_ctx = mercurial.context.memctx(
             repo=self.repository._repo,
             parents=parents,
             text=b'',
-            files=self.get_paths(),
+            files=[safe_bytes(x) for x in self.get_paths()],
             filectxfn=filectxfn,
-            user=author,
+            user=safe_bytes(author),
             date=date,
             extra=kwargs)
 
--- a/kallithea/lib/vcs/backends/hg/repository.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/lib/vcs/backends/hg/repository.py	Wed Jan 08 03:08:09 2020 +0100
@@ -178,10 +178,10 @@
                 changeset.short_id)
 
         if date is None:
-            date = datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S')
+            date = safe_bytes(datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S'))
 
         try:
-            mercurial.tags.tag(self._repo, name, changeset._ctx.node(), message, local, user, date)
+            mercurial.tags.tag(self._repo, safe_bytes(name), changeset._ctx.node(), safe_bytes(message), local, safe_bytes(user), date)
         except mercurial.error.Abort as e:
             raise RepositoryError(e.message)
 
@@ -207,11 +207,11 @@
         if message is None:
             message = "Removed tag %s" % name
         if date is None:
-            date = datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S')
+            date = safe_bytes(datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S'))
         local = False
 
         try:
-            mercurial.tags.tag(self._repo, name, mercurial.commands.nullid, message, local, user, date)
+            mercurial.tags.tag(self._repo, safe_bytes(name), mercurial.commands.nullid, safe_bytes(message), local, safe_bytes(user), date)
             self.tags = self._get_tags()
         except mercurial.error.Abort as e:
             raise RepositoryError(e.message)
@@ -368,11 +368,11 @@
                 if not update_after_clone:
                     opts.update({'noupdate': True})
                 MercurialRepository._check_url(url, self.baseui)
-                mercurial.commands.clone(self.baseui, url, self.path, **opts)
+                mercurial.commands.clone(self.baseui, url, safe_bytes(self.path), **opts)
 
                 # Don't try to create if we've already cloned repo
                 create = False
-            return mercurial.localrepo.instance(self.baseui, self.path, create=create)
+            return mercurial.localrepo.instance(self.baseui, safe_bytes(self.path), create=create)
         except (mercurial.error.Abort, mercurial.error.RepoError) as err:
             if create:
                 msg = "Cannot create repository at %s. Original error was %s" \
@@ -537,9 +537,9 @@
         if branch_name:
             filter_.append(b'branch("%s")' % safe_bytes(branch_name))
         if start_date:
-            filter_.append(b'date(">%s")' % start_date)
+            filter_.append(b'date(">%s")' % safe_bytes(str(start_date)))
         if end_date:
-            filter_.append(b'date("<%s")' % end_date)
+            filter_.append(b'date("<%s")' % safe_bytes(str(end_date)))
         if filter_ or max_revisions:
             if filter_:
                 revspec = b' and '.join(filter_)
@@ -563,8 +563,7 @@
         """
         Tries to pull changes from external location.
         """
-        url = self._get_url(url)
-        other = mercurial.hg.peer(self._repo, {}, url)
+        other = mercurial.hg.peer(self._repo, {}, safe_bytes(self._get_url(url)))
         try:
             mercurial.exchange.pull(self._repo, other, heads=None, force=None)
         except mercurial.error.Abort as err:
@@ -596,8 +595,8 @@
         if config_file:
             config = mercurial.ui.ui()
             for path in config_file:
-                config.readconfig(path)
-        return config.config(section, name)
+                config.readconfig(safe_bytes(path))
+        return config.config(safe_bytes(section), safe_bytes(name))
 
     def get_user_name(self, config_file=None):
         """
--- a/kallithea/model/db.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/model/db.py	Wed Jan 08 03:08:09 2020 +0100
@@ -2549,5 +2549,5 @@
         # the full public key is too long to be suitable as database key - instead,
         # use fingerprints similar to 'ssh-keygen -E sha256 -lf ~/.ssh/id_rsa.pub'
         self._public_key = full_key
-        enc_key = full_key.split(" ")[1]
+        enc_key = safe_bytes(full_key.split(" ")[1])
         self.fingerprint = base64.b64encode(hashlib.sha256(base64.b64decode(enc_key)).digest()).replace(b'\n', b'').rstrip(b'=').decode()
--- a/kallithea/model/scm.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/model/scm.py	Wed Jan 08 03:08:09 2020 +0100
@@ -41,7 +41,7 @@
 from kallithea.lib.exceptions import IMCCommitError, NonRelativePathError
 from kallithea.lib.hooks import process_pushed_raw_ids
 from kallithea.lib.utils import action_logger, get_filesystem_repos, make_ui
-from kallithea.lib.utils2 import safe_str, safe_unicode, set_hook_environment
+from kallithea.lib.utils2 import safe_bytes, safe_str, safe_unicode, set_hook_environment
 from kallithea.lib.vcs import get_backend
 from kallithea.lib.vcs.backends.base import EmptyChangeset
 from kallithea.lib.vcs.exceptions import RepositoryError
@@ -716,11 +716,11 @@
         if not os.path.isdir(loc):
             os.makedirs(loc)
 
-        tmpl_post = b"#!%s\n" % self._get_git_hook_interpreter()
+        tmpl_post = b"#!%s\n" % safe_bytes(self._get_git_hook_interpreter())
         tmpl_post += pkg_resources.resource_string(
             'kallithea', os.path.join('config', 'post_receive_tmpl.py')
         )
-        tmpl_pre = b"#!%s\n" % self._get_git_hook_interpreter()
+        tmpl_pre = b"#!%s\n" % safe_bytes(self._get_git_hook_interpreter())
         tmpl_pre += pkg_resources.resource_string(
             'kallithea', os.path.join('config', 'pre_receive_tmpl.py')
         )
@@ -750,7 +750,7 @@
                 log.debug('writing %s hook file !', h_type)
                 try:
                     with open(_hook_file, 'wb') as f:
-                        tmpl = tmpl.replace(b'_TMPL_', kallithea.__version__)
+                        tmpl = tmpl.replace(b'_TMPL_', safe_bytes(kallithea.__version__))
                         f.write(tmpl)
                     os.chmod(_hook_file, 0o755)
                 except IOError as e:
--- a/kallithea/tests/other/test_libs.py	Fri Dec 27 01:46:11 2019 +0100
+++ b/kallithea/tests/other/test_libs.py	Wed Jan 08 03:08:09 2020 +0100
@@ -31,7 +31,7 @@
 import mock
 from tg.util.webtest import test_context
 
-from kallithea.lib.utils2 import AttributeDict
+from kallithea.lib.utils2 import AttributeDict, safe_bytes
 from kallithea.model.db import Repository
 from kallithea.tests import base
 
@@ -227,7 +227,7 @@
 
     def test_alternative_gravatar(self):
         from kallithea.lib.helpers import gravatar_url
-        _md5 = lambda s: hashlib.md5(s).hexdigest()
+        _md5 = lambda s: hashlib.md5(safe_bytes(s)).hexdigest()
 
         # mock tg.tmpl_context
         def fake_tmpl_context(_url):