2012-12-22 00:27:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses "flake8" to detect various errors in Python code.
|
|
|
|
* Requires version 1.7.0 or newer of flake8.
|
|
|
|
*
|
|
|
|
* @group linter
|
|
|
|
*/
|
2013-08-23 20:52:54 +02:00
|
|
|
final class ArcanistFlake8Linter extends ArcanistExternalLinter {
|
2012-12-22 00:27:52 +01:00
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'flake8';
|
|
|
|
}
|
|
|
|
|
2013-08-23 20:58:07 +02:00
|
|
|
public function getLinterConfigurationName() {
|
|
|
|
return 'flake8';
|
|
|
|
}
|
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
public function getDefaultFlags() {
|
|
|
|
// TODO: Deprecated.
|
2013-10-23 00:34:06 +02:00
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
2014-04-24 01:22:49 +02:00
|
|
|
return $config->getConfigFromAnySource('lint.flake8.options', array());
|
2012-12-22 00:27:52 +01:00
|
|
|
}
|
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
public function getDefaultBinary() {
|
2013-10-23 00:34:06 +02:00
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
|
|
|
$prefix = $config->getConfigFromAnySource('lint.flake8.prefix');
|
|
|
|
$bin = $config->getConfigFromAnySource('lint.flake8.bin', 'flake8');
|
2012-12-22 00:27:52 +01:00
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
if ($prefix || ($bin != 'flake8')) {
|
|
|
|
return $prefix.'/'.$bin;
|
2013-01-09 22:37:08 +01:00
|
|
|
}
|
2012-12-22 00:27:52 +01:00
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
return 'flake8';
|
|
|
|
}
|
2012-12-22 00:27:52 +01:00
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
public function getInstallInstructions() {
|
|
|
|
return pht('Install flake8 using `easy_install flake8`.');
|
2012-12-22 00:27:52 +01:00
|
|
|
}
|
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
public function supportsReadDataFromStdin() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-22 00:27:52 +01:00
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
public function getReadDataFromStdinFilename() {
|
|
|
|
return '-';
|
|
|
|
}
|
2012-12-22 00:27:52 +01:00
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
public function shouldExpectCommandErrors() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-22 00:27:52 +01:00
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
|
|
|
|
$lines = phutil_split_lines($stdout, $retain_endings = false);
|
2012-12-22 00:27:52 +01:00
|
|
|
|
|
|
|
$messages = array();
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$matches = null;
|
|
|
|
// stdin:2: W802 undefined name 'foo' # pyflakes
|
|
|
|
// stdin:3:1: E302 expected 2 blank lines, found 1 # pep8
|
2013-08-23 20:52:54 +02:00
|
|
|
$regexp = '/^(.*?):(\d+):(?:(\d+):)? (\S+) (.*)$/';
|
|
|
|
if (!preg_match($regexp, $line, $matches)) {
|
2012-12-22 00:27:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($matches as $key => $match) {
|
|
|
|
$matches[$key] = trim($match);
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = new ArcanistLintMessage();
|
|
|
|
$message->setPath($path);
|
|
|
|
$message->setLine($matches[2]);
|
2013-08-23 20:52:54 +02:00
|
|
|
if (!empty($matches[3])) {
|
2012-12-22 00:27:52 +01:00
|
|
|
$message->setChar($matches[3]);
|
2013-08-23 20:52:54 +02:00
|
|
|
}
|
2012-12-22 00:27:52 +01:00
|
|
|
$message->setCode($matches[4]);
|
2012-12-22 01:13:16 +01:00
|
|
|
$message->setName($this->getLinterName().' '.$matches[3]);
|
|
|
|
$message->setDescription($matches[5]);
|
2013-08-23 20:58:07 +02:00
|
|
|
$message->setSeverity($this->getLintMessageSeverity($matches[4]));
|
2013-08-23 20:52:54 +02:00
|
|
|
|
|
|
|
$messages[] = $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($err && !$messages) {
|
|
|
|
return false;
|
2012-12-22 00:27:52 +01:00
|
|
|
}
|
2013-08-23 20:52:54 +02:00
|
|
|
|
|
|
|
return $messages;
|
2012-12-22 00:27:52 +01:00
|
|
|
}
|
|
|
|
|
2013-08-23 20:58:07 +02:00
|
|
|
protected function getDefaultMessageSeverity($code) {
|
|
|
|
if (preg_match('/^C/', $code)) {
|
|
|
|
// "C": Cyclomatic complexity
|
|
|
|
return ArcanistLintSeverity::SEVERITY_ADVICE;
|
|
|
|
} else if (preg_match('/^W/', $code)) {
|
|
|
|
// "W": PEP8 Warning
|
|
|
|
return ArcanistLintSeverity::SEVERITY_WARNING;
|
|
|
|
} else {
|
|
|
|
// "E": PEP8 Error
|
|
|
|
// "F": PyFlakes Error
|
|
|
|
return ArcanistLintSeverity::SEVERITY_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLintCodeFromLinterConfigurationKey($code) {
|
|
|
|
if (!preg_match('/^(E|W|C|F)\d+$/', $code)) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Unrecognized lint message code "%s". Expected a valid flake8 '.
|
|
|
|
'lint code like "%s", or "%s", or "%s", or "%s".',
|
|
|
|
$code,
|
|
|
|
"E225",
|
|
|
|
"W291",
|
|
|
|
"F811",
|
|
|
|
"C901"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
2012-12-22 00:27:52 +01:00
|
|
|
}
|