From 1a5c1dce07468c6e544ff5f6dbda7e64e0e76452 Mon Sep 17 00:00:00 2001
From: Valentin Schneider <valentin.schneider@arm.com>
Date: Mon, 22 May 2017 17:57:32 +0100
Subject: [PATCH] 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
---
 devlib/target.py | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/devlib/target.py b/devlib/target.py
index 35473b4..16ccea6 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -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)