2011-05-19 11:00:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses "PyFlakes" to detect various errors in Python code.
|
|
|
|
*
|
|
|
|
* @group linter
|
|
|
|
*/
|
2012-01-31 21:07:05 +01:00
|
|
|
final class ArcanistPyFlakesLinter extends ArcanistLinter {
|
2011-05-19 11:00:59 +02:00
|
|
|
|
|
|
|
public function willLintPaths(array $paths) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'PyFlakes';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLintSeverityMap() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLintNameMap() {
|
|
|
|
return array(
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPyFlakesOptions() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function lintPath($path) {
|
|
|
|
$working_copy = $this->getEngine()->getWorkingCopy();
|
|
|
|
$pyflakes_path = $working_copy->getConfig('lint.pyflakes.path');
|
|
|
|
$pyflakes_prefix = $working_copy->getConfig('lint.pyflakes.prefix');
|
|
|
|
|
|
|
|
// Default to just finding pyflakes in the users path
|
|
|
|
$pyflakes_bin = 'pyflakes';
|
|
|
|
$python_path = '';
|
|
|
|
|
|
|
|
// If a pyflakes path was specified, then just use that as the
|
|
|
|
// pyflakes binary and assume that the libraries will be imported
|
|
|
|
// correctly.
|
|
|
|
//
|
|
|
|
// If no pyflakes path was specified and a pyflakes prefix was
|
|
|
|
// specified, then use the binary from this prefix and add it to
|
|
|
|
// the PYTHONPATH environment variable so that the libs are imported
|
|
|
|
// correctly. This is useful when pyflakes is installed into a
|
|
|
|
// non-default location.
|
|
|
|
if ($pyflakes_path !== null) {
|
|
|
|
$pyflakes_bin = $pyflakes_path;
|
|
|
|
} else if ($pyflakes_prefix !== null) {
|
|
|
|
$pyflakes_bin = $pyflakes_prefix.'/bin/pyflakes';
|
|
|
|
$python_path = $pyflakes_prefix.'/lib/python2.6/site-packages:';
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = $this->getPyFlakesOptions();
|
|
|
|
|
|
|
|
$f = new ExecFuture(
|
|
|
|
"/usr/bin/env PYTHONPATH=%s\$PYTHONPATH ".
|
|
|
|
"{$pyflakes_bin} {$options}", $python_path);
|
|
|
|
$f->write($this->getData($path));
|
|
|
|
|
|
|
|
try {
|
|
|
|
list($stdout, $_) = $f->resolvex();
|
|
|
|
} catch (CommandException $e) {
|
|
|
|
// PyFlakes will return an exit code of 1 if warnings/errors
|
|
|
|
// are found but print nothing to stderr in this case. Therefore,
|
|
|
|
// if we see any output on stderr or a return code other than 1 or 0,
|
|
|
|
// pyflakes failed.
|
|
|
|
if ($e->getError() !== 1 || $e->getStderr() !== '') {
|
|
|
|
throw $e;
|
|
|
|
} else {
|
|
|
|
$stdout = $e->getStdout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$lines = explode("\n", $stdout);
|
|
|
|
$messages = array();
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$matches = null;
|
|
|
|
if (!preg_match('/^(.*?):(\d+): (.*)$/', $line, $matches)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($matches as $key => $match) {
|
|
|
|
$matches[$key] = trim($match);
|
|
|
|
}
|
2012-07-19 19:40:31 +02:00
|
|
|
|
|
|
|
$severity = ArcanistLintSeverity::SEVERITY_WARNING;
|
|
|
|
$description = $matches[3];
|
2012-11-22 02:16:52 +01:00
|
|
|
|
|
|
|
$error_regexp = '/(^undefined|^duplicate|before assignment$)/';
|
|
|
|
if (preg_match($error_regexp, $description)) {
|
2012-07-19 19:40:31 +02:00
|
|
|
$severity = ArcanistLintSeverity::SEVERITY_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-05-19 11:00:59 +02:00
|
|
|
$message = new ArcanistLintMessage();
|
|
|
|
$message->setPath($path);
|
|
|
|
$message->setLine($matches[2]);
|
|
|
|
$message->setCode($this->getLinterName());
|
2012-07-19 19:40:31 +02:00
|
|
|
$message->setDescription($description);
|
|
|
|
$message->setSeverity($severity);
|
2011-05-19 11:00:59 +02:00
|
|
|
$this->addLintMessage($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|