| 1 | # -*- coding: utf-8 -*- |
---|
| 2 | # |
---|
| 3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
| 4 | # Copyright (C) 2006 Matthew Good <matt@matt-good.net> |
---|
| 5 | # Copyright (C) 2007-2010 Edgewall Software |
---|
| 6 | # All rights reserved. |
---|
| 7 | # |
---|
| 8 | # This software is licensed as described in the file COPYING, which |
---|
| 9 | # you should have received as part of this distribution. The terms |
---|
| 10 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
| 11 | |
---|
1 | 12 | """Recipe commands for tools commonly used in Java projects.""" |
---|
| 13 | |
---|
1 | 14 | from glob import glob |
---|
1 | 15 | import logging |
---|
1 | 16 | import os |
---|
1 | 17 | import posixpath |
---|
1 | 18 | import shlex |
---|
1 | 19 | import tempfile |
---|
| 20 | |
---|
1 | 21 | from bitten.build import CommandLine |
---|
1 | 22 | from bitten.util import xmlio |
---|
| 23 | |
---|
1 | 24 | log = logging.getLogger('bitten.build.javatools') |
---|
| 25 | |
---|
1 | 26 | __docformat__ = 'restructuredtext en' |
---|
| 27 | |
---|
1 | 28 | def ant(ctxt, file_=None, target=None, keep_going=False, args=None): |
---|
| 29 | """Run an Ant build. |
---|
| 30 | |
---|
| 31 | :param ctxt: the build context |
---|
| 32 | :type ctxt: `Context` |
---|
| 33 | :param file\_: name of the Ant build file |
---|
| 34 | :param target: name of the target that should be executed (optional) |
---|
| 35 | :param keep_going: whether Ant should keep going when errors are encountered |
---|
| 36 | :param args: additional arguments to pass to Ant |
---|
| 37 | """ |
---|
0 | 38 | executable = 'ant' |
---|
0 | 39 | ant_home = ctxt.config.get_dirpath('ant.home') |
---|
0 | 40 | if ant_home: |
---|
0 | 41 | executable = os.path.join(ant_home, 'bin', 'ant') |
---|
| 42 | |
---|
0 | 43 | java_home = ctxt.config.get_dirpath('java.home') |
---|
0 | 44 | if java_home: |
---|
0 | 45 | os.environ['JAVA_HOME'] = java_home |
---|
| 46 | |
---|
0 | 47 | logfile = tempfile.NamedTemporaryFile(prefix='ant_log', suffix='.xml') |
---|
0 | 48 | logfile.close() |
---|
0 | 49 | if args: |
---|
0 | 50 | args = shlex.split(args) |
---|
0 | 51 | else: |
---|
0 | 52 | args = [] |
---|
0 | 53 | args += ['-noinput', '-listener', 'org.apache.tools.ant.XmlLogger', |
---|
0 | 54 | '-Dant.XmlLogger.stylesheet.uri', '""', |
---|
0 | 55 | '-DXmlLogger.file', logfile.name] |
---|
0 | 56 | if file_: |
---|
0 | 57 | args += ['-buildfile', ctxt.resolve(file_)] |
---|
0 | 58 | if keep_going: |
---|
0 | 59 | args.append('-keep-going') |
---|
0 | 60 | if target: |
---|
0 | 61 | args.append(target) |
---|
| 62 | |
---|
0 | 63 | shell = False |
---|
0 | 64 | if os.name == 'nt': |
---|
| 65 | # Need to execute ant.bat through a shell on Windows |
---|
0 | 66 | shell = True |
---|
| 67 | |
---|
0 | 68 | cmdline = CommandLine(executable, args, cwd=ctxt.basedir, shell=shell) |
---|
0 | 69 | for out, err in cmdline.execute(): |
---|
0 | 70 | if out is not None: |
---|
0 | 71 | log.info(out) |
---|
0 | 72 | if err is not None: |
---|
0 | 73 | log.error(err) |
---|
| 74 | |
---|
0 | 75 | error_logged = False |
---|
0 | 76 | log_elem = xmlio.Fragment() |
---|
0 | 77 | try: |
---|
0 | 78 | xml_log = xmlio.parse(file(logfile.name, 'r')) |
---|
0 | 79 | def collect_log_messages(node): |
---|
0 | 80 | for child in node.children(): |
---|
0 | 81 | if child.name == 'message': |
---|
0 | 82 | if child.attr['priority'] == 'debug': |
---|
0 | 83 | continue |
---|
0 | 84 | log_elem.append(xmlio.Element('message', |
---|
0 | 85 | level=child.attr['priority'])[ |
---|
0 | 86 | child.gettext().replace(ctxt.basedir + os.sep, '') |
---|
0 | 87 | .replace(ctxt.basedir, '') |
---|
0 | 88 | ]) |
---|
0 | 89 | else: |
---|
0 | 90 | collect_log_messages(child) |
---|
0 | 91 | collect_log_messages(xml_log) |
---|
| 92 | |
---|
0 | 93 | if 'error' in xml_log.attr: |
---|
0 | 94 | ctxt.error(xml_log.attr['error']) |
---|
0 | 95 | error_logged = True |
---|
| 96 | |
---|
0 | 97 | except xmlio.ParseError, e: |
---|
0 | 98 | log.warning('Error parsing Ant XML log file (%s)', e) |
---|
0 | 99 | ctxt.log(log_elem) |
---|
| 100 | |
---|
0 | 101 | if not error_logged and cmdline.returncode != 0: |
---|
0 | 102 | ctxt.error('Ant failed (%s)' % cmdline.returncode) |
---|
| 103 | |
---|
| 104 | |
---|
1 | 105 | def _fix_traceback(result): |
---|
| 106 | """ |
---|
| 107 | Sometimes the traceback isn't prefixed with the exception type and |
---|
| 108 | message, so add it in if needed. |
---|
| 109 | """ |
---|
2 | 110 | attr = result[0].attr |
---|
2 | 111 | tracebackprefix = "" |
---|
| 112 | # py.test does not include the type tag in some cases so don't do this |
---|
| 113 | # fix when it is missing |
---|
2 | 114 | if "type" in attr: |
---|
0 | 115 | tracebackprefix = "%s: %s" % (attr['type'], attr.get('message', '')) |
---|
2 | 116 | if result[0].gettext().startswith(tracebackprefix): |
---|
2 | 117 | return result[0].gettext() |
---|
2 | 118 | else: |
---|
0 | 119 | return "\n".join((tracebackprefix, result[0].gettext())) |
---|
| 120 | |
---|
| 121 | |
---|
1 | 122 | def junit(ctxt, file_=None, srcdir=None): |
---|
| 123 | """Extract test results from a JUnit XML report. |
---|
| 124 | |
---|
| 125 | :param ctxt: the build context |
---|
| 126 | :type ctxt: `Context` |
---|
| 127 | :param file\_: path to the JUnit XML test results; may contain globbing |
---|
| 128 | wildcards for matching multiple results files |
---|
| 129 | :param srcdir: name of the directory containing the test sources, used to |
---|
| 130 | link test results to the corresponding source files |
---|
| 131 | """ |
---|
3 | 132 | assert file_, 'Missing required attribute "file"' |
---|
3 | 133 | try: |
---|
3 | 134 | total, failed = 0, 0 |
---|
3 | 135 | results = xmlio.Fragment() |
---|
6 | 136 | for path in glob(ctxt.resolve(file_)): |
---|
3 | 137 | fileobj = file(path, 'r') |
---|
3 | 138 | try: |
---|
6 | 139 | for testcase in xmlio.parse(fileobj).children('testcase'): |
---|
3 | 140 | test = xmlio.Element('test') |
---|
3 | 141 | test.attr['fixture'] = testcase.attr['classname'] |
---|
3 | 142 | test.attr['name'] = testcase.attr['name'] |
---|
3 | 143 | if 'time' in testcase.attr: |
---|
3 | 144 | test.attr['duration'] = testcase.attr['time'] |
---|
3 | 145 | if srcdir is not None: |
---|
0 | 146 | cls = testcase.attr['classname'].split('.') |
---|
0 | 147 | test.attr['file'] = posixpath.join(srcdir, *cls) + \ |
---|
0 | 148 | '.java' |
---|
| 149 | |
---|
3 | 150 | result = list(testcase.children()) |
---|
3 | 151 | if result: |
---|
2 | 152 | junit_status = result[0].name |
---|
2 | 153 | test.append(xmlio.Element('traceback')[_fix_traceback(result)]) |
---|
2 | 154 | if junit_status == 'skipped': |
---|
1 | 155 | test.attr['status'] = 'ignore' |
---|
1 | 156 | elif junit_status == 'error': |
---|
1 | 157 | test.attr['status'] = 'error' |
---|
1 | 158 | failed += 1 |
---|
1 | 159 | else: |
---|
0 | 160 | test.attr['status'] = 'failure' |
---|
0 | 161 | failed += 1 |
---|
0 | 162 | else: |
---|
1 | 163 | test.attr['status'] = 'success' |
---|
| 164 | |
---|
3 | 165 | results.append(test) |
---|
3 | 166 | total += 1 |
---|
3 | 167 | finally: |
---|
3 | 168 | fileobj.close() |
---|
3 | 169 | if failed: |
---|
1 | 170 | ctxt.error('%d of %d test%s failed' % (failed, total, |
---|
1 | 171 | total != 1 and 's' or '')) |
---|
3 | 172 | ctxt.report('test', results) |
---|
0 | 173 | except IOError, e: |
---|
0 | 174 | log.warning('Error opening JUnit results file (%s)', e) |
---|
0 | 175 | except xmlio.ParseError, e: |
---|
0 | 176 | log.warning('Error parsing JUnit results file (%s)', e) |
---|
| 177 | |
---|
| 178 | |
---|
2 | 179 | class _LineCounter(object): |
---|
1 | 180 | def __init__(self): |
---|
3 | 181 | self.lines = [] |
---|
3 | 182 | self.covered = 0 |
---|
3 | 183 | self.num_lines = 0 |
---|
| 184 | |
---|
1 | 185 | def __getitem__(self, idx): |
---|
0 | 186 | if idx >= len(self.lines): |
---|
0 | 187 | return 0 |
---|
0 | 188 | return self.lines[idx] |
---|
| 189 | |
---|
1 | 190 | def __setitem__(self, idx, val): |
---|
6 | 191 | idx = int(idx) - 1 # 1-indexed to 0-indexed |
---|
6 | 192 | from itertools import repeat |
---|
6 | 193 | if idx >= len(self.lines): |
---|
6 | 194 | self.lines.extend(repeat('-', idx - len(self.lines) + 1)) |
---|
6 | 195 | self.lines[idx] = val |
---|
6 | 196 | self.num_lines += 1 |
---|
6 | 197 | if val != '0': |
---|
3 | 198 | self.covered += 1 |
---|
| 199 | |
---|
1 | 200 | def line_hits(self): |
---|
3 | 201 | return ' '.join(self.lines) |
---|
1 | 202 | line_hits = property(line_hits) |
---|
| 203 | |
---|
1 | 204 | def percentage(self): |
---|
3 | 205 | if self.num_lines == 0: |
---|
1 | 206 | return 0 |
---|
2 | 207 | return int(round(self.covered * 100. / self.num_lines)) |
---|
1 | 208 | percentage = property(percentage) |
---|
| 209 | |
---|
| 210 | |
---|
1 | 211 | def cobertura(ctxt, file_=None): |
---|
| 212 | """Extract test coverage information from a Cobertura XML report. |
---|
| 213 | |
---|
| 214 | :param ctxt: the build context |
---|
| 215 | :type ctxt: `Context` |
---|
| 216 | :param file\_: path to the Cobertura XML output |
---|
| 217 | """ |
---|
3 | 218 | assert file_, 'Missing required attribute "file"' |
---|
| 219 | |
---|
3 | 220 | coverage = xmlio.Fragment() |
---|
3 | 221 | doc = xmlio.parse(open(ctxt.resolve(file_))) |
---|
3 | 222 | srcdir = [s.gettext().strip() for ss in doc.children('sources') |
---|
6 | 223 | for s in ss.children('source')][0] |
---|
| 224 | |
---|
3 | 225 | classes = [cls for pkgs in doc.children('packages') |
---|
3 | 226 | for pkg in pkgs.children('package') |
---|
3 | 227 | for clss in pkg.children('classes') |
---|
6 | 228 | for cls in clss.children('class')] |
---|
| 229 | |
---|
3 | 230 | counters = {} |
---|
3 | 231 | class_names = {} |
---|
| 232 | |
---|
6 | 233 | for cls in classes: |
---|
3 | 234 | filename = cls.attr['filename'].replace(os.sep, '/') |
---|
3 | 235 | name = cls.attr['name'] |
---|
3 | 236 | if not '$' in name: # ignore internal classes |
---|
3 | 237 | class_names[filename] = name |
---|
3 | 238 | counter = counters.get(filename) |
---|
3 | 239 | if counter is None: |
---|
3 | 240 | counter = counters[filename] = _LineCounter() |
---|
3 | 241 | lines = [l for ls in cls.children('lines') |
---|
9 | 242 | for l in ls.children('line')] |
---|
9 | 243 | for line in lines: |
---|
6 | 244 | counter[line.attr['number']] = line.attr['hits'] |
---|
| 245 | |
---|
6 | 246 | for filename, name in class_names.iteritems(): |
---|
3 | 247 | counter = counters[filename] |
---|
3 | 248 | module = xmlio.Element('coverage', name=name, |
---|
3 | 249 | file=posixpath.join(srcdir, filename), |
---|
3 | 250 | lines=counter.num_lines, |
---|
3 | 251 | percentage=counter.percentage) |
---|
3 | 252 | module.append(xmlio.Element('line_hits')[counter.line_hits]) |
---|
3 | 253 | coverage.append(module) |
---|
3 | 254 | ctxt.report('coverage', coverage) |
---|