diff src/memory/memory.py @ 1003:52ec79aa5bbe

memory: fixes the method fixLocalDir for pid_dir and log_dir to use local_dir as default
author souliane <souliane@mailoo.org>
date Wed, 30 Apr 2014 19:54:21 +0200
parents 301b342c697a
children a7d33c7a8277
line wrap: on
line diff
--- a/src/memory/memory.py	Wed Apr 30 16:34:09 2014 +0200
+++ b/src/memory/memory.py	Wed Apr 30 19:54:21 2014 +0200
@@ -107,6 +107,50 @@
         return self._sessions.iterkeys()
 
 
+# XXX: tmp update code, will be removed in the future
+# When you remove this, please add the default value for
+# 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG
+def fixLocalDir(silent=True):
+    """Retro-compatibility with the previous local_dir default value.
+
+    @param silent (boolean): toggle logging output (must be True when called from sat.sh)
+    """
+    user_config = SafeConfigParser()
+    try:
+        user_config.read(C.CONFIG_FILES)
+    except:
+        pass  # file is readable but its structure if wrong
+    try:
+        current_value = user_config.get('DEFAULT', 'local_dir')
+    except (NoOptionError, NoSectionError):
+        current_value = ''
+    if current_value:
+        return  # nothing to do
+    old_default = '~/.sat'
+    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})
+
+
 class Memory(object):
     """This class manage all persistent informations"""
 
@@ -118,8 +162,8 @@
                                   #     /!\ an entity is not necessarily in roster
         self.subscriptions = {}
         self.disco = Discovery(host)
+        fixLocalDir(False)  # XXX: tmp update code, will be removed in the future
         self.config = self.parseMainConf()
-        self.__fixLocalDir()
         database_file = os.path.expanduser(os.path.join(self.getConfig('', 'local_dir'), C.SAVEFILE_DATABASE))
         self.storage = SqliteStorage(database_file, host.__version__)
         PersistentDict.storage = self.storage
@@ -140,37 +184,6 @@
             log.error(_("Can't read main config !"))
         return config
 
-    # XXX: tmp update code, will be removed in the future
-    # When you remove this, please also remove sat.core.constants.Const.DEFAULT_LOCAL_DIR
-    # and add the default value for 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG
-    def __fixLocalDir(self):
-        """Retro-compatibility with the previous local_dir default value."""
-        if self.getConfig('', 'local_dir'):
-            return  # nothing to do
-        old_default = '~/.sat'
-        if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE):
-            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 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
-            log.warning(_("Auto-update: local_dir set to %(path)s in the file %(config_file)s") % {'path': old_default, 'config_file': file_})
-            default = old_default
-        else:  # use the new default local_dir
-            default = C.DEFAULT_LOCAL_DIR
-        self.config.set('', 'local_dir', default)  # for the currently running instance
-
     def getConfig(self, section, name):
         """Get the main configuration option
         @param section: section of the config file (None or '' for DEFAULT)