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:52:54 +02:00
|
|
|
public function getDefaultFlags() {
|
|
|
|
// TODO: Deprecated.
|
|
|
|
|
2012-12-22 00:27:52 +01:00
|
|
|
$working_copy = $this->getEngine()->getWorkingCopy();
|
|
|
|
|
|
|
|
$options = $working_copy->getConfig('lint.flake8.options', '');
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2013-08-23 20:52:54 +02:00
|
|
|
public function getDefaultBinary() {
|
2012-12-22 00:27:52 +01:00
|
|
|
$working_copy = $this->getEngine()->getWorkingCopy();
|
|
|
|
$prefix = $working_copy->getConfig('lint.flake8.prefix');
|
2013-01-09 22:37:08 +01:00
|
|
|
$bin = $working_copy->getConfig('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);
|
|
|
|
}
|
|
|
|
|
2012-12-22 01:13:16 +01:00
|
|
|
if (substr($matches[4], 0, 1) == 'E') {
|
2012-12-22 00:27:52 +01:00
|
|
|
$severity = ArcanistLintSeverity::SEVERITY_ERROR;
|
2012-12-22 01:13:16 +01:00
|
|
|
} else {
|
|
|
|
$severity = ArcanistLintSeverity::SEVERITY_WARNING;
|
2012-12-22 00:27:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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]);
|
2012-12-22 00:27:52 +01:00
|
|
|
$message->setSeverity($severity);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|