Package common :: Module check_paths
[hide private]
[frames] | no frames]

Source Code for Module common.check_paths

  1  # -*- coding:utf-8 -*- 
  2  ## src/common/check_paths.py 
  3  ## 
  4  ## Copyright (C) 2005-2006 Travis Shirk <travis AT pobox.com> 
  5  ##                         Nikos Kouremenos <kourem AT gmail.com> 
  6  ## Copyright (C) 2005-2010 Yann Leboulanger <asterix AT lagaule.org> 
  7  ## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com> 
  8  ## Copyright (C) 2007 Tomasz Melcer <liori AT exroot.org> 
  9  ## Copyright (C) 2008 Jean-Marie Traissard <jim AT lapin.org> 
 10  ## 
 11  ## This file is part of Gajim. 
 12  ## 
 13  ## Gajim is free software; you can redistribute it and/or modify 
 14  ## it under the terms of the GNU General Public License as published 
 15  ## by the Free Software Foundation; version 3 only. 
 16  ## 
 17  ## Gajim is distributed in the hope that it will be useful, 
 18  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 20  ## GNU General Public License for more details. 
 21  ## 
 22  ## You should have received a copy of the GNU General Public License 
 23  ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 
 24  ## 
 25   
 26  import os 
 27  import sys 
 28  import stat 
 29   
 30  from common import gajim 
 31  import logger 
 32   
 33  # DO NOT MOVE ABOVE OF import gajim 
 34  import sqlite3 as sqlite 
 35   
