blob: 4ae871c65773908e5c1b9dd2bc481a4cb8866143 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001import os
2import sys
3import errno
4import datetime
5import itertools
6from commands import runCmd
7
8def get_host_dumper(d):
9 cmds = d.getVar("testimage_dump_host", True)
10 parent_dir = d.getVar("TESTIMAGE_DUMP_DIR", True)
11 return HostDumper(cmds, parent_dir)
12
13
14class BaseDumper(object):
15 """ Base class to dump commands from host/target """
16
17 def __init__(self, cmds, parent_dir):
18 self.cmds = []
19 self.parent_dir = parent_dir
20 if not cmds:
21 return
22 for cmd in cmds.split('\n'):
23 cmd = cmd.lstrip()
24 if not cmd or cmd[0] == '#':
25 continue
26 self.cmds.append(cmd)
27
28 def create_dir(self, dir_suffix):
29 dump_subdir = ("%s_%s" % (
30 datetime.datetime.now().strftime('%Y%m%d%H%M'),
31 dir_suffix))
32 dump_dir = os.path.join(self.parent_dir, dump_subdir)
33 try:
34 os.makedirs(dump_dir)
35 except OSError as err:
36 if err.errno != errno.EEXIST:
37 raise err
38 self.dump_dir = dump_dir
39
40 def _write_dump(self, command, output):
41 if isinstance(self, HostDumper):
42 prefix = "host"
43 elif isinstance(self, TargetDumper):
44 prefix = "target"
45 else:
46 prefix = "unknown"
47 for i in itertools.count():
48 filename = "%s_%02d_%s" % (prefix, i, command)
49 fullname = os.path.join(self.dump_dir, filename)
50 if not os.path.exists(fullname):
51 break
52 with open(fullname, 'w') as dump_file:
53 dump_file.write(output)
54
55
56class HostDumper(BaseDumper):
57 """ Class to get dumps from the host running the tests """
58
59 def __init__(self, cmds, parent_dir):
60 super(HostDumper, self).__init__(cmds, parent_dir)
61
62 def dump_host(self, dump_dir=""):
63 if dump_dir:
64 self.dump_dir = dump_dir
65 for cmd in self.cmds:
66 result = runCmd(cmd, ignore_status=True)
67 self._write_dump(cmd.split()[0], result.output)
68
69
70class TargetDumper(BaseDumper):
71 """ Class to get dumps from target, it only works with QemuRunner """
72
73 def __init__(self, cmds, parent_dir, qemurunner):
74 super(TargetDumper, self).__init__(cmds, parent_dir)
75 self.runner = qemurunner
76
77 def dump_target(self, dump_dir=""):
78 if dump_dir:
79 self.dump_dir = dump_dir
80 for cmd in self.cmds:
81 # We can continue with the testing if serial commands fail
82 try:
83 (status, output) = self.runner.run_serial(cmd)
84 self._write_dump(cmd.split()[0], output)
85 except:
86 print("Tried to dump info from target but "
87 "serial console failed")