changeset 1045:65fffdcb97f1

memory: auto-update configuration file with libervia's passphrase when migrating the database
author souliane <souliane@mailoo.org>
date Fri, 23 May 2014 09:59:15 +0200
parents 85c110c0be86
children a874a79ad0f5
files src/memory/memory.py src/memory/sqlite.py
diffstat 2 files changed, 22 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/memory/memory.py	Wed May 21 23:17:51 2014 +0200
+++ b/src/memory/memory.py	Fri May 23 09:59:15 2014 +0200
@@ -21,7 +21,6 @@
 
 import os.path
 import csv
-from xdg import BaseDirectory
 from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
 from uuid import uuid4
 from sat.core.log import getLogger
@@ -35,6 +34,7 @@
 from sat.memory.params import Params
 from sat.memory.disco import Discovery
 from sat.memory.crypto import BlockCipher
+from sat.tools.config import fixConfigOption
 
 
 class Sessions(object):
@@ -180,25 +180,7 @@
     if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE):
         if not silent:
             log.warning(_("A database has been found in the default local_dir for previous versions (< 0.5)"))
-        config = SafeConfigParser()
-        target_file = None
-        for file_ in C.CONFIG_FILES[::-1]:
-            # we will eventually update the existing file with the highest priority, if it's a user personal file...
-            if not silent:
-                log.debug(_("Testing file %s") % file_)
-            if os.path.isfile(file_):
-                if file_.startswith(os.path.expanduser('~')):
-                    config.read([file_])
-                    target_file = file_
-                break
-        if not target_file:
-            # ... otherwise we create a new config file for that user
-            target_file = BaseDirectory.save_config_path('sat') + '/sat.conf'
-        config.set('', 'local_dir', old_default)
-        with open(target_file, 'wb') as configfile:
-            config.write(configfile)  # for the next time that user launches sat
-        if not silent:
-            log.warning(_("Auto-update: local_dir set to %(path)s in the file %(config_file)s") % {'path': old_default, 'config_file': target_file})
+        fixConfigOption('', 'local_dir', old_default)
 
 
 class Memory(object):
--- a/src/memory/sqlite.py	Wed May 21 23:17:51 2014 +0200
+++ b/src/memory/sqlite.py	Fri May 23 09:59:15 2014 +0200
@@ -23,6 +23,7 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 from sat.memory.crypto import BlockCipher, PasswordHasher
+from sat.tools.config import fixConfigOption
 from twisted.enterprise import adbapi
 from twisted.internet import defer
 from collections import OrderedDict
@@ -798,6 +799,25 @@
             d_list.addCallback(lambda dummy: ret)
             return d_list
 
+        def updateLiberviaConf(values):
+            try:
+                profile_id = values[0][0]
+            except IndexError:
+                return  # no profile called "libervia"
+
+            def cb(selected):
+                try:
+                    password = selected[0][0]
+                except IndexError:
+                    log.error("Libervia profile exists but no password is set! Update Libervia configuration will be skipped.")
+                    return
+                fixConfigOption('libervia', 'passphrase', password, False)
+            d = self.dbpool.runQuery("SELECT value FROM param_ind WHERE category=? AND name=? AND profile_id=?", xmpp_pass_path + (profile_id,))
+            d.addCallback(cb)
+
+        d = self.dbpool.runQuery("SELECT id FROM profiles WHERE name='libervia'")
+        d.addCallback(updateLiberviaConf)
+
         d = self.dbpool.runQuery("SELECT profile_id,value FROM param_ind WHERE category=? AND name=?", xmpp_pass_path)
         d.addCallback(encrypt_values)
         return d