changeset 586:87c20b8c5472 default master

port to python3
author Matt Johnston <matt@ucc.asn.au>
date Mon, 09 Sep 2019 22:24:10 +0800
parents 1c484dab4e83
children
files web/atomicfile.py web/log.py web/secure.py web/settings.py web/templog-uwsgi.ini web/templog.py
diffstat 6 files changed, 30 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/web/atomicfile.py	Mon Sep 09 22:23:45 2019 +0800
+++ b/web/atomicfile.py	Mon Sep 09 22:24:10 2019 +0800
@@ -23,8 +23,8 @@
                     f.write(data)
                     return True
 
-        except IOError, e:
-            print>>sys.stderr, e
+        except IOError as e:
+            print(e, file=sys.stderr)
 
         return False
 
@@ -41,7 +41,7 @@
 
                     return f.read()
 
-        except IOError, e:
-            print>>sys.stderr, e
+        except IOError as e:
+            print(e, file=sys.stderr)
 
         return None
--- a/web/log.py	Mon Sep 09 22:23:45 2019 +0800
+++ b/web/log.py	Mon Sep 09 22:24:10 2019 +0800
@@ -58,8 +58,8 @@
 # stolen from viewmtn, stolen from monotone-viz
 def colour_from_string(str):
     def f(off):
-        return ord(hashval[off]) / 256.0
-    hashval = hashlib.sha1(str).digest()
+        return hashval[off] / 256.0
+    hashval = hashlib.sha1(str.encode()).digest()
     hue = f(5)
     li = f(1) * 0.15 + 0.55
     sat = f(2) * 0.5 + .5
@@ -138,7 +138,7 @@
     graph_args.append('LINE0.5:limitfermheat#%(colour)s' % locals())
 
     # lines are done afterwards so they can be layered
-    sensor_lines.sort(key = lambda (legend, line): "Wort" in legend)
+    sensor_lines.sort(key = lambda legend_line: "Wort" in legend_line[0])
     graph_args += (line for (legend, line) in sensor_lines)
 
     #print>>sys.stderr, '\n'.join(graph_args)
@@ -197,7 +197,7 @@
 def sensor_update(sensor_id, measurements):
     try:
         open(sensor_rrd_path(sensor_id))
-    except IOError, e:
+    except IOError as e:
         create_rrd(sensor_id)
 
     if measurements:
@@ -208,8 +208,8 @@
         for v in values:
             try:
                 rrdtool.update(rrdfile, v)
-            except rrdtool.error, e:
-                print>>sys.stderr, "Bad rrdtool update '%s': %s" % (v, str(e))
+            except rrdtool.error as e:
+                print("Bad rrdtool update '%s': %s" % (v, str(e)), file=sys.stderr)
                 traceback.print_exc(file=sys.stderr)
 
         # be paranoid
@@ -234,10 +234,6 @@
     f.seek(max(0, size-30000))
     return '\n'.join(l.strip() for l in f.readlines()[-400:])
 
-def convert_ds18b20_12bit(reading):
-    value = struct.unpack('>h', binascii.unhexlify(reading))[0]
-    return value * 0.0625
-
 def time_rem(name, entries):
     val_ticks = int(entries[name])
     val_rem = int(entries['%s_rem' % name])
@@ -269,7 +265,7 @@
     measurements = {}
     for rs, t in readings:
         real_t = t + time_diff
-        for s, v in rs.iteritems():
+        for s, v in rs.items():
             measurements.setdefault(s, []).append((real_t, v))
 
     # one-off measurements here
@@ -280,7 +276,7 @@
 
     write_current_params(current_params, current_epoch)
 
-    for s, vs in measurements.iteritems():
+    for s, vs in measurements.items():
         sensor_update(s, vs)
 
     timedelta = time.time() - start_time
@@ -316,7 +312,7 @@
     if not vals:
         return None
 
-    for k, v in _FIELD_DEFAULTS.iteritems():
+    for k, v in _FIELD_DEFAULTS.items():
         n = {'name': k, 'value': type(v)(vals[k])}
         if type(v) is bool:
             kind = 'yesno'
@@ -352,8 +348,8 @@
     for i in p:
         params[i['name']] = i['value']
 
-    if params.viewkeys() != _FIELD_DEFAULTS.viewkeys():
-        diff = params.viewkeys() ^ _FIELD_DEFAULTS.viewkeys()
+    if params.keys() != _FIELD_DEFAULTS.keys():
+        diff = params.keys() ^ _FIELD_DEFAULTS.keys()
         return "Key mismatch, difference %s" % str(diff)
 
     for k, v in params.items():
--- a/web/secure.py	Mon Sep 09 22:23:45 2019 +0800
+++ b/web/secure.py	Mon Sep 09 22:24:10 2019 +0800
@@ -3,7 +3,6 @@
 import time
 import fcntl
 import hmac
-import binascii
 import sys
 import hashlib
 
@@ -27,7 +26,7 @@
 CLEAN_RE = re.compile('[^a-z0-9A-Z]')
 
 def cookie_hash(c):
-    return hashlib.sha256(c).hexdigest()
+    return hashlib.sha256(c.encode()).hexdigest()
 
 def init_cookie():
     """ Generates a new httponly auth cookie if required. 
@@ -35,7 +34,7 @@
     """
     c = bottle.request.get_cookie(AUTH_COOKIE)
     if not c:
