From 4b22f7014e220a94fbf76aa7a323dd2556408ce1 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 8 Dec 2015 00:15:47 +0100 Subject: [PATCH] Ported gpgfs to Python 3.5. Whooo! --- gpgfs.py | 85 +++++++++++++++++++++++++++-------------------------- gpgstore.py | 14 ++++----- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/gpgfs.py b/gpgfs.py index 95e8cd0..2b2fc8c 100755 --- a/gpgfs.py +++ b/gpgfs.py @@ -8,12 +8,12 @@ import sys import logging import struct import time -from cStringIO import StringIO +from io import BytesIO import gpgstore from contextlib import contextmanager from threading import Lock -magic = 'GPGFS1\n' +magic = b'GPGFS1\n' log = logging.getLogger('gpgfs') @@ -22,30 +22,31 @@ class Entry: Filesystem object, either file or directory. ''' def __init__(self, **kwargs): - for k,v in kwargs.iteritems(): + for k,v in kwargs.items(): setattr(self, k, v) def read_index(store, path): if not store.exists(path): now = time.time() root = Entry(children={}, nlink=3, size=0, - mode=stat.S_IFDIR | 0755, + mode=stat.S_IFDIR | 0o755, mtime=now, ctime=now) write_index(store, path, root) log.info('created %s', path) return root data = store.get(path, format=gpgstore.FMT_GPG) - buf = StringIO(data) - if buf.read(len(magic)) != magic: - raise IOError, 'index parse error: %s' % path + buf = BytesIO(data) + temp = buf.read(len(magic)) + if temp != magic: + raise IOError('index parse error: %s' % path) read_atom(buf) root = Entry(**read_dict(buf)) return root def write_index(store, path, root): - buf = StringIO() + buf = BytesIO() buf.write(magic) - header = '' + header = b'' write_atom(buf, header) write_dict(buf, root) store.put(buf.getvalue(), path=path, format=gpgstore.FMT_GPG) @@ -53,36 +54,36 @@ def write_index(store, path, root): def write_dict(fd, dct): # breadth-first children = [] - buf = StringIO() + buf = BytesIO() if not isinstance(dct, dict): dct = dct.__dict__ for key in dct: - write_atom(buf, key.encode('utf8')) + write_atom(buf, key.encode('utf-8')) val = dct[key] if isinstance(val, dict): - buf.write('D') + buf.write(b'D') children.append(val) elif isinstance(val, Entry): - buf.write('E') + buf.write(b'E') children.append(val) - elif isinstance(val, (int, long)): + elif isinstance(val, (int)): if val < 2**32: - buf.write('I') + buf.write(b'I') buf.write(struct.pack('