17
17
18
18
MetadataContainerT = TypeVar ('MetadataContainerT' , bound = 'MetadataContainer' )
19
19
20
- # Config directory
21
- DEFAULT_CONFIG_DIR = Path ('~/.config/tmt' )
20
+ # Config directories
21
+ DEFAULT_USER_CONFIG_DIR = Path ('~/.config/tmt' )
22
+ DEFAULT_GLOBAL_CONFIG_DIR = Path ('/etc/tmt/config' )
22
23
23
24
24
- def effective_config_dir () -> Path :
25
+ def _effective_user_config_dir () -> Path :
25
26
"""
26
27
Find out what the actual config directory is.
27
28
@@ -32,7 +33,7 @@ def effective_config_dir() -> Path:
32
33
if 'TMT_CONFIG_DIR' in os .environ :
33
34
return Path (os .environ ['TMT_CONFIG_DIR' ]).expanduser ()
34
35
35
- return DEFAULT_CONFIG_DIR .expanduser ()
36
+ return DEFAULT_USER_CONFIG_DIR .expanduser ()
36
37
37
38
38
39
class Config :
@@ -45,17 +46,21 @@ def __init__(self, logger: tmt.log.Logger) -> None:
45
46
Initialize config directory path
46
47
"""
47
48
48
- self .path = effective_config_dir ()
49
49
self .logger = logger
50
50
51
+ self .user_path = _effective_user_config_dir ()
52
+ self .global_path = DEFAULT_GLOBAL_CONFIG_DIR
53
+
51
54
try :
52
- self .path .mkdir (parents = True , exist_ok = True )
55
+ self .user_path .mkdir (parents = True , exist_ok = True )
53
56
except OSError as error :
54
- raise tmt .utils .GeneralError (f"Failed to create config '{ self .path } '." ) from error
57
+ raise tmt .utils .GeneralError (
58
+ f"Failed to create user config path '{ self .user_path } '."
59
+ ) from error
55
60
56
61
@property
57
62
def _last_run_symlink (self ) -> Path :
58
- return self .path / 'last-run'
63
+ return self .user_path / 'last-run'
59
64
60
65
@property
61
66
def last_run (self ) -> Optional [Path ]:
@@ -83,31 +88,48 @@ def last_run(self, workdir: Path) -> None:
83
88
"'tmt run --last' might not pick the right run directory."
84
89
)
85
90
except OSError as error :
86
- raise tmt .utils .GeneralError (f"Unable to save last run '{ self .path } '.\n { error } " )
91
+ raise tmt .utils .GeneralError (f"Unable to save last run '{ self .user_path } '." ) from error
87
92
88
93
@functools .cached_property
89
- def fmf_tree (self ) -> Optional [fmf .Tree ]:
94
+ def _fmf_tree (self ) -> tuple [Optional [Path ], Optional [fmf .Tree ]]:
95
+ try :
96
+ return (self .user_path , fmf .Tree (self .user_path ))
97
+
98
+ except (fmf .utils .FileError , fmf .utils .RootError ):
99
+ self .logger .debug (f"Config tree not found in user path '{ self .user_path } '." )
100
+
101
+ try :
102
+ return (self .global_path , fmf .Tree (self .global_path ))
103
+
104
+ except (fmf .utils .FileError , fmf .utils .RootError ):
105
+ self .logger .debug (f"Config tree not found in global path '{ self .global_path } '." )
106
+
107
+ return None , None
108
+
109
+ @functools .cached_property
110
+ def tree (self ) -> Optional [fmf .Tree ]:
90
111
"""
91
112
Return the configuration tree
92
113
"""
93
114
94
- try :
95
- return fmf .Tree (self .path )
96
- except fmf .utils .RootError :
97
- self .logger .debug (f"Config tree not found in '{ self .path } '." )
115
+ return self ._fmf_tree [1 ]
98
116
99
- return None
117
+ @functools .cached_property
118
+ def tree_location (self ) -> Path :
119
+ path , _ = self ._fmf_tree
120
+
121
+ return path if path is not None else self .global_path
100
122
101
123
def _parse_config_subtree (
102
124
self , path : str , model : type [MetadataContainerT ]
103
125
) -> Optional [MetadataContainerT ]:
104
- if self .fmf_tree is None :
126
+ if self .tree is None :
105
127
return None
106
128
107
- subtree = cast (Optional [fmf .Tree ], self .fmf_tree .find (path ))
129
+ subtree = cast (Optional [fmf .Tree ], self .tree .find (path ))
108
130
109
131
if not subtree :
110
- self .logger .debug (f"Config path '{ path } ' not found in '{ self .path } '." )
132
+ self .logger .debug (f"Config path '{ path } ' not found in '{ self .tree_location } '." )
111
133
112
134
return None
113
135
0 commit comments