1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-10 00:42:40 +01:00

Port flake8 to ArcanistExternalLinter

Summary:
Ref T3186. Brings another linter onboard. This one uses the stdin stuff.

The unit test was ostensibly broken so I fixed it, but that might just be some kind of version issue.

Test Plan: Unit tests.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T3186

Differential Revision: https://secure.phabricator.com/D6802
This commit is contained in:
epriestley 2013-08-23 11:52:54 -07:00
parent e23fc30c19
commit 6e5be59ad6
5 changed files with 40 additions and 49 deletions

View file

@ -221,7 +221,7 @@ phutil_register_library_map(array(
'ArcanistFeatureWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistFeatureWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistFilenameLinter' => 'ArcanistLinter', 'ArcanistFilenameLinter' => 'ArcanistLinter',
'ArcanistFlagWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistFlagWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistFlake8Linter' => 'ArcanistLinter', 'ArcanistFlake8Linter' => 'ArcanistExternalLinter',
'ArcanistFlake8LinterTestCase' => 'ArcanistArcanistLinterTestCase', 'ArcanistFlake8LinterTestCase' => 'ArcanistArcanistLinterTestCase',
'ArcanistFutureLinter' => 'ArcanistLinter', 'ArcanistFutureLinter' => 'ArcanistLinter',
'ArcanistGeneratedLinter' => 'ArcanistLinter', 'ArcanistGeneratedLinter' => 'ArcanistLinter',

View file

@ -371,7 +371,7 @@ abstract class ArcanistExternalLinter extends ArcanistFutureLinter {
'%C %C', '%C %C',
$bin, $bin,
$this->getReadDataFromStdinFilename()); $this->getReadDataFromStdinFilename());
$future->write($this->getFileData($path)); $future->write($this->getEngine()->loadData($path));
} else { } else {
// TODO: In commit hook mode, we need to do more handling here. // TODO: In commit hook mode, we need to do more handling here.
$disk_path = $this->getEngine()->getFilePathOnDisk($path); $disk_path = $this->getEngine()->getFilePathOnDisk($path);

View file

@ -6,17 +6,15 @@
* *
* @group linter * @group linter
*/ */
final class ArcanistFlake8Linter extends ArcanistLinter { final class ArcanistFlake8Linter extends ArcanistExternalLinter {
public function willLintPaths(array $paths) {
return;
}
public function getLinterName() { public function getLinterName() {
return 'flake8'; return 'flake8';
} }
public function getFlake8Options() { public function getDefaultFlags() {
// TODO: Deprecated.
$working_copy = $this->getEngine()->getWorkingCopy(); $working_copy = $this->getEngine()->getWorkingCopy();
$options = $working_copy->getConfig('lint.flake8.options', ''); $options = $working_copy->getConfig('lint.flake8.options', '');
@ -24,59 +22,44 @@ final class ArcanistFlake8Linter extends ArcanistLinter {
return $options; return $options;
} }
public function getFlake8Path() { public function getDefaultBinary() {
$working_copy = $this->getEngine()->getWorkingCopy(); $working_copy = $this->getEngine()->getWorkingCopy();
$prefix = $working_copy->getConfig('lint.flake8.prefix'); $prefix = $working_copy->getConfig('lint.flake8.prefix');
$bin = $working_copy->getConfig('lint.flake8.bin', 'flake8'); $bin = $working_copy->getConfig('lint.flake8.bin', 'flake8');
if ($prefix !== null) { if ($prefix || ($bin != 'flake8')) {
if (!Filesystem::pathExists($prefix.'/'.$bin)) { return $prefix.'/'.$bin;
throw new ArcanistUsageException(
"Unable to find flake8 binary in a specified directory. Make sure ".
"that 'lint.flake8.prefix' and 'lint.flake8.bin' keys are set ".
"correctly. If you'd rather use a copy of flake8 installed ".
"globally, you can just remove these keys from your .arcconfig.");
}
$bin = csprintf("%s/%s", $prefix, $bin);
return $bin;
} }
// Look for globally installed flake8 return 'flake8';
list($err) = exec_manual('which %s', $bin);
if ($err) {
throw new ArcanistUsageException(
"flake8 does not appear to be installed on this system. Install it ".
"(e.g., with 'easy_install flake8') or configure ".
"'lint.flake8.prefix' in your .arcconfig to point to the directory ".
"where it resides.");
}
return $bin;
} }
public function lintPath($path) { public function getInstallInstructions() {
$flake8_bin = $this->getFlake8Path(); return pht('Install flake8 using `easy_install flake8`.');
$options = $this->getFlake8Options(); }
$f = new ExecFuture("%C %C -", $flake8_bin, $options); public function supportsReadDataFromStdin() {
$f->write($this->getData($path)); return true;
}
list($err, $stdout, $stderr) = $f->resolve(); public function getReadDataFromStdinFilename() {
return '-';
}
if ($err === 2) { public function shouldExpectCommandErrors() {
throw new Exception("flake8 failed to run correctly:\n".$stderr); return true;
} }
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
$lines = phutil_split_lines($stdout, $retain_endings = false);
$lines = explode("\n", $stdout);
$messages = array(); $messages = array();
foreach ($lines as $line) { foreach ($lines as $line) {
$matches = null; $matches = null;
// stdin:2: W802 undefined name 'foo' # pyflakes // stdin:2: W802 undefined name 'foo' # pyflakes
// stdin:3:1: E302 expected 2 blank lines, found 1 # pep8 // stdin:3:1: E302 expected 2 blank lines, found 1 # pep8
if (!preg_match('/^(.*?):(\d+):(?:(\d+):)? (\S+) (.*)$/', $regexp = '/^(.*?):(\d+):(?:(\d+):)? (\S+) (.*)$/';
$line, $matches)) { if (!preg_match($regexp, $line, $matches)) {
continue; continue;
} }
foreach ($matches as $key => $match) { foreach ($matches as $key => $match) {
@ -92,14 +75,22 @@ final class ArcanistFlake8Linter extends ArcanistLinter {
$message = new ArcanistLintMessage(); $message = new ArcanistLintMessage();
$message->setPath($path); $message->setPath($path);
$message->setLine($matches[2]); $message->setLine($matches[2]);
if (!empty($matches[3])) if (!empty($matches[3])) {
$message->setChar($matches[3]); $message->setChar($matches[3]);
}
$message->setCode($matches[4]); $message->setCode($matches[4]);
$message->setName($this->getLinterName().' '.$matches[3]); $message->setName($this->getLinterName().' '.$matches[3]);
$message->setDescription($matches[5]); $message->setDescription($matches[5]);
$message->setSeverity($severity); $message->setSeverity($severity);
$this->addLintMessage($message);
$messages[] = $message;
} }
if ($err && !$messages) {
return false;
}
return $messages;
} }
} }

View file

@ -9,7 +9,7 @@ final class ArcanistFlake8LinterTestCase
public function testFlake8Lint() { public function testFlake8Lint() {
return $this->executeTestsInDirectory( return $this->executeTestsInDirectory(
dirname(__FILE__).'/python/', dirname(__FILE__).'/flake8/',
new ArcanistFlake8Linter()); new ArcanistFlake8Linter());
} }

View file

@ -3,5 +3,5 @@ x = 'y'
def hello(): def hello():
return foo return foo
~~~~~~~~~~ ~~~~~~~~~~
warning:1:8 error:3:1
warning:4: warning:4:1