Commit 848c5cbd authored by Philipp Hörist's avatar Philipp Hörist

More refactoring around paths

- Remove old config dir migration
- Remove some unused config paths
- Rewrite configpaths.init()
- Simplify creating paths in check_paths
parent 72e69a59
......@@ -66,7 +66,6 @@ logger = None
# some plugins use that
gajimpaths = configpaths.gajimpaths
VCARD_PATH = configpaths.get('VCARD')
AVATAR_PATH = configpaths.get('AVATAR')
MY_EMOTS_PATH = configpaths.get('MY_EMOTS')
MY_ICONSETS_PATH = configpaths.get('MY_ICONSETS')
......@@ -74,7 +73,6 @@ MY_MOOD_ICONSETS_PATH = configpaths.get('MY_MOOD_ICONSETS')
MY_ACTIVITY_ICONSETS_PATH = configpaths.get('MY_ACTIVITY_ICONSETS')
MY_CACERTS = configpaths.get('MY_CACERTS')
MY_PEER_CERTS_PATH = configpaths.get('MY_PEER_CERTS')
TMP = configpaths.get('TMP')
DATA_DIR = configpaths.get('DATA')
ICONS_DIR = configpaths.get('ICONS')
HOME_DIR = configpaths.get('HOME')
......
......@@ -24,18 +24,18 @@
##
import os
import shutil
import sys
import sqlite3
from gajim.common import app
from gajim.common import logger
from gajim.common import configpaths
from gajim.common.const import PathType
# DO NOT MOVE ABOVE OF import gajim
import sqlite3 as sqlite
def create_log_db():
print(_('creating logs database'))
con = sqlite.connect(logger.LOG_DB_PATH)
con = sqlite3.connect(logger.LOG_DB_PATH)
os.chmod(logger.LOG_DB_PATH, 0o600) # rw only for us
cur = con.cursor()
# create the tables
......@@ -96,7 +96,7 @@ def create_log_db():
def create_cache_db():
print(_('creating cache database'))
con = sqlite.connect(logger.CACHE_DB_PATH)
con = sqlite3.connect(logger.CACHE_DB_PATH)
os.chmod(logger.CACHE_DB_PATH, 0o600) # rw only for us
cur = con.cursor()
cur.executescript(
......@@ -154,7 +154,7 @@ def split_db():
create_cache_db()
back = os.getcwd()
os.chdir(OLD_LOG_DB_FOLDER)
con = sqlite.connect('logs.db')
con = sqlite3.connect('logs.db')
os.chdir(back)
cur = con.cursor()
cur.execute('''SELECT name FROM sqlite_master WHERE type = 'table';''')
......@@ -171,97 +171,12 @@ def split_db():
con.commit()
cur.executescript('DROP TABLE %s;' % table)
con.commit()
except sqlite.OperationalError as e:
except sqlite3.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
def check_and_possibly_move_config():
LOG_DB_PATH = logger.LOG_DB_PATH
CACHE_DB_PATH = logger.CACHE_DB_PATH
vars = {}
vars['VCARD_PATH'] = app.VCARD_PATH
vars['AVATAR_PATH'] = app.AVATAR_PATH
vars['MY_EMOTS_PATH'] = app.MY_EMOTS_PATH
vars['MY_ICONSETS_PATH'] = app.MY_ICONSETS_PATH
vars['MY_MOOD_ICONSETS_PATH'] = app.MY_MOOD_ICONSETS_PATH
vars['MY_ACTIVITY_ICONSETS_PATH'] = app.MY_ACTIVITY_ICONSETS_PATH
from gajim.common import configpaths
MY_DATA = configpaths.get('MY_DATA')
MY_CONFIG = configpaths.get('MY_CONFIG')
if os.path.exists(LOG_DB_PATH):
# File already exists
return
if os.name == 'nt':
try:
OLD_LOG_DB_FOLDER = os.path.join(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 %(src)s to %(dst)s') % {'src': src, 'dst': dst})
shutil.move(src, dst)
app.logger.init_vars()
app.logger.attach_cache_database()
def check_and_possibly_create_paths():
LOG_DB_PATH = logger.LOG_DB_PATH
LOG_DB_FOLDER, LOG_DB_FILE = os.path.split(LOG_DB_PATH)
......@@ -269,51 +184,13 @@ def check_and_possibly_create_paths():
CACHE_DB_PATH = logger.CACHE_DB_PATH
CACHE_DB_FOLDER, CACHE_DB_FILE = os.path.split(CACHE_DB_PATH)
VCARD_PATH = app.VCARD_PATH
AVATAR_PATH = app.AVATAR_PATH
from gajim.common import configpaths
MY_DATA = configpaths.get('MY_DATA')
MY_CONFIG = configpaths.get('MY_CONFIG')
MY_CACHE = configpaths.get('MY_CACHE')
XTLS_CERTS = configpaths.get('MY_PEER_CERTS')
LOCAL_XTLS_CERTS = configpaths.get('MY_CERT')
PLUGINS_CONFIG_PATH = app.PLUGINS_CONFIG_DIR
if not os.path.exists(MY_DATA):
create_path(MY_DATA)
elif os.path.isfile(MY_DATA):
print(_('%s is a file but it should be a directory') % MY_DATA)
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(MY_CONFIG):
create_path(MY_CONFIG)
elif os.path.isfile(MY_CONFIG):
print(_('%s is a file but it should be a directory') % MY_CONFIG)
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(MY_CACHE):
create_path(MY_CACHE)
elif os.path.isfile(MY_CACHE):
print(_('%s is a file but it should be a directory') % MY_CACHE)
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(VCARD_PATH):
create_path(VCARD_PATH)
elif os.path.isfile(VCARD_PATH):
print(_('%s is a file but it should be a directory') % VCARD_PATH)
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(AVATAR_PATH):
create_path(AVATAR_PATH)
elif os.path.isfile(AVATAR_PATH):
print(_('%s is a file but it should be a directory') % AVATAR_PATH)
print(_('Gajim will now exit'))
sys.exit()
for path in configpaths.get_paths(PathType.FOLDER):
if not os.path.exists(path):
create_path(path)
elif os.path.isfile(path):
print(_('%s is a file but it should be a directory') % path)
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(LOG_DB_FOLDER):
create_path(LOG_DB_FOLDER)
......@@ -322,13 +199,6 @@ def check_and_possibly_create_paths():
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(PLUGINS_CONFIG_PATH):
create_path(PLUGINS_CONFIG_PATH)
elif os.path.isfile(PLUGINS_CONFIG_PATH):
print(_('%s is a file but it should be a directory') % PLUGINS_CONFIG_PATH)
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(CACHE_DB_FOLDER):
create_path(CACHE_DB_FOLDER)
elif os.path.isfile(CACHE_DB_FOLDER):
......@@ -336,8 +206,6 @@ def check_and_possibly_create_paths():
print(_('Gajim will now exit'))
sys.exit()
check_and_possibly_move_config()
if not os.path.exists(LOG_DB_PATH):
if os.path.exists(CACHE_DB_PATH):
os.remove(CACHE_DB_PATH)
......@@ -356,11 +224,6 @@ def check_and_possibly_create_paths():
print(_('Gajim will now exit'))
sys.exit()
if not os.path.exists(XTLS_CERTS):
create_path(XTLS_CERTS)
if not os.path.exists(LOCAL_XTLS_CERTS):
create_path(LOCAL_XTLS_CERTS)
def create_path(directory):
head, tail = os.path.split(directory)
if not os.path.exists(head):
......
......@@ -6,6 +6,7 @@
## Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
## Copyright (C) 2007 Brendan Taylor <whateley AT gmail.com>
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
## Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
##
## This file is part of Gajim.
##
......@@ -23,28 +24,23 @@
##
import os
import sys
import tempfile
from enum import Enum, unique
@unique
class Type(Enum):
CONFIG = 0
CACHE = 1
DATA = 2
def windowsify(s):
if os.name == 'nt':
return s.capitalize()
return s
from gajim.common.const import PathType, PathLocation
def get(key):
return _paths[key]
def get_paths(type_):
for key, value in _paths.items():
location, path, path_type = value
if type_ != path_type:
continue
yield _paths[key]
def set_separation(active: bool):
_paths.profile_separation = active
......@@ -63,7 +59,7 @@ def init():
class ConfigPaths:
def __init__(self):
self.paths = {}
self._paths = {}
self.profile = ''
self.profile_separation = False
self.custom_config_root = None
......@@ -93,104 +89,93 @@ class ConfigPaths:
import pkg_resources
basedir = pkg_resources.resource_filename("gajim", ".")
self.add('DATA', None, os.path.join(basedir, 'data'))
self.add('GUI', None, os.path.join(basedir, 'data', 'gui'))
self.add('ICONS', None, os.path.join(basedir, 'data', 'icons'))
self.add('HOME', None, os.path.expanduser('~'))
self.add('PLUGINS_BASE', None, os.path.join(basedir, 'data', 'plugins'))
def add(self, name, type_, path):
self.paths[name] = (type_, path)
source_paths = [
('DATA', os.path.join(basedir, 'data')),
('GUI', os.path.join(basedir, 'data', 'gui')),
('ICONS', os.path.join(basedir, 'data', 'icons')),
('HOME', os.path.expanduser('~')),
('PLUGINS_BASE', os.path.join(basedir, 'data', 'plugins')),
]
for path in source_paths:
self._add(*path)
def __getitem__(self, key):
type_, path = self.paths[key]
if type_ == Type.CONFIG:
location, path, _ = self._paths[key]
if location == PathLocation.CONFIG:
return os.path.join(self.config_root, path)
elif type_ == Type.CACHE:
elif location == PathLocation.CACHE:
return os.path.join(self.cache_root, path)
elif type_ == Type.DATA:
elif location == PathLocation.DATA:
return os.path.join(self.data_root, path)
return path
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
def items(self):
for key in self.paths.keys():
yield (key, self[key])
for key, value in self._paths.items():
yield (key, value)
def _prepare(self, path, unique):
if os.name == 'nt':
path = path.capitalize()
if self.profile:
if unique or self.profile_separation:
return '%s.%s' % (path, self.profile)
return path
def _add(self, name, path, location=None, path_type=None, unique=False):
if location is not None:
path = self._prepare(path, unique)
self._paths[name] = (location, path, path_type)
def init(self):
if self.custom_config_root:
self.cache_root = self.data_root = self.config_root = self.custom_config_root
self.add('CONFIG_ROOT', None, self.config_root)
self.add('CACHE_ROOT', None, self.cache_root)
self.add('DATA_ROOT', None, self.data_root)
self.init_profile(self.profile)
if len(self.profile) > 0 and self.profile_separation:
self.profile = u'.' + self.profile
else:
self.profile = ''
d = {'LOG_DB': 'logs.db', 'MY_CACERTS': 'cacerts.pem',
'MY_EMOTS': 'emoticons', 'MY_ICONSETS': 'iconsets',
'MY_MOOD_ICONSETS': 'moods', 'MY_ACTIVITY_ICONSETS': 'activities',
'PLUGINS_USER': 'plugins'}
for name in d:
d[name] += self.profile
self.add(name, Type.DATA, windowsify(d[name]))
if len(self.profile):
self.add('MY_DATA', Type.DATA, 'data.dir')
else:
self.add('MY_DATA', Type.DATA, '')
d = {'CACHE_DB': 'cache.db',
'VCARD': 'vcards',
'AVATAR': 'avatars'}
for name in d:
d[name] += self.profile
self.add(name, Type.CACHE, windowsify(d[name]))
if len(self.profile):
self.add('MY_CACHE', Type.CACHE, 'cache.dir')
else:
self.add('MY_CACHE', Type.CACHE, '')
if len(self.profile):
self.add('MY_CONFIG', Type.CONFIG, 'config.dir')
else:
self.add('MY_CONFIG', Type.CONFIG, '')
try:
self.add('TMP', None, tempfile.gettempdir())
except IOError as e:
print('Error opening tmp folder: %s\nUsing %s' % (
str(e), os.path.expanduser('~')), file=sys.stderr)
self.add('TMP', None, os.path.expanduser('~'))
def init_profile(self, profile):
conffile = windowsify('config')
secretsfile = windowsify('secrets')
pluginsconfdir = windowsify('pluginsconfig')
certsdir = windowsify(u'certs')
localcertsdir = windowsify(u'localcerts')
if len(profile) > 0:
conffile += '.' + profile
secretsfile += '.' + profile
pluginsconfdir += '.' + profile
certsdir += u'.' + profile
localcertsdir += u'.' + profile
self.add('SECRETS_FILE', Type.DATA, secretsfile)
self.add('MY_PEER_CERTS', Type.DATA, certsdir)
self.add('CONFIG_FILE', Type.CONFIG, conffile)
self.add('PLUGINS_CONFIG_DIR', Type.CONFIG, pluginsconfdir)
self.add('MY_CERT', Type.CONFIG, localcertsdir)
user_dir_paths = [
('TMP', tempfile.gettempdir()),
('CONFIG_ROOT', self.config_root),
('CACHE_ROOT', self.cache_root),
('DATA_ROOT', self.data_root),
]
for path in user_dir_paths:
self._add(*path)
# These paths are unique per profile
unique_profile_paths = [
# Data paths
('SECRETS_FILE', 'secrets', PathLocation.DATA, PathType.FILE),
('MY_PEER_CERTS', 'certs', PathLocation.DATA, PathType.FOLDER),
# Config paths
('CONFIG_FILE', 'config', PathLocation.CONFIG, PathType.FILE),
('PLUGINS_CONFIG_DIR', 'pluginsconfig', PathLocation.CONFIG, PathType.FOLDER),
('MY_CERT', 'localcerts', PathLocation.CONFIG, PathType.FOLDER),
]
for path in unique_profile_paths:
self._add(*path, unique=True)
# These paths are only unique per profile if the commandline arg
# `separate` is passed
paths = [
# Data paths
('LOG_DB', 'logs.db', PathLocation.DATA, PathType.FILE),
('MY_CACERTS', 'cacerts.pem', PathLocation.DATA, PathType.FILE),
('MY_EMOTS', 'emoticons', PathLocation.DATA, PathType.FOLDER),
('MY_ICONSETS', 'iconsets', PathLocation.DATA, PathType.FOLDER),
('MY_MOOD_ICONSETS', 'moods', PathLocation.DATA, PathType.FOLDER),
('MY_ACTIVITY_ICONSETS', 'activities', PathLocation.DATA, PathType.FOLDER),
('PLUGINS_USER', 'plugins', PathLocation.DATA, PathType.FOLDER),
# Cache paths
('CACHE_DB', 'cache.db', PathLocation.CACHE, PathType.FILE),
('AVATAR', 'avatars', PathLocation.CACHE, PathType.FOLDER),
]
for path in paths:
self._add(*path)
_paths = ConfigPaths()
......
......@@ -41,6 +41,17 @@ class ArchiveState(IntEnum):
NEVER = 0
ALL = 1
@unique
class PathLocation(IntEnum):
CONFIG = 0
CACHE = 1
DATA = 2
@unique
class PathType(IntEnum):
FILE = 0
FOLDER = 1
THANKS = u"""\
Alexander Futász
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment