2011-01-09 15:22:25 -08:00
|
|
|
<?php
|
|
|
|
|
2011-02-19 11:36:08 -08:00
|
|
|
/**
|
|
|
|
* Uses "pep8.py" to enforce PEP8 rules for Python.
|
|
|
|
*
|
|
|
|
* @group linter
|
|
|
|
*/
|
2013-08-23 11:52:44 -07:00
|
|
|
final class ArcanistPEP8Linter extends ArcanistExternalLinter {
|
2011-01-09 15:22:25 -08:00
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'PEP8';
|
|
|
|
}
|
|
|
|
|
2013-08-23 11:58:07 -07:00
|
|
|
public function getLinterConfigurationName() {
|
|
|
|
return 'pep8';
|
|
|
|
}
|
|
|
|
|
2012-12-12 18:05:48 -08:00
|
|
|
public function getCacheVersion() {
|
2013-08-23 11:52:44 -07:00
|
|
|
list($stdout) = execx('%C --version', $this->getExecutableCommand());
|
2014-04-23 16:22:49 -07:00
|
|
|
return $stdout.implode(' ', $this->getCommandFlags());
|
2012-12-12 18:05:48 -08:00
|
|
|
}
|
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
public function getDefaultFlags() {
|
|
|
|
// TODO: Warn that all of this is deprecated.
|
2013-10-22 15:34:06 -07:00
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
2014-03-04 11:00:01 -08:00
|
|
|
return $config->getConfigFromAnySource(
|
|
|
|
'lint.pep8.options',
|
2014-04-23 16:22:49 -07:00
|
|
|
$this->getConfig('options', array()));
|
2011-01-09 15:22:25 -08:00
|
|
|
}
|
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
public function shouldUseInterpreter() {
|
|
|
|
return ($this->getDefaultBinary() !== 'pep8');
|
|
|
|
}
|
2012-01-17 12:38:54 -08:00
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
public function getDefaultInterpreter() {
|
|
|
|
return 'python2.6';
|
|
|
|
}
|
2012-01-17 12:38:54 -08:00
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
public function getDefaultBinary() {
|
|
|
|
if (Filesystem::binaryExists('pep8')) {
|
|
|
|
return 'pep8';
|
2012-01-17 12:38:54 -08:00
|
|
|
}
|
|
|
|
|
2013-10-22 15:34:06 -07:00
|
|
|
$config = $this->getEngine()->getConfigurationManager();
|
|
|
|
$old_prefix = $config->getConfigFromAnySource('lint.pep8.prefix');
|
|
|
|
$old_bin = $config->getConfigFromAnySource('lint.pep8.bin');
|
2012-01-17 12:38:54 -08:00
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
if ($old_prefix || $old_bin) {
|
|
|
|
// TODO: Deprecation warning.
|
|
|
|
$old_bin = nonempty($old_bin, 'pep8');
|
|
|
|
return $old_prefix.'/'.$old_bin;
|
2013-02-14 15:49:22 -08:00
|
|
|
}
|
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
$arc_root = dirname(phutil_get_library_root('arcanist'));
|
|
|
|
return $arc_root.'/externals/pep8/pep8.py';
|
|
|
|
}
|
2013-02-14 16:16:08 -08:00
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
public function getInstallInstructions() {
|
|
|
|
return pht('Install PEP8 using `easy_install pep8`.');
|
|
|
|
}
|
2013-02-14 16:16:08 -08:00
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
public function shouldExpectCommandErrors() {
|
|
|
|
return true;
|
2013-02-14 16:16:08 -08:00
|
|
|
}
|
2011-01-09 15:22:25 -08:00
|
|
|
|
2013-08-23 11:52:44 -07:00
|
|
|
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
|
|
|
|
$lines = phutil_split_lines($stdout, $retain_endings = false);
|
|
|
|
|
2011-01-09 15:22:25 -08:00
|
|
|
$messages = array();
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$matches = null;
|
|
|
|
if (!preg_match('/^(.*?):(\d+):(\d+): (\S+) (.*)$/', $line, $matches)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($matches as $key => $match) {
|
|
|
|
$matches[$key] = trim($match);
|
|
|
|
}
|
|
|
|
$message = new ArcanistLintMessage();
|
|
|
|
$message->setPath($path);
|
|
|
|
$message->setLine($matches[2]);
|
|
|
|
$message->setChar($matches[3]);
|
|
|
|
$message->setCode($matches[4]);
|
|
|
|
$message->setName('PEP8 '.$matches[4]);
|
|
|
|
$message->setDescription($matches[5]);
|
2013-08-23 11:58:07 -07:00
|
|
|
$message->setSeverity($this->getLintMessageSeverity($matches[4]));
|
2013-08-23 11:52:44 -07:00
|
|
|
|
|
|
|
$messages[] = $message;
|
2011-01-09 15:22:25 -08:00
|
|
|
}
|
2013-08-23 11:52:44 -07:00
|
|
|
|
|
|
|
if ($err && !$messages) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $messages;
|
2011-01-09 15:22:25 -08:00
|
|
|
}
|
|
|
|
|
2013-08-23 11:58:07 -07:00
|
|
|
protected function getDefaultMessageSeverity($code) {
|
|
|
|
if (preg_match('/^W/', $code)) {
|
|
|
|
return ArcanistLintSeverity::SEVERITY_WARNING;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// TODO: Once severities/.arclint are more usable, restore this to
|
|
|
|
// "ERROR".
|
|
|
|
// return ArcanistLintSeverity::SEVERITY_ERROR;
|
|
|
|
|
|
|
|
return ArcanistLintSeverity::SEVERITY_WARNING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLintCodeFromLinterConfigurationKey($code) {
|
|
|
|
if (!preg_match('/^(E|W)\d+$/', $code)) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Unrecognized lint message code "%s". Expected a valid PEP8 '.
|
|
|
|
'lint code like "%s" or "%s".',
|
|
|
|
$code,
|
|
|
|
"E101",
|
|
|
|
"W291"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
2011-01-09 15:22:25 -08:00
|
|
|
}
|