Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement profiles locator class, used by profiles loader
- Loading branch information
Showing
4 changed files
with
119 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import unittest | ||
import os | ||
import shutil | ||
import tempfile | ||
from tuned.profiles.locator import Locator | ||
|
||
class LocatorTestCase(unittest.TestCase): | ||
def setUp(self): | ||
self.locator = Locator(self._tmp_load_dirs) | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
tmpdir1 = tempfile.mkdtemp() | ||
tmpdir2 = tempfile.mkdtemp() | ||
cls._tmp_load_dirs = [tmpdir1, tmpdir2] | ||
|
||
cls._create_profile(tmpdir1, "balanced") | ||
cls._create_profile(tmpdir1, "powersafe") | ||
cls._create_profile(tmpdir2, "custom") | ||
cls._create_profile(tmpdir2, "balanced") | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
for tmp_dir in cls._tmp_load_dirs: | ||
shutil.rmtree(tmp_dir, True) | ||
|
||
@classmethod | ||
def _create_profile(cls, load_dir, profile_name): | ||
profile_dir = os.path.join(load_dir, profile_name) | ||
conf_name = os.path.join(profile_dir, "tuned.conf") | ||
os.mkdir(profile_dir) | ||
with open(conf_name, "w") as conf_file: | ||
pass | ||
|
||
def test_init(self): | ||
Locator([]) | ||
|
||
def test_init_invalid_type(self): | ||
with self.assertRaises(TypeError): | ||
Locator("string") | ||
|
||
def test_get_known_names(self): | ||
known = self.locator.get_known_names() | ||
self.assertListEqual(known, ["balanced", "custom", "powersafe"]) | ||
|
||
def test_get_config(self): | ||
config_name = self.locator.get_config("custom") | ||
self.assertEquals(config_name, os.path.join(self._tmp_load_dirs[1], "custom", "tuned.conf")) | ||
|
||
def test_get_config_priority(self): | ||
customized = self.locator.get_config("balanced") | ||
self.assertEquals(customized, os.path.join(self._tmp_load_dirs[1], "balanced", "tuned.conf")) | ||
system = self.locator.get_config("balanced", [customized]) | ||
self.assertEquals(system, os.path.join(self._tmp_load_dirs[0], "balanced", "tuned.conf")) | ||
none = self.locator.get_config("balanced", [customized, system]) | ||
self.assertIsNone(none) | ||
|
||
def test_ignore_nonexistent_dirs(self): | ||
locator = Locator([self._tmp_load_dirs[0], "/tmp/some-dir-which-does-not-exist-for-sure"]) | ||
balanced = locator.get_config("balanced") | ||
self.assertEquals(balanced, os.path.join(self._tmp_load_dirs[0], "balanced", "tuned.conf")) | ||
known = locator.get_known_names() | ||
self.assertListEqual(known, ["balanced", "powersafe"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
|
||
class Locator(object): | ||
""" | ||
Profiles locator and enumerator. | ||
""" | ||
|
||
__slots__ = ["_load_directories"] | ||
|
||
def __init__(self, load_directories): | ||
if type(load_directories) is not list: | ||
raise TypeError("load_directories parameter is not a list") | ||
self._load_directories = load_directories | ||
|
||
@property | ||
def load_directories(self): | ||
return self._load_directories | ||
|
||
def _get_config_filename(self, *path_parts): | ||
path_parts = list(path_parts) + ["tuned.conf"] | ||
config_name = os.path.join(*path_parts) | ||
return os.path.normpath(config_name) | ||
|
||
def get_config(self, profile_name, skip_files=None): | ||
for dir_name in reversed(self._load_directories): | ||
config_file = self._get_config_filename(dir_name, profile_name) | ||
|
||
if skip_files is not None and config_file in skip_files: | ||
continue | ||
|
||
if os.path.isfile(config_file): | ||
return config_file | ||
|
||
return None | ||
|
||
def get_known_names(self): | ||
profiles = set() | ||
for dir_name in self._load_directories: | ||
try: | ||
for profile_name in os.listdir(dir_name): | ||
config_file = self._get_config_filename(dir_name, profile_name) | ||
if os.path.isfile(config_file): | ||
profiles.add(profile_name) | ||
except OSError: | ||
pass | ||
|
||
return sorted(list(profiles)) |