4
* Uses "flake8" to detect various errors in Python code.
5
* Requires version 1.7.0 or newer of flake8.
7
final class ArcanistFlake8Linter extends ArcanistExternalLinter {
9
public function getInfoName() {
13
public function getInfoURI() {
14
return 'https://pypi.python.org/pypi/flake8';
17
public function getInfoDescription() {
19
'Uses `flake8` to run several linters (PyFlakes, pep8, and a McCabe '.
20
'complexity checker) on Python source files.');
23
public function getLinterName() {
27
public function getLinterConfigurationName() {
31
public function getDefaultFlags() {
32
return $this->getDeprecatedConfiguration('lint.flake8.options', array());
35
public function getDefaultBinary() {
36
$prefix = $this->getDeprecatedConfiguration('lint.flake8.prefix');
37
$bin = $this->getDeprecatedConfiguration('lint.flake8.bin', 'flake8');
40
return $prefix.'/'.$bin;
46
public function getVersion() {
47
list($stdout) = execx('%C --version', $this->getExecutableCommand());
50
if (preg_match('/^(?P<version>\d+\.\d+(?:\.\d+)?)\b/', $stdout, $matches)) {
51
return $matches['version'];
57
public function getInstallInstructions() {
58
return pht('Install flake8 using `easy_install flake8`.');
61
public function shouldExpectCommandErrors() {
65
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
66
$lines = phutil_split_lines($stdout, false);
69
foreach ($lines as $line) {
71
// stdin:2: W802 undefined name 'foo' # pyflakes
72
// stdin:3:1: E302 expected 2 blank lines, found 1 # pep8
73
$regexp = '/^(.*?):(\d+):(?:(\d+):)? (\S+) (.*)$/';
74
if (!preg_match($regexp, $line, $matches)) {
77
foreach ($matches as $key => $match) {
78
$matches[$key] = trim($match);
81
$message = new ArcanistLintMessage();
82
$message->setPath($path);
83
$message->setLine($matches[2]);
84
if (!empty($matches[3])) {
85
$message->setChar($matches[3]);
87
$message->setCode($matches[4]);
88
$message->setName($this->getLinterName().' '.$matches[3]);
89
$message->setDescription($matches[5]);
90
$message->setSeverity($this->getLintMessageSeverity($matches[4]));
92
$messages[] = $message;
95
if ($err && !$messages) {
102
protected function getDefaultMessageSeverity($code) {
103
if (preg_match('/^C/', $code)) {
104
// "C": Cyclomatic complexity
105
return ArcanistLintSeverity::SEVERITY_ADVICE;
106
} else if (preg_match('/^W/', $code)) {
108
return ArcanistLintSeverity::SEVERITY_WARNING;
111
// "F": PyFlakes Error
112
return ArcanistLintSeverity::SEVERITY_ERROR;
116
protected function getLintCodeFromLinterConfigurationKey($code) {
117
if (!preg_match('/^(E|W|C|F)\d+$/', $code)) {
120
'Unrecognized lint message code "%s". Expected a valid flake8 '.
121
'lint code like "%s", or "%s", or "%s", or "%s".',