Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse order for parsing files in XDG_CONFIG_DIRS #4630

Merged
merged 4 commits into from
Nov 6, 2024
Merged
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
15 changes: 11 additions & 4 deletions easybuild/tools/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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')):
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion test/framework/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
7 changes: 4 additions & 3 deletions test/framework/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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']
Expand Down
Loading