~ubuntu-branches/ubuntu/vivid/phabricator/vivid

« back to all changes in this revision

Viewing changes to arcanist/src/lint/linter/ArcanistFlake8Linter.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Uses "flake8" to detect various errors in Python code.
 
5
 * Requires version 1.7.0 or newer of flake8.
 
6
 */
 
7
final class ArcanistFlake8Linter extends ArcanistExternalLinter {
 
8
 
 
9
  public function getInfoName() {
 
10
    return 'Flake8';
 
11
  }
 
12
 
 
13
  public function getInfoURI() {
 
14
    return 'https://pypi.python.org/pypi/flake8';
 
15
  }
 
16
 
 
17
  public function getInfoDescription() {
 
18
    return pht(
 
19
      'Uses `flake8` to run several linters (PyFlakes, pep8, and a McCabe '.
 
20
      'complexity checker) on Python source files.');
 
21
  }
 
22
 
 
23
  public function getLinterName() {
 
24
    return 'flake8';
 
25
  }
 
26
 
 
27
  public function getLinterConfigurationName() {
 
28
    return 'flake8';
 
29
  }
 
30
 
 
31
  public function getDefaultFlags() {
 
32
    return $this->getDeprecatedConfiguration('lint.flake8.options', array());
 
33
  }
 
34
 
 
35
  public function getDefaultBinary() {
 
36
    $prefix = $this->getDeprecatedConfiguration('lint.flake8.prefix');
 
37
    $bin = $this->getDeprecatedConfiguration('lint.flake8.bin', 'flake8');
 
38
 
 
39
    if ($prefix) {
 
40
      return $prefix.'/'.$bin;
 
41
    } else {
 
42
      return $bin;
 
43
    }
 
44
  }
 
45
 
 
46
  public function getVersion() {
 
47
    list($stdout) = execx('%C --version', $this->getExecutableCommand());
 
48
 
 
49
    $matches = array();
 
50
    if (preg_match('/^(?P<version>\d+\.\d+(?:\.\d+)?)\b/', $stdout, $matches)) {
 
51
      return $matches['version'];
 
52
    } else {
 
53
      return false;
 
54
    }
 
55
  }
 
56
 
 
57
  public function getInstallInstructions() {
 
58
    return pht('Install flake8 using `easy_install flake8`.');
 
59
  }
 
60
 
 
61
  public function shouldExpectCommandErrors() {
 
62
    return true;
 
63
  }
 
64
 
 
65
  protected function parseLinterOutput($path, $err, $stdout, $stderr) {
 
66
    $lines = phutil_split_lines($stdout, false);
 
67
 
 
68
    $messages = array();
 
69
    foreach ($lines as $line) {
 
70
      $matches = null;
 
71
      // stdin:2: W802 undefined name 'foo'  # pyflakes
 
72
      // stdin:3:1: E302 expected 2 blank lines, found 1  # pep8
 
73
      $regexp = '/^(.*?):(\d+):(?:(\d+):)? (\S+) (.*)$/';
 
74
      if (!preg_match($regexp, $line, $matches)) {
 
75
        continue;
 
76
      }
 
77
      foreach ($matches as $key => $match) {
 
78
        $matches[$key] = trim($match);
 
79
      }
 
80
 
 
81
      $message = new ArcanistLintMessage();
 
82
      $message->setPath($path);
 
83
      $message->setLine($matches[2]);
 
84
      if (!empty($matches[3])) {
 
85
        $message->setChar($matches[3]);
 
86
      }
 
87
      $message->setCode($matches[4]);
 
88
      $message->setName($this->getLinterName().' '.$matches[3]);
 
89
      $message->setDescription($matches[5]);
 
90
      $message->setSeverity($this->getLintMessageSeverity($matches[4]));
 
91
 
 
92
      $messages[] = $message;
 
93
    }
 
94
 
 
95
    if ($err && !$messages) {
 
96
      return false;
 
97
    }
 
98
 
 
99
    return $messages;
 
100
  }
 
101
 
 
102
  protected function getDefaultMessageSeverity($code) {
 
103
    if (preg_match('/^C/', $code)) {
 
104
      // "C": Cyclomatic complexity
 
105
      return ArcanistLintSeverity::SEVERITY_ADVICE;
 
106
    } else if (preg_match('/^W/', $code)) {
 
107
      // "W": PEP8 Warning
 
108
      return ArcanistLintSeverity::SEVERITY_WARNING;
 
109
    } else {
 
110
      // "E": PEP8 Error
 
111
      // "F": PyFlakes Error
 
112
      return ArcanistLintSeverity::SEVERITY_ERROR;
 
113
    }
 
114
  }
 
115
 
 
116
  protected function getLintCodeFromLinterConfigurationKey($code) {
 
117
    if (!preg_match('/^(E|W|C|F)\d+$/', $code)) {
 
118
      throw new Exception(
 
119
        pht(
 
120
          'Unrecognized lint message code "%s". Expected a valid flake8 '.
 
121
          'lint code like "%s", or "%s", or "%s", or "%s".',
 
122
          $code,
 
123
          'E225',
 
124
          'W291',
 
125
          'F811',
 
126
          'C901'));
 
127
    }
 
128
 
 
129
    return $code;
 
130
  }
 
131
 
 
132
}