comparison 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
comparison
equal deleted inserted replaced
1002:291eb8216f6e 1003:52ec79aa5bbe
105 105
106 def iterkeys(self): 106 def iterkeys(self):
107 return self._sessions.iterkeys() 107 return self._sessions.iterkeys()
108 108
109 109
110 # XXX: tmp update code, will be removed in the future
111 # When you remove this, please add the default value for
112 # 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG
113 def fixLocalDir(silent=True):
114 """Retro-compatibility with the previous local_dir default value.
115
116 @param silent (boolean): toggle logging output (must be True when called from sat.sh)
117 """
118 user_config = SafeConfigParser()
119 try:
120 user_config.read(C.CONFIG_FILES)
121 except:
122 pass # file is readable but its structure if wrong
123 try:
124 current_value = user_config.get('DEFAULT', 'local_dir')
125 except (NoOptionError, NoSectionError):
126 current_value = ''
127 if current_value:
128 return # nothing to do
129 old_default = '~/.sat'
130 if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE):
131 if not silent:
132 log.warning(_("A database has been found in the default local_dir for previous versions (< 0.5)"))
133 config = SafeConfigParser()
134 target_file = None
135 for file_ in C.CONFIG_FILES[::-1]:
136 # we will eventually update the existing file with the highest priority, if it's a user personal file...
137 if not silent:
138 log.debug(_("Testing file %s") % file_)
139 if os.path.isfile(file_):
140 if file_.startswith(os.path.expanduser('~')):
141 config.read([file_])
142 target_file = file_
143 break
144 if not target_file:
145 # ... otherwise we create a new config file for that user
146 target_file = BaseDirectory.save_config_path('sat') + '/sat.conf'
147 config.set('', 'local_dir', old_default)
148 with open(target_file, 'wb') as configfile:
149 config.write(configfile) # for the next time that user launches sat
150 if not silent:
151 log.warning(_("Auto-update: local_dir set to %(path)s in the file %(config_file)s") % {'path': old_default, 'config_file': target_file})
152
153
110 class Memory(object): 154 class Memory(object):
111 """This class manage all persistent informations""" 155 """This class manage all persistent informations"""
112 156
113 def __init__(self, host): 157 def __init__(self, host):
114 log.info(_("Memory manager init")) 158 log.info(_("Memory manager init"))
116 self.host = host 160 self.host = host
117 self._entities_cache = {} # XXX: keep presence/last resource/other data in cache 161 self._entities_cache = {} # XXX: keep presence/last resource/other data in cache
118 # /!\ an entity is not necessarily in roster 162 # /!\ an entity is not necessarily in roster
119 self.subscriptions = {} 163 self.subscriptions = {}
120 self.disco = Discovery(host) 164 self.disco = Discovery(host)
165 fixLocalDir(False) # XXX: tmp update code, will be removed in the future
121 self.config = self.parseMainConf() 166 self.config = self.parseMainConf()
122 self.__fixLocalDir()
123 database_file = os.path.expanduser(os.path.join(self.getConfig('', 'local_dir'), C.SAVEFILE_DATABASE)) 167 database_file = os.path.expanduser(os.path.join(self.getConfig('', 'local_dir'), C.SAVEFILE_DATABASE))
124 self.storage = SqliteStorage(database_file, host.__version__) 168 self.storage = SqliteStorage(database_file, host.__version__)
125 PersistentDict.storage = self.storage 169 PersistentDict.storage = self.storage
126 self.params = Params(host, self.storage) 170 self.params = Params(host, self.storage)
127 log.info(_("Loading default params template")) 171 log.info(_("Loading default params template"))
137 try: 181 try:
138 config.read(C.CONFIG_FILES) 182 config.read(C.CONFIG_FILES)
139 except: 183 except:
140 log.error(_("Can't read main config !")) 184 log.error(_("Can't read main config !"))
141 return config 185 return config
142
143 # XXX: tmp update code, will be removed in the future
144 # When you remove this, please also remove sat.core.constants.Const.DEFAULT_LOCAL_DIR
145 # and add the default value for 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG
146 def __fixLocalDir(self):
147 """Retro-compatibility with the previous local_dir default value."""
148 if self.getConfig('', 'local_dir'):
149 return # nothing to do
150 old_default = '~/.sat'
151 if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE):
152 log.warning(_("A database has been found in the default local_dir for previous versions (< 0.5)"))
153 config = SafeConfigParser()
154 target_file = None
155 for file_ in C.CONFIG_FILES[::-1]:
156 # we will eventually update the existing file with the highest priority, if it's a user personal file...
157 if os.path.isfile(file_):
158 if file_.startswith(os.path.expanduser('~')):
159 config.read([file_])
160 target_file = file_
161 break
162 if not target_file:
163 # ... otherwise we create a new config file for that user
164 target_file = BaseDirectory.save_config_path('sat') + '/sat.conf'
165 config.set('', 'local_dir', old_default)
166 with open(target_file, 'wb') as configfile:
167 config.write(configfile) # for the next time that user launches sat
168 log.warning(_("Auto-update: local_dir set to %(path)s in the file %(config_file)s") % {'path': old_default, 'config_file': file_})
169 default = old_default
170 else: # use the new default local_dir
171 default = C.DEFAULT_LOCAL_DIR
172 self.config.set('', 'local_dir', default) # for the currently running instance
173 186
174 def getConfig(self, section, name): 187 def getConfig(self, section, name):
175 """Get the main configuration option 188 """Get the main configuration option
176 @param section: section of the config file (None or '' for DEFAULT) 189 @param section: section of the config file (None or '' for DEFAULT)
177 @param name: name of the option 190 @param name: name of the option