pymtt
 All Classes Namespaces Files Functions Variables Groups
TextFile.py
Go to the documentation of this file.
1 # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: f; python-indent: 4 -*-
2 #
3 # Copyright (c) 2015-2018 Intel, Inc. All rights reserved.
4 # $COPYRIGHT$
5 #
6 # Additional copyrights may follow
7 #
8 # $HEADER$
9 #
10 
11 from __future__ import print_function
12 import os
13 import sys
14 from ReporterMTTStage import *
15 
16 ## @addtogroup Stages
17 # @{
18 # @addtogroup Reporter
19 # @section TextFile
20 # File reporter plugin
21 # @param filename Name of the file into which the report is to be written
22 # @param summary_footer Footer to be placed at bottom of summary
23 # @param detail_header Header to be put at top of detail report
24 # @param detail_footer Footer to be placed at bottome of detail report
25 # @param textwrap Max line length before wrapping
26 # @}
28 
29  def __init__(self):
30  # initialise parent class
31  ReporterMTTStage.__init__(self)
32  self.options = {}
33  self.options['filename'] = (None, "Name of the file into which the report is to be written")
34  self.options['summary_footer'] = (None, "Footer to be placed at bottom of summary")
35  self.options['detail_header'] = (None, "Header to be put at top of detail report")
36  self.options['detail_footer'] = (None, "Footer to be placed at bottome of detail report")
37  self.options['textwrap'] = ("80", "Max line length before wrapping")
38  self.fh = sys.stdout
39 
40  def activate(self):
41  # get the automatic procedure from IPlugin
42  IPlugin.activate(self)
43  return
44 
45 
46  def deactivate(self):
47  IPlugin.deactivate(self)
48  return
49 
50  def print_name(self):
51  return "TextFile"
52 
53  def print_options(self, testDef, prefix):
54  lines = testDef.printOptions(self.options)
55  for line in lines:
56  print(prefix + line)
57  return
58 
59  def _print_stderr_block(self, name, lines, tabs=1):
60  if lines:
61  print("\t"*tabs,"ERROR ({name})".format(name=name), file=self.fh)
62  for l in lines:
63  print("\t"*(tabs)," ",l, file=self.fh)
64 
65  def execute(self, log, keyvals, testDef):
66  testDef.logger.verbose_print("TextFile Reporter")
67  # pickup the options
68  cmds = {}
69  testDef.parseOptions(log, self.options, keyvals, cmds)
70  if cmds['filename'] is not None:
71  self.fh = open(cmds['filename'] if os.path.isabs(cmds['filename']) \
72  else os.path.join(cmds['scratch'],cmds['filename']), 'w')
73  if testDef.options['description'] is not None:
74  print(testDef.options['description'], file=self.fh)
75  print(file=self.fh)
76  # get the entire log of results
77  fullLog = testDef.logger.getLog(None)
78  for lg in fullLog:
79  try:
80  print("Section:",lg['section'],"Status:",lg['status'], file=self.fh)
81  try:
82  if lg['parameters'] is not None:
83  print("\tInput parameters:", file=self.fh)
84  for p in lg['parameters']:
85  print("\t\t",p[0],"=",p[1], file=self.fh)
86  except KeyError:
87  pass
88  try:
89  if lg['options'] is not None:
90  print("\tFinal options:", file=self.fh)
91  opts = lg['options']
92  keys = list(opts.keys())
93  for p in keys:
94  print("\t\t",p,"=",opts[p], file=self.fh)
95  except KeyError:
96  pass
97  if 0 != lg['status']:
98  if "stderr" in lg:
99  self._print_stderr_block("stderr", lg['stderr'], tabs=1)
100  if "stdout" in lg:
101  self._print_stderr_block("stdout", lg['stdout'], tabs=1)
102  except KeyError:
103  pass
104  try:
105  if lg['compiler'] is not None:
106  print("Compiler:", file=self.fh)
107  comp = lg['compiler']
108  print("\t",comp['family'], file=self.fh)
109  print("\t",comp['version'], file=self.fh)
110  except KeyError:
111  pass
112  try:
113  if lg['profile'] is not None:
114  prf = lg['profile']
115  keys = list(prf.keys())
116  # find the max length of the keys
117  max1 = 0
118  for key in keys:
119  if len(key) > max1:
120  max1 = len(key)
121  # add some padding
122  max1 = max1 + 4
123  # now provide the output
124  print("\tProfile:", file=self.fh)
125  sp = " "
126  for key in keys:
127  line = key + (max1-len(key))*sp + '\n'.join(prf[key])
128  print("\t\t",line, file=self.fh)
129  except KeyError:
130  pass
131  try:
132  if lg['numTests'] is not None:
133  print("\tTests:",lg['numTests'],"Pass:",lg['numPass'],"Skip:",lg['numSkip'],"Fail:",lg['numFail'], file=self.fh)
134  except KeyError:
135  pass
136  try:
137  if lg['testresults'] is not None:
138  for test in lg['testresults']:
139  tname = os.path.basename(test['test'])
140  print("\t\t",tname," Status:",test['status'], file=self.fh)
141  if 0 != test['status']:
142  if "stderr" in test:
143  self._print_error_block("stderr", test['stderr'], tabs=3)
144  if "stdout" in test:
145  self._print_error_block("stdout", test['stdout'], tabs=3)
146  except KeyError:
147  pass
148  print(file=self.fh)
149  if cmds['filename'] is not None:
150  self.fh.close()
151  log['status'] = 0
152  return
def _print_stderr_block
Definition: TextFile.py:59