From 34fc48331fb036c723d75acce203be3b988dc5e9 Mon Sep 17 00:00:00 2001 From: drebs Date: Mon, 24 Dec 2012 12:42:56 -0200 Subject: GPGWrapper now extends gnupg.GPG --- src/leap/soledad/util.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'src/leap/soledad/util.py') diff --git a/src/leap/soledad/util.py b/src/leap/soledad/util.py index 41fd4548..5d1e263e 100644 --- a/src/leap/soledad/util.py +++ b/src/leap/soledad/util.py @@ -2,7 +2,7 @@ import os import gnupg import re -class GPGWrapper(): +class GPGWrapper(gnupg.GPG): """ This is a temporary class for handling GPG requests, and should be replaced by a more general class used throughout the project. @@ -12,13 +12,13 @@ class GPGWrapper(): GNUPG_BINARY = "/usr/bin/gpg" # this has to be changed based on OS def __init__(self, gpghome=GNUPG_HOME, gpgbinary=GNUPG_BINARY): - self.gpg = gnupg.GPG(gnupghome=gpghome, gpgbinary=gpgbinary) + super(GPGWrapper, self).__init__(gnupghome=gpghome, gpgbinary=gpgbinary) def find_key(self, email): """ Find user's key based on their email. """ - for key in self.gpg.list_keys(): + for key in self.list_keys(): for uid in key['uids']: if re.search(email, uid): return key @@ -26,23 +26,17 @@ class GPGWrapper(): def encrypt(self, data, recipient, sign=None, always_trust=True, passphrase=None, symmetric=False): - return self.gpg.encrypt(data, recipient, sign=sign, - always_trust=always_trust, - passphrase=passphrase, symmetric=symmetric) + # TODO: devise a way so we don't need to "always trust". + return super(GPGWrapper, self).encrypt(data, recipient, sign=sign, + always_trust=always_trust, + passphrase=passphrase, + symmetric=symmetric) def decrypt(self, data, always_trust=True, passphrase=None): - result = self.gpg.decrypt(data, always_trust=always_trust, - passphrase=passphrase) - return result - - def import_keys(self, data): - return self.gpg.import_keys(data) - - def gen_key_input(self, **kwargs): - return self.gpg.gen_key_input(**kwargs) - - def gen_key(self, input): - return self.gpg.gen_key(input) + # TODO: devise a way so we don't need to "always trust". + return super(GPGWrapper, self).decrypt(data, + always_trust=always_trust, + passphrase=passphrase) #---------------------------------------------------------------------------- -- cgit v1.2.3 From 948f12a655435968f2754209f8031516433255f4 Mon Sep 17 00:00:00 2001 From: drebs Date: Mon, 24 Dec 2012 12:46:37 -0200 Subject: Add send_keys to GPGWrapper --- src/leap/soledad/util.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/leap/soledad/util.py') diff --git a/src/leap/soledad/util.py b/src/leap/soledad/util.py index 5d1e263e..af38cd76 100644 --- a/src/leap/soledad/util.py +++ b/src/leap/soledad/util.py @@ -38,6 +38,20 @@ class GPGWrapper(gnupg.GPG): always_trust=always_trust, passphrase=passphrase) + def send_keys(self, keyserver, *keyids): + """ + Send keys to a keyserver + """ + result = self.result_map['list'](self) + logger.debug('send_keys: %r', keyids) + data = _make_binary_stream("", self.encoding) + args = ['--keyserver', keyserver, '--send-keys'] + args.extend(keyids) + self._handle_io(args, data, result, binary=True) + logger.debug('send_keys result: %r', result.__dict__) + data.close() + return result + #---------------------------------------------------------------------------- # u1db Transaction and Sync logs. -- cgit v1.2.3 From 9b7728b80027f35c013a449b884235cf8eac503c Mon Sep 17 00:00:00 2001 From: drebs Date: Wed, 9 Jan 2013 11:46:58 -0200 Subject: CouchDatabase passes u1db LocalDatabaseTests. --- src/leap/soledad/util.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/leap/soledad/util.py') diff --git a/src/leap/soledad/util.py b/src/leap/soledad/util.py index af38cd76..8683fbb9 100644 --- a/src/leap/soledad/util.py +++ b/src/leap/soledad/util.py @@ -149,7 +149,13 @@ class TransactionLog(SimpleLog): cur_gen, _, newest_trans_id = results[0] return cur_gen, newest_trans_id, changes - + + + def get_transaction_log(self): + """ + Return only a list of (doc_id, transaction_id) + """ + return map(lambda x: (x[1], x[2]), sorted(self._log)) class SyncLog(SimpleLog): @@ -182,3 +188,12 @@ class SyncLog(SimpleLog): self.append((other_replica_uid, other_generation, other_transaction_id)) +class ConflictLog(SimpleLog): + """ + A list of (doc_id, my_doc_rev, my_content) tuples. + """ + + def delete_conflicts(self, conflicts): + for conflict in conflicts: + self.log = self.filter(lambda x: + x[0] != conflict[0] or x[1] != conflict[1]) -- cgit v1.2.3 From 8909a16a12c17098ae8d5ecd31af6765b543622c Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 10 Jan 2013 13:18:48 -0200 Subject: CouchDatabase passes all relevant u1db tests. --- src/leap/soledad/util.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/leap/soledad/util.py') diff --git a/src/leap/soledad/util.py b/src/leap/soledad/util.py index 8683fbb9..8a8bedfb 100644 --- a/src/leap/soledad/util.py +++ b/src/leap/soledad/util.py @@ -74,13 +74,13 @@ class SimpleLog(object): self._log.append(msg) def reduce(self, func, initializer=None): - return reduce(func, self.log, initializer) + return reduce(func, self._log, initializer) def map(self, func): - return map(func, self.log) + return map(func, self._get_log()) def filter(self, func): - return filter(func, self.log) + return filter(func, self._get_log()) class TransactionLog(SimpleLog): @@ -141,7 +141,7 @@ class TransactionLog(SimpleLog): newest_trans_id = changes[0][2] changes.reverse() else: - results = self.log + results = self._get_log() if not results: cur_gen = 0 newest_trans_id = '' @@ -164,7 +164,7 @@ class SyncLog(SimpleLog): """ def find_by_replica_uid(self, replica_uid): - if not self.log: + if not self._get_log(): return () return self.reduce(lambda x, y: y if y[0] == replica_uid else x) @@ -184,7 +184,7 @@ class SyncLog(SimpleLog): Set the last-known generation and transaction id for the other database replica. """ - self.log = self.filter(lambda x: x[0] != other_replica_uid) + self._log = self.filter(lambda x: x[0] != other_replica_uid) self.append((other_replica_uid, other_generation, other_transaction_id)) @@ -192,8 +192,22 @@ class ConflictLog(SimpleLog): """ A list of (doc_id, my_doc_rev, my_content) tuples. """ + + def __init__(self, factory): + super(ConflictLog, self).__init__() + self._factory = factory def delete_conflicts(self, conflicts): for conflict in conflicts: - self.log = self.filter(lambda x: + self._log = self.filter(lambda x: x[0] != conflict[0] or x[1] != conflict[1]) + + def get_conflicts(self, doc_id): + conflicts = self.filter(lambda x: x[0] == doc_id) + if not conflicts: + return [] + return reversed(map(lambda x: self._factory(doc_id, x[1], x[2]), + conflicts)) + + def has_conflicts(self, doc_id): + return bool(self.filter(lambda x: x[0] == doc_id)) -- cgit v1.2.3 From 7759f6c6b862e792adb4a005d9ec27e313fb6e06 Mon Sep 17 00:00:00 2001 From: drebs Date: Tue, 15 Jan 2013 18:52:56 -0200 Subject: Refactor and fix ObjectStore. --- src/leap/soledad/util.py | 159 +---------------------------------------------- 1 file changed, 1 insertion(+), 158 deletions(-) (limited to 'src/leap/soledad/util.py') diff --git a/src/leap/soledad/util.py b/src/leap/soledad/util.py index 8a8bedfb..a26bff31 100644 --- a/src/leap/soledad/util.py +++ b/src/leap/soledad/util.py @@ -53,161 +53,4 @@ class GPGWrapper(gnupg.GPG): return result -#---------------------------------------------------------------------------- -# u1db Transaction and Sync logs. -#---------------------------------------------------------------------------- - -class SimpleLog(object): - def __init__(self): - self._log = [] - - def _set_log(self, log): - self._log = log - - def _get_log(self): - return self._log - - log = property( - _get_log, _set_log, doc="Log contents.") - - def append(self, msg): - self._log.append(msg) - - def reduce(self, func, initializer=None): - return reduce(func, self._log, initializer) - - def map(self, func): - return map(func, self._get_log()) - - def filter(self, func): - return filter(func, self._get_log()) - - -class TransactionLog(SimpleLog): - """ - An ordered list of (generation, doc_id, transaction_id) tuples. - """ - - def _set_log(self, log): - self._log = log - - def _get_log(self): - return sorted(self._log, reverse=True) - - log = property( - _get_log, _set_log, doc="Log contents.") - - def get_generation(self): - """ - Return the current generation. - """ - gens = self.map(lambda x: x[0]) - if not gens: - return 0 - return max(gens) - - def get_generation_info(self): - """ - Return the current generation and transaction id. - """ - if not self._log: - return(0, '') - info = self.map(lambda x: (x[0], x[2])) - return reduce(lambda x, y: x if (x[0] > y[0]) else y, info) - - def get_trans_id_for_gen(self, gen): - """ - Get the transaction id corresponding to a particular generation. - """ - log = self.reduce(lambda x, y: y if y[0] == gen else x) - if log is None: - return None - return log[2] - - def whats_changed(self, old_generation): - """ - Return a list of documents that have changed since old_generation. - """ - results = self.filter(lambda x: x[0] > old_generation) - seen = set() - changes = [] - newest_trans_id = '' - for generation, doc_id, trans_id in results: - if doc_id not in seen: - changes.append((doc_id, generation, trans_id)) - seen.add(doc_id) - if changes: - cur_gen = changes[0][1] # max generation - newest_trans_id = changes[0][2] - changes.reverse() - else: - results = self._get_log() - if not results: - cur_gen = 0 - newest_trans_id = '' - else: - cur_gen, _, newest_trans_id = results[0] - - return cur_gen, newest_trans_id, changes - - - def get_transaction_log(self): - """ - Return only a list of (doc_id, transaction_id) - """ - return map(lambda x: (x[1], x[2]), sorted(self._log)) - - -class SyncLog(SimpleLog): - """ - A list of (replica_id, generation, transaction_id) tuples. - """ - - def find_by_replica_uid(self, replica_uid): - if not self._get_log(): - return () - return self.reduce(lambda x, y: y if y[0] == replica_uid else x) - - def get_replica_gen_and_trans_id(self, other_replica_uid): - """ - Return the last known generation and transaction id for the other db - replica. - """ - info = self.find_by_replica_uid(other_replica_uid) - if not info: - return (0, '') - return (info[1], info[2]) - - def set_replica_gen_and_trans_id(self, other_replica_uid, - other_generation, other_transaction_id): - """ - Set the last-known generation and transaction id for the other - database replica. - """ - self._log = self.filter(lambda x: x[0] != other_replica_uid) - self.append((other_replica_uid, other_generation, - other_transaction_id)) - -class ConflictLog(SimpleLog): - """ - A list of (doc_id, my_doc_rev, my_content) tuples. - """ - - def __init__(self, factory): - super(ConflictLog, self).__init__() - self._factory = factory - - def delete_conflicts(self, conflicts): - for conflict in conflicts: - self._log = self.filter(lambda x: - x[0] != conflict[0] or x[1] != conflict[1]) - - def get_conflicts(self, doc_id): - conflicts = self.filter(lambda x: x[0] == doc_id) - if not conflicts: - return [] - return reversed(map(lambda x: self._factory(doc_id, x[1], x[2]), - conflicts)) - - def has_conflicts(self, doc_id): - return bool(self.filter(lambda x: x[0] == doc_id)) + -- cgit v1.2.3