From 7562655cf4bf28a1ebd6c458334da0c166f34e61 Mon Sep 17 00:00:00 2001 From: efkin Date: Mon, 13 Mar 2017 21:17:18 +0100 Subject: [refactor] Improve python3 compatibility With this commit all tests on py34 tox environment are collected. --- client/src/leap/soledad/client/_crypto.py | 8 ++++---- client/src/leap/soledad/client/_secrets/storage.py | 2 +- client/src/leap/soledad/client/adbapi.py | 9 ++++++--- client/src/leap/soledad/client/api.py | 15 +++++++-------- client/src/leap/soledad/client/http_target/api.py | 2 +- .../src/leap/soledad/client/http_target/fetch_protocol.py | 4 ++-- .../src/leap/soledad/client/http_target/send_protocol.py | 5 ++--- client/src/leap/soledad/client/sqlcipher.py | 9 ++++++--- common/src/leap/soledad/common/command.py | 2 +- common/src/leap/soledad/common/couch/__init__.py | 5 ++--- common/src/leap/soledad/common/couch/state.py | 3 ++- .../leap/soledad/common/l2db/backends/sqlite_backend.py | 13 ++++++++----- common/src/leap/soledad/common/l2db/query_parser.py | 2 +- common/src/leap/soledad/common/l2db/remote/http_app.py | 6 +++--- common/src/leap/soledad/common/l2db/remote/http_client.py | 7 +++---- .../src/leap/soledad/common/l2db/remote/http_database.py | 2 +- common/src/leap/soledad/common/l2db/sync.py | 2 +- server/src/leap/soledad/server/__init__.py | 2 +- server/src/leap/soledad/server/gzip_middleware.py | 2 +- server/src/leap/soledad/server/sync.py | 2 +- testing/test_soledad/u1db_tests/__init__.py | 11 +++++------ testing/test_soledad/util.py | 11 +++++------ testing/tests/sqlcipher/test_backend.py | 12 +++++++----- 23 files changed, 71 insertions(+), 65 deletions(-) diff --git a/client/src/leap/soledad/client/_crypto.py b/client/src/leap/soledad/client/_crypto.py index f91084a4..f9a20285 100644 --- a/client/src/leap/soledad/client/_crypto.py +++ b/client/src/leap/soledad/client/_crypto.py @@ -30,7 +30,7 @@ import struct import time from io import BytesIO -from itertools import imap +from six.moves import map as imap from collections import namedtuple from twisted.internet import defer @@ -43,7 +43,7 @@ from cryptography.hazmat.backends.multibackend import MultiBackend from cryptography.hazmat.backends.openssl.backend \ import Backend as OpenSSLBackend -from zope.interface import implements +from zope.interface import implementer SECRET_LENGTH = 64 @@ -290,7 +290,7 @@ class BlobDecryptor(object): magic, sch, meth, ts, iv, doc_id, rev, doc_size = unpacked_data else: raise InvalidBlob("Unexpected preamble size %d", len(preamble)) - except struct.error, e: + except struct.error as e: raise InvalidBlob(e) if magic != BLOB_SIGNATURE_MAGIC: @@ -325,12 +325,12 @@ class BlobDecryptor(object): return d +@implementer(interfaces.IConsumer) class AESWriter(object): """ A Twisted's Consumer implementation that takes an input file descriptor and applies AES-256 cipher in GCM mode. """ - implements(interfaces.IConsumer) def __init__(self, key, iv=None, _buffer=None, tag=None, mode=modes.GCM): if len(key) != 32: diff --git a/client/src/leap/soledad/client/_secrets/storage.py b/client/src/leap/soledad/client/_secrets/storage.py index 056c4322..6ea89900 100644 --- a/client/src/leap/soledad/client/_secrets/storage.py +++ b/client/src/leap/soledad/client/_secrets/storage.py @@ -16,7 +16,7 @@ # along with this program. If not, see . import json -import urlparse +import six.moves.urllib.parse as urlparse from hashlib import sha256 diff --git a/client/src/leap/soledad/client/adbapi.py b/client/src/leap/soledad/client/adbapi.py index a5328d2b..2bc146bd 100644 --- a/client/src/leap/soledad/client/adbapi.py +++ b/client/src/leap/soledad/client/adbapi.py @@ -26,14 +26,17 @@ from functools import partial from twisted.enterprise import adbapi from twisted.internet.defer import DeferredSemaphore from zope.proxy import ProxyBase, setProxiedObject -from pysqlcipher import dbapi2 from leap.soledad.common.log import getLogger from leap.soledad.common.errors import DatabaseAccessError - from leap.soledad.client import sqlcipher as soledad_sqlcipher from leap.soledad.client.pragmas import set_init_pragmas +if sys.version_info[0] < 3: + from pysqlcipher import dbapi2 +else: + from pysqlcipher3 import dbapi2 + logger = getLogger(__name__) @@ -276,7 +279,7 @@ class U1DBConnectionPool(adbapi.ConnectionPool): conn.rollback() except: logger.error(None, "Rollback failed") - raise excType, excValue, excTraceback + raise excType(excValue, excTraceback) def finalClose(self): """ diff --git a/client/src/leap/soledad/client/api.py b/client/src/leap/soledad/client/api.py index c8c3ce30..3b17f188 100644 --- a/client/src/leap/soledad/client/api.py +++ b/client/src/leap/soledad/client/api.py @@ -27,20 +27,19 @@ remote storage in the server side. """ import binascii import errno -import httplib import os import socket import ssl import uuid -import urlparse from itertools import chain - -from StringIO import StringIO +import six.moves.http_client as httplib +import six.moves.urllib.parse as urlparse +from six import StringIO from collections import defaultdict from twisted.internet import defer -from zope.interface import implements +from zope.interface import implementer from leap.common.config import get_path_prefix from leap.common.plugins import collect_plugins @@ -79,6 +78,9 @@ Soledad client and server. SOLEDAD_CERT = None +@implementer(soledad_interfaces.ILocalStorage, + soledad_interfaces.ISyncableStorage, + soledad_interfaces.ISecretsStorage) class Soledad(object): """ Soledad provides encrypted data storage and sync. @@ -111,9 +113,6 @@ class Soledad(object): there's indeed new data to be synchronized between local database replica and server's replica. --- not used right now. """ - implements(soledad_interfaces.ILocalStorage, - soledad_interfaces.ISyncableStorage, - soledad_interfaces.ISecretsStorage) local_db_file_name = 'soledad.u1db' secrets_file_name = "soledad.json" diff --git a/client/src/leap/soledad/client/http_target/api.py b/client/src/leap/soledad/client/http_target/api.py index fc65c9bd..c68185c6 100644 --- a/client/src/leap/soledad/client/http_target/api.py +++ b/client/src/leap/soledad/client/http_target/api.py @@ -18,7 +18,7 @@ import os import json import base64 -from StringIO import StringIO +from six import StringIO from uuid import uuid4 from twisted.internet import defer diff --git a/client/src/leap/soledad/client/http_target/fetch_protocol.py b/client/src/leap/soledad/client/http_target/fetch_protocol.py index c7eabe2b..851eb3a1 100644 --- a/client/src/leap/soledad/client/http_target/fetch_protocol.py +++ b/client/src/leap/soledad/client/http_target/fetch_protocol.py @@ -16,7 +16,7 @@ # along with this program. If not, see . import json from functools import partial -from cStringIO import StringIO +from six import StringIO from twisted.web._newclient import ResponseDone from leap.soledad.common.l2db import errors from leap.soledad.common.l2db.remote import utils @@ -70,7 +70,7 @@ class DocStreamReceiver(ReadBodyProtocol): self.dataBuffer = self.metadata else: self.dataBuffer = self.finish() - except errors.BrokenSyncStream, e: + except errors.BrokenSyncStream as e: return self.deferred.errback(e) return ReadBodyProtocol.connectionLost(self, reason) diff --git a/client/src/leap/soledad/client/http_target/send_protocol.py b/client/src/leap/soledad/client/http_target/send_protocol.py index 0cb6d039..4941aa34 100644 --- a/client/src/leap/soledad/client/http_target/send_protocol.py +++ b/client/src/leap/soledad/client/http_target/send_protocol.py @@ -14,20 +14,19 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from zope.interface import implements +from zope.interface import implementer from twisted.internet import defer from twisted.internet import reactor from twisted.web.iweb import IBodyProducer from twisted.web.iweb import UNKNOWN_LENGTH +@implementer(IBodyProducer) class DocStreamProducer(object): """ A producer that writes the body of a request to a consumer. """ - implements(IBodyProducer) - def __init__(self, producer): """ Initialize the string produer. diff --git a/client/src/leap/soledad/client/sqlcipher.py b/client/src/leap/soledad/client/sqlcipher.py index a3e45228..2c995d5a 100644 --- a/client/src/leap/soledad/client/sqlcipher.py +++ b/client/src/leap/soledad/client/sqlcipher.py @@ -42,11 +42,10 @@ SQLCipher 1.1 databases, we do not implement them as all SQLCipher databases handled by Soledad should be created by SQLCipher >= 2.0. """ import os +import sys from functools import partial -from pysqlcipher import dbapi2 as sqlcipher_dbapi2 - from twisted.internet import reactor from twisted.internet import defer from twisted.enterprise import adbapi @@ -62,6 +61,10 @@ from leap.soledad.client.http_target import SoledadHTTPSyncTarget from leap.soledad.client.sync import SoledadSynchronizer from leap.soledad.client import pragmas +if sys.version_info[0] < 3: + from pysqlcipher import dbapi2 as sqlcipher_dbapi2 +else: + from pysqlcipher3 import dbapi2 as sqlcipher_dbapi2 logger = getLogger(__name__) @@ -306,7 +309,7 @@ class SQLCipherDatabase(sqlite_backend.SQLitePartialExpandDatabase): )) try: c.execute(statement, tuple(args)) - except sqlcipher_dbapi2.OperationalError, e: + except sqlcipher_dbapi2.OperationalError as e: raise sqlcipher_dbapi2.OperationalError( str(e) + '\nstatement: %s\nargs: %s\n' % (statement, args)) res = c.fetchall() diff --git a/common/src/leap/soledad/common/command.py b/common/src/leap/soledad/common/command.py index 811bf135..66aa6b7a 100644 --- a/common/src/leap/soledad/common/command.py +++ b/common/src/leap/soledad/common/command.py @@ -45,7 +45,7 @@ def exec_validated_cmd(cmd, argument, validator=None): try: process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - except OSError, e: + except OSError as e: return 1, e (out, err) = process.communicate() code = process.wait() diff --git a/common/src/leap/soledad/common/couch/__init__.py b/common/src/leap/soledad/common/couch/__init__.py index 2e6f734e..2343e849 100644 --- a/common/src/leap/soledad/common/couch/__init__.py +++ b/common/src/leap/soledad/common/couch/__init__.py @@ -25,9 +25,8 @@ import re import uuid import binascii - -from StringIO import StringIO -from urlparse import urljoin +from six import StringIO +from six.moves.urllib.parse import urljoin from contextlib import contextmanager diff --git a/common/src/leap/soledad/common/couch/state.py b/common/src/leap/soledad/common/couch/state.py index a4841d0d..8cbe0934 100644 --- a/common/src/leap/soledad/common/couch/state.py +++ b/common/src/leap/soledad/common/couch/state.py @@ -19,7 +19,8 @@ Server state using CouchDatabase as backend. """ import couchdb import re -from urlparse import urljoin + +from six.moves.urllib.parse import urljoin from leap.soledad.common.log import getLogger from leap.soledad.common.couch import CouchDatabase diff --git a/common/src/leap/soledad/common/l2db/backends/sqlite_backend.py b/common/src/leap/soledad/common/l2db/backends/sqlite_backend.py index 27db65af..4f7b1259 100644 --- a/common/src/leap/soledad/common/l2db/backends/sqlite_backend.py +++ b/common/src/leap/soledad/common/l2db/backends/sqlite_backend.py @@ -58,7 +58,7 @@ class SQLiteDatabase(CommonBackend): try: c.execute("SELECT value FROM u1db_config" " WHERE name = 'index_storage'") - except dbapi2.OperationalError, e: + except dbapi2.OperationalError as e: # The table does not exist yet return None, e else: @@ -668,7 +668,7 @@ class SQLiteDatabase(CommonBackend): c = self._db_handle.cursor() try: c.execute(statement, tuple(args)) - except dbapi2.OperationalError, e: + except dbapi2.OperationalError as e: raise dbapi2.OperationalError( str(e) + '\nstatement: %s\nargs: %s\n' % (statement, args)) @@ -768,7 +768,7 @@ class SQLiteDatabase(CommonBackend): c = self._db_handle.cursor() try: c.execute(statement, tuple(args)) - except dbapi2.OperationalError, e: + except dbapi2.OperationalError as e: raise dbapi2.OperationalError( str(e) + '\nstatement: %s\nargs: %s\n' % (statement, args)) @@ -798,7 +798,7 @@ class SQLiteDatabase(CommonBackend): value_fields)) try: c.execute(statement, tuple(definition)) - except dbapi2.OperationalError, e: + except dbapi2.OperationalError as e: raise dbapi2.OperationalError( str(e) + '\nstatement: %s\nargs: %s\n' % (statement, tuple(definition))) @@ -893,7 +893,10 @@ class SQLitePartialExpandDatabase(SQLiteDatabase): stored_def = self._get_index_definition(index_name) if stored_def == [x[-1] for x in definition]: return - raise errors.IndexNameTakenError, e, sys.exc_info()[2] + raise errors.IndexNameTakenError( + str(e) + + str(sys.exc_info()[2]) + ) new_fields = set( [f for f in index_expressions if f not in cur_fields]) if new_fields: diff --git a/common/src/leap/soledad/common/l2db/query_parser.py b/common/src/leap/soledad/common/l2db/query_parser.py index dd35b12a..15a9ac80 100644 --- a/common/src/leap/soledad/common/l2db/query_parser.py +++ b/common/src/leap/soledad/common/l2db/query_parser.py @@ -323,7 +323,7 @@ class Parser(object): else: try: inner = arg_type(arg) - except ValueError, e: + except ValueError as e: raise errors.IndexDefinitionParseError( "Invalid value %r for argument type %r " "(%r)." % (arg, arg_type, e)) diff --git a/common/src/leap/soledad/common/l2db/remote/http_app.py b/common/src/leap/soledad/common/l2db/remote/http_app.py index 496274b2..a4eddb36 100644 --- a/common/src/leap/soledad/common/l2db/remote/http_app.py +++ b/common/src/leap/soledad/common/l2db/remote/http_app.py @@ -21,11 +21,11 @@ HTTP Application exposing U1DB. # TODO -- deprecate, use twisted/txaio. import functools -import httplib +import six.moves.http_client as httplib import inspect import json import sys -import urlparse +import six.moves.urllib.parse as urlparse import routes.mapper @@ -610,7 +610,7 @@ class HTTPApp(object): try: resource = self._lookup_resource(environ, responder) HTTPInvocationByMethodWithBody(resource, environ, self)() - except errors.U1DBError, e: + except errors.U1DBError as e: self.request_u1db_error(environ, e) status = http_errors.wire_description_to_status.get( e.wire_description, 500) diff --git a/common/src/leap/soledad/common/l2db/remote/http_client.py b/common/src/leap/soledad/common/l2db/remote/http_client.py index 53363c0a..1124b038 100644 --- a/common/src/leap/soledad/common/l2db/remote/http_client.py +++ b/common/src/leap/soledad/common/l2db/remote/http_client.py @@ -16,14 +16,13 @@ """Base class to make requests to a remote HTTP server.""" -import httplib import json import socket import ssl import sys -import urlparse import urllib - +import six.moves.urllib.parse as urlparse +import six.moves.http_client as httplib from time import sleep from leap.soledad.common.l2db import errors from leap.soledad.common.l2db.remote import http_errors @@ -168,7 +167,7 @@ class HTTPClientBase(object): try: self._conn.request(method, url_query, body, headers) return self._response() - except errors.Unavailable, e: + except errors.Unavailable as e: sleep(delay) raise e diff --git a/common/src/leap/soledad/common/l2db/remote/http_database.py b/common/src/leap/soledad/common/l2db/remote/http_database.py index 7512379f..7e61e5a4 100644 --- a/common/src/leap/soledad/common/l2db/remote/http_database.py +++ b/common/src/leap/soledad/common/l2db/remote/http_database.py @@ -87,7 +87,7 @@ class HTTPDatabase(http_client.HTTPClientBase, Database): 'GET', ['doc', doc_id], {"include_deleted": include_deleted}) except errors.DocumentDoesNotExist: return None - except errors.HTTPError, e: + except errors.HTTPError as e: if (e.status == DOCUMENT_DELETED_STATUS and 'x-u1db-rev' in e.headers): res = None diff --git a/common/src/leap/soledad/common/l2db/sync.py b/common/src/leap/soledad/common/l2db/sync.py index 5e9b22f4..32281f30 100644 --- a/common/src/leap/soledad/common/l2db/sync.py +++ b/common/src/leap/soledad/common/l2db/sync.py @@ -15,7 +15,7 @@ # along with u1db. If not, see . """The synchronization utilities for U1DB.""" -from itertools import izip +from six.moves import zip as izip from leap.soledad.common import l2db from leap.soledad.common.l2db import errors diff --git a/server/src/leap/soledad/server/__init__.py b/server/src/leap/soledad/server/__init__.py index 5bed22c9..6640bab4 100644 --- a/server/src/leap/soledad/server/__init__.py +++ b/server/src/leap/soledad/server/__init__.py @@ -82,7 +82,7 @@ documents on the shared database is handled by `leap.soledad.server.auth` module. """ -import urlparse +import six.moves.urllib.parse as urlparse import sys from leap.soledad.common.l2db.remote import http_app, utils diff --git a/server/src/leap/soledad/server/gzip_middleware.py b/server/src/leap/soledad/server/gzip_middleware.py index 986c5738..c77f9f67 100644 --- a/server/src/leap/soledad/server/gzip_middleware.py +++ b/server/src/leap/soledad/server/gzip_middleware.py @@ -17,7 +17,7 @@ """ Gzip middleware for WSGI apps. """ -import StringIO +from six import StringIO from gzip import GzipFile diff --git a/server/src/leap/soledad/server/sync.py b/server/src/leap/soledad/server/sync.py index b553a056..6791c06c 100644 --- a/server/src/leap/soledad/server/sync.py +++ b/server/src/leap/soledad/server/sync.py @@ -18,7 +18,7 @@ Server side synchronization infrastructure. """ import time -from itertools import izip +from six.moves import zip as izip from leap.soledad.common.l2db import sync from leap.soledad.common.l2db.remote import http_app diff --git a/testing/test_soledad/u1db_tests/__init__.py b/testing/test_soledad/u1db_tests/__init__.py index 36db548d..1575b859 100644 --- a/testing/test_soledad/u1db_tests/__init__.py +++ b/testing/test_soledad/u1db_tests/__init__.py @@ -28,12 +28,6 @@ import sys from six import StringIO from wsgiref import simple_server - -if sys.version_info[0] < 3: - from pysqlcipher import dbapi2 -else: - from pysqlcipher3 import dbapi2 - import testscenarios from twisted.trial import unittest from twisted.web.server import Site @@ -48,6 +42,11 @@ from leap.soledad.common.l2db.remote import server_state from leap.soledad.common.l2db.remote import http_app from leap.soledad.common.l2db.remote import http_target +if sys.version_info[0] < 3: + from pysqlcipher import dbapi2 +else: + from pysqlcipher3 import dbapi2 + class TestCase(unittest.TestCase): diff --git a/testing/test_soledad/util.py b/testing/test_soledad/util.py index 91291faf..6ffb60b6 100644 --- a/testing/test_soledad/util.py +++ b/testing/test_soledad/util.py @@ -31,11 +31,6 @@ from six import StringIO from uuid import uuid4 from mock import Mock -if sys.version_info[0] < 3: - from pysqlcipher import dbapi2 -else: - from pysqlcipher3 import dbapi2 - from twisted.trial import unittest from leap.common.testing.basetest import BaseLeapTest @@ -48,7 +43,6 @@ from leap.soledad.common.document import SoledadDocument from leap.soledad.common.couch import CouchDatabase from leap.soledad.common.couch.state import CouchServerState - from leap.soledad.client import Soledad from leap.soledad.client import http_target from leap.soledad.client import auth @@ -58,6 +52,11 @@ from leap.soledad.client._crypto import is_symmetrically_encrypted from leap.soledad.server import SoledadApp +if sys.version_info[0] < 3: + from pysqlcipher import dbapi2 +else: + from pysqlcipher3 import dbapi2 + PASSWORD = '123456' ADDRESS = 'user-1234' diff --git a/testing/tests/sqlcipher/test_backend.py b/testing/tests/sqlcipher/test_backend.py index 643fa041..6e9595db 100644 --- a/testing/tests/sqlcipher/test_backend.py +++ b/testing/tests/sqlcipher/test_backend.py @@ -22,11 +22,6 @@ import pytest import time import threading import sys -if sys.version_info[0] < 3: - from pysqlcipher import dbapi2 -else: - from pysqlcipher3 import dbapi2 -from testscenarios import TestWithScenarios # l2db stuff. from leap.soledad.common.l2db import errors @@ -48,6 +43,13 @@ from test_soledad.util import SQLCIPHER_SCENARIOS from test_soledad.util import PASSWORD from test_soledad.util import BaseSoledadTest +from testscenarios import TestWithScenarios + +if sys.version_info[0] < 3: + from pysqlcipher import dbapi2 +else: + from pysqlcipher3 import dbapi2 + def sqlcipher_open(path, passphrase, create=True, document_factory=None): return SQLCipherDatabase( -- cgit v1.2.3