1
0
Fork 0

profiles: fix loading multiple profiles if there are repeating profiles

The original idea behind this patch is credited to
Jiří Mencák <jmencak@redhat.com>.

Resolves: rhbz#1825882

Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
This commit is contained in:
Jaroslav Škarvada 2021-06-09 09:57:15 +02:00
parent d32240c7bb
commit bd6697f8f4
No known key found for this signature in database
GPG key ID: D8E1C00E076E840B
3 changed files with 11 additions and 3 deletions

View file

@ -46,14 +46,18 @@ class LocatorTestCase(unittest.TestCase):
def test_get_config(self):
config_name = self.locator.get_config("custom")
self.assertEqual(config_name, os.path.join(self._tmp_load_dirs[1], "custom", "tuned.conf"))
# none matched, none skipped
config_name = self.locator.get_config("non-existent")
self.assertIsNone(config_name)
def test_get_config_priority(self):
customized = self.locator.get_config("balanced")
self.assertEqual(customized, os.path.join(self._tmp_load_dirs[1], "balanced", "tuned.conf"))
system = self.locator.get_config("balanced", [customized])
self.assertEqual(system, os.path.join(self._tmp_load_dirs[0], "balanced", "tuned.conf"))
none = self.locator.get_config("balanced", [customized, system])
self.assertIsNone(none)
# none matched, but at least one skipped
empty = self.locator.get_config("balanced", [customized, system])
self.assertEqual(empty, "")
def test_ignore_nonexistent_dirs(self):
locator = Locator([self._tmp_load_dirs[0], "/tmp/some-dir-which-does-not-exist-for-sure"])

View file

@ -77,6 +77,8 @@ class Loader(object):
def _load_profile(self, profile_names, profiles, processed_files):
for name in profile_names:
filename = self._profile_locator.get_config(name, processed_files)
if filename == "":
continue
if filename is None:
raise InvalidProfileException("Cannot find profile '%s' in '%s'." % (name, list(reversed(self._profile_locator._load_directories))))
processed_files.append(filename)

View file

@ -24,17 +24,19 @@ class Locator(object):
return os.path.normpath(config_name)
def get_config(self, profile_name, skip_files=None):
ret = None
for dir_name in reversed(self._load_directories):
# basename is protection not to get out of the path
config_file = self._get_config_filename(dir_name, os.path.basename(profile_name))
if skip_files is not None and config_file in skip_files:
ret = ""
continue
if os.path.isfile(config_file):
return config_file
return None
return ret
def check_profile_name_format(self, profile_name):
return profile_name is not None and profile_name != "" and "/" not in profile_name