@@ -626,9 +626,10 @@ def is_ready(self) -> bool:
626
626
raise NotImplementedError
627
627
628
628
@property
629
- def logs (self ) -> list [str ]:
629
+ def log_names (self ) -> list [str ]:
630
+ """Return name list of logs the guest could provide."""
630
631
631
- raise NotImplementedError
632
+ return []
632
633
633
634
@classmethod
634
635
def options (cls , how : Optional [str ] = None ) -> list [tmt .options .ClickOptionDecoratorType ]:
@@ -1109,38 +1110,55 @@ def requires(cls) -> list['tmt.base.Dependency']:
1109
1110
return []
1110
1111
1111
1112
def acquire_log (self , log_name : str ) -> Optional [str ]:
1112
- """fetch and return content of a requested log"""
1113
+ """
1114
+ Fetch and return content of a log.
1115
+ :param log_name: name of the log.
1116
+ :returns: content of the log.
1117
+ """
1113
1118
raise NotImplementedError
1114
1119
1115
1120
def store_log (
1116
1121
self ,
1117
1122
log_path : Path ,
1118
1123
log_content : str ,
1119
1124
log_name : Optional [str ] = None ) -> None :
1120
- """save log content and return the path"""
1121
- if log_name :
1122
- # if log_path contains log file name
1123
- if log_path .is_dir ():
1124
- log_path .write_text (log_content )
1125
- else :
1126
- (log_path / log_name ).write_text (log_content )
1127
- else :
1125
+ """
1126
+ Save log content to a file.
1127
+ :param log_path: a path to save into,could be a directory
1128
+ or a file path.
1129
+ :param log_content: content of the log.
1130
+ :param log_name: name of the log, if not set, log_path
1131
+ is supposed to be '/dev/null' or a file path.
1132
+ """
1133
+ # if log_path is file path or /dev/null
1134
+ if not log_path .is_dir () or str (log_path ) == '/dev/null' :
1128
1135
log_path .write_text (log_content )
1136
+ # log_path is a directory
1137
+ else :
1138
+ if log_name :
1139
+ name_str = log_name
1140
+ else :
1141
+ name_str = 'tmt-guestlog-' + \
1142
+ datetime .datetime .now (datetime .timezone .utc ).strftime ("%Y%m%d%H%M%S" )
1143
+ self .warn (f'log_name is not set, using file name:{ name_str } ' )
1144
+ (log_path / name_str ).write_text (log_content )
1129
1145
1130
1146
def handle_guest_logs (self ,
1131
1147
log_path : Optional [Path ] = None ,
1132
- logs : Optional [list [str ]] = None ) -> None :
1133
- """get log content and save it to the workdir/provision/,
1134
- list the log dir in guests.yaml"""
1135
- logs = logs or []
1136
- log_path = log_path or self .workdir
1137
- for log_name in logs :
1148
+ log_names : Optional [list [str ]] = None ) -> None :
1149
+ """
1150
+ Get log content and save it to a directory.
1151
+ :param log_path: a directory to save into.If not set,self.workdir
1152
+ or Path.cwd() will be used.
1153
+ :param log_names: name list of logs need to be handled.If not set,
1154
+ self.log_names will be used.
1155
+ """
1156
+ log_names = log_names or self .log_names
1157
+ log_path = log_path or self .workdir or Path .cwd ()
1158
+ for log_name in log_names :
1138
1159
log_content = self .acquire_log (log_name )
1139
1160
if log_content :
1140
- if log_path :
1141
- self .store_log (log_path , log_content , log_name )
1142
- else :
1143
- self .store_log (Path .cwd (), log_content , log_name )
1161
+ self .store_log (log_path , log_content , log_name )
1144
1162
else :
1145
1163
self .debug (f'no content in { log_name } ' )
1146
1164
0 commit comments