Skip to content

Commit 234ad84

Browse files
authored
Merge pull request #4630 from bartoldeman/xdg-config-dirs-order
Reverse order for parsing files in `XDG_CONFIG_DIRS`
2 parents b5c1dd9 + 7ebbee1 commit 234ad84

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

easybuild/tools/options.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
from easybuild.tools.repository.repository import avail_repositories
107107
from easybuild.tools.systemtools import DARWIN, UNKNOWN, check_python_version, get_cpu_architecture, get_cpu_family
108108
from easybuild.tools.systemtools import get_cpu_features, get_gpu_info, get_os_type, get_system_info
109+
from easybuild.tools.utilities import flatten
109110
from easybuild.tools.version import this_is_easybuild
110111

111112

@@ -125,7 +126,8 @@ def terminal_supports_colors(stream):
125126

126127
XDG_CONFIG_HOME = os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), ".config"))
127128
XDG_CONFIG_DIRS = os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(os.pathsep)
128-
DEFAULT_SYS_CFGFILES = [f for d in XDG_CONFIG_DIRS for f in sorted(glob.glob(os.path.join(d, 'easybuild.d', '*.cfg')))]
129+
DEFAULT_SYS_CFGFILES = [[f for f in sorted(glob.glob(os.path.join(d, 'easybuild.d', '*.cfg')))]
130+
for d in XDG_CONFIG_DIRS]
129131
DEFAULT_USER_CFGFILE = os.path.join(XDG_CONFIG_HOME, 'easybuild', 'config.cfg')
130132

131133
DEFAULT_LIST_PR_STATE = GITHUB_PR_STATE_OPEN
@@ -213,7 +215,11 @@ class EasyBuildOptions(GeneralOption):
213215
VERSION = this_is_easybuild()
214216

215217
DEFAULT_LOGLEVEL = 'INFO'
216-
DEFAULT_CONFIGFILES = DEFAULT_SYS_CFGFILES[:]
218+
# https://specifications.freedesktop.org/basedir-spec/latest/
219+
# says precedence should be
220+
# XDG_CONFIG_HOME > 1st entry of XDG_CONFIG_DIRS > 2nd entry ...
221+
# EasyBuild parses this list backwards, gives priority to last entry
222+
DEFAULT_CONFIGFILES = flatten(DEFAULT_SYS_CFGFILES[::-1])
217223
if 'XDG_CONFIG_DIRS' not in os.environ:
218224
old_etc_location = os.path.join('/etc', 'easybuild.d')
219225
if os.path.isdir(old_etc_location) and glob.glob(os.path.join(old_etc_location, '*.cfg')):
@@ -1394,9 +1400,10 @@ def show_default_configfiles(self):
13941400
"* user-level: %s" % os.path.join('${XDG_CONFIG_HOME:-$HOME/.config}', 'easybuild', 'config.cfg'),
13951401
" -> %s => %s" % (DEFAULT_USER_CFGFILE, ('not found', 'found')[os.path.exists(DEFAULT_USER_CFGFILE)]),
13961402
"* system-level: %s" % os.path.join('${XDG_CONFIG_DIRS:-/etc/xdg}', 'easybuild.d', '*.cfg'),
1397-
" -> %s => %s" % (system_cfg_glob_paths, ', '.join(DEFAULT_SYS_CFGFILES) or "(no matches)"),
1403+
" -> %s => %s" % (system_cfg_glob_paths, ', '.join(flatten(DEFAULT_SYS_CFGFILES)) or "(no matches)"),
13981404
'',
1399-
"Default list of existing configuration files (%d): %s" % (found_cfgfile_cnt, found_cfgfile_list),
1405+
"Default list of existing configuration files (%d, most important last):" % found_cfgfile_cnt,
1406+
found_cfgfile_list,
14001407
]
14011408
return '\n'.join(lines)
14021409

test/framework/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def test_XDG_CONFIG_env_vars(self):
448448

449449
# $XDG_CONFIG_HOME not set, multiple directories listed in $XDG_CONFIG_DIRS
450450
del os.environ['XDG_CONFIG_HOME'] # unset, so should become default
451-
os.environ['XDG_CONFIG_DIRS'] = os.pathsep.join([dir1, dir2, dir3])
451+
os.environ['XDG_CONFIG_DIRS'] = os.pathsep.join([dir3, dir2, dir1])
452452
cfg_files = [
453453
os.path.join(dir1, 'easybuild.d', 'bar.cfg'),
454454
os.path.join(dir1, 'easybuild.d', 'foo.cfg'),

test/framework/options.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,7 +3544,8 @@ def test_show_default_configfiles(self):
35443544
expected_tmpl += '\n'.join([
35453545
"%s",
35463546
'',
3547-
"Default list of existing configuration files (%d): %s",
3547+
"Default list of existing configuration files (%d, most important last):",
3548+
"%s",
35483549
])
35493550

35503551
# put dummy cfgfile in place in $HOME (to predict last line of output which only lists *existing* files)
@@ -3563,7 +3564,7 @@ def test_show_default_configfiles(self):
35633564

35643565
xdg_config_home = os.path.join(self.test_prefix, 'home')
35653566
os.environ['XDG_CONFIG_HOME'] = xdg_config_home
3566-
xdg_config_dirs = [os.path.join(self.test_prefix, 'etc', 'xdg'), os.path.join(self.test_prefix, 'moaretc')]
3567+
xdg_config_dirs = [os.path.join(self.test_prefix, 'moaretc'), os.path.join(self.test_prefix, 'etc', 'xdg')]
35673568
os.environ['XDG_CONFIG_DIRS'] = os.pathsep.join(xdg_config_dirs)
35683569

35693570
# put various dummy cfgfiles in place
@@ -3585,7 +3586,7 @@ def test_show_default_configfiles(self):
35853586
expected = expected_tmpl % (xdg_config_home, os.pathsep.join(xdg_config_dirs),
35863587
"%s => found" % os.path.join(xdg_config_home, 'easybuild', 'config.cfg'),
35873588
'{' + ', '.join(xdg_config_dirs) + '}',
3588-
', '.join(cfgfiles[:-1]), 4, ', '.join(cfgfiles))
3589+
', '.join(cfgfiles[1:3]+[cfgfiles[0]]), 4, ', '.join(cfgfiles))
35893590
self.assertIn(expected, logtxt)
35903591

35913592
del os.environ['XDG_CONFIG_DIRS']

0 commit comments

Comments
 (0)