Skip to content

Commit

Permalink
Implement common methods status(), read(), write()
Browse files Browse the repository at this point in the history
  • Loading branch information
psss committed Oct 25, 2019
1 parent 62f3fde commit ae7b8a0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions tmt/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions tmt/steps/discover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
35 changes: 34 additions & 1 deletion tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ae7b8a0

Please sign in to comment.