-        c = binascii.hexlify(os.urandom(AUTH_COOKIE_LEN))
+        c = os.urandom(AUTH_COOKIE_LEN).hex()
         years = 60*60*24*365
         bottle.response.set_cookie(AUTH_COOKIE, c, secure=True, httponly=True, max_age=10*years)
     return cookie_hash(c)
@@ -49,11 +48,11 @@
 def setup_csrf():
     NONCE_SIZE=16
     global _csrf_fd, _csrf_key
-    _csrf_fd = os.fdopen(os.open('%s/csrf.dat' % config.DATA_PATH, os.O_RDWR | os.O_CREAT, 0600), 'r+')
+    _csrf_fd = os.fdopen(os.open('%s/csrf.dat' % config.DATA_PATH, os.O_RDWR | os.O_CREAT, 0o600), 'r+')
 
     try:
         fcntl.lockf(_csrf_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
-        _csrf_fd.write("%d-%s" % (os.getpid(), binascii.hexlify(os.urandom(NONCE_SIZE))))
+        _csrf_fd.write("%d-%s" % (os.getpid(), os.urandom(NONCE_SIZE).hex()))
         _csrf_fd.flush()
         _csrf_fd.seek(0)
     except IOError:
@@ -66,39 +65,33 @@
 def get_csrf_blob():
     expiry = int(config.CSRF_TIMEOUT + time.time())
     content = '%s-%s' % (init_cookie(), expiry)
-    mac = hmac.new(_csrf_key, content).hexdigest()
+    mac = hmac.new(_csrf_key.encode(), content.encode()).hexdigest()
     return "%s-%s" % (content, mac)
 
 def check_csrf_blob(blob):
     toks = blob.split('-')
     if len(toks) != 3:
-        print>>sys.stderr, "wrong toks"
         return False
 
     user, expiry, mac = toks
     if user != init_cookie():
-        print>>sys.stderr, "wrong user"
         return False
 
     try:
         exp = int(expiry)
     except ValueError:
-        print>>sys.stderr, "failed exp"
         return False
 
     if exp < 1000000000:
         return False
 
     if exp < time.time():
-        print>>sys.stderr, "expired %d %d" % (exp, time.time())
         return False
 
     check_content = "%s-%s" % (user, expiry)
-    check_mac = hmac.new(_csrf_key, check_content).hexdigest()
+    check_mac = hmac.new(_csrf_key.encode(), check_content.encode()).hexdigest()
     if mac == check_mac:
-        print>>sys.stderr, "good hmac"
         return True
 
-    print>>sys.stderr, "fail"
     return False
 
--- a/web/settings.py	Mon Sep 09 22:23:45 2019 +0800
+++ b/web/settings.py	Mon Sep 09 22:24:10 2019 +0800
@@ -1,6 +1,5 @@
-import gevent
+import gevent.event
 import fcntl
-import hashlib
 
 import binascii
 import os
@@ -38,7 +37,7 @@
         return '%s-%s' % (self.epoch, self.tag)
 
     def random(self):
-        return binascii.hexlify(os.urandom(self.RAND_SIZE))
+        return os.urandom(self.RAND_SIZE).hex()
 
     def update(self, contents, epoch = None):
         """ replaces settings contents and updates waiters if changed """
--- a/web/templog-uwsgi.ini	Mon Sep 09 22:23:45 2019 +0800
+++ b/web/templog-uwsgi.ini	Mon Sep 09 22:24:10 2019 +0800
@@ -11,9 +11,6 @@
 wsgi-file=templog.uwsgi
 gevent=100
 
-# for client certificates. default limit is 4096, not sufficient
-buffer-size=20000
-
 user = matt
 group = matt
 
--- a/web/templog.py	Mon Sep 09 22:23:45 2019 +0800
+++ b/web/templog.py	Mon Sep 09 22:24:10 2019 +0800
@@ -6,11 +6,9 @@
 import zlib
 from datetime import datetime, timedelta
 import time
-import urllib
+import urllib.request, urllib.parse, urllib.error
 import sys
-import os
 import traceback
-import fcntl
 import hashlib
 
 import bottle
@@ -35,16 +33,16 @@
 
 @route('/update', method='post')
 def update():
-    js_enc = request.forms.data
+    js_enc = request.forms.data.strip().encode()
     mac = request.forms.hmac
 
-    h = hmac.new(config.HMAC_KEY, js_enc.strip(), hashlib.sha256).hexdigest()
+    h = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest()
     if h != mac:
         raise bottle.HTTPError(code = 403, output = "Bad key")
 
     js = zlib.decompress(binascii.a2b_base64(js_enc))
 
-    params = json.loads(js)
+    params = json.loads(js.decode())
 
     log.parse(params)
 
@@ -59,7 +57,7 @@
     return log.graph_png(start_epoch, length_minutes * 60)
 
 def encode_data(data, mimetype):
-    return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip())
+    return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).decode().rstrip())
 
 @route('/graph.png')
 def graph():
@@ -108,7 +106,7 @@
         csrf_blob = secure.get_csrf_blob(),
         allowed = allowed,
         cookie_hash = cookie_hash,
-        email = urllib.quote(config.EMAIL))
+        email = urllib.parse.quote(config.EMAIL))
 
 def get_request_zoom():
     """ returns (length, end) tuple.
@@ -151,7 +149,7 @@
     request.query.replace('length', minutes)
     request.query.replace('end', endstr)
 
-    urlparams = urllib.urlencode(request.query)
+    urlparams = urllib.parse.urlencode(request.query)
     graphdata = encode_data(make_graph(minutes, endstr), 'image/png')
     return bottle.template('top', urlparams=urlparams,
                     end = endstr,