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.
|
|
|
|
*/
|
2013-08-23 20:52:54 +02:00
|
|
|
final class ArcanistFlake8Linter extends ArcanistExternalLinter {
|
2012-12-22 00:27:52 +01:00
|
|
|
|
2014-05-12 01:16:45 +02:00
|
|
|
public function getInfoName() {
|
|
|
|
return 'Flake8';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInfoURI() {
|
|
|
|
return 'https://pypi.python.org/pypi/flake8';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInfoDescription() {
|
|
|
|
return pht(
|
|
|
|
'Uses `flake8` to run several linters (PyFlakes, pep8, and a McCabe '.
|
|
|
|
'complexity checker) on Python source files.');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
Add a `getVersion` function to `ArcanistExternalLinter`.
Summary:
This method will, theoretically, allow `arc lint` to be configured to require some minimum version of an external linter (although this would probably require significantly more work).
Additionally, the existence of this method simplifies the `getCacheVersion` function which, previously, was implemented by the external linters individually. Instead, a general approach to determining the version for cacheing purposes can be used.
Fixes T4954.
Test Plan: I'm not sure how to test this.
Reviewers: chad, epriestley, #blessed_reviewers
Reviewed By: epriestley, #blessed_reviewers
Subscribers: epriestley, Korvin
Maniphest Tasks: T4954
Differential Revision: https://secure.phabricator.com/D8971
2014-05-06 00:10:20 +02:00
|
|
|
public function getVersion() {
|
|
|
|
list($stdout) = execx('%C --version', $this->getExecutableCommand());
|
|
|
|
|
|
|
|
$matches = array();
|
|
|
|
if (preg_match('/^(?P<version>\d+\.\d+\.\d+)\b/', $stdout, $matches)) {
|
|
|
|
return $matches['version'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|