From b75d05cbff2c7caf2bd3b310ce3bdd9ff30daa04 Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Wed, 3 Jan 2024 15:10:54 -0800 Subject: [PATCH] Test of upgrade takes unadulterated config file --- src/weecfg/tests/test_upgrade_config.py | 11 ++++++--- src/weecfg/update_config.py | 7 ++++-- src/weectllib/station_actions.py | 32 +++++++++++++++++-------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/weecfg/tests/test_upgrade_config.py b/src/weecfg/tests/test_upgrade_config.py index 24a57b36..80e4e4e2 100644 --- a/src/weecfg/tests/test_upgrade_config.py +++ b/src/weecfg/tests/test_upgrade_config.py @@ -144,10 +144,15 @@ class ConfigTest(unittest.TestCase): """Test an upgrade against a typical user's configuration file""" # Start with a typical V2.0 user file: - config_dict = configobj.ConfigObj('weewx20_user.conf', encoding='utf-8') - + config_dict = configobj.ConfigObj('weewx20_user.conf', + interpolation=False, + encoding='utf-8', + file_error=True) # The current config file becomes the template: - template = configobj.ConfigObj(current_config_dict_path, encoding='utf-8') + template = configobj.ConfigObj(current_config_dict_path, + interpolation=False, + encoding='utf-8', + file_error=True) # First update, then merge: weecfg.update_config.update_and_merge(config_dict, template) diff --git a/src/weecfg/update_config.py b/src/weecfg/update_config.py index 3dfccd2d..54137137 100644 --- a/src/weecfg/update_config.py +++ b/src/weecfg/update_config.py @@ -964,8 +964,11 @@ def update_to_v50(config_dict): - If the config file uses '/' for WEEWX_ROOT, set it to '/etc/weewx' """ - if 'WEEWX_ROOT_CONFIG' in config_dict and config_dict['WEEWX_ROOT_CONFIG'] == '/': - config_dict['WEEWX_ROOT_CONFIG'] = '/etc/weewx' + if 'WEEWX_ROOT' in config_dict: + if config_dict['WEEWX_ROOT'] == '/': + config_dict['WEEWX_ROOT'] = '/etc/weewx' + # Some older versions of weewx.conf use a trailing slash on WEEWX_ROOT. Standardize. + config_dict['WEEWX_ROOT'] = os.path.normpath(config_dict['WEEWX_ROOT']) config_dict['version'] = '5.0.0' diff --git a/src/weectllib/station_actions.py b/src/weectllib/station_actions.py index caaa28a3..14d6d784 100644 --- a/src/weectllib/station_actions.py +++ b/src/weectllib/station_actions.py @@ -771,17 +771,29 @@ def station_upgrade(config_dict, dist_config_path=None, examples_root=None, print("Nothing done.") return - # Unless we've been given a path to the new configuration file, retrieve it from - # package resources. - if dist_config_path: - dist_config_dict = configobj.ConfigObj(dist_config_path, encoding='utf-8', file_error=True) - else: - # Retrieve the new configuration file from package resources: - with importlib_resources.open_text('weewx_data', 'weewx.conf', encoding='utf-8') as fd: - dist_config_dict = configobj.ConfigObj(fd, encoding='utf-8', file_error=True) - if 'config' in what: - weecfg.update_config.update_and_merge(config_dict, dist_config_dict) + # Update the config file. Note that the upgrade algorithms take an unadulterated version + # of the dictionaries, so reread them using ConfigObj: + stn_config_dict = configobj.ConfigObj(config_path, + encoding='utf-8', + interpolation=False, + file_error=True) + # Unless we've been given a path to the new configuration file, retrieve it from + # package resources. + if dist_config_path: + dist_config_dict = configobj.ConfigObj(dist_config_path, + encoding='utf-8', + interpolation=False, + file_error=True) + else: + # Retrieve the new configuration file from package resources: + with importlib_resources.open_text('weewx_data', 'weewx.conf', encoding='utf-8') as fd: + dist_config_dict = configobj.ConfigObj(fd, + encoding='utf-8', + interpolation=False, + file_error=True) + + weecfg.update_config.update_and_merge(stn_config_dict, dist_config_dict) print(f"Finished upgrading configuration file {config_path}") print(f"Saving configuration file {config_path}") # Save the updated config file with backup