changeset bc762a11878d in trypod:default
details: https://hg.tryton.org/trypod?cmd=changeset;node=bc762a11878d
description:
        Fix unicode/bytes with mercurial
diffstat:

 trypod.py |  63 +++++++++++++++++++++++++++++++++++----------------------------
 1 files changed, 35 insertions(+), 28 deletions(-)

diffs (156 lines):

diff -r 97c81501ee16 -r bc762a11878d trypod.py
--- a/trypod.py Sun Oct 18 14:59:36 2020 +0200
+++ b/trypod.py Sun Oct 18 15:33:27 2020 +0200
@@ -14,6 +14,7 @@
 from flask_caching import Cache
 from roundup.password import Password
 from mercurial import ui, hg
+from mercurial.encoding import unitolocal, unifromlocal
 from mercurial.hgweb.hgwebdir_mod import findrepos
 
 app = Flask(__name__)
@@ -76,15 +77,16 @@
     paths = {}
     branches = {}
     lui = ui.ui()
-    lui.readconfig(app.config['HG_CONFIG'], trust=True)
-    for name, path in findrepos(lui.configitems('paths')):
+    lui.readconfig(unitolocal(app.config['HG_CONFIG']), trust=True)
+    for name, path in findrepos(lui.configitems(b'paths')):
+        name = unifromlocal(name)
         repo = hg.repository(lui, path)
         repos.append(name)
         paths[name] = path
         branches[name] = []
         for branch, heads, tip, isclosed in repo.branchmap().iterbranches():
             if not isclosed:
-                branches[name].append(branch)
+                branches[name].append(unifromlocal(branch))
         branches[name].sort(reverse=True)
     return render_template('home.html',
         repos=repos, paths=paths, branches=branches)
@@ -93,34 +95,37 @@
 @app.context_processor
 def utility_processor():
     def badge(repo, path, branch):
+        branch = unitolocal(branch)
         lui = ui.ui()
-        lui.readconfig(os.path.join(path, '.hg', 'hgrc'))
-        url = lui.config('trypod', 'url_%s' % branch)
+        lui.readconfig(os.path.join(path, b'.hg', b'hgrc'))
+        url = lui.config(b'trypod', b'url_%s' % branch)
         if url is None:
-            url = lui.config('trypod', 'url', '')
+            url = lui.config(b'trypod', b'url', b'')
         url = _raw_url(url)
         # drone 0.3
-        url = url.replace('api/hook', 'api/badge')
+        url = url.replace(b'api/hook', b'api/badge')
         # drone 0.8
-        url = url.replace('hook', 'api/badges')
-        url += ('/' + lui.config('trypod', 'owner', '') + '/'
-            + _normalize_name(repo) + '/status.svg?branch=' + branch)
-        return url
+        url = url.replace(b'hook', b'api/badges')
+        url += (b'/' + lui.config(b'trypod', b'owner', b'') + b'/'
+            + unitolocal(_normalize_name(repo))
+            + b'/status.svg?branch=' + branch)
+        return unifromlocal(url)
 
     def url(repo, path, branch):
+        branch = unitolocal(branch)
         lui = ui.ui()
-        lui.readconfig(os.path.join(path, '.hg', 'hgrc'))
-        url = lui.config('trypod', 'url_%s' % branch)
+        lui.readconfig(os.path.join(path, b'.hg', b'hgrc'))
+        url = lui.config(b'trypod', b'url_%s' % branch)
         if url is None:
-            url = lui.config('trypod', 'url', '')
+            url = lui.config(b'trypod', b'url', b'')
         url = _raw_url(url)
         # drone 0.3
-        url = url.replace('/api/hook', '')
+        url = url.replace(b'/api/hook', b'')
         # drone 0.8
-        url = url.replace('/hook', '')
-        url += '/' + lui.config('trypod', 'owner', '')
-        url += '/' + _normalize_name(repo)
-        return url
+        url = url.replace(b'/hook', b'')
+        url += b'/' + lui.config(b'trypod', b'owner', b'')
+        url += b'/' + unitolocal(_normalize_name(repo))
+        return unifromlocal(url)
     return dict(badge=badge, url=url)
 
 
@@ -147,7 +152,7 @@
     repo = {
         'name': name,
         'url': get_url(_denormalize_name(name)),
-        'owner': (repo.ui.config('trypod', 'owner')
+        'owner': (unifromlocal(repo.ui.config(b'trypod', b'owner'))
             or app.config['OWNER']),
         }
     return Response(json.dumps(repo), mimetype='application/json')
@@ -159,10 +164,12 @@
     lui = ui.ui()
     for name, path in _repos(lui):
         repo = hg.repository(lui, path)
+        name = unifromlocal(name)
         urls.append({
                 'name': _normalize_name(name),
                 'url': get_url(name),
-                'owner': (repo.ui.config('trypod', 'owner')
+                'owner': (
+                    unifromlocal(repo.ui.config(b'trypod', b'owner', b''))
                     or app.config['OWNER']),
                 })
     return Response(json.dumps(urls), mimetype='application/json')
@@ -235,10 +242,10 @@
 def activate(name, token):
     if token != app.config['TOKEN']:
         abort(403)
-    url = request.get_data()
+    url = request.get_data().decode('utf-8')
     repo = get_repo(name)
     config = ConfigParser()
-    hgrc = os.path.join(repo.path, 'hgrc')
+    hgrc = os.path.join(unifromlocal(repo.path), 'hgrc')
     config.read(hgrc)
     hook = 'python:%s:hook' % (
         os.path.join(os.getcwd(), os.path.dirname(__file__), 'hook.py'))
@@ -266,7 +273,7 @@
         abort(403)
     repo = get_repo(name)
     config = ConfigParser()
-    hgrc = os.path.join(repo.path, 'hgrc')
+    hgrc = os.path.join(unifromlocal(repo.path), 'hgrc')
     config.read(hgrc)
     try:
         config.remove_option('hooks', 'incoming.trypod')
@@ -325,21 +332,21 @@
 
 def _raw_url(url):
     parsed = urlparse(url)
-    return urlunparse(parsed[:3] + ('', '', ''))
+    return urlunparse(parsed[:3] + (b'', b'', b''))
 
 
 def _repos(lui=None):
     if lui is None:
         lui = ui.ui()
-    lui.readconfig(app.config['HG_CONFIG'], trust=True)
-    paths = lui.configitems('paths')
+    lui.readconfig(unitolocal(app.config['HG_CONFIG']), trust=True)
+    paths = lui.configitems(b'paths')
     return findrepos(paths)
 
 
 def get_repo(name):
     lui = ui.ui()
     for lname, path in _repos(lui):
-        if _normalize_name(lname) == name:
+        if _normalize_name(unifromlocal(lname)) == name:
             return hg.repository(lui, path)
 
 

Reply via email to