1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-02-07 05:30:44 +00:00

target: Add dry run for command execution

It sometimes can be useful to enable dry-run before calling
high-level functions in order to check what individual commands
are being called (or do whatever else with them).

This patch adds dry-run with an on/off switch:
once start_dryrun() is called, every subsequent command passed to
execute() will be accumulated in a list.
stop_dryrun() disables dry-run, and the accumulated commands can
be fetched via Target.dryrun
This commit is contained in:
Valentin Schneider 2017-05-22 17:57:32 +01:00
parent 1f7421bc39
commit 1a5c1dce07

View File

@ -145,6 +145,10 @@ class Target(object):
else:
return None
@property
def dryrun(self):
return self._dryrun
def __init__(self,
connection_settings=None,
platform=None,
@ -186,6 +190,8 @@ class Target(object):
self._cache = {}
self._connections = {}
self.busybox = None
self._dryrun_enabled = False
self._dryrun = []
if load_default_modules:
module_lists = [self.default_modules]
@ -311,8 +317,20 @@ class Target(object):
# execution
def start_dryrun(self):
if not self._dryrun_enabled:
self._dryrun_enabled = True
self._dryrun = []
def stop_dryrun(self):
if self._dryrun_enabled:
self._dryrun_enabled = False
def execute(self, command, timeout=None, check_exit_code=True, as_root=False):
return self.conn.execute(command, timeout, check_exit_code, as_root)
if self._dryrun_enabled:
self._dryrun.append(command)
else:
return self.conn.execute(command, timeout, check_exit_code, as_root)
def background(self, command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, as_root=False):
return self.conn.background(command, stdout, stderr, as_root)