2011-05-22 12:32:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2011-06-30 04:32:03 +02:00
|
|
|
* Uses "PyLint" to detect various errors in Python code. To use this linter,
|
|
|
|
* you must install pylint and configure which codes you want to be reported as
|
|
|
|
* errors, warnings and advice.
|
|
|
|
*
|
|
|
|
* You should be able to install pylint with ##sudo easy_install pylint##. If
|
|
|
|
* your system is unusual, you can manually specify the location of pylint and
|
2011-10-25 22:46:13 +02:00
|
|
|
* its dependencies by configuring these keys in your .arcconfig:
|
2011-06-30 04:32:03 +02:00
|
|
|
*
|
|
|
|
* lint.pylint.prefix
|
|
|
|
* lint.pylint.logilab_astng.prefix
|
|
|
|
* lint.pylint.logilab_common.prefix
|
|
|
|
*
|
2011-10-25 22:46:13 +02:00
|
|
|
* You can specify additional command-line options to pass to PyLint by
|
2011-10-26 00:23:08 +02:00
|
|
|
* setting ##lint.pylint.options##. You may also specify a list of additional
|
|
|
|
* entries for PYTHONPATH with ##lint.pylint.pythonpath##. Those can be
|
|
|
|
* absolute or relative to the project root.
|
|
|
|
*
|
2011-10-25 22:47:03 +02:00
|
|
|
* If you have a PyLint rcfile, specify its path with
|
|
|
|
* ##lint.pylint.rcfile##. It can be absolute or relative to the project
|
|
|
|
* root. Be sure not to define ##output-format##, or if you do, set it to
|
|
|
|
* ##text##.
|
2011-06-30 04:32:03 +02:00
|
|
|
*
|
2011-10-25 22:46:13 +02:00
|
|
|
* Specify which PyLint messages map to which Arcanist messages by defining
|
|
|
|
* the following regular expressions:
|
2011-06-30 04:32:03 +02:00
|
|
|
*
|
|
|
|
* lint.pylint.codes.error
|
|
|
|
* lint.pylint.codes.warning
|
|
|
|
* lint.pylint.codes.advice
|
|
|
|
*
|
2011-10-25 22:46:13 +02:00
|
|
|
* The regexps are run in that order; the first to match determines which
|
|
|
|
* Arcanist severity applies, if any. For example, to capture all PyLint
|
|
|
|
* "E...." errors as Arcanist errors, set ##lint.pylint.codes.error## to:
|
2011-06-30 04:32:03 +02:00
|
|
|
*
|
|
|
|
* ^E.*
|
|
|
|
*
|
2011-10-25 22:46:13 +02:00
|
|
|
* You can also match more granularly:
|
2011-06-30 04:32:03 +02:00
|
|
|
*
|
|
|
|
* ^E(0001|0002)$
|
|
|
|
*
|
2011-10-25 22:46:13 +02:00
|
|
|
* According to ##man pylint##, there are 5 kind of messages:
|
|
|
|
*
|
|
|
|
* (C) convention, for programming standard violation
|
|
|
|
* (R) refactor, for bad code smell
|
|
|
|
* (W) warning, for python specific problems
|
|
|
|
* (E) error, for probable bugs in the code
|
|
|
|
* (F) fatal, if an error occurred which prevented pylint from
|
|
|
|
* doing further processing.
|
2011-05-22 12:32:26 +02:00
|
|
|
*
|
|
|
|
* @group linter
|
|
|
|
*/
|
2012-01-31 21:07:05 +01:00
|
|
|
final class ArcanistPyLintLinter extends ArcanistLinter {
|
2011-05-22 12:32:26 +02:00
|
|
|
|
|
|
|
private function getMessageCodeSeverity($code) {
|
2011-06-30 04:32:03 +02:00
|
|
|
|
2013-10-23 00:34:06 +02:00
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
2011-06-30 04:32:03 +02:00
|
|
|
|
2013-10-23 00:34:06 +02:00
|
|
|
$error_regexp =
|
|
|
|
$config->getConfigFromAnySource('lint.pylint.codes.error');
|
|
|
|
$warning_regexp =
|
|
|
|
$config->getConfigFromAnySource('lint.pylint.codes.warning');
|
|
|
|
$advice_regexp =
|
|
|
|
$config->getConfigFromAnySource('lint.pylint.codes.advice');
|
2011-06-30 04:32:03 +02:00
|
|
|
|
|
|
|
if (!$error_regexp && !$warning_regexp && !$advice_regexp) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
"You are invoking the PyLint linter but have not configured any of ".
|
|
|
|
"'lint.pylint.codes.error', 'lint.pylint.codes.warning', or ".
|
|
|
|
"'lint.pylint.codes.advice'. Consult the documentation for ".
|
|
|
|
"ArcanistPyLintLinter.");
|
|
|
|
}
|
|
|
|
|
2011-05-22 12:32:26 +02:00
|
|
|
$code_map = array(
|
2011-06-30 04:32:03 +02:00
|
|
|
ArcanistLintSeverity::SEVERITY_ERROR => $error_regexp,
|
|
|
|
ArcanistLintSeverity::SEVERITY_WARNING => $warning_regexp,
|
|
|
|
ArcanistLintSeverity::SEVERITY_ADVICE => $advice_regexp,
|
2011-05-22 12:32:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($code_map as $sev => $codes) {
|
2011-06-30 04:32:03 +02:00
|
|
|
if ($codes === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!is_array($codes)) {
|
|
|
|
$codes = array($codes);
|
|
|
|
}
|
|
|
|
foreach ($codes as $code_re) {
|
|
|
|
if (preg_match("/{$code_re}/", $code)) {
|
|
|
|
return $sev;
|
2011-05-22 12:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the message code doesn't match any of the provided regex's,
|
|
|
|
// then just disable it.
|
|
|
|
return ArcanistLintSeverity::SEVERITY_DISABLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPyLintPath() {
|
2014-05-23 22:53:05 +02:00
|
|
|
$pylint_bin = 'pylint';
|
2011-05-22 12:32:26 +02:00
|
|
|
|
|
|
|
// Use the PyLint prefix specified in the config file
|
2013-10-23 00:34:06 +02:00
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
|
|
|
$prefix = $config->getConfigFromAnySource('lint.pylint.prefix');
|
2011-05-22 12:32:26 +02:00
|
|
|
if ($prefix !== null) {
|
2014-05-23 22:53:05 +02:00
|
|
|
$pylint_bin = $prefix.'/bin/'.$pylint_bin;
|
2011-05-22 12:32:26 +02:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:32:03 +02:00
|
|
|
if (!Filesystem::pathExists($pylint_bin)) {
|
|
|
|
|
|
|
|
list($err) = exec_manual('which %s', $pylint_bin);
|
|
|
|
if ($err) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
"PyLint does not appear to be installed on this system. Install it ".
|
|
|
|
"(e.g., with 'sudo easy_install pylint') or configure ".
|
|
|
|
"'lint.pylint.prefix' in your .arcconfig to point to the directory ".
|
|
|
|
"where it resides.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-22 12:32:26 +02:00
|
|
|
return $pylint_bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPyLintPythonPath() {
|
|
|
|
// Get non-default install locations for pylint and its dependencies
|
|
|
|
// libraries.
|
2013-10-23 00:34:06 +02:00
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
2011-05-22 12:32:26 +02:00
|
|
|
$prefixes = array(
|
2013-10-23 00:34:06 +02:00
|
|
|
$config->getConfigFromAnySource('lint.pylint.prefix'),
|
|
|
|
$config->getConfigFromAnySource('lint.pylint.logilab_astng.prefix'),
|
|
|
|
$config->getConfigFromAnySource('lint.pylint.logilab_common.prefix'),
|
2011-05-22 12:32:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Add the libraries to the python search path
|
|
|
|
$python_path = array();
|
|
|
|
foreach ($prefixes as $prefix) {
|
|
|
|
if ($prefix !== null) {
|
2013-01-26 14:31:25 +01:00
|
|
|
$python_path[] = $prefix.'/lib/python2.7/site-packages';
|
|
|
|
$python_path[] = $prefix.'/lib/python2.7/dist-packages';
|
2011-05-22 12:32:26 +02:00
|
|
|
$python_path[] = $prefix.'/lib/python2.6/site-packages';
|
2013-01-26 14:31:25 +01:00
|
|
|
$python_path[] = $prefix.'/lib/python2.6/dist-packages';
|
2011-05-22 12:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 00:34:06 +02:00
|
|
|
$working_copy = $this->getEngine()->getWorkingCopy();
|
|
|
|
$config_paths = $config->getConfigFromAnySource('lint.pylint.pythonpath');
|
2011-10-26 00:23:08 +02:00
|
|
|
if ($config_paths !== null) {
|
|
|
|
foreach ($config_paths as $config_path) {
|
|
|
|
if ($config_path !== null) {
|
|
|
|
$python_path[] =
|
|
|
|
Filesystem::resolvePath($config_path,
|
|
|
|
$working_copy->getProjectRoot());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-22 12:32:26 +02:00
|
|
|
$python_path[] = '';
|
2014-05-23 22:53:05 +02:00
|
|
|
return implode(':', $python_path);
|
2011-05-22 12:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getPyLintOptions() {
|
2011-10-25 22:46:13 +02:00
|
|
|
// '-rn': don't print lint report/summary at end
|
2014-04-10 15:07:42 +02:00
|
|
|
$options = array('-rn');
|
|
|
|
|
|
|
|
// Version 0.x.x include the pylint message ids in the output
|
2014-05-23 22:53:05 +02:00
|
|
|
if (version_compare($this->getLinterVersion(), '1', 'lt')) {
|
|
|
|
array_push($options, '-iy', '--output-format=text');
|
2014-04-10 15:07:42 +02:00
|
|
|
}
|
|
|
|
// Version 1.x.x set the output specifically to the 0.x.x format
|
|
|
|
else {
|
|
|
|
array_push($options, "--msg-template='{msg_id}:{line:3d}: {obj}: {msg}'");
|
|
|
|
}
|
2011-05-22 12:32:26 +02:00
|
|
|
|
|
|
|
$working_copy = $this->getEngine()->getWorkingCopy();
|
2013-10-23 00:34:06 +02:00
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
2011-10-25 22:47:03 +02:00
|
|
|
|
|
|
|
// Specify an --rcfile, either absolute or relative to the project root.
|
|
|
|
// Stupidly, the command line args above are overridden by rcfile, so be
|
|
|
|
// careful.
|
2013-10-23 00:34:06 +02:00
|
|
|
$rcfile = $config->getConfigFromAnySource('lint.pylint.rcfile');
|
2011-10-25 22:47:03 +02:00
|
|
|
if ($rcfile !== null) {
|
|
|
|
$rcfile = Filesystem::resolvePath(
|
|
|
|
$rcfile,
|
|
|
|
$working_copy->getProjectRoot());
|
|
|
|
$options[] = csprintf('--rcfile=%s', $rcfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any options defined in the config file for PyLint
|
2013-10-23 00:34:06 +02:00
|
|
|
$config_options = $config->getConfigFromAnySource('lint.pylint.options');
|
2011-05-22 12:32:26 +02:00
|
|
|
if ($config_options !== null) {
|
2011-10-25 22:46:13 +02:00
|
|
|
$options = array_merge($options, $config_options);
|
2011-05-22 12:32:26 +02:00
|
|
|
}
|
|
|
|
|
2014-05-23 22:53:05 +02:00
|
|
|
return implode(' ', $options);
|
2011-05-22 12:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'PyLint';
|
|
|
|
}
|
|
|
|
|
2014-04-10 15:07:42 +02:00
|
|
|
private function getLinterVersion() {
|
|
|
|
|
|
|
|
$pylint_bin = $this->getPyLintPath();
|
|
|
|
$options = '--version';
|
|
|
|
|
|
|
|
list($stdout) = execx(
|
|
|
|
'%s %s',
|
|
|
|
$pylint_bin,
|
|
|
|
$options);
|
|
|
|
|
|
|
|
$lines = explode("\n", $stdout);
|
|
|
|
$matches = null;
|
|
|
|
|
|
|
|
// If the version command didn't return anything or the regex didn't match
|
|
|
|
// Assume a future version that at least is compatible with 1.x.x
|
|
|
|
if (count($lines) == 0 ||
|
|
|
|
!preg_match('/pylint\s((?:\d+\.?)+)/', $lines[0], $matches)) {
|
2014-05-23 22:53:05 +02:00
|
|
|
return '999';
|
2014-04-10 15:07:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
|
2011-05-22 12:32:26 +02:00
|
|
|
public function lintPath($path) {
|
|
|
|
$pylint_bin = $this->getPyLintPath();
|
|
|
|
$python_path = $this->getPyLintPythonPath();
|
|
|
|
$options = $this->getPyLintOptions();
|
|
|
|
$path_on_disk = $this->getEngine()->getFilePathOnDisk($path);
|
|
|
|
|
|
|
|
try {
|
|
|
|
list($stdout, $_) = execx(
|
2013-02-02 22:07:39 +01:00
|
|
|
'/usr/bin/env PYTHONPATH=%s$PYTHONPATH %s %C %s',
|
|
|
|
$python_path,
|
|
|
|
$pylint_bin,
|
|
|
|
$options,
|
|
|
|
$path_on_disk);
|
2011-05-22 12:32:26 +02:00
|
|
|
} catch (CommandException $e) {
|
2011-10-25 22:46:13 +02:00
|
|
|
if ($e->getError() == 32) {
|
|
|
|
// According to ##man pylint## the exit status of 32 means there was a
|
|
|
|
// usage error. That's bad, so actually exit abnormally.
|
2011-05-22 12:32:26 +02:00
|
|
|
throw $e;
|
2011-10-25 22:46:13 +02:00
|
|
|
} else {
|
|
|
|
// The other non-zero exit codes mean there were messages issued,
|
|
|
|
// which is expected, so don't exit.
|
|
|
|
$stdout = $e->getStdout();
|
2011-05-22 12:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$lines = explode("\n", $stdout);
|
|
|
|
$messages = array();
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$matches = null;
|
2011-10-28 00:37:45 +02:00
|
|
|
if (!preg_match(
|
|
|
|
'/([A-Z]\d+): *(\d+)(?:|,\d*): *(.*)$/',
|
|
|
|
$line, $matches)) {
|
2011-05-22 12:32:26 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($matches as $key => $match) {
|
|
|
|
$matches[$key] = trim($match);
|
|
|
|
}
|
2011-06-30 04:32:03 +02:00
|
|
|
|
2011-05-22 12:32:26 +02:00
|
|
|
$message = new ArcanistLintMessage();
|
|
|
|
$message->setPath($path);
|
|
|
|
$message->setLine($matches[2]);
|
|
|
|
$message->setCode($matches[1]);
|
2014-05-23 22:53:05 +02:00
|
|
|
$message->setName($this->getLinterName().' '.$matches[1]);
|
2011-05-22 12:32:26 +02:00
|
|
|
$message->setDescription($matches[3]);
|
|
|
|
$message->setSeverity($this->getMessageCodeSeverity($matches[1]));
|
|
|
|
$this->addLintMessage($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|