Ejemplo n.º 1
0
def split_db():
    print('spliting database')
    if os.name == 'nt':
        try:
            import configpaths
            OLD_LOG_DB_FOLDER = os.path.join(configpaths.fse(
                os.environ['appdata']), 'Gajim')
        except KeyError:
            OLD_LOG_DB_FOLDER = '.'
    else:
        OLD_LOG_DB_FOLDER = os.path.expanduser('~/.gajim')

    tmp = logger.CACHE_DB_PATH
    logger.CACHE_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, 'cache.db')
    create_cache_db()
    back = os.getcwd()
    os.chdir(OLD_LOG_DB_FOLDER)
    con = sqlite.connect('logs.db')
    os.chdir(back)
    cur = con.cursor()
    cur.execute('''SELECT name FROM sqlite_master WHERE type = 'table';''')
    tables = cur.fetchall() # we get [('jids',), ('unread_messages',), ...
    tables = [t[0] for t in tables]
    cur.execute("ATTACH DATABASE '%s' AS cache" % logger.CACHE_DB_PATH)
    for table in ('caps_cache', 'rooms_last_message_time', 'roster_entry',
    'roster_group', 'transports_cache'):
        if table not in tables:
            continue
        try:
            cur.executescript(
                    'INSERT INTO cache.%s SELECT * FROM %s;' % (table, table))
            con.commit()
            cur.executescript('DROP TABLE %s;' % table)
            con.commit()
        except sqlite.OperationalError as e:
            print('error moving table %s to cache.db: %s' % (table, str(e)),
                file=sys.stderr)
    con.close()
    logger.CACHE_DB_PATH = tmp
Ejemplo n.º 2
0
def check_and_possibly_move_config():
    LOG_DB_PATH = logger.LOG_DB_PATH
    CACHE_DB_PATH = logger.CACHE_DB_PATH
    vars = {}
    vars['VCARD_PATH'] = gajim.VCARD_PATH
    vars['AVATAR_PATH'] = gajim.AVATAR_PATH
    vars['MY_EMOTS_PATH'] = gajim.MY_EMOTS_PATH
    vars['MY_ICONSETS_PATH'] = gajim.MY_ICONSETS_PATH
    vars['MY_MOOD_ICONSETS_PATH'] = gajim.MY_MOOD_ICONSETS_PATH
    vars['MY_ACTIVITY_ICONSETS_PATH'] = gajim.MY_ACTIVITY_ICONSETS_PATH
    from common import configpaths
    MY_DATA = configpaths.gajimpaths['MY_DATA']
    MY_CONFIG = configpaths.gajimpaths['MY_CONFIG']
    MY_CACHE = configpaths.gajimpaths['MY_CACHE']

    if os.path.exists(LOG_DB_PATH):
        # File already exists
        return

    if os.name == 'nt':
        try:
            OLD_LOG_DB_FOLDER = os.path.join(configpaths.fse(
                os.environ['appdata']), 'Gajim')
        except KeyError:
            OLD_LOG_DB_FOLDER = '.'
    else:
        OLD_LOG_DB_FOLDER = os.path.expanduser('~/.gajim')
    if not os.path.exists(OLD_LOG_DB_FOLDER):
        return
    OLD_LOG_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, 'logs.db')
    OLD_CACHE_DB_PATH = os.path.join(OLD_LOG_DB_FOLDER, 'cache.db')
    vars['OLD_VCARD_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, 'vcards')
    vars['OLD_AVATAR_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, 'avatars')
    vars['OLD_MY_EMOTS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, 'emoticons')
    vars['OLD_MY_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, 'iconsets')
    vars['OLD_MY_MOOD_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER, 'moods')
    vars['OLD_MY_ACTIVITY_ICONSETS_PATH'] = os.path.join(OLD_LOG_DB_FOLDER,
            'activities')
    OLD_CONFIG_FILES = []
    OLD_DATA_FILES = []
    for f in os.listdir(OLD_LOG_DB_FOLDER):
        if f == 'config' or f.startswith('config.'):
            OLD_CONFIG_FILES.append(f)
        if f == 'secrets' or f.startswith('secrets.'):
            OLD_DATA_FILES.append(f)
        if f == 'cacerts.pem':
            OLD_DATA_FILES.append(f)

    if not os.path.exists(OLD_LOG_DB_PATH):
        return

    if not os.path.exists(OLD_CACHE_DB_PATH):
        # split database
        split_db()

    to_move = {}
    to_move[OLD_LOG_DB_PATH] = LOG_DB_PATH
    to_move[OLD_CACHE_DB_PATH] = CACHE_DB_PATH

    for folder in ('VCARD_PATH', 'AVATAR_PATH', 'MY_EMOTS_PATH',
    'MY_ICONSETS_PATH', 'MY_MOOD_ICONSETS_PATH', 'MY_ACTIVITY_ICONSETS_PATH'):
        src = vars['OLD_' + folder]
        dst = vars[folder]
        to_move[src] = dst

    # move config files
    for f in OLD_CONFIG_FILES:
        src = os.path.join(OLD_LOG_DB_FOLDER, f)
        dst = os.path.join(MY_CONFIG, f)
        to_move[src] = dst

    # Move data files (secrets, cacert.pem)
    for f in OLD_DATA_FILES:
        src = os.path.join(OLD_LOG_DB_FOLDER, f)
        dst = os.path.join(MY_DATA, f)
        to_move[src] = dst

    for src, dst in to_move.items():
        if os.path.exists(dst):
            continue
        if not os.path.exists(src):
            continue
        print(_('moving %s to %s') % (src, dst))
        shutil.move(src, dst)
    gajim.logger.init_vars()
    gajim.logger.attach_cache_database()