36 -def create_log_db():
37 print _('creating logs database') 38 con = sqlite.connect(logger.LOG_DB_PATH) 39 os.chmod(logger.LOG_DB_PATH, 0600) # rw only for us 40 cur = con.cursor() 41 # create the tables 42 # kind can be 43 # status, gcstatus, gc_msg, (we only recv for those 3), 44 # single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent 45 # to meet all our needs 46 # logs.jid_id --> jids.jid_id but Sqlite doesn't do FK etc so it's done in python code 47 # jids.jid text column will be JID if TC-related, room_jid if GC-related, 48 # ROOM_JID/nick if pm-related. 49 # also check optparser.py, which updates databases on gajim updates 50 cur.executescript( 51 ''' 52 CREATE TABLE jids( 53 jid_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, 54 jid TEXT UNIQUE, 55 type INTEGER 56 ); 57 58 CREATE TABLE unread_messages( 59 message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, 60 jid_id INTEGER, 61 shown BOOLEAN default 0 62 ); 63 64 CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id); 65 66 CREATE TABLE logs( 67 log_line_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, 68 jid_id INTEGER, 69 contact_name TEXT, 70 time INTEGER, 71 kind INTEGER, 72 show INTEGER, 73 message TEXT, 74 subject TEXT 75 ); 76 77 CREATE INDEX idx_logs_jid_id_time ON logs (jid_id, time DESC); 78 ''' 79 ) 80 81 con.commit() 82 con.close()
83
84 -def create_cache_db():
85 print _('creating cache database') 86 con = sqlite.connect(logger.CACHE_DB_PATH) 87 os.chmod(logger.CACHE_DB_PATH, 0600) # rw only for us 88 cur = con.cursor() 89 cur.executescript( 90 ''' 91 CREATE TABLE transports_cache ( 92 transport TEXT UNIQUE, 93 type INTEGER 94 ); 95 96 CREATE TABLE caps_cache ( 97 hash_method TEXT, 98 hash TEXT, 99 data BLOB, 100 last_seen INTEGER); 101 102 CREATE TABLE rooms_last_message_time( 103 jid_id INTEGER PRIMARY KEY UNIQUE, 104 time INTEGER 105 ); 106 107 CREATE TABLE IF NOT EXISTS roster_entry( 108 account_jid_id INTEGER, 109 jid_id INTEGER, 110 name TEXT, 111 subscription INTEGER, 112 ask BOOLEAN, 113 PRIMARY KEY (account_jid_id, jid_id) 114 ); 115 116 CREATE TABLE IF NOT EXISTS roster_group( 117 account_jid_id INTEGER, 118 jid_id INTEGER, 119 group_name TEXT, 120 PRIMARY KEY (account_jid_id, jid_id, group_name) 121 ); 122 ''' 123 ) 124 125 con.commit() 126 con.close()
127
128 -def split_db():
129 print 'spliting database' 130 if os.name == 'nt': 131 try: 132 import configpaths 133 OLD_LOG_DB_FOLDER = os.path.join(configpaths.fse( 134 os.environ[u'appdata']), u'Gajim') 135 except KeyError: 136 OLD_LOG_DB_FOLDER = u'.' 137 else: 138 OLD_LOG_DB_FOLDER = os.path.expanduser(u'~/.gajim') 139 140 tmp = logger.CACHE_DB_PATH 141 logger.CACHE_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, 'cache.db') 142 create_cache_db() 143 back = os.getcwd() 144 os.chdir(OLD_LOG_DB_FOLDER) 145 con = sqlite.connect('logs.db') 146 os.chdir(back) 147 cur = con.cursor() 148 cur.execute('''SELECT name FROM sqlite_master WHERE type = 'table';''') 149 tables = cur.fetchall() # we get [(u'jids',), (u'unread_messages',), ... 150 tables = [t[0] for t in tables] 151 cur.execute("ATTACH DATABASE '%s' AS cache" % logger.CACHE_DB_PATH) 152 for table in ('caps_cache', 'rooms_last_message_time', 'roster_entry', 153 'roster_group', 'transports_cache'): 154 if table not in tables: 155 continue 156 try: 157 cur.executescript( 158 'INSERT INTO cache.%s SELECT * FROM %s;' % (table, table)) 159 con.commit() 160 cur.executescript('DROP TABLE %s;' % table) 161 con.commit() 162 except sqlite.OperationalError, e: 163 print >> sys.stderr, 'error moving table %s to cache.db: %s' % \ 164 (table, str(e)) 165 con.close() 166 logger.CACHE_DB_PATH = tmp
167
168 -def check_and_possibly_move_config():
169 LOG_DB_PATH = logger.LOG_DB_PATH 170 CACHE_DB_PATH = logger.CACHE_DB_PATH 171 vars = {} 172 vars['VCARD_PATH'] = gajim.VCARD_PATH 173 vars['AVATAR_PATH'] = gajim.AVATAR_PATH 174 vars['MY_EMOTS_PATH'] = gajim.MY_EMOTS_PATH 175 vars['MY_ICONSETS_PATH'] = gajim.MY_ICONSETS_PATH 176 vars['MY_MOOD_ICONSETS_PATH'] = gajim.MY_MOOD_ICONSETS_PATH 177 vars['MY_ACTIVITY_ICONSETS_PATH'] = gajim.MY_ACTIVITY_ICONSETS_PATH 178 import configpaths 179 MY_DATA = configpaths.gajimpaths['MY_DATA'] 180 MY_CONFIG = configpaths.gajimpaths['MY_CONFIG'] 181 MY_CACHE = configpaths.gajimpaths['MY_CACHE'] 182 183 if os.path.exists(LOG_DB_PATH): 184 # File already exists 185 return 186 187 if os.name == 'nt': 188 try: 189 OLD_LOG_DB_FOLDER = os.path.join(configpaths.fse( 190 os.environ[u'appdata']), u'Gajim') 191 except KeyError: 192 OLD_LOG_DB_FOLDER = u'.' 193 else: 194 OLD_LOG_DB_FOLDER = os.path.expanduser(u'~/.gajim') 195 if not os.path.exists(OLD_LOG_DB_FOLDER): 196 return 197 OLD_LOG_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, u'logs.db') 198 OLD_CACHE_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, u'cache.db') 199 vars['OLD_VCARD_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'vcards') 200 vars['OLD_AVATAR_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'avatars') 201 vars['OLD_MY_EMOTS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'emoticons') 202 vars['OLD_MY_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'iconsets') 203 vars['OLD_MY_MOOD_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, u'moods') 204 vars['OLD_MY_ACTIVITY_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, 205 u'activities') 206 OLD_CONFIG_FILES = [] 207 OLD_DATA_FILES = [] 208 for f in os.listdir(OLD_LOG_DB_FOLDER): 209 if f == 'config' or f.startswith('config.'): 210 OLD_CONFIG_FILES.append(f) 211 if f == 'secrets' or f.startswith('secrets.'): 212 OLD_DATA_FILES.append(f) 213 if f == 'cacerts.pem': 214 OLD_DATA_FILES.append(f) 215 216 if not os.path.exists(OLD_LOG_DB_PATH): 217 return 218 219 if not os.path.exists(OLD_CACHE_DB_PATH): 220 # split database 221 split_db() 222 223 to_move = {} 224 to_move[OLD_LOG_DB_PATH] = LOG_DB_PATH 225 to_move[OLD_CACHE_DB_PATH] = CACHE_DB_PATH 226 227 for folder in ('VCARD_PATH', 'AVATAR_PATH', 'MY_EMOTS_PATH', 228 'MY_ICONSETS_PATH', 'MY_MOOD_ICONSETS_PATH', 'MY_ACTIVITY_ICONSETS_PATH'): 229 src = vars['OLD_' + folder] 230 dst = vars[folder] 231 to_move[src] = dst 232 233 # move config files 234 for f in OLD_CONFIG_FILES: 235 src = os.path.join(OLD_LOG_DB_FOLDER, f) 236 dst = os.path.join(MY_CONFIG, f) 237 to_move[src] = dst 238 239 # Move data files (secrets, cacert.pem) 240 for f in OLD_DATA_FILES: 241 src = os.path.join(OLD_LOG_DB_FOLDER, f) 242 dst = os.path.join(MY_DATA, f) 243 to_move[src] = dst 244 245 for src, dst in to_move.items(): 246 if os.path.exists(dst): 247 continue 248 if not os.path.exists(src): 249 continue 250 print 'moving %s to %s' % (src, dst) 251 os.renames(src, dst) 252 gajim.logger.init_vars() 253 gajim.logger.attach_cache_database()
254
255 -def check_and_possibly_create_paths():
256 check_and_possibly_move_config() 257 258 LOG_DB_PATH = logger.LOG_DB_PATH 259 LOG_DB_FOLDER, LOG_DB_FILE = os.path.split(LOG_DB_PATH) 260 261 CACHE_DB_PATH = logger.CACHE_DB_PATH 262 CACHE_DB_FOLDER, CACHE_DB_FILE = os.path.split(CACHE_DB_PATH) 263 264 VCARD_PATH = gajim.VCARD_PATH 265 AVATAR_PATH = gajim.AVATAR_PATH 266 import configpaths 267 MY_DATA = configpaths.gajimpaths['MY_DATA'] 268 MY_CONFIG = configpaths.gajimpaths['MY_CONFIG'] 269 MY_CACHE = configpaths.gajimpaths['MY_CACHE'] 270 271 PLUGINS_CONFIG_PATH = gajim.PLUGINS_CONFIG_DIR 272 273 if not os.path.exists(MY_DATA): 274 create_path(MY_DATA) 275 elif os.path.isfile(MY_DATA): 276 print _('%s is a file but it should be a directory') % MY_DATA 277 print _('Gajim will now exit') 278 sys.exit() 279 280 if not os.path.exists(MY_CONFIG): 281 create_path(MY_CONFIG) 282 elif os.path.isfile(MY_CONFIG): 283 print _('%s is a file but it should be a directory') % MY_CONFIG 284 print _('Gajim will now exit') 285 sys.exit() 286 287 if not os.path.exists(MY_CACHE): 288 create_path(MY_CACHE) 289 elif os.path.isfile(MY_CACHE): 290 print _('%s is a file but it should be a directory') % MY_CACHE 291 print _('Gajim will now exit') 292 sys.exit() 293 294 if not os.path.exists(VCARD_PATH): 295 create_path(VCARD_PATH) 296 elif os.path.isfile(VCARD_PATH): 297 print _('%s is a file but it should be a directory') % VCARD_PATH 298 print _('Gajim will now exit') 299 sys.exit() 300 301 if not os.path.exists(AVATAR_PATH): 302 create_path(AVATAR_PATH) 303 elif os.path.isfile(AVATAR_PATH): 304 print _('%s is a file but it should be a directory') % AVATAR_PATH 305 print _('Gajim will now exit') 306 sys.exit() 307 308 if not os.path.exists(LOG_DB_FOLDER): 309 create_path(LOG_DB_FOLDER) 310 elif os.path.isfile(LOG_DB_FOLDER): 311 print _('%s is a file but it should be a directory') % LOG_DB_FOLDER 312 print _('Gajim will now exit') 313 sys.exit() 314 315 if not os.path.exists(LOG_DB_PATH): 316 create_log_db() 317 gajim.logger.init_vars() 318 elif os.path.isdir(LOG_DB_PATH): 319 print _('%s is a directory but should be a file') % LOG_DB_PATH 320 print _('Gajim will now exit') 321 sys.exit() 322 323 if not os.path.exists(CACHE_DB_FOLDER): 324 create_path(CACHE_DB_FOLDER) 325 elif os.path.isfile(CACHE_DB_FOLDER): 326 print _('%s is a file but it should be a directory') % CACHE_DB_FOLDER 327 print _('Gajim will now exit') 328 sys.exit() 329 330 if not os.path.exists(CACHE_DB_PATH): 331 create_cache_db() 332 gajim.logger.attach_cache_database() 333 elif os.path.isdir(CACHE_DB_PATH): 334 print _('%s is a directory but should be a file') % CACHE_DB_PATH 335 print _('Gajim will now exit') 336 sys.exit() 337 338 if not os.path.exists(PLUGINS_CONFIG_PATH): 339 create_path(PLUGINS_CONFIG_PATH) 340 elif os.path.isfile(PLUGINS_CONFIG_PATH): 341 print _('%s is a file but it should be a directory') % PLUGINS_CONFIG_PATH 342 print _('Gajim will now exit') 343 sys.exit()
344
345 -def create_path(directory):
346 head, tail = os.path.split(directory) 347 if not os.path.exists(head): 348 create_path(head) 349 if os.path.exists(directory): 350 return 351 print _('creating %s directory') % directory 352 os.mkdir(directory, 0700)
353