diff --git a/easybuild/tools/options.py b/easybuild/tools/options.py index 8a95aa7226..c927b841a2 100644 --- a/easybuild/tools/options.py +++ b/easybuild/tools/options.py @@ -105,6 +105,7 @@ from easybuild.tools.repository.repository import avail_repositories from easybuild.tools.systemtools import DARWIN, UNKNOWN, check_python_version, get_cpu_architecture, get_cpu_family from easybuild.tools.systemtools import get_cpu_features, get_gpu_info, get_os_type, get_system_info +from easybuild.tools.utilities import flatten from easybuild.tools.version import this_is_easybuild @@ -124,7 +125,8 @@ def terminal_supports_colors(stream): XDG_CONFIG_HOME = os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), ".config")) XDG_CONFIG_DIRS = os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(os.pathsep) -DEFAULT_SYS_CFGFILES = [f for d in XDG_CONFIG_DIRS for f in sorted(glob.glob(os.path.join(d, 'easybuild.d', '*.cfg')))] +DEFAULT_SYS_CFGFILES = [[f for f in sorted(glob.glob(os.path.join(d, 'easybuild.d', '*.cfg')))] + for d in XDG_CONFIG_DIRS] DEFAULT_USER_CFGFILE = os.path.join(XDG_CONFIG_HOME, 'easybuild', 'config.cfg') DEFAULT_LIST_PR_STATE = GITHUB_PR_STATE_OPEN @@ -212,7 +214,11 @@ class EasyBuildOptions(GeneralOption): VERSION = this_is_easybuild() DEFAULT_LOGLEVEL = 'INFO' - DEFAULT_CONFIGFILES = DEFAULT_SYS_CFGFILES[:] + # https://specifications.freedesktop.org/basedir-spec/latest/ + # says precedence should be + # XDG_CONFIG_HOME > 1st entry of XDG_CONFIG_DIRS > 2nd entry ... + # EasyBuild parses this list backwards, gives priority to last entry + DEFAULT_CONFIGFILES = flatten(DEFAULT_SYS_CFGFILES[::-1]) if 'XDG_CONFIG_DIRS' not in os.environ: old_etc_location = os.path.join('/etc', 'easybuild.d') if os.path.isdir(old_etc_location) and glob.glob(os.path.join(old_etc_location, '*.cfg')): @@ -1329,9 +1335,10 @@ def show_default_configfiles(self): "* user-level: %s" % os.path.join('${XDG_CONFIG_HOME:-$HOME/.config}', 'easybuild', 'config.cfg'), " -> %s => %s" % (DEFAULT_USER_CFGFILE, ('not found', 'found')[os.path.exists(DEFAULT_USER_CFGFILE)]), "* system-level: %s" % os.path.join('${XDG_CONFIG_DIRS:-/etc/xdg}', 'easybuild.d', '*.cfg'), - " -> %s => %s" % (system_cfg_glob_paths, ', '.join(DEFAULT_SYS_CFGFILES) or "(no matches)"), + " -> %s => %s" % (system_cfg_glob_paths, ', '.join(flatten(DEFAULT_SYS_CFGFILES)) or "(no matches)"), '', - "Default list of existing configuration files (%d): %s" % (found_cfgfile_cnt, found_cfgfile_list), + "Default list of existing configuration files (%d, most important last):" % found_cfgfile_cnt, + found_cfgfile_list, ] return '\n'.join(lines) diff --git a/test/framework/config.py b/test/framework/config.py index 337c7b9b87..08a1a83355 100644 --- a/test/framework/config.py +++ b/test/framework/config.py @@ -448,7 +448,7 @@ def test_XDG_CONFIG_env_vars(self): # $XDG_CONFIG_HOME not set, multiple directories listed in $XDG_CONFIG_DIRS del os.environ['XDG_CONFIG_HOME'] # unset, so should become default - os.environ['XDG_CONFIG_DIRS'] = os.pathsep.join([dir1, dir2, dir3]) + os.environ['XDG_CONFIG_DIRS'] = os.pathsep.join([dir3, dir2, dir1]) cfg_files = [ os.path.join(dir1, 'easybuild.d', 'bar.cfg'), os.path.join(dir1, 'easybuild.d', 'foo.cfg'), diff --git a/test/framework/options.py b/test/framework/options.py index d30fa7b390..007986c39c 100644 --- a/test/framework/options.py +++ b/test/framework/options.py @@ -3523,7 +3523,8 @@ def test_show_default_configfiles(self): expected_tmpl += '\n'.join([ "%s", '', - "Default list of existing configuration files (%d): %s", + "Default list of existing configuration files (%d, most important last):", + "%s", ]) # put dummy cfgfile in place in $HOME (to predict last line of output which only lists *existing* files) @@ -3542,7 +3543,7 @@ def test_show_default_configfiles(self): xdg_config_home = os.path.join(self.test_prefix, 'home') os.environ['XDG_CONFIG_HOME'] = xdg_config_home - xdg_config_dirs = [os.path.join(self.test_prefix, 'etc', 'xdg'), os.path.join(self.test_prefix, 'moaretc')] + xdg_config_dirs = [os.path.join(self.test_prefix, 'moaretc'), os.path.join(self.test_prefix, 'etc', 'xdg')] os.environ['XDG_CONFIG_DIRS'] = os.pathsep.join(xdg_config_dirs) # put various dummy cfgfiles in place @@ -3564,7 +3565,7 @@ def test_show_default_configfiles(self): expected = expected_tmpl % (xdg_config_home, os.pathsep.join(xdg_config_dirs), "%s => found" % os.path.join(xdg_config_home, 'easybuild', 'config.cfg'), '{' + ', '.join(xdg_config_dirs) + '}', - ', '.join(cfgfiles[:-1]), 4, ', '.join(cfgfiles)) + ', '.join(cfgfiles[1:3]+[cfgfiles[0]]), 4, ', '.join(cfgfiles)) self.assertIn(expected, logtxt) del os.environ['XDG_CONFIG_DIRS']