Changeset View
Changeset View
Standalone View
Standalone View
src/lint/linter/ArcanistPyLintLinter.php
| Show First 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | private function getMessageCodeSeverity($code) { | ||||
| } | } | ||||
| // If the message code doesn't match any of the provided regex's, | // If the message code doesn't match any of the provided regex's, | ||||
| // then just disable it. | // then just disable it. | ||||
| return ArcanistLintSeverity::SEVERITY_DISABLED; | return ArcanistLintSeverity::SEVERITY_DISABLED; | ||||
| } | } | ||||
| private function getPyLintPath() { | private function getPyLintPath() { | ||||
| $pylint_bin = "pylint"; | $pylint_bin = 'pylint'; | ||||
| // Use the PyLint prefix specified in the config file | // Use the PyLint prefix specified in the config file | ||||
| $config = $this->getEngine()->getConfigurationManager(); | $config = $this->getEngine()->getConfigurationManager(); | ||||
| $prefix = $config->getConfigFromAnySource('lint.pylint.prefix'); | $prefix = $config->getConfigFromAnySource('lint.pylint.prefix'); | ||||
| if ($prefix !== null) { | if ($prefix !== null) { | ||||
| $pylint_bin = $prefix."/bin/".$pylint_bin; | $pylint_bin = $prefix.'/bin/'.$pylint_bin; | ||||
| } | } | ||||
| if (!Filesystem::pathExists($pylint_bin)) { | if (!Filesystem::pathExists($pylint_bin)) { | ||||
| list($err) = exec_manual('which %s', $pylint_bin); | list($err) = exec_manual('which %s', $pylint_bin); | ||||
| if ($err) { | if ($err) { | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| "PyLint does not appear to be installed on this system. Install it ". | "PyLint does not appear to be installed on this system. Install it ". | ||||
| Show All 35 Lines | if ($config_paths !== null) { | ||||
| $python_path[] = | $python_path[] = | ||||
| Filesystem::resolvePath($config_path, | Filesystem::resolvePath($config_path, | ||||
| $working_copy->getProjectRoot()); | $working_copy->getProjectRoot()); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| $python_path[] = ''; | $python_path[] = ''; | ||||
| return implode(":", $python_path); | return implode(':', $python_path); | ||||
| } | } | ||||
| private function getPyLintOptions() { | private function getPyLintOptions() { | ||||
| // '-rn': don't print lint report/summary at end | // '-rn': don't print lint report/summary at end | ||||
| $options = array('-rn'); | $options = array('-rn'); | ||||
| // Version 0.x.x include the pylint message ids in the output | // Version 0.x.x include the pylint message ids in the output | ||||
| if (version_compare($this->getLinterVersion(), "1", 'lt')) { | if (version_compare($this->getLinterVersion(), '1', 'lt')) { | ||||
| array_push($options, '-iy', "--output-format=text"); | array_push($options, '-iy', '--output-format=text'); | ||||
| } | } | ||||
| // Version 1.x.x set the output specifically to the 0.x.x format | // Version 1.x.x set the output specifically to the 0.x.x format | ||||
| else { | else { | ||||
| array_push($options, "--msg-template='{msg_id}:{line:3d}: {obj}: {msg}'"); | array_push($options, "--msg-template='{msg_id}:{line:3d}: {obj}: {msg}'"); | ||||
| } | } | ||||
| $working_copy = $this->getEngine()->getWorkingCopy(); | $working_copy = $this->getEngine()->getWorkingCopy(); | ||||
| $config = $this->getEngine()->getConfigurationManager(); | $config = $this->getEngine()->getConfigurationManager(); | ||||
| Show All 10 Lines | private function getPyLintOptions() { | ||||
| } | } | ||||
| // Add any options defined in the config file for PyLint | // Add any options defined in the config file for PyLint | ||||
| $config_options = $config->getConfigFromAnySource('lint.pylint.options'); | $config_options = $config->getConfigFromAnySource('lint.pylint.options'); | ||||
| if ($config_options !== null) { | if ($config_options !== null) { | ||||
| $options = array_merge($options, $config_options); | $options = array_merge($options, $config_options); | ||||
| } | } | ||||
| return implode(" ", $options); | return implode(' ', $options); | ||||
| } | } | ||||
| public function getLinterName() { | public function getLinterName() { | ||||
| return 'PyLint'; | return 'PyLint'; | ||||
| } | } | ||||
| private function getLinterVersion() { | private function getLinterVersion() { | ||||
| $pylint_bin = $this->getPyLintPath(); | $pylint_bin = $this->getPyLintPath(); | ||||
| $options = '--version'; | $options = '--version'; | ||||
| list($stdout) = execx( | list($stdout) = execx( | ||||
| '%s %s', | '%s %s', | ||||
| $pylint_bin, | $pylint_bin, | ||||
| $options); | $options); | ||||
| $lines = explode("\n", $stdout); | $lines = explode("\n", $stdout); | ||||
| $matches = null; | $matches = null; | ||||
| // If the version command didn't return anything or the regex didn't match | // 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 | // Assume a future version that at least is compatible with 1.x.x | ||||
| if (count($lines) == 0 || | if (count($lines) == 0 || | ||||
| !preg_match('/pylint\s((?:\d+\.?)+)/', $lines[0], $matches)) { | !preg_match('/pylint\s((?:\d+\.?)+)/', $lines[0], $matches)) { | ||||
| return "999"; | return '999'; | ||||
| } | } | ||||
| return $matches[1]; | return $matches[1]; | ||||
| } | } | ||||
| public function lintPath($path) { | public function lintPath($path) { | ||||
| $pylint_bin = $this->getPyLintPath(); | $pylint_bin = $this->getPyLintPath(); | ||||
| $python_path = $this->getPyLintPythonPath(); | $python_path = $this->getPyLintPythonPath(); | ||||
| Show All 31 Lines | foreach ($lines as $line) { | ||||
| foreach ($matches as $key => $match) { | foreach ($matches as $key => $match) { | ||||
| $matches[$key] = trim($match); | $matches[$key] = trim($match); | ||||
| } | } | ||||
| $message = new ArcanistLintMessage(); | $message = new ArcanistLintMessage(); | ||||
| $message->setPath($path); | $message->setPath($path); | ||||
| $message->setLine($matches[2]); | $message->setLine($matches[2]); | ||||
| $message->setCode($matches[1]); | $message->setCode($matches[1]); | ||||
| $message->setName($this->getLinterName()." ".$matches[1]); | $message->setName($this->getLinterName().' '.$matches[1]); | ||||
| $message->setDescription($matches[3]); | $message->setDescription($matches[3]); | ||||
| $message->setSeverity($this->getMessageCodeSeverity($matches[1])); | $message->setSeverity($this->getMessageCodeSeverity($matches[1])); | ||||
| $this->addLintMessage($message); | $this->addLintMessage($message); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||