1 | # -*- coding: UTF-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2007-2010 Edgewall Software |
---|
4 | # Copyright (C) 2007 Wei Zhuo <weizhuo@gmail.com> |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | "Recipe commands for tools commonly used in PHP projects." |
---|
12 | |
---|
13 | import logging |
---|
14 | import os |
---|
15 | import shlex |
---|
16 | |
---|
17 | from bitten.util import xmlio |
---|
18 | from bitten.build import shtools |
---|
19 | |
---|
20 | log = logging.getLogger('bitten.build.phptools') |
---|
21 | |
---|
22 | def phing(ctxt, file_=None, target=None, executable=None, args=None): |
---|
23 | """Run a phing build""" |
---|
24 | if args: |
---|
25 | args = shlex.split(args) |
---|
26 | else: |
---|
27 | args = [] |
---|
28 | args += ['-logger', 'phing.listener.DefaultLogger', |
---|
29 | '-buildfile', ctxt.resolve(file_ or 'build.xml')] |
---|
30 | if target: |
---|
31 | args.append(target) |
---|
32 | |
---|
33 | returncode = shtools.execute(ctxt, file_=executable or 'phing', args=args) |
---|
34 | if returncode != 0: |
---|
35 | ctxt.error('Phing failed (%s)' % returncode) |
---|
36 | |
---|
37 | def phpcs(ctxt, file_=None): |
---|
38 | """Extract test results from a PHP Code Sniffer full report.""" |
---|
39 | assert file_, 'Missing required attribute "file"' |
---|
40 | msg_categories = dict(WARNING='warning', ERROR='error') |
---|
41 | |
---|
42 | problems = xmlio.Fragment() |
---|
43 | try: |
---|
44 | fd = open(ctxt.resolve(file_), 'r') |
---|
45 | try: |
---|
46 | filename = None |
---|
47 | for line in fd: |
---|
48 | line = line.strip() |
---|
49 | if (not line or line.startswith("-") or |
---|
50 | line.startswith("FOUND")): |
---|
51 | continue |
---|
52 | if line.startswith("FILE:"): |
---|
53 | filename = line.split(":", 1)[1].strip() |
---|
54 | if (os.path.isabs(filename) and |
---|
55 | filename.startswith(ctxt.basedir)): |
---|
56 | filename = filename[len(ctxt.basedir) + 1:] |
---|
57 | filename = filename.replace('\\', '/') |
---|
58 | continue |
---|
59 | parts = line.split("|") |
---|
60 | if len(parts) != 3 or not parts[0]: |
---|
61 | continue |
---|
62 | |
---|
63 | lineno = int(parts[0].strip()) |
---|
64 | type = msg_categories[parts[1].strip()] |
---|
65 | tag = type |
---|
66 | category = type |
---|
67 | msg = parts[2].strip() |
---|
68 | problems.append(xmlio.Element('problem', category=category, |
---|
69 | type=type, tag=tag, |
---|
70 | line=lineno, file=filename)[ |
---|
71 | xmlio.Element('msg')[msg]]) |
---|
72 | ctxt.report('lint', problems) |
---|
73 | except Exception, e: |
---|
74 | log.warning('Error parsing phpcs report file (%s)', e) |
---|
75 | finally: |
---|
76 | fd.close() |
---|
77 | except IOError, e: |
---|
78 | log.warning('Error opening phpcs report file (%s)', e) |
---|
79 | |
---|
80 | |
---|
81 | def phpunit(ctxt, file_=None): |
---|
82 | """Extract test results from a PHPUnit XML report.""" |
---|
83 | assert file_, 'Missing required attribute "file"' |
---|
84 | |
---|
85 | def _process_testsuite(testsuite, results, parent_file=''): |
---|
86 | for testcase in testsuite.children(): |
---|
87 | if testcase.name == 'testsuite': |
---|
88 | _process_testsuite(testcase, results, |
---|
89 | parent_file=testcase.attr.get('file', parent_file)) |
---|
90 | continue |
---|
91 | test = xmlio.Element('test') |
---|
92 | test.attr['fixture'] = testsuite.attr['name'] |
---|
93 | test.attr['name'] = testcase.attr['name'] |
---|
94 | test.attr['duration'] = testcase.attr['time'] |
---|
95 | result = list(testcase.children()) |
---|
96 | if result: |
---|
97 | test.append(xmlio.Element('traceback')[ |
---|
98 | result[0].gettext() |
---|
99 | ]) |
---|
100 | test.attr['status'] = result[0].name |
---|
101 | else: |
---|
102 | test.attr['status'] = 'success' |
---|
103 | if 'file' in testsuite.attr or parent_file: |
---|
104 | testfile = os.path.realpath( |
---|
105 | testsuite.attr.get('file', parent_file)) |
---|
106 | if testfile.startswith(ctxt.basedir): |
---|
107 | testfile = testfile[len(ctxt.basedir) + 1:] |
---|
108 | testfile = testfile.replace(os.sep, '/') |
---|
109 | test.attr['file'] = testfile |
---|
110 | results.append(test) |
---|
111 | |
---|
112 | try: |
---|
113 | total, failed = 0, 0 |
---|
114 | results = xmlio.Fragment() |
---|
115 | fileobj = file(ctxt.resolve(file_), 'r') |
---|
116 | try: |
---|
117 | for testsuite in xmlio.parse(fileobj).children('testsuite'): |
---|
118 | total += int(testsuite.attr['tests']) |
---|
119 | failed += int(testsuite.attr['failures']) + \ |
---|
120 | int(testsuite.attr['errors']) |
---|
121 | |
---|
122 | _process_testsuite(testsuite, results) |
---|
123 | finally: |
---|
124 | fileobj.close() |
---|
125 | if failed: |
---|
126 | ctxt.error('%d of %d test%s failed' % (failed, total, |
---|
127 | total != 1 and 's' or '')) |
---|
128 | ctxt.report('test', results) |
---|
129 | except IOError, e: |
---|
130 | ctxt.log('Error opening PHPUnit results file (%s)' % e) |
---|
131 | except xmlio.ParseError, e: |
---|
132 | ctxt.log('Error parsing PHPUnit results file (%s)' % e) |
---|
133 | |
---|
134 | def coverage(ctxt, file_=None): |
---|
135 | """Extract data from Phing or PHPUnit code coverage report.""" |
---|
136 | assert file_, 'Missing required attribute "file"' |
---|
137 | |
---|
138 | def _process_phing_coverage(ctxt, element, coverage): |
---|
139 | for cls in element.children('class'): |
---|
140 | statements = float(cls.attr['statementcount']) |
---|
141 | covered = float(cls.attr['statementscovered']) |
---|
142 | if statements: |
---|
143 | percentage = covered / statements * 100 |
---|
144 | else: |
---|
145 | percentage = 100 |
---|
146 | class_coverage = xmlio.Element('coverage', |
---|
147 | name=cls.attr['name'], |
---|
148 | lines=int(statements), |
---|
149 | percentage=percentage |
---|
150 | ) |
---|
151 | source = list(cls.children())[0] |
---|
152 | if 'sourcefile' in source.attr: |
---|
153 | sourcefile = os.path.realpath(source.attr['sourcefile']) |
---|
154 | if sourcefile.startswith(ctxt.basedir): |
---|
155 | sourcefile = sourcefile[len(ctxt.basedir) + 1:] |
---|
156 | sourcefile = sourcefile.replace(os.sep, '/') |
---|
157 | class_coverage.attr['file'] = sourcefile |
---|
158 | coverage.append(class_coverage) |
---|
159 | |
---|
160 | def _process_phpunit_coverage(ctxt, element, coverage): |
---|
161 | for cls in element._node.getElementsByTagName('class'): |
---|
162 | sourcefile = cls.parentNode.getAttribute('name') |
---|
163 | if not os.path.isabs(sourcefile): |
---|
164 | sourcefile = os.path.join(ctxt.basedir, sourcefile) |
---|
165 | if sourcefile.startswith(ctxt.basedir): |
---|
166 | loc, ncloc = 0, 0.0 |
---|
167 | for line in cls.parentNode.getElementsByTagName('line'): |
---|
168 | if str(line.getAttribute('type')) == 'stmt': |
---|
169 | loc += 1 |
---|
170 | if int(line.getAttribute('count')) == 0: |
---|
171 | ncloc += 1 |
---|
172 | if loc > 0: |
---|
173 | percentage = 100 - (ncloc / loc * 100) |
---|
174 | else: |
---|
175 | percentage = 100 |
---|
176 | |
---|
177 | if sourcefile.startswith(ctxt.basedir): |
---|
178 | sourcefile = sourcefile[len(ctxt.basedir) + 1:] |
---|
179 | class_coverage = xmlio.Element('coverage', |
---|
180 | name=cls.getAttribute('name'), |
---|
181 | lines=int(loc), |
---|
182 | percentage=int(percentage), |
---|
183 | file=sourcefile.replace(os.sep, '/')) |
---|
184 | coverage.append(class_coverage) |
---|
185 | |
---|
186 | try: |
---|
187 | summary_file = file(ctxt.resolve(file_), 'r') |
---|
188 | summary = xmlio.parse(summary_file) |
---|
189 | coverage = xmlio.Fragment() |
---|
190 | try: |
---|
191 | for element in summary.children(): |
---|
192 | if element.name == 'package': |
---|
193 | _process_phing_coverage(ctxt, element, coverage) |
---|
194 | elif element.name == 'project': |
---|
195 | _process_phpunit_coverage(ctxt, element, coverage) |
---|
196 | finally: |
---|
197 | summary_file.close() |
---|
198 | ctxt.report('coverage', coverage) |
---|
199 | except IOError, e: |
---|
200 | ctxt.log('Error opening coverage summary file (%s)' % e) |
---|
201 | except xmlio.ParseError, e: |
---|
202 | ctxt.log('Error parsing coverage summary file (%s)' % e) |
---|