improve PyFlakes, add a unit test
[buildbot.git] / buildbot / steps / python.py
blob2c35818f2402c735b75ed03f4f089aa0d792ed42
2 from buildbot.status.builder import SUCCESS, FAILURE, WARNINGS
3 from buildbot.steps.shell import ShellCommand
5 try:
6 import cStringIO
7 StringIO = cStringIO.StringIO
8 except ImportError:
9 from StringIO import StringIO
12 class BuildEPYDoc(ShellCommand):
13 name = "epydoc"
14 command = ["make", "epydocs"]
15 description = ["building", "epydocs"]
16 descriptionDone = ["epydoc"]
18 def createSummary(self, log):
19 import_errors = 0
20 warnings = 0
21 errors = 0
23 for line in StringIO(log.getText()):
24 if line.startswith("Error importing "):
25 import_errors += 1
26 if line.find("Warning: ") != -1:
27 warnings += 1
28 if line.find("Error: ") != -1:
29 errors += 1
31 self.descriptionDone = self.descriptionDone[:]
32 if import_errors:
33 self.descriptionDone.append("ierr=%d" % import_errors)
34 if warnings:
35 self.descriptionDone.append("warn=%d" % warnings)
36 if errors:
37 self.descriptionDone.append("err=%d" % errors)
39 self.import_errors = import_errors
40 self.warnings = warnings
41 self.errors = errors
43 def evaluateCommand(self, cmd):
44 if cmd.rc != 0:
45 return FAILURE
46 if self.warnings or self.errors:
47 return WARNINGS
48 return SUCCESS
51 class PyFlakes(ShellCommand):
52 name = "pyflakes"
53 command = ["make", "pyflakes"]
54 description = ["running", "pyflakes"]
55 descriptionDone = ["pyflakes"]
56 flunkOnFailure = False
57 flunkingIssues = ["undefined"] # any pyflakes lines like this cause FAILURE
59 MESSAGES = ("unused", "undefined", "redefs", "import*", "misc")
61 def createSummary(self, log):
62 counts = {}
63 summaries = {}
64 for m in self.MESSAGES:
65 counts[m] = 0
66 summaries[m] = []
68 for line in StringIO(log.getText()).readlines():
69 if "imported but unused" in line:
70 m = "unused"
71 elif "*' used; unable to detect undefined names" in line:
72 m = "import*"
73 elif "undefined name" in line:
74 m = "undefined"
75 elif "redefinition of unused" in line:
76 m = "redefs"
77 else:
78 m = "misc"
79 summaries[m].append(line)
80 counts[m] += 1
82 self.descriptionDone = self.descriptionDone[:]
83 for m in self.MESSAGES:
84 if counts[m]:
85 self.descriptionDone.append("%s=%d" % (m, counts[m]))
86 self.addCompleteLog(m, "".join(summaries[m]))
87 self.setProperty("pyflakes-%s" % m, counts[m])
88 self.setProperty("pyflakes-total", sum(counts.values()))
91 def evaluateCommand(self, cmd):
92 if cmd.rc != 0:
93 return FAILURE
94 for m in self.flunkingIssues:
95 if self.getProperty("pyflakes-%s" % m):
96 return FAILURE
97 if self.getProperty("pyflakes-total"):
98 return WARNINGS
99 return SUCCESS