tests/filterpyflakes.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sun, 06 Dec 2015 22:22:09 -0800
changeset 27285 aef5b606d3ee
parent 26023 48671378daeb
child 28724 cf339d6ac7c7
permissions -rwxr-xr-x
tests/filterpyflakes: use absolute_import
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14140
82f0412ef7de tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff changeset
     1
#!/usr/bin/env python
82f0412ef7de tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff changeset
     2
82f0412ef7de tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff changeset
     3
# Filter output by pyflakes to control which warnings we check
82f0412ef7de tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff changeset
     4
27285
aef5b606d3ee tests/filterpyflakes: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26023
diff changeset
     5
from __future__ import absolute_import
aef5b606d3ee tests/filterpyflakes: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26023
diff changeset
     6
aef5b606d3ee tests/filterpyflakes: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26023
diff changeset
     7
import re
aef5b606d3ee tests/filterpyflakes: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26023
diff changeset
     8
import sys
14140
82f0412ef7de tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff changeset
     9
19335
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    10
def makekey(typeandline):
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    11
    """
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    12
    for sorting lines by: msgtype, path/to/file, lineno, message
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    13
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    14
    typeandline is a sequence of a message type and the entire message line
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    15
    the message line format is path/to/file:line: message
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    16
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    17
    >>> makekey((3, 'example.py:36: any message'))
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    18
    (3, 'example.py', 36, ' any message')
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    19
    >>> makekey((7, 'path/to/file.py:68: dummy message'))
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    20
    (7, 'path/to/file.py', 68, ' dummy message')
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    21
    >>> makekey((2, 'fn:88: m')) > makekey((2, 'fn:9: m'))
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    22
    True
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    23
    """
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    24
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    25
    msgtype, line = typeandline
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    26
    fname, line, message = line.split(":", 2)
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    27
    # line as int for ordering 9 before 88
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    28
    return msgtype, fname, int(line), message
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    29
14173
419539ea79cb test-pyflake: improve sorting algorithm
timeless <timeless@mozdev.org>
parents: 14140
diff changeset
    30
419539ea79cb test-pyflake: improve sorting algorithm
timeless <timeless@mozdev.org>
parents: 14140
diff changeset
    31
lines = []
14140
82f0412ef7de tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff changeset
    32
for line in sys.stdin:
19335
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    33
    # We whitelist tests (see more messages in pyflakes.messages)
14175
b452abffcb15 tests: add pyflakes checking for assigned to but never used
timeless <timeless@mozdev.org>
parents: 14173
diff changeset
    34
    pats = [
21271
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    35
            (r"imported but unused", None),
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    36
            (r"local variable '.*' is assigned to but never used", None),
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    37
            (r"unable to detect undefined names", None),
21293
507ce509fd22 filterpyflakes: make memoryview filtering unconditional
Matt Mackall <mpm@selenic.com>
parents: 21271
diff changeset
    38
            (r"undefined name '.*'",
21294
1ae3cd6f836c filterpyflakes: filter WindowsError unconditionally
Matt Mackall <mpm@selenic.com>
parents: 21293
diff changeset
    39
             r"undefined name '(WindowsError|memoryview)'")
14175
b452abffcb15 tests: add pyflakes checking for assigned to but never used
timeless <timeless@mozdev.org>
parents: 14173
diff changeset
    40
           ]
21293
507ce509fd22 filterpyflakes: make memoryview filtering unconditional
Matt Mackall <mpm@selenic.com>
parents: 21271
diff changeset
    41
21271
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    42
    for msgtype, (pat, excl) in enumerate(pats):
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    43
        if re.search(pat, line) and (not excl or not re.search(excl, line)):
19335
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    44
            break # pattern matches
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    45
    else:
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    46
        continue # no pattern matched, next line
14209
08d84bdce1a5 pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents: 14176
diff changeset
    47
    fn = line.split(':', 1)[0]
26023
48671378daeb tests: make filterpyflakes.py read target files relatively to cwd
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21294
diff changeset
    48
    f = open(fn)
14209
08d84bdce1a5 pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents: 14176
diff changeset
    49
    data = f.read()
08d84bdce1a5 pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents: 14176
diff changeset
    50
    f.close()
19381
e033a7d444ac tests: do not skip code-checking on some whole files
Simon Heimberg <simohe@besonet.ch>
parents: 19335
diff changeset
    51
    if 'no-' 'check-code' in data:
14209
08d84bdce1a5 pyflakes: ignore files marked no-check-code
Augie Fackler <durin42@gmail.com>
parents: 14176
diff changeset
    52
        continue
19335
77440de177f7 tests: simplify and document the sorting of pyflake messages
Simon Heimberg <simohe@besonet.ch>
parents: 14209
diff changeset
    53
    lines.append((msgtype, line))
14173
419539ea79cb test-pyflake: improve sorting algorithm
timeless <timeless@mozdev.org>
parents: 14140
diff changeset
    54
19872
681f7b9213a4 check-code: check for spaces around = for named parameters
Mads Kiilerich <madski@unity3d.com>
parents: 19381
diff changeset
    55
for msgtype, line in sorted(lines, key=makekey):
14140
82f0412ef7de tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff changeset
    56
    sys.stdout.write(line)
82f0412ef7de tests: add pyflakes checking for unused imports
timeless <timeless@mozdev.org>
parents:
diff changeset
    57
print
21271
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    58
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    59
# self test of "undefined name" detection for other than 'memoryview'
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    60
if False:
4adc090fa2fb tests: ignore "undefined name 'memoryview'" pyflakes error on earlier Python
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21232
diff changeset
    61
    print undefinedname