2011-01-10 00:22:25 +01:00
|
|
|
<?php
|
|
|
|
|
2011-02-19 20:36:08 +01:00
|
|
|
/**
|
|
|
|
* Uses "pep8.py" to enforce PEP8 rules for Python.
|
|
|
|
*
|
|
|
|
* @group linter
|
|
|
|
*/
|
2012-01-31 21:07:05 +01:00
|
|
|
final class ArcanistPEP8Linter extends ArcanistLinter {
|
2011-01-10 00:22:25 +01:00
|
|
|
|
|
|
|
public function willLintPaths(array $paths) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'PEP8';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLintSeverityMap() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLintNameMap() {
|
2012-01-17 21:38:54 +01:00
|
|
|
return array();
|
2011-01-10 00:22:25 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 03:05:48 +01:00
|
|
|
public function getCacheVersion() {
|
2012-12-21 05:43:39 +01:00
|
|
|
return '1.3.4';
|
2012-12-13 03:05:48 +01:00
|
|
|
}
|
|
|
|
|
2011-01-10 00:22:25 +01:00
|
|
|
public function getPEP8Options() {
|
2012-01-17 21:38:54 +01:00
|
|
|
$working_copy = $this->getEngine()->getWorkingCopy();
|
|
|
|
$options = $working_copy->getConfig('lint.pep8.options');
|
|
|
|
|
|
|
|
if ($options === null) {
|
2012-11-03 22:47:38 +01:00
|
|
|
$options = $this->getConfig('options');
|
2012-01-17 21:38:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
2011-01-10 00:22:25 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 21:38:54 +01:00
|
|
|
public function getPEP8Path() {
|
|
|
|
$working_copy = $this->getEngine()->getWorkingCopy();
|
|
|
|
$prefix = $working_copy->getConfig('lint.pep8.prefix');
|
|
|
|
$bin = $working_copy->getConfig('lint.pep8.bin');
|
|
|
|
|
|
|
|
if ($bin === null && $prefix === null) {
|
|
|
|
$bin = csprintf('/usr/bin/env python2.6 %s',
|
|
|
|
phutil_get_library_root('arcanist').
|
|
|
|
'/../externals/pep8/pep8.py');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($bin === null) {
|
|
|
|
$bin = 'pep8';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prefix !== null) {
|
|
|
|
if (!Filesystem::pathExists($prefix.'/'.$bin)) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
"Unable to find PEP8 binary in a specified directory. Make sure ".
|
|
|
|
"that 'lint.pep8.prefix' and 'lint.pep8.bin' keys are set ".
|
|
|
|
"correctly. If you'd rather use a copy of PEP8 installed ".
|
2013-01-11 00:22:49 +01:00
|
|
|
"globally, you can just remove these keys from your .arcconfig.");
|
2012-01-17 21:38:54 +01:00
|
|
|
}
|
2011-01-10 00:22:25 +01:00
|
|
|
|
2012-01-17 21:38:54 +01:00
|
|
|
$bin = csprintf("%s/%s", $prefix, $bin);
|
|
|
|
|
|
|
|
return $bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for globally installed PEP8
|
|
|
|
list($err) = exec_manual('which %s', $bin);
|
|
|
|
if ($err) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
"PEP8 does not appear to be installed on this system. Install it ".
|
|
|
|
"(e.g., with 'easy_install pep8') or configure ".
|
|
|
|
"'lint.pep8.prefix' in your .arcconfig to point to the directory ".
|
|
|
|
"where it resides.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function lintPath($path) {
|
|
|
|
$pep8_bin = $this->getPEP8Path();
|
2011-01-10 00:22:25 +01:00
|
|
|
$options = $this->getPEP8Options();
|
|
|
|
|
2011-05-19 01:04:06 +02:00
|
|
|
list($rc, $stdout) = exec_manual(
|
2012-01-17 21:38:54 +01:00
|
|
|
"%C %C %s",
|
2011-01-10 00:22:25 +01:00
|
|
|
$pep8_bin,
|
2012-01-17 21:38:54 +01:00
|
|
|
$options,
|
2011-01-10 00:22:25 +01:00
|
|
|
$this->getEngine()->getFilePathOnDisk($path));
|
|
|
|
|
|
|
|
$lines = explode("\n", $stdout);
|
|
|
|
$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);
|
|
|
|
}
|
2012-11-04 05:52:30 +01:00
|
|
|
if (!$this->isMessageEnabled($matches[4])) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-01-10 00:22:25 +01:00
|
|
|
$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]);
|
2012-11-04 05:52:30 +01:00
|
|
|
$message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
|
2011-01-10 00:22:25 +01:00
|
|
|
$this->addLintMessage($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|