diff --git a/SCAutolib/cli_commands.py b/SCAutolib/cli_commands.py index a736fa7..7743d87 100644 --- a/SCAutolib/cli_commands.py +++ b/SCAutolib/cli_commands.py @@ -285,7 +285,7 @@ def run_all(ctx, actions, install_missing): ctx.obj["CONTROLLER"].setup_graphical(install_missing, True) from SCAutolib.models.gui import GUI - gui = GUI() + gui = GUI(from_cli=True) for action in actions: if "init" in action: gui.__enter__() diff --git a/SCAutolib/models/gui.py b/SCAutolib/models/gui.py index e8a2392..f0fc9a2 100644 --- a/SCAutolib/models/gui.py +++ b/SCAutolib/models/gui.py @@ -1,7 +1,6 @@ import inspect -import os -from os.path import join from time import sleep, time +from pathlib import Path import cv2 import keyboard @@ -24,7 +23,14 @@ def __init__(self, directory: str, html_file: str = None): """ self.directory = directory self.html_file = html_file + + taken_images = [str(image).split('/')[-1] + for image in Path(directory).iterdir()] + taken_images.sort(reverse=True) + self.screenshot_num = 1 + if len(taken_images) > 0: + self.screenshot_num = int(taken_images[0].split('.')[0]) + 1 def screenshot(self, timeout: float = 30): """Runs ffmpeg to take a screenshot. @@ -237,7 +243,8 @@ def wrapper(self, *args, **kwargs): class GUI: """Represents the GUI and allows controlling the system under test.""" - def __init__(self, wait_time: float = 5, res_dir_name: str = None): + def __init__(self, wait_time: float = 5, res_dir_name: str = None, + from_cli: bool = False): """Initializes the GUI of system under test. :param wait_time: Time to wait after each action @@ -247,30 +254,50 @@ def __init__(self, wait_time: float = 5, res_dir_name: str = None): self.wait_time = wait_time self.gdm_init_time = 10 + self.from_cli = from_cli # Create the directory for screenshots + self.html_directory = Path("/tmp/SC-tests") if res_dir_name: - self.html_directory = '/tmp/SC-tests/' + res_dir_name + self.html_directory = self.html_directory.joinpath(res_dir_name) + elif from_cli: + run_dirs = [str(run_dir).split('/')[-1] + for run_dir in self.html_directory.iterdir() + if "cli_gui" in str(run_dir)] + run_dirs.sort(reverse=True) + + last_run_dir = Path(run_dirs[0]) if len(run_dirs) > 0 else None + if last_run_dir and not last_run_dir.joinpath('done').exists(): + # Use the old run directory + logger.debug("Using HTML logging file from last time.") + self.html_directory = self.html_directory.joinpath( + last_run_dir) + else: + # Create new run directory + logger.debug("Creating new HTML logging file.") + self.html_directory = self.html_directory.joinpath( + str(int(time())) + '_cli_gui') else: calling_func = inspect.stack()[1][3] - self.html_directory = '/tmp/SC-tests/' + str(int(time())) - self.html_directory += "_" + calling_func + self.html_directory = self.html_directory.joinpath( + str(int(time())) + '_' + calling_func) - self.screenshot_directory = self.html_directory + "/screenshots" + self.screenshot_directory = self.html_directory.joinpath("screenshots") # will create both dirs - os.makedirs(self.screenshot_directory, exist_ok=True) + self.screenshot_directory.mkdir(parents=True, exist_ok=True) - self.html_file = join(self.html_directory, "index.html") - with open(self.html_file, 'w') as fp: - fp.write( - "\n" - "
\n" - "\n" - "\n" - "" + fmt + "
") ) + if self.from_cli: + logger.addHandler(self.fileHandler) + self.mouse = Mouse() # workaround for keyboard library @@ -300,11 +330,17 @@ def __enter__(self): # This would break the display sleep(self.gdm_init_time) - logger.addHandler(self.fileHandler) + if not self.from_cli: + logger.addHandler(self.fileHandler) return self def __exit__(self, type, value, traceback): + done_file = self.html_directory.joinpath('done') + print(done_file) + if done_file.exists(): + return + run(['systemctl', 'stop', 'gdm'], check=True) with open(self.html_file, 'a') as fp: @@ -313,7 +349,13 @@ def __exit__(self, type, value, traceback): "\n" ) - logger.removeHandler(self.fileHandler) + print(done_file) + with open(done_file, 'w') as fp: + fp.write("done") + + if not self.from_cli: + logger.removeHandler(self.fileHandler) + logger.info(f"HTML file with results created in {self.html_directory}.") @action_decorator