Skip to content

Commit

Permalink
Implement run() to easily execute in the workdir
Browse files Browse the repository at this point in the history
  • Loading branch information
psss committed Oct 25, 2019
1 parent 8ad6119 commit 62f3fde
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
23 changes: 6 additions & 17 deletions tmt/steps/discover/fmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import fmf
import tmt
import shutil
import subprocess
import tmt.steps.discover

from click import echo
Expand Down Expand Up @@ -52,26 +51,16 @@ def go(self):
shutil.copytree(directory, testdir)
# Clone git repository
else:
echo(f"Cloning '{self.repository}' to '{testdir}'.")
command = f'git clone {self.repository} {testdir}'
try:
subprocess.run(command.split(), check=True)
except subprocess.CalledProcessError as error:
raise tmt.utils.GeneralError(
"Failed to clone git repository '{}': {}".format(
self.repository, error))
self.run(
f"git clone {self.repository} {testdir}",
f"Clone '{self.repository}' to '{testdir}'.")
# Checkout revision if requested
if self.revision:
try:
command = f'git checkout -f {self.revision}'
subprocess.run(command.split(), cwd=testdir, check=True)
except subprocess.CalledProcessError as error:
raise tmt.utils.GeneralError(
"Failed to checkout revision '{}': {}".format(
self.revision, error))
self.run(
f"git checkout -f {self.revision}",
f"Checkout revision '{self.revision}'.")
self.tests_tree = fmf.Tree(testdir)


def tests(self):
""" Return all discovered tests """
return [
Expand Down
20 changes: 19 additions & 1 deletion tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from collections import OrderedDict
import unicodedata
import subprocess
import fmf.utils
import pprint
import shlex
Expand Down Expand Up @@ -54,6 +55,23 @@ def opt(self, option, default=None):
""" Get an option from the command line context (instance version) """
return self.__class__._opt(option, default)

def run(self, command, message=None):
""" Run command in the workdir, give message, handle errors """
# Use a generic message if none given, prepare error message
if not message:
message = "Run command '{}'.".format(
' '.join(command) if isinstance(command, list) else command)
echo(message)
message = "Failed to " + message[0].lower() + message[1:]

# Split command if needed and run it
if isinstance(command, str):
command = command.split()
try:
subprocess.run(command, check=True, cwd=self.workdir)
except subprocess.CalledProcessError as error:
raise GeneralError(f"{message}\n{error}")

def _workdir_init(self, id_):
"""
Initialize the work directory
Expand Down Expand Up @@ -81,7 +99,7 @@ def _workdir_init(self, id_):
if not os.path.exists(workdir):
break
if id_ == WORKDIR_MAX:
raise tmt.utils.GeneralError(
raise GeneralError(
"Cleanup the '{}' directory.".format(WORKDIR_ROOT))

# Create the workdir
Expand Down

0 comments on commit 62f3fde

Please sign in to comment.