From ae7b8a0a81efd892dd64c7f390ffe41a7a1fff68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0pl=C3=ADchal?= Date: Fri, 25 Oct 2019 15:34:46 +0200 Subject: [PATCH] Implement common methods status(), read(), write() --- tmt/steps/__init__.py | 3 +++ tmt/steps/discover/__init__.py | 6 ++++++ tmt/utils.py | 35 +++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/tmt/steps/__init__.py b/tmt/steps/__init__.py index 0b3343ea53..e5216377f5 100644 --- a/tmt/steps/__init__.py +++ b/tmt/steps/__init__.py @@ -49,6 +49,9 @@ def __init__(self, data={}, plan=None, name=None): # Ensure that each config has a name if 'name' not in data and len(self.data) > 1: raise GeneralError(f"Missing '{self}' name in '{self.plan}'.") + # Get or set the status + if self.status is None: + self.status('todo') @property def verbose(self): diff --git a/tmt/steps/discover/__init__.py b/tmt/steps/discover/__init__.py index 8a810cb6bb..ea72421a9b 100644 --- a/tmt/steps/discover/__init__.py +++ b/tmt/steps/discover/__init__.py @@ -34,9 +34,15 @@ def show(self): def go(self): """ Execute all steps """ + # Nothing to do if already done + if self.status() == 'done': + return + # Go! + self.status('going') super(Discover, self).go() for step in self.steps: step.go() + self.status('done') def tests(self): """ Return a list of all tests """ diff --git a/tmt/utils.py b/tmt/utils.py index 52d9d8ac43..86b846da6c 100644 --- a/tmt/utils.py +++ b/tmt/utils.py @@ -30,7 +30,7 @@ class Common(object): Takes care of command line context and workdir handling. """ - # Command line context and workdir + # Command line context, workdir and status _context = None _workdir = None @@ -72,6 +72,39 @@ def run(self, command, message=None): except subprocess.CalledProcessError as error: raise GeneralError(f"{message}\n{error}") + def read(self, path): + """ Read a file from the workdir """ + path = os.path.join(self.workdir, path) + try: + with open(path) as data: + return data.read() + except OSError as error: + raise GeneralError(f"Failed to read '{path}'.\n{error}") + + def write(self, path, data): + """ Write a file to the workdir """ + path = os.path.join(self.workdir, path) + try: + with open(path, 'w') as target: + return target.write(data) + except OSError as error: + raise GeneralError(f"Failed to read '{path}'.\n{error}") + + def status(self, status=None): + """ Get and set current status, store in workdir """ + # Check for valid values + if status and status not in ['todo', 'done', 'going']: + raise GeneralError(f"Invalid status '{status}'.") + # Store status + if status: + self.write('status.txt', status + '\n') + # Read status + else: + try: + return self.read('status.txt').strip() + except GeneralError: + return None + def _workdir_init(self, id_): """ Initialize the work directory