Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tryton/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Transfer configuration, profiles and plugins from previous version

Version 6.4.6 - 2022-11-17
--------------------------
* Bug fixes (see mercurial logs for details)
Expand Down
5 changes: 4 additions & 1 deletion tryton/tryton/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from gi.repository import Gdk, Gio, Gtk

from tryton import common, gui, translate
from tryton.config import CONFIG, get_config_dir
from tryton.config import CONFIG, copy_previous_configuration, get_config_dir
from tryton.gui.window.dblogin import DBLogin


Expand Down Expand Up @@ -59,6 +59,9 @@ def excepthook(type_, value, traceback_):
common.error(value, ''.join(traceback.format_tb(traceback_)))
sys.excepthook = excepthook

copy_previous_configuration('tryton.cfg')
copy_previous_configuration('profiles.cfg')
copy_previous_configuration('plugins')
CONFIG.parse()
if CONFIG.arguments:
url = CONFIG.arguments[0]
Expand Down
45 changes: 39 additions & 6 deletions tryton/tryton/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import optparse
import os
import shutil
import sys

from gi.repository import GdkPixbuf
Expand All @@ -15,16 +16,48 @@
_ = gettext.gettext


def get_config_dir():
def _reverse_series_iterator(starting_version):
major, minor = map(int, starting_version.split('.'))
while major >= 0 and minor >= 0:
yield f"{major}.{minor}"
if minor == 0:
major -= 1
minor = 8
else:
minor -= 2 if not minor % 2 else 1


def copy_previous_configuration(config_element):
current_version = __version__.rsplit('.', 1)[0]
config_dir = get_config_root()
for version in _reverse_series_iterator(current_version):
config_path = os.path.join(config_dir, version, config_element)
if version == current_version and os.path.exists(config_path):
break
elif os.path.exists(config_path):
if os.path.isfile(config_path):
shutil.copy(config_path, get_config_dir())
elif os.path.isdir(config_path):
shutil.copytree(
config_path,
os.path.join(get_config_dir(), config_element))
break


def get_config_root():
if os.name == 'nt':
appdata = os.environ['APPDATA']
if not isinstance(appdata, str):
appdata = str(appdata, sys.getfilesystemencoding())
return os.path.join(appdata, '.config', 'tryton',
__version__.rsplit('.', 1)[0])
config_path = os.getenv('XDG_CONFIG_HOME', os.path.join('~', '.config'))
return os.path.expanduser(
os.path.join(config_path, 'tryton', __version__.rsplit('.', 1)[0]))
config_path = os.path.join(appdata, '.config')
else:
config_path = os.path.expanduser(os.getenv(
'XDG_CONFIG_HOME', os.path.join('~', '.config')))
return os.path.join(config_path, 'tryton')


def get_config_dir():
return os.path.join(get_config_root(), __version__.rsplit('.', 1)[0])


if not os.path.isdir(get_config_dir()):
Expand Down
29 changes: 22 additions & 7 deletions tryton/tryton/gui/window/dblogin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import gettext
import logging
import os
import re
import shutil
import tempfile
import threading

from gi.repository import GLib, GObject, Gtk
Expand All @@ -19,6 +22,7 @@

_ = gettext.gettext
logger = logging.getLogger(__name__)
DEMO_HOSTNAME = re.compile(r"demo\d+\.\d+\.tryton\.org")


class DBListEditor(object):
Expand Down Expand Up @@ -495,20 +499,31 @@ def __init__(self):
grid.attach(self.entry_date, 1, 6, 2, 1)

# Profile informations
self.profile_cfg = os.path.join(get_config_dir(), 'profiles.cfg')
config_dir = get_config_dir()
self.profile_cfg = os.path.join(config_dir, 'profiles.cfg')
self.profiles = configparser.ConfigParser()
if not os.path.exists(self.profile_cfg):
try:
self.profiles.read(self.profile_cfg)
except configparser.Error:
# reset self.profiles as parsing errors may leave wrong data in
# the parser
self.profiles = configparser.ConfigParser()
with tempfile.NamedTemporaryFile(
delete=False, prefix='profiles_', suffix='.cfg',
dir=config_dir) as temp_file:
temp_name = temp_file.name
shutil.copy(self.profile_cfg, temp_name)
logger.error(
f"Failed to parse {self.profiles_cfg}. "
f"A backup can be found at {temp_name}",
exc_info=True)
if not self.profiles.sections():
short_version = '.'.join(__version__.split('.', 2)[:2])
name = 'demo%s.tryton.org' % short_version
self.profiles.add_section(name)
self.profiles.set(name, 'host', name)
self.profiles.set(name, 'database', 'demo%s' % short_version)
self.profiles.set(name, 'username', 'demo')
else:
try:
self.profiles.read(self.profile_cfg)
except configparser.ParsingError:
logger.error("Fail to parse profiles.cfg", exc_info=True)
for section in self.profiles.sections():
active = all(self.profiles.has_option(section, option)
for option in ('host', 'database'))
Expand Down