Merge pull request #184 from setrofim/master

ANSI escape sequences stripping fixes for gem5
diff --git a/devlib/instrument/acmecape.py b/devlib/instrument/acmecape.py
index 1053c9d..818094f 100644
--- a/devlib/instrument/acmecape.py
+++ b/devlib/instrument/acmecape.py
@@ -37,7 +37,7 @@
     mode = CONTINUOUS
 
     def __init__(self, target,
-                 iio_capture=which('iio_capture'),
+                 iio_capture=which('iio-capture'),
                  host='baylibre-acme.local',
                  iio_device='iio:device0',
                  buffer_size=256):
@@ -77,17 +77,25 @@
     def stop(self):
         self.process.terminate()
         timeout_secs = 10
+        output = ''
         for _ in xrange(timeout_secs):
             if self.process.poll() is not None:
                 break
             time.sleep(1)
         else:
-            output = _read_nonblock(self.process.stdout)
+            output += _read_nonblock(self.process.stdout)
             self.process.kill()
             self.logger.error('iio-capture did not terminate gracefully')
             if self.process.poll() is None:
                 msg = 'Could not terminate iio-capture:\n{}'
                 raise HostError(msg.format(output))
+        if self.process.returncode != 15: # iio-capture exits with 15 when killed
+            output += self.process.stdout.read()
+            self.logger.info('ACME instrument encountered an error, '
+                             'you may want to try rebooting the ACME device:\n'
+                             '  ssh root@{} reboot'.format(self.host))
+            raise HostError('iio-capture exited with an error ({}), output:\n{}'
+                            .format(self.process.returncode, output))
         if not os.path.isfile(self.raw_data_file):
             raise HostError('Output CSV not generated.')
 
diff --git a/devlib/target.py b/devlib/target.py
index 6d2a12d..b2cfd2f 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -215,7 +215,9 @@
         tid = id(threading.current_thread())
         self._connections[tid] = self.get_connection(timeout=timeout)
         self._resolve_paths()
-        self.busybox = self.get_installed('busybox')
+        self.execute('mkdir -p {}'.format(self.working_directory))
+        self.execute('mkdir -p {}'.format(self.executables_directory))
+        self.busybox = self.install(os.path.join(PACKAGE_BIN_DIRECTORY, self.abi, 'busybox'))
         self.platform.update_from_target(self)
         self._update_modules('connected')
         if self.platform.big_core and self.load_default_modules:
@@ -232,10 +234,6 @@
         return self.conn_cls(timeout=timeout, **self.connection_settings)  # pylint: disable=not-callable
 
     def setup(self, executables=None):
-        self.execute('mkdir -p {}'.format(self.working_directory))
-        self.execute('mkdir -p {}'.format(self.executables_directory))
-        self.busybox = self.install(os.path.join(PACKAGE_BIN_DIRECTORY, self.abi, 'busybox'))
-
         self._setup_shutils()
 
         for host_exe in (executables or []):  # pylint: disable=superfluous-parens
@@ -952,6 +950,7 @@
                                             shell_prompt=shell_prompt,
                                             conn_cls=conn_cls)
         self.package_data_directory = package_data_directory
+        self.clear_logcat_lock = threading.Lock()
 
     def reset(self, fastboot=False):  # pylint: disable=arguments-differ
         try:
@@ -1184,7 +1183,8 @@
         adb_command(self.adb_name, command, timeout=timeout)
 
     def clear_logcat(self):
-        adb_command(self.adb_name, 'logcat -c', timeout=30)
+        with self.clear_logcat_lock:
+            adb_command(self.adb_name, 'logcat -c', timeout=30)
 
     def get_logcat_monitor(self, regexps=None):
         return LogcatMonitor(self, regexps)
diff --git a/devlib/utils/android.py b/devlib/utils/android.py
index ce1ab0b..0cdd2b0 100644
--- a/devlib/utils/android.py
+++ b/devlib/utils/android.py
@@ -135,7 +135,11 @@
         _check_env()
         command = [aapt, 'dump', 'badging', apk_path]
         logger.debug(' '.join(command))
-        output = subprocess.check_output(command)
+        try:
+            output = subprocess.check_output(command, stderr=subprocess.STDOUT)
+        except subprocess.CalledProcessError as e:
+            raise HostError('Error parsing APK file {}. `aapt` says:\n{}'
+                            .format(apk_path, e.output))
         for line in output.split('\n'):
             if line.startswith('application-label:'):
                 self.label = line.split(':')[1].strip().replace('\'', '')