contrib/check-code.py
author Martin von Zweigbergk <martinvonz@google.com>
Thu, 10 Dec 2020 01:18:15 -0800
changeset 46114 c6ae1982b2a1
parent 45942 89a2afe31e82
child 46819 d4ba4d51f85f
permissions -rwxr-xr-x
localrepo: delete obsolete comment about `prefix in repo` raising exception We dropped support for `prefix in repo`, where `prefix` is a hex prefix, in 8b86acc7aa64 (context: drop support for looking up context by ambiguous changeid (API), 2018-04-28), after having deprecated it a while before that. These days you get a `ProgrammingError` instead if you pass in a short nodeid. Differential Revision: https://phab.mercurial-scm.org/D9599
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43503
diff changeset
     1
#!/usr/bin/env python3
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
# check-code - a style and portability checker for Mercurial
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
#
10290
7cc60de189d7 check-code: fix copyright date
Matt Mackall <mpm@selenic.com>
parents: 10287
diff changeset
     5
# Copyright 2010 Matt Mackall <mpm@selenic.com>
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     6
#
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
# This software may be used and distributed according to the terms of the
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     8
# GNU General Public License version 2 or any later version.
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     9
20241
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    10
"""style and portability checker for Mercurial
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    11
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    12
when a rule triggers wrong, do one of the following (prefer one from top):
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    13
 * do the work-around the rule suggests
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    14
 * doublecheck that it is a false match
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    15
 * improve the rule pattern
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    16
 * add an ignore pattern to the rule (3rd arg) which matches your good line
28700
35ad5bcdeb7e py24: remove check-code py24 notation
timeless <timeless@mozdev.org>
parents: 28595
diff changeset
    17
   (you can append a short comment and match this, like: #re-raises)
20241
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    18
 * change the pattern to a warning and list the exception in test-check-code-hg
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    19
 * ONLY use no--check-code for skipping entire files from external sources
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    20
"""
8071b4eddefa check-code: explain what to do when a check-code rule mismatches
Simon Heimberg <simohe@besonet.ch>
parents: 20239
diff changeset
    21
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
    22
from __future__ import absolute_import, print_function
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
    23
import glob
13074
637627f31c74 check-code: check for gratuitous whitespace after Python keywords
Thomas Arendsen Hein <thomas@jtah.de>
parents: 13031
diff changeset
    24
import keyword
10895
217557b26bc7 check-code: add a warnings level
Matt Mackall <mpm@selenic.com>
parents: 10814
diff changeset
    25
import optparse
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
    26
import os
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
    27
import re
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
    28
import sys
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    29
29145
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
    30
if sys.version_info[0] < 3:
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
    31
    opentext = open
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
    32
else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    33
29145
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
    34
    def opentext(f):
39056
c52a8af4052a contrib: have check-code look at files in latin1 instead of ascii
Augie Fackler <augie@google.com>
parents: 38784
diff changeset
    35
        return open(f, encoding='latin1')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    36
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    37
19310
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    38
try:
29143
8ed693ec5398 check-code: handle range/xrange divergence
timeless <timeless@mozdev.org>
parents: 29142
diff changeset
    39
    xrange
8ed693ec5398 check-code: handle range/xrange divergence
timeless <timeless@mozdev.org>
parents: 29142
diff changeset
    40
except NameError:
8ed693ec5398 check-code: handle range/xrange divergence
timeless <timeless@mozdev.org>
parents: 29142
diff changeset
    41
    xrange = range
8ed693ec5398 check-code: handle range/xrange divergence
timeless <timeless@mozdev.org>
parents: 29142
diff changeset
    42
try:
19310
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    43
    import re2
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    44
except ImportError:
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    45
    re2 = None
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    46
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
    47
import testparseutil
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
    48
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    49
19310
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    50
def compilere(pat, multiline=False):
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    51
    if multiline:
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    52
        pat = '(?m)' + pat
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    53
    if re2:
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    54
        try:
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    55
            return re2.compile(pat)
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    56
        except re2.error:
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    57
            pass
30ea54660d14 check-code: introduce function for using re2 when available
Simon Heimberg <simohe@besonet.ch>
parents: 19309
diff changeset
    58
    return re.compile(pat)
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    59
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    60
29398
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    61
# check "rules depending on implementation of repquote()" in each
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    62
# patterns (especially pypats), before changing around repquote()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    63
_repquotefixedmap = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    64
    ' ': ' ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    65
    '\n': '\n',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    66
    '.': 'p',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    67
    ':': 'q',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    68
    '%': '%',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    69
    '\\': 'b',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    70
    '*': 'A',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    71
    '+': 'P',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    72
    '-': 'M',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    73
}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    74
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    75
29398
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    76
def _repquoteencodechr(i):
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    77
    if i > 255:
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    78
        return 'u'
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    79
    c = chr(i)
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    80
    if c in _repquotefixedmap:
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    81
        return _repquotefixedmap[c]
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    82
    if c.isalpha():
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    83
        return 'x'
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    84
    if c.isdigit():
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    85
        return 'n'
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    86
    return 'o'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    87
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    88
29398
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    89
_repquotett = ''.join(_repquoteencodechr(i) for i in xrange(256))
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    90
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    91
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    92
def repquote(m):
19999
169cb9e47f8e check-code: more replacement characters
Simon Heimberg <simohe@besonet.ch>
parents: 19998
diff changeset
    93
    t = m.group('text')
29398
2a54cf92c773 check-code: build translation table for repquote in global for efficiency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29397
diff changeset
    94
    t = t.translate(_repquotett)
10722
c4fb2103e734 check-code: improve quote detection regexp, add tests
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10720
diff changeset
    95
    return m.group('quote') + t + m.group('quote')
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    96
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
    97
10727
62b8f15683f2 check-code: more tests and more robust python filtering
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10723
diff changeset
    98
def reppython(m):
62b8f15683f2 check-code: more tests and more robust python filtering
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10723
diff changeset
    99
    comment = m.group('comment')
62b8f15683f2 check-code: more tests and more robust python filtering
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10723
diff changeset
   100
    if comment:
18959
2f6418d8a4c9 check-code: catch trailing space in comments
Mads Kiilerich <madski@unity3d.com>
parents: 18835
diff changeset
   101
        l = len(comment.rstrip())
2f6418d8a4c9 check-code: catch trailing space in comments
Mads Kiilerich <madski@unity3d.com>
parents: 18835
diff changeset
   102
        return "#" * l + comment[l:]
10727
62b8f15683f2 check-code: more tests and more robust python filtering
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10723
diff changeset
   103
    return repquote(m)
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   104
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   105
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   106
def repcomment(m):
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   107
    return m.group(1) + "#" * len(m.group(2))
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   108
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   109
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   110
def repccomment(m):
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   111
    t = re.sub(r"((?<=\n) )|\S", "x", m.group(2))
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   112
    return m.group(1) + t + "*/"
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   113
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   114
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   115
def repcallspaces(m):
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   116
    t = re.sub(r"\n\s+", "\n", m.group(2))
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   117
    return m.group(1) + t
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   118
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   119
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   120
def repinclude(m):
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   121
    return m.group(1) + "<foo>"
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   122
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   123
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   124
def rephere(m):
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   125
    t = re.sub(r"\S", "x", m.group(2))
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   126
    return m.group(1) + t
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   127
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   128
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   129
testpats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   130
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   131
        (r'\b(push|pop)d\b', "don't use 'pushd' or 'popd', use 'cd'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   132
        (r'\W\$?\(\([^\)\n]*\)\)', "don't use (()) or $(()), use 'expr'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   133
        (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   134
        (r'(?<!hg )grep.* -a', "don't use 'grep -a', use in-line python"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   135
        (r'sed.*-i', "don't use 'sed -i', use a temporary file"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   136
        (r'\becho\b.*\\n', "don't use 'echo \\n', use printf"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   137
        (r'echo -n', "don't use 'echo -n', use printf"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   138
        (r'(^|\|\s*)\bwc\b[^|]*$\n(?!.*\(re\))', "filter wc output"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   139
        (r'head -c', "don't use 'head -c', use 'dd'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   140
        (r'tail -n', "don't use the '-n' option to tail, just use '-<num>'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   141
        (r'sha1sum', "don't use sha1sum, use $TESTDIR/md5sum.py"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   142
        (r'\bls\b.*-\w*R', "don't use 'ls -R', use 'find'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   143
        (r'printf.*[^\\]\\([1-9]|0\d)', r"don't use 'printf \NNN', use Python"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   144
        (r'printf.*[^\\]\\x', "don't use printf \\x, use Python"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   145
        (r'rm -rf \*', "don't use naked rm -rf, target a directory"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   146
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   147
            r'\[[^\]]+==',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   148
            '[ foo == bar ] is a bashism, use [ foo = bar ] instead',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   149
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   150
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   151
            r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   152
            "use egrep for extended grep syntax",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   153
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   154
        (r'(^|\|\s*)e?grep .*\\S', "don't use \\S in regular expression"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   155
        (r'(?<!!)/bin/', "don't use explicit paths for tools"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   156
        (r'#!.*/bash', "don't use bash in shebang, use sh"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   157
        (r'[^\n]\Z', "no trailing newline"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   158
        (r'export .*=', "don't export and assign at once"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   159
        (r'^source\b', "don't use 'source', use '.'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   160
        (r'touch -d', "don't use 'touch -d', use 'touch -t' instead"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   161
        (r'\bls +[^|\n-]+ +-', "options to 'ls' must come before filenames"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   162
        (r'[^>\n]>\s*\$HGRCPATH', "don't overwrite $HGRCPATH, append to it"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   163
        (r'^stop\(\)', "don't use 'stop' as a shell function name"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   164
        (r'(\[|\btest\b).*-e ', "don't use 'test -e', use 'test -f'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   165
        (r'\[\[\s+[^\]]*\]\]', "don't use '[[ ]]', use '[ ]'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   166
        (r'^alias\b.*=', "don't use alias, use a function"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   167
        (r'if\s*!', "don't use '!' to negate exit status"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   168
        (r'/dev/u?random', "don't use entropy, use /dev/zero"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   169
        (r'do\s*true;\s*done', "don't use true as loop body, use sleep 0"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   170
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   171
            r'sed (-e )?\'(\d+|/[^/]*/)i(?!\\\n)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   172
            "put a backslash-escaped newline after sed 'i' command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   173
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   174
        (r'^diff *-\w*[uU].*$\n(^  \$ |^$)', "prefix diff -u/-U with cmp"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   175
        (r'^\s+(if)? diff *-\w*[uU]', "prefix diff -u/-U with cmp"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   176
        (r'[\s="`\']python\s(?!bindings)', "don't use 'python', use '$PYTHON'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   177
        (r'seq ', "don't use 'seq', use $TESTDIR/seq.py"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   178
        (r'\butil\.Abort\b', "directly use error.Abort"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   179
        (r'\|&', "don't use |&, use 2>&1"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   180
        (r'\w =  +\w', "only one space after = allowed"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   181
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   182
            r'\bsed\b.*[^\\]\\n',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   183
            "don't use 'sed ... \\n', use a \\ and a newline",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   184
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   185
        (r'env.*-u', "don't use 'env -u VAR', use 'unset VAR'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   186
        (r'cp.* -r ', "don't use 'cp -r', use 'cp -R'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   187
        (r'grep.* -[ABC]', "don't use grep's context flags"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   188
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   189
            r'find.*-printf',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   190
            "don't use 'find -printf', it doesn't exist on BSD find(1)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   191
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   192
        (r'\$RANDOM ', "don't use bash-only $RANDOM to generate random values"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   193
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   194
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   195
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   196
        (r'^function', "don't use 'function', use old style"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   197
        (r'^diff.*-\w*N', "don't use 'diff -N'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   198
        (r'\$PWD|\${PWD}', "don't use $PWD, use `pwd`"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   199
        (r'^([^"\'\n]|("[^"\n]*")|(\'[^\'\n]*\'))*\^', "^ must be quoted"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   200
        (r'kill (`|\$\()', "don't use kill, use killdaemons.py"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   201
    ],
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   202
]
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   203
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   204
testfilters = [
34060
e267d4ee4f2d check-code: forbid using bash in shebang
Jun Wu <quark@fb.com>
parents: 33369
diff changeset
   205
    (r"( *)(#([^!][^\n]*\S)?)", repcomment),
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   206
    (r"<<(\S+)((.|\n)*?\n\1)", rephere),
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   207
]
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   208
15372
695ac6aca77f check-code: fix issues with finding patterns in unified tests, fix tests
Matt Mackall <mpm@selenic.com>
parents: 15364
diff changeset
   209
uprefix = r"^  \$ "
12364
e128fa4615f2 check-code: add some basic support for unified tests
Matt Mackall <mpm@selenic.com>
parents: 11886
diff changeset
   210
utestpats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   211
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   212
        (r'^(\S.*||  [$>] \S.*)[ \t]\n', "trailing whitespace on non-output"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   213
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   214
            uprefix + r'.*\|\s*sed[^|>\n]*\n',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   215
            "use regex test output patterns instead of sed",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   216
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   217
        (uprefix + r'(true|exit 0)', "explicit zero exit unnecessary"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   218
        (uprefix + r'.*(?<!\[)\$\?', "explicit exit code checks unnecessary"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   219
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   220
            uprefix + r'.*\|\| echo.*(fail|error)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   221
            "explicit exit code checks unnecessary",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   222
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   223
        (uprefix + r'set -e', "don't use set -e"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   224
        (uprefix + r'(\s|fi\b|done\b)', "use > for continued lines"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   225
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   226
            uprefix + r'.*:\.\S*/',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   227
            "x:.y in a path does not work on msys, rewrite "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   228
            "as x://.y, or see `hg log -k msys` for alternatives",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   229
            r'-\S+:\.|' '# no-msys',  # -Rxxx
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   230
        ),  # in test-pull.t which is skipped on windows
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   231
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   232
            r'^  [^$>].*27\.0\.0\.1',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   233
            'use $LOCALIP not an explicit loopback address',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   234
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   235
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   236
            r'^  (?![>$] ).*\$LOCALIP.*[^)]$',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   237
            'mark $LOCALIP output lines with (glob) to help tests in BSD jails',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   238
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   239
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   240
            r'^  (cat|find): .*: \$ENOENT\$',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   241
            'use test -f to test for file existence',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   242
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   243
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   244
            r'^  diff -[^ -]*p',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   245
            "don't use (external) diff with -p for portability",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   246
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   247
        (r' readlink ', 'use readlink.py instead of readlink'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   248
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   249
            r'^  [-+][-+][-+] .* [-+]0000 \(glob\)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   250
            "glob timezone field in diff output for portability",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   251
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   252
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   253
            r'^  @@ -[0-9]+ [+][0-9]+,[0-9]+ @@',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   254
            "use '@@ -N* +N,n @@ (glob)' style chunk header for portability",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   255
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   256
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   257
            r'^  @@ -[0-9]+,[0-9]+ [+][0-9]+ @@',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   258
            "use '@@ -N,n +N* @@ (glob)' style chunk header for portability",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   259
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   260
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   261
            r'^  @@ -[0-9]+ [+][0-9]+ @@',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   262
            "use '@@ -N* +N* @@ (glob)' style chunk header for portability",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   263
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   264
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   265
            uprefix + r'hg( +-[^ ]+( +[^ ]+)?)* +extdiff'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   266
            r'( +(-[^ po-]+|--(?!program|option)[^ ]+|[^-][^ ]*))*$',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   267
            "use $RUNTESTDIR/pdiff via extdiff (or -o/-p for false-positives)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   268
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   269
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   270
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   271
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   272
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   273
            r'^  (?!.*\$LOCALIP)[^*?/\n]* \(glob\)$',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   274
            "glob match with no glob string (?, *, /, and $LOCALIP)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   275
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   276
    ],
12364
e128fa4615f2 check-code: add some basic support for unified tests
Matt Mackall <mpm@selenic.com>
parents: 11886
diff changeset
   277
]
e128fa4615f2 check-code: add some basic support for unified tests
Matt Mackall <mpm@selenic.com>
parents: 11886
diff changeset
   278
35315
e223c0438f89 check-code: allow tabs in heredoc
Yuya Nishihara <yuya@tcha.org>
parents: 35251
diff changeset
   279
# transform plain test rules to unified test's
14203
b230922eb0c3 check-code: fix checking for sh style in .t tests
Mads Kiilerich <mads@kiilerich.com>
parents: 14169
diff changeset
   280
for i in [0, 1]:
22101
6fa40bd78bc8 check-code: allow an escape pattern to be specified for testpattern
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22100
diff changeset
   281
    for tp in testpats[i]:
6fa40bd78bc8 check-code: allow an escape pattern to be specified for testpattern
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22100
diff changeset
   282
        p = tp[0]
6fa40bd78bc8 check-code: allow an escape pattern to be specified for testpattern
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22100
diff changeset
   283
        m = tp[1]
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43112
diff changeset
   284
        if p.startswith('^'):
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43112
diff changeset
   285
            p = "^  [$>] (%s)" % p[1:]
14203
b230922eb0c3 check-code: fix checking for sh style in .t tests
Mads Kiilerich <mads@kiilerich.com>
parents: 14169
diff changeset
   286
        else:
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43112
diff changeset
   287
            p = "^  [$>] .*(%s)" % p
22101
6fa40bd78bc8 check-code: allow an escape pattern to be specified for testpattern
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22100
diff changeset
   288
        utestpats[i].append((p, m) + tp[2:])
12364
e128fa4615f2 check-code: add some basic support for unified tests
Matt Mackall <mpm@selenic.com>
parents: 11886
diff changeset
   289
35315
e223c0438f89 check-code: allow tabs in heredoc
Yuya Nishihara <yuya@tcha.org>
parents: 35251
diff changeset
   290
# don't transform the following rules:
e223c0438f89 check-code: allow tabs in heredoc
Yuya Nishihara <yuya@tcha.org>
parents: 35251
diff changeset
   291
# "  > \t" and "  \t" should be allowed in unified tests
e223c0438f89 check-code: allow tabs in heredoc
Yuya Nishihara <yuya@tcha.org>
parents: 35251
diff changeset
   292
testpats[0].append((r'^( *)\t', "don't use tabs to indent"))
e223c0438f89 check-code: allow tabs in heredoc
Yuya Nishihara <yuya@tcha.org>
parents: 35251
diff changeset
   293
utestpats[0].append((r'^( ?)\t', "don't use tabs to indent"))
e223c0438f89 check-code: allow tabs in heredoc
Yuya Nishihara <yuya@tcha.org>
parents: 35251
diff changeset
   294
12364
e128fa4615f2 check-code: add some basic support for unified tests
Matt Mackall <mpm@selenic.com>
parents: 11886
diff changeset
   295
utestfilters = [
17711
cf204e9829f4 check-code: replace heredocs in unified tests
Idan Kamara <idankk86@gmail.com>
parents: 17620
diff changeset
   296
    (r"<<(\S+)((.|\n)*?\n  > \1)", rephere),
34060
e267d4ee4f2d check-code: forbid using bash in shebang
Jun Wu <quark@fb.com>
parents: 33369
diff changeset
   297
    (r"( +)(#([^!][^\n]*\S)?)", repcomment),
12364
e128fa4615f2 check-code: add some basic support for unified tests
Matt Mackall <mpm@selenic.com>
parents: 11886
diff changeset
   298
]
e128fa4615f2 check-code: add some basic support for unified tests
Matt Mackall <mpm@selenic.com>
parents: 11886
diff changeset
   299
41821
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   300
# common patterns to check *.py
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   301
commonpypats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   302
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   303
        (r'\\$', 'Use () to wrap long lines in Python, not \\'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   304
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   305
            r'^\s*def\s*\w+\s*\(.*,\s*\(',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   306
            "tuple parameter unpacking not available in Python 3+",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   307
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   308
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   309
            r'lambda\s*\(.*,.*\)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   310
            "tuple parameter unpacking not available in Python 3+",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   311
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   312
        (r'(?<!def)\s+(cmp)\(', "cmp is not available in Python 3+"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   313
        (r'(?<!\.)\breduce\s*\(.*', "reduce is not available in Python 3+"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   314
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   315
            r'\bdict\(.*=',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   316
            'dict() is different in Py2 and 3 and is slower than {}',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   317
            'dict-from-generator',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   318
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   319
        (r'\.has_key\b', "dict.has_key is not available in Python 3+"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   320
        (r'\s<>\s', '<> operator is not available in Python 3+, use !='),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   321
        (r'^\s*\t', "don't use tabs"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   322
        (r'\S;\s*\n', "semicolon"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   323
        (r'[^_]_\([ \t\n]*(?:"[^"]+"[ \t\n+]*)+%', "don't use % inside _()"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   324
        (r"[^_]_\([ \t\n]*(?:'[^']+'[ \t\n+]*)+%", "don't use % inside _()"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   325
        (r'(\w|\)),\w', "missing whitespace after ,"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   326
        (r'(\w|\))[+/*\-<>]\w', "missing whitespace in expression"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   327
        (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   328
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   329
            (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   330
                # a line ending with a colon, potentially with trailing comments
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   331
                r':([ \t]*#[^\n]*)?\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   332
                # one that is not a pass and not only a comment
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   333
                r'(?P<indent>[ \t]+)[^#][^\n]+\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   334
                # more lines at the same indent level
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   335
                r'((?P=indent)[^\n]+\n)*'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   336
                # a pass at the same indent level, which is bogus
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   337
                r'(?P=indent)pass[ \t\n#]'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   338
            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   339
            'omit superfluous pass',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   340
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   341
        (r'[^\n]\Z', "no trailing newline"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   342
        (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   343
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   344
            r'^\s+(self\.)?[A-Za-z][a-z0-9]+[A-Z]\w* = ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   345
            "don't use camelcase in identifiers",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   346
            r'#.*camelcase-required',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   347
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   348
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   349
            r'^\s*(if|while|def|class|except|try)\s[^[\n]*:\s*[^\\n]#\s]+',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   350
            "linebreak after :",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   351
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   352
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   353
            r'class\s[^( \n]+:',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   354
            "old-style class, use class foo(object)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   355
            r'#.*old-style',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   356
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   357
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   358
            r'class\s[^( \n]+\(\):',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   359
            "class foo() creates old style object, use class foo(object)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   360
            r'#.*old-style',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   361
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   362
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   363
            r'\b(%s)\('
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   364
            % '|'.join(k for k in keyword.kwlist if k not in ('print', 'exec')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   365
            "Python keyword is not a function",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   366
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   367
        #    (r'class\s[A-Z][^\(]*\((?!Exception)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   368
        #     "don't capitalize non-exception classes"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   369
        #    (r'in range\(', "use xrange"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   370
        #    (r'^\s*print\s+', "avoid using print in core and extensions"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   371
        (r'[\x80-\xff]', "non-ASCII character literal"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   372
        (r'("\')\.format\(', "str.format() has no bytes counterpart, use %"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   373
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   374
            r'([\(\[][ \t]\S)|(\S[ \t][\)\]])',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   375
            "gratuitous whitespace in () or []",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   376
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   377
        #    (r'\s\s=', "gratuitous whitespace before ="),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   378
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   379
            r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\S',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   380
            "missing whitespace around operator",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   381
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   382
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   383
            r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\s',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   384
            "missing whitespace around operator",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   385
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   386
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   387
            r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\S',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   388
            "missing whitespace around operator",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   389
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   390
        (r'[^^+=*/!<>&| %-](\s=|=\s)[^= ]', "wrong whitespace around ="),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   391
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   392
            r'\([^()]*( =[^=]|[^<>!=]= )',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   393
            "no whitespace around = for named parameters",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   394
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   395
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   396
            r'raise [^,(]+, (\([^\)]+\)|[^,\(\)]+)$',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   397
            "don't use old-style two-argument raise, use Exception(message)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   398
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   399
        (r' is\s+(not\s+)?["\'0-9-]', "object comparison with literal"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   400
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   401
            r' [=!]=\s+(True|False|None)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   402
            "comparison with singleton, use 'is' or 'is not' instead",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   403
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   404
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   405
            r'^\s*(while|if) [01]:',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   406
            "use True/False for constant Boolean expression",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   407
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   408
        (r'^\s*if False(:| +and)', 'Remove code instead of using `if False`'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   409
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   410
            r'(?:(?<!def)\s+|\()hasattr\(',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   411
            'hasattr(foo, bar) is broken on py2, use util.safehasattr(foo, bar) '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   412
            'instead',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   413
            r'#.*hasattr-py3-only',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   414
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   415
        (r'opener\([^)]*\).read\(', "use opener.read() instead"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   416
        (r'opener\([^)]*\).write\(', "use opener.write() instead"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   417
        (r'(?i)descend[e]nt', "the proper spelling is descendAnt"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   418
        (r'\.debug\(\_', "don't mark debug messages for translation"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   419
        (r'\.strip\(\)\.split\(\)', "no need to strip before splitting"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   420
        (r'^\s*except\s*:', "naked except clause", r'#.*re-raises'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   421
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   422
            r'^\s*except\s([^\(,]+|\([^\)]+\))\s*,',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   423
            'legacy exception syntax; use "as" instead of ","',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   424
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   425
        (r'release\(.*wlock, .*lock\)', "wrong lock release order"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   426
        (r'\bdef\s+__bool__\b', "__bool__ should be __nonzero__ in Python 2"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   427
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   428
            r'os\.path\.join\(.*, *(""|\'\')\)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   429
            "use pathutil.normasprefix(path) instead of os.path.join(path, '')",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   430
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   431
        (r'\s0[0-7]+\b', 'legacy octal syntax; use "0o" prefix instead of "0"'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   432
        # XXX only catch mutable arguments on the first line of the definition
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   433
        (r'def.*[( ]\w+=\{\}', "don't use mutable default arguments"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   434
        (r'\butil\.Abort\b', "directly use error.Abort"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   435
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   436
            r'^@(\w*\.)?cachefunc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   437
            "module-level @cachefunc is risky, please avoid",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   438
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   439
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   440
            r'^import Queue',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   441
            "don't use Queue, use pycompat.queue.Queue + "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   442
            "pycompat.queue.Empty",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   443
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   444
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   445
            r'^import cStringIO',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   446
            "don't use cStringIO.StringIO, use util.stringio",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   447
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   448
        (r'^import urllib', "don't use urllib, use util.urlreq/util.urlerr"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   449
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   450
            r'^import SocketServer',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   451
            "don't use SockerServer, use util.socketserver",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   452
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   453
        (r'^import urlparse', "don't use urlparse, use util.urlreq"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   454
        (r'^import xmlrpclib', "don't use xmlrpclib, use util.xmlrpclib"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   455
        (r'^import cPickle', "don't use cPickle, use util.pickle"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   456
        (r'^import pickle', "don't use pickle, use util.pickle"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   457
        (r'^import httplib', "don't use httplib, use util.httplib"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   458
        (r'^import BaseHTTPServer', "use util.httpserver instead"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   459
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   460
            r'^(from|import) mercurial\.(cext|pure|cffi)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   461
            "use mercurial.policy.importmod instead",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   462
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   463
        (r'\.next\(\)', "don't use .next(), use next(...)"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   464
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   465
            r'([a-z]*).revision\(\1\.node\(',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   466
            "don't convert rev to node before passing to revision(nodeorrev)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   467
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   468
        (r'platform\.system\(\)', "don't use platform.system(), use pycompat"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   469
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   470
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   471
    [],
41821
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   472
]
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   473
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   474
# patterns to check normal *.py files
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   475
pypats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   476
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   477
        # Ideally, these should be placed in "commonpypats" for
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   478
        # consistency of coding rules in Mercurial source tree.
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   479
        # But on the other hand, these are not so seriously required for
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   480
        # python code fragments embedded in test scripts. Fixing test
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   481
        # scripts for these patterns requires many changes, and has less
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   482
        # profit than effort.
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   483
        (r'raise Exception', "don't raise generic exceptions"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   484
        (r'[\s\(](open|file)\([^)]*\)\.read\(', "use util.readfile() instead"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   485
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   486
            r'[\s\(](open|file)\([^)]*\)\.write\(',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   487
            "use util.writefile() instead",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   488
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   489
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   490
            r'^[\s\(]*(open(er)?|file)\([^)]*\)(?!\.close\(\))',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   491
            "always assign an opened file to a variable, and close it afterwards",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   492
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   493
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   494
            r'[\s\(](open|file)\([^)]*\)\.(?!close\(\))',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   495
            "always assign an opened file to a variable, and close it afterwards",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   496
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   497
        (r':\n(    )*( ){1,3}[^ ]', "must indent 4 spaces"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   498
        (r'^import atexit', "don't use atexit, use ui.atexit"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   499
        # rules depending on implementation of repquote()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   500
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   501
            r' x+[xpqo%APM][\'"]\n\s+[\'"]x',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   502
            'string join across lines with no space',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   503
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   504
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   505
            r'''(?x)ui\.(status|progress|write|note|warn)\(
29397
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   506
         [ \t\n#]*
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   507
         (?# any strings/comments might precede a string, which
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   508
           # contains translatable message)
43081
e65e7290041e contrib: fix check-code to be able to detect missing _() with bytestrings
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   509
         b?((['"]|\'\'\'|""")[ \npq%bAPMxno]*(['"]|\'\'\'|""")[ \t\n#]+)*
29397
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   510
         (?# sequence consisting of below might precede translatable message
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   511
           # - formatting string: "% 10s", "%05d", "% -3.2f", "%*s", "%%" ...
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   512
           # - escaped character: "\\", "\n", "\0" ...
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   513
           # - character other than '%', 'b' as '\', and 'x' as alphabet)
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   514
         (['"]|\'\'\'|""")
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   515
         ((%([ n]?[PM]?([np]+|A))?x)|%%|b[bnx]|[ \nnpqAPMo])*x
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   516
         (?# this regexp can't use [^...] style,
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   517
           # because _preparepats forcibly adds "\n" into [^...],
844f72885fb9 check-code: detect "missing _() in ui message" more exactly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29330
diff changeset
   518
           # even though this regexp wants match it against "\n")''',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   519
            "missing _() in ui message (use () to hide false-positives)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   520
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   521
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   522
    + commonpypats[0],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   523
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   524
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   525
        # rules depending on implementation of repquote()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   526
        (r'(^| )pp +xxxxqq[ \n][^\n]', "add two newlines after '.. note::'"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   527
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   528
    + commonpypats[1],
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   529
]
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   530
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   531
# patterns to check *.py for embedded ones in test script
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   532
embeddedpypats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   533
    [] + commonpypats[0],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   534
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   535
    [] + commonpypats[1],
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   536
]
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   537
41821
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   538
# common filters to convert *.py
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   539
commonpyfilters = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   540
    (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   541
        r"""(?msx)(?P<comment>\#.*?$)|
10727
62b8f15683f2 check-code: more tests and more robust python filtering
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10723
diff changeset
   542
         ((?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!")))
62b8f15683f2 check-code: more tests and more robust python filtering
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10723
diff changeset
   543
          (?P<text>(([^\\]|\\.)*?))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   544
          (?P=quote))""",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   545
        reppython,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   546
    ),
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   547
]
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   548
41821
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   549
# filters to convert normal *.py files
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   550
pyfilters = [] + commonpyfilters
41821
14e8d042993a contrib: split pypats list in check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41761
diff changeset
   551
34648
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   552
# non-filter patterns
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   553
pynfpats = [
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   554
    [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   555
        (r'pycompat\.osname\s*[=!]=\s*[\'"]nt[\'"]', "use pycompat.iswindows"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   556
        (r'pycompat\.osname\s*[=!]=\s*[\'"]posix[\'"]', "use pycompat.isposix"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   557
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   558
            r'pycompat\.sysplatform\s*[!=]=\s*[\'"]darwin[\'"]',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   559
            "use pycompat.isdarwin",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   560
        ),
34648
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   561
    ],
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   562
    # warnings
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   563
    [],
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   564
]
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   565
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   566
# filters to convert *.py for embedded ones in test script
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   567
embeddedpyfilters = [] + commonpyfilters
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   568
31602
772878ac930e checkcode: enforce lowercase for extension docstring title
Jun Wu <quark@fb.com>
parents: 31572
diff changeset
   569
# extension non-filter patterns
772878ac930e checkcode: enforce lowercase for extension docstring title
Jun Wu <quark@fb.com>
parents: 31572
diff changeset
   570
pyextnfpats = [
772878ac930e checkcode: enforce lowercase for extension docstring title
Jun Wu <quark@fb.com>
parents: 31572
diff changeset
   571
    [(r'^"""\n?[A-Z]', "don't capitalize docstring title")],
772878ac930e checkcode: enforce lowercase for extension docstring title
Jun Wu <quark@fb.com>
parents: 31572
diff changeset
   572
    # warnings
772878ac930e checkcode: enforce lowercase for extension docstring title
Jun Wu <quark@fb.com>
parents: 31572
diff changeset
   573
    [],
772878ac930e checkcode: enforce lowercase for extension docstring title
Jun Wu <quark@fb.com>
parents: 31572
diff changeset
   574
]
772878ac930e checkcode: enforce lowercase for extension docstring title
Jun Wu <quark@fb.com>
parents: 31572
diff changeset
   575
18960
170fc0949fb6 check-code: check txt files for trailing whitespace
Mads Kiilerich <madski@unity3d.com>
parents: 18959
diff changeset
   576
txtfilters = []
170fc0949fb6 check-code: check txt files for trailing whitespace
Mads Kiilerich <madski@unity3d.com>
parents: 18959
diff changeset
   577
170fc0949fb6 check-code: check txt files for trailing whitespace
Mads Kiilerich <madski@unity3d.com>
parents: 18959
diff changeset
   578
txtpats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   579
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   580
        (r'\s$', 'trailing whitespace'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   581
        ('.. note::[ \n][^\n]', 'add two newlines after note::'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   582
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   583
    [],
18960
170fc0949fb6 check-code: check txt files for trailing whitespace
Mads Kiilerich <madski@unity3d.com>
parents: 18959
diff changeset
   584
]
170fc0949fb6 check-code: check txt files for trailing whitespace
Mads Kiilerich <madski@unity3d.com>
parents: 18959
diff changeset
   585
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   586
cpats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   587
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   588
        (r'//', "don't use //-style comments"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   589
        (r'\S\t', "don't use tabs except for indent"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   590
        (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   591
        (r'(while|if|do|for)\(', "use space after while/if/do/for"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   592
        (r'return\(', "return is not a function"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   593
        (r' ;', "no space before ;"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   594
        (r'[^;] \)', "no space before )"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   595
        (r'[)][{]', "space between ) and {"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   596
        (r'\w+\* \w+', "use int *foo, not int* foo"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   597
        (r'\W\([^\)]+\) \w+', "use (int)foo, not (int) foo"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   598
        (r'\w+ (\+\+|--)', "use foo++, not foo ++"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   599
        (r'\w,\w', "missing whitespace after ,"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   600
        (r'^[^#]\w[+/*]\w', "missing whitespace in expression"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   601
        (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   602
        (r'^#\s+\w', "use #foo, not # foo"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   603
        (r'[^\n]\Z', "no trailing newline"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   604
        (r'^\s*#import\b', "use only #include in standard C code"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   605
        (r'strcpy\(', "don't use strcpy, use strlcpy or memcpy"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   606
        (r'strcat\(', "don't use strcat"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   607
        # rules depending on implementation of repquote()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   608
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   609
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   610
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   611
        # rules depending on implementation of repquote()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   612
    ],
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   613
]
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   614
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   615
cfilters = [
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   616
    (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
10722
c4fb2103e734 check-code: improve quote detection regexp, add tests
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10720
diff changeset
   617
    (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   618
    (r'''(#\s*include\s+<)([^>]+)>''', repinclude),
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   619
    (r'(\()([^)]+\))', repcallspaces),
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   620
]
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   621
14137
83a94c2fe6f4 check-code: check for repo in revlog and ui in util
timeless <timeless@mozdev.org>
parents: 14136
diff changeset
   622
inutilpats = [
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45830
diff changeset
   623
    [
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45830
diff changeset
   624
        (r'\bui\.', "don't use ui in util"),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45830
diff changeset
   625
    ],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   626
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   627
    [],
14137
83a94c2fe6f4 check-code: check for repo in revlog and ui in util
timeless <timeless@mozdev.org>
parents: 14136
diff changeset
   628
]
83a94c2fe6f4 check-code: check for repo in revlog and ui in util
timeless <timeless@mozdev.org>
parents: 14136
diff changeset
   629
83a94c2fe6f4 check-code: check for repo in revlog and ui in util
timeless <timeless@mozdev.org>
parents: 14136
diff changeset
   630
inrevlogpats = [
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45830
diff changeset
   631
    [
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45830
diff changeset
   632
        (r'\brepo\.', "don't use repo in revlog"),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45830
diff changeset
   633
    ],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   634
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   635
    [],
14137
83a94c2fe6f4 check-code: check for repo in revlog and ui in util
timeless <timeless@mozdev.org>
parents: 14136
diff changeset
   636
]
83a94c2fe6f4 check-code: check for repo in revlog and ui in util
timeless <timeless@mozdev.org>
parents: 14136
diff changeset
   637
21487
c26464ce0781 check-code: check for consistent usage of the websub filter in hgweb templates
Steven Brown <StevenGBrown@gmail.com>
parents: 21222
diff changeset
   638
webtemplatefilters = []
c26464ce0781 check-code: check for consistent usage of the websub filter in hgweb templates
Steven Brown <StevenGBrown@gmail.com>
parents: 21222
diff changeset
   639
c26464ce0781 check-code: check for consistent usage of the websub filter in hgweb templates
Steven Brown <StevenGBrown@gmail.com>
parents: 21222
diff changeset
   640
webtemplatepats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   641
    [],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   642
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   643
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   644
            r'{desc(\|(?!websub|firstline)[^\|]*)+}',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   645
            'follow desc keyword with either firstline or websub',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   646
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   647
    ],
21487
c26464ce0781 check-code: check for consistent usage of the websub filter in hgweb templates
Steven Brown <StevenGBrown@gmail.com>
parents: 21222
diff changeset
   648
]
c26464ce0781 check-code: check for consistent usage of the websub filter in hgweb templates
Steven Brown <StevenGBrown@gmail.com>
parents: 21222
diff changeset
   649
30246
b4c0f8d5edd2 contrib: check reference to old selenic.com domain
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30245
diff changeset
   650
allfilesfilters = []
b4c0f8d5edd2 contrib: check reference to old selenic.com domain
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30245
diff changeset
   651
b4c0f8d5edd2 contrib: check reference to old selenic.com domain
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30245
diff changeset
   652
allfilespats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   653
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   654
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   655
            r'(http|https)://[a-zA-Z0-9./]*selenic.com/',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   656
            'use mercurial-scm.org domain URL',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   657
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   658
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   659
            r'mercurial@selenic\.com',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   660
            'use mercurial-scm.org domain for mercurial ML address',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   661
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   662
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   663
            r'mercurial-devel@selenic\.com',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   664
            'use mercurial-scm.org domain for mercurial-devel ML address',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   665
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   666
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   667
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   668
    [],
30246
b4c0f8d5edd2 contrib: check reference to old selenic.com domain
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30245
diff changeset
   669
]
b4c0f8d5edd2 contrib: check reference to old selenic.com domain
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30245
diff changeset
   670
30665
01721d382c16 py3: add warnings in check-code related to py3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30557
diff changeset
   671
py3pats = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   672
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   673
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   674
            r'os\.environ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   675
            "use encoding.environ instead (py3)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   676
            r'#.*re-exports',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   677
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   678
        (r'os\.name', "use pycompat.osname instead (py3)"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   679
        (r'os\.getcwd', "use encoding.getcwd instead (py3)", r'#.*re-exports'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   680
        (r'os\.sep', "use pycompat.ossep instead (py3)"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   681
        (r'os\.pathsep', "use pycompat.ospathsep instead (py3)"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   682
        (r'os\.altsep', "use pycompat.osaltsep instead (py3)"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   683
        (r'sys\.platform', "use pycompat.sysplatform instead (py3)"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   684
        (r'getopt\.getopt', "use pycompat.getoptb instead (py3)"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   685
        (r'os\.getenv', "use encoding.environ.get instead"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   686
        (r'os\.setenv', "modifying the environ dict is not preferred"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   687
        (r'(?<!pycompat\.)xrange', "use pycompat.xrange instead (py3)"),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   688
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   689
    # warnings
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   690
    [],
30665
01721d382c16 py3: add warnings in check-code related to py3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30557
diff changeset
   691
]
01721d382c16 py3: add warnings in check-code related to py3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30557
diff changeset
   692
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   693
checks = [
21222
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   694
    ('python', r'.*\.(py|cgi)$', r'^#!.*python', pyfilters, pypats),
34648
4889b84b15f2 check-code: suggest pycompat.is(posix|windows|darwin)
Jun Wu <quark@fb.com>
parents: 34642
diff changeset
   695
    ('python', r'.*\.(py|cgi)$', r'^#!.*python', [], pynfpats),
31602
772878ac930e checkcode: enforce lowercase for extension docstring title
Jun Wu <quark@fb.com>
parents: 31572
diff changeset
   696
    ('python', r'.*hgext.*\.py$', '', [], pyextnfpats),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   697
    (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   698
        'python 3',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   699
        r'.*(hgext|mercurial)/(?!demandimport|policy|pycompat).*\.py',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   700
        '',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   701
        pyfilters,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   702
        py3pats,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   703
    ),
21222
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   704
    ('test script', r'(.*/)?test-[^.~]*$', '', testfilters, testpats),
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   705
    ('c', r'.*\.[ch]$', '', cfilters, cpats),
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   706
    ('unified test', r'.*\.t$', '', utestfilters, utestpats),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   707
    (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   708
        'layering violation repo in revlog',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   709
        r'mercurial/revlog\.py',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   710
        '',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   711
        pyfilters,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   712
        inrevlogpats,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   713
    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   714
    (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   715
        'layering violation ui in util',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   716
        r'mercurial/util\.py',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   717
        '',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   718
        pyfilters,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   719
        inutilpats,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   720
    ),
21222
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   721
    ('txt', r'.*\.txt$', '', txtfilters, txtpats),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   722
    (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   723
        'web template',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   724
        r'mercurial/templates/.*\.tmpl',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   725
        '',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   726
        webtemplatefilters,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   727
        webtemplatepats,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   728
    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   729
    ('all except for .po', r'.*(?<!\.po)$', '', allfilesfilters, allfilespats),
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   730
]
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   731
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   732
# (desc,
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   733
#  func to pick up embedded code fragments,
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   734
#  list of patterns to convert target files
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   735
#  list of patterns to detect errors/warnings)
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   736
embeddedchecks = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   737
    (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   738
        'embedded python',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   739
        testparseutil.pyembedded,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   740
        embeddedpyfilters,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   741
        embeddedpypats,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   742
    )
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   743
]
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   744
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   745
19307
5443d40d524b check-code: only fix patterns once
Simon Heimberg <simohe@besonet.ch>
parents: 19168
diff changeset
   746
def _preparepats():
41822
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   747
    def preparefailandwarn(failandwarn):
19307
5443d40d524b check-code: only fix patterns once
Simon Heimberg <simohe@besonet.ch>
parents: 19168
diff changeset
   748
        for pats in failandwarn:
5443d40d524b check-code: only fix patterns once
Simon Heimberg <simohe@besonet.ch>
parents: 19168
diff changeset
   749
            for i, pseq in enumerate(pats):
5443d40d524b check-code: only fix patterns once
Simon Heimberg <simohe@besonet.ch>
parents: 19168
diff changeset
   750
                # fix-up regexes for multi-line searches
19378
9de689d20230 cleanup: drop unused variables and an unused import
Simon Heimberg <simohe@besonet.ch>
parents: 19310
diff changeset
   751
                p = pseq[0]
36957
a8d540d2628c contrib: fix a subtle bug in check-code's regex rewriting
Augie Fackler <augie@google.com>
parents: 36949
diff changeset
   752
                # \s doesn't match \n (done in two steps)
a8d540d2628c contrib: fix a subtle bug in check-code's regex rewriting
Augie Fackler <augie@google.com>
parents: 36949
diff changeset
   753
                # first, we replace \s that appears in a set already
a8d540d2628c contrib: fix a subtle bug in check-code's regex rewriting
Augie Fackler <augie@google.com>
parents: 36949
diff changeset
   754
                p = re.sub(r'\[\\s', r'[ \\t', p)
a8d540d2628c contrib: fix a subtle bug in check-code's regex rewriting
Augie Fackler <augie@google.com>
parents: 36949
diff changeset
   755
                # now we replace other \s instances.
a8d540d2628c contrib: fix a subtle bug in check-code's regex rewriting
Augie Fackler <augie@google.com>
parents: 36949
diff changeset
   756
                p = re.sub(r'(?<!(\\|\[))\\s', r'[ \\t]', p)
19307
5443d40d524b check-code: only fix patterns once
Simon Heimberg <simohe@besonet.ch>
parents: 19168
diff changeset
   757
                # [^...] doesn't match newline
5443d40d524b check-code: only fix patterns once
Simon Heimberg <simohe@besonet.ch>
parents: 19168
diff changeset
   758
                p = re.sub(r'(?<!\\)\[\^', r'[^\\n', p)
5443d40d524b check-code: only fix patterns once
Simon Heimberg <simohe@besonet.ch>
parents: 19168
diff changeset
   759
19308
84faaacbd3fa check-code: compile all patterns on initialisation
Simon Heimberg <simohe@besonet.ch>
parents: 19307
diff changeset
   760
                pats[i] = (re.compile(p, re.MULTILINE),) + pseq[1:]
41822
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   761
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   762
    def preparefilters(filters):
19309
7d77fa1cd537 check-code: compile filters when loading
Simon Heimberg <simohe@besonet.ch>
parents: 19308
diff changeset
   763
        for i, flt in enumerate(filters):
7d77fa1cd537 check-code: compile filters when loading
Simon Heimberg <simohe@besonet.ch>
parents: 19308
diff changeset
   764
            filters[i] = re.compile(flt[0]), flt[1]
19307
5443d40d524b check-code: only fix patterns once
Simon Heimberg <simohe@besonet.ch>
parents: 19168
diff changeset
   765
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   766
    for cs in (checks, embeddedchecks):
41822
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   767
        for c in cs:
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   768
            failandwarn = c[-1]
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   769
            preparefailandwarn(failandwarn)
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   770
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   771
            filters = c[-2]
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   772
            preparefilters(filters)
55ae5cd31f76 contrib: refactor preparation logic for patterns of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41821
diff changeset
   773
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   774
10719
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   775
class norepeatlogger(object):
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   776
    def __init__(self):
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   777
        self._lastseen = None
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   778
11604
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   779
    def log(self, fname, lineno, line, msg, blame):
10719
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   780
        """print error related a to given line of a given file.
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   781
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   782
        The faulty line will also be printed but only once in the case
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   783
        of multiple errors.
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   784
10719
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   785
        :fname: filename
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   786
        :lineno: line number
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   787
        :line: actual content of the line
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   788
        :msg: error message
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   789
        """
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   790
        msgid = fname, lineno, line
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   791
        if msgid != self._lastseen:
11604
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   792
            if blame:
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
   793
                print("%s:%d (%s):" % (fname, lineno, blame))
11604
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   794
            else:
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
   795
                print("%s:%d:" % (fname, lineno))
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
   796
            print(" > %s" % line)
10719
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   797
            self._lastseen = msgid
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
   798
        print(" " + msg)
10719
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   799
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   800
10719
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   801
_defaultlogger = norepeatlogger()
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   802
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   803
11604
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   804
def getblame(f):
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   805
    lines = []
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   806
    for l in os.popen('hg annotate -un %s' % f):
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   807
        start, line = l.split(':', 1)
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   808
        user, rev = start.split()
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   809
        lines.append((line[1:-1], user, rev))
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   810
    return lines
c5d40818b270 check-code: add --blame switch
Matt Mackall <mpm@selenic.com>
parents: 11602
diff changeset
   811
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   812
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   813
def checkfile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   814
    f,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   815
    logfunc=_defaultlogger.log,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   816
    maxerr=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   817
    warnings=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   818
    blame=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   819
    debug=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   820
    lineno=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   821
):
10719
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   822
    """checks style and portability of a given file
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   823
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   824
    :f: filepath
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   825
    :logfunc: function used to report error
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   826
              logfunc(filename, linenumber, linecontent, errormessage)
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17347
diff changeset
   827
    :maxerr: number of error to display before aborting.
15873
a153a86a472c tests: keep track of all check-code.py warnings
Mads Kiilerich <mads@kiilerich.com>
parents: 15611
diff changeset
   828
             Set to false (default) to report all errors
10720
fbcccf9ec58f check-code: add a return value to checkfile function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10719
diff changeset
   829
fbcccf9ec58f check-code: add a return value to checkfile function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10719
diff changeset
   830
    return True if no error is found, False otherwise.
10719
3be9ae49b628 code-code: Add a logfunc argument to checkfile
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10718
diff changeset
   831
    """
10720
fbcccf9ec58f check-code: add a return value to checkfile function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10719
diff changeset
   832
    result = True
21222
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   833
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   834
    try:
29145
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
   835
        with opentext(f) as fp:
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
   836
            try:
41365
876494fd967d cleanup: delete lots of unused local variables
Martin von Zweigbergk <martinvonz@google.com>
parents: 39818
diff changeset
   837
                pre = fp.read()
29145
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
   838
            except UnicodeDecodeError as e:
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
   839
                print("%s while reading %s" % (e, f))
c641b8dfb98c check-code: handle py3 open divergence
timeless <timeless@mozdev.org>
parents: 29144
diff changeset
   840
                return result
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25659
diff changeset
   841
    except IOError as e:
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
   842
        print("Skipping %s, %s" % (f, str(e).split(':', 1)[0]))
21222
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   843
        return result
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   844
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   845
    # context information shared while single checkfile() invocation
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   846
    context = {'blamecache': None}
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   847
21222
4840abc83970 check-code: look at shebang to identify Python scripts
Matt Mackall <mpm@selenic.com>
parents: 21097
diff changeset
   848
    for name, match, magic, filters, pats in checks:
14135
673abd432104 check-code: adding debug flag
timeless <timeless@mozdev.org>
parents: 14009
diff changeset
   849
        if debug:
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
   850
            print(name, f)
28050
7e9e39228de6 check-code: examine magic pattern matching against contents of a file
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28035
diff changeset
   851
        if not (re.match(match, f) or (magic and re.search(magic, pre))):
14135
673abd432104 check-code: adding debug flag
timeless <timeless@mozdev.org>
parents: 14009
diff changeset
   852
            if debug:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   853
                print(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   854
                    "Skipping %s for %s it doesn't match %s" % (name, match, f)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   855
                )
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   856
            continue
19382
5aeb03b48ab4 check-code: concatenate "check-code" on compile time
Simon Heimberg <simohe@besonet.ch>
parents: 19380
diff changeset
   857
        if "no-" "check-code" in pre:
27560
15b06f306c1f check-code: improve test-check-code error diffs
timeless <timeless@mozdev.org>
parents: 27557
diff changeset
   858
            # If you're looking at this line, it's because a file has:
15b06f306c1f check-code: improve test-check-code error diffs
timeless <timeless@mozdev.org>
parents: 27557
diff changeset
   859
            # no- check- code
15b06f306c1f check-code: improve test-check-code error diffs
timeless <timeless@mozdev.org>
parents: 27557
diff changeset
   860
            # but the reason to output skipping is to make life for
15b06f306c1f check-code: improve test-check-code error diffs
timeless <timeless@mozdev.org>
parents: 27557
diff changeset
   861
            # tests easier. So, instead of writing it with a normal
15b06f306c1f check-code: improve test-check-code error diffs
timeless <timeless@mozdev.org>
parents: 27557
diff changeset
   862
            # spelling, we write it with the expected spelling from
15b06f306c1f check-code: improve test-check-code error diffs
timeless <timeless@mozdev.org>
parents: 27557
diff changeset
   863
            # tests/test-check-code.t
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
   864
            print("Skipping %s it has no-che?k-code (glob)" % f)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   865
            return "Skip"  # skip checking this file
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   866
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   867
        fc = _checkfiledata(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   868
            name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   869
            f,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   870
            pre,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   871
            filters,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   872
            pats,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   873
            context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   874
            logfunc,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   875
            maxerr,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   876
            warnings,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   877
            blame,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   878
            debug,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   879
            lineno,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   880
        )
41824
519b2faea261 contrib: change return value of file checking function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41823
diff changeset
   881
        if fc:
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   882
            result = False
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   883
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   884
    if f.endswith('.t') and "no-" "check-code" not in pre:
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   885
        if debug:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   886
            print("Checking embedded code in %s" % f)
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   887
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   888
        prelines = pre.splitlines()
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   889
        embeddederros = []
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   890
        for name, embedded, filters, pats in embeddedchecks:
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   891
            # "reset curmax at each repetition" treats maxerr as "max
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   892
            # nubmer of errors in an actual file per entry of
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   893
            # (embedded)checks"
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   894
            curmaxerr = maxerr
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   895
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   896
            for found in embedded(f, prelines, embeddederros):
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   897
                filename, starts, ends, code = found
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   898
                fc = _checkfiledata(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   899
                    name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   900
                    f,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   901
                    code,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   902
                    filters,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   903
                    pats,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   904
                    context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   905
                    logfunc,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   906
                    curmaxerr,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   907
                    warnings,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   908
                    blame,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   909
                    debug,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   910
                    lineno,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   911
                    offset=starts - 1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   912
                )
41826
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   913
                if fc:
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   914
                    result = False
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   915
                    if curmaxerr:
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   916
                        if fc >= curmaxerr:
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   917
                            break
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   918
                        curmaxerr -= fc
867883d454ea contrib: make check-code.py check code fragments embedded in test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41825
diff changeset
   919
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   920
    return result
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   921
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   922
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   923
def _checkfiledata(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   924
    name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   925
    f,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   926
    filedata,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   927
    filters,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   928
    pats,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   929
    context,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   930
    logfunc,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   931
    maxerr,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   932
    warnings,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   933
    blame,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   934
    debug,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   935
    lineno,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   936
    offset=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   937
):
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   938
    """Execute actual error check for file data
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   939
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   940
    :name: of the checking category
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   941
    :f: filepath
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   942
    :filedata: content of a file
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   943
    :filters: to be applied before checking
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   944
    :pats: to detect errors
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   945
    :context: a dict of information shared while single checkfile() invocation
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   946
              Valid keys: 'blamecache'.
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   947
    :logfunc: function used to report error
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   948
              logfunc(filename, linenumber, linecontent, errormessage)
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   949
    :maxerr: number of error to display before aborting, or False to
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   950
             report all errors
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   951
    :warnings: whether warning level checks should be applied
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   952
    :blame: whether blame information should be displayed at error reporting
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   953
    :debug: whether debug information should be displayed
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   954
    :lineno: whether lineno should be displayed at error reporting
41825
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
   955
    :offset: line number offset of 'filedata' in 'f' for checking
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
   956
             an embedded code fragment, or None (offset=0 is different
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
   957
             from offset=None)
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   958
41824
519b2faea261 contrib: change return value of file checking function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41823
diff changeset
   959
    returns number of detected errors.
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   960
    """
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   961
    blamecache = context['blamecache']
41825
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
   962
    if offset is None:
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
   963
        lineoffset = 0
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
   964
    else:
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
   965
        lineoffset = offset
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   966
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   967
    fc = 0
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   968
    pre = post = filedata
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
   969
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   970
    if True:  # TODO: get rid of this redundant 'if' block
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   971
        for p, r in filters:
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   972
            post = re.sub(p, r, post)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
   973
        nerrs = len(pats[0])  # nerr elements are errors
14009
64de9ca66511 check-code: separate warnings to avoid repetitive str.startswith
Idan Kamara <idankk86@gmail.com>
parents: 14005
diff changeset
   974
        if warnings:
64de9ca66511 check-code: separate warnings to avoid repetitive str.startswith
Idan Kamara <idankk86@gmail.com>
parents: 14005
diff changeset
   975
            pats = pats[0] + pats[1]
64de9ca66511 check-code: separate warnings to avoid repetitive str.startswith
Idan Kamara <idankk86@gmail.com>
parents: 14005
diff changeset
   976
        else:
64de9ca66511 check-code: separate warnings to avoid repetitive str.startswith
Idan Kamara <idankk86@gmail.com>
parents: 14005
diff changeset
   977
            pats = pats[0]
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   978
        # print post # uncomment to show filtered version
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   979
14135
673abd432104 check-code: adding debug flag
timeless <timeless@mozdev.org>
parents: 14009
diff changeset
   980
        if debug:
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
   981
            print("Checking %s for %s" % (name, f))
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   982
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   983
        prelines = None
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   984
        errors = []
19422
d9e86d656017 check-code: automatically preppend "warning: " to all warning messages
Simon Heimberg <simohe@besonet.ch>
parents: 19382
diff changeset
   985
        for i, pat in enumerate(pats):
16705
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16704
diff changeset
   986
            if len(pat) == 3:
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16704
diff changeset
   987
                p, msg, ignore = pat
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16704
diff changeset
   988
            else:
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16704
diff changeset
   989
                p, msg = pat
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16704
diff changeset
   990
                ignore = None
20005
22154ec6fb8b check-code: prepend warning prefix only once, but for each warning
Simon Heimberg <simohe@besonet.ch>
parents: 19999
diff changeset
   991
            if i >= nerrs:
22154ec6fb8b check-code: prepend warning prefix only once, but for each warning
Simon Heimberg <simohe@besonet.ch>
parents: 19999
diff changeset
   992
                msg = "warning: " + msg
16705
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16704
diff changeset
   993
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   994
            pos = 0
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   995
            n = 0
19308
84faaacbd3fa check-code: compile all patterns on initialisation
Simon Heimberg <simohe@besonet.ch>
parents: 19307
diff changeset
   996
            for m in p.finditer(post):
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   997
                if prelines is None:
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   998
                    prelines = pre.splitlines()
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
   999
                    postlines = post.splitlines(True)
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1000
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1001
                start = m.start()
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1002
                while n < len(postlines):
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1003
                    step = len(postlines[n])
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1004
                    if pos + step > start:
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1005
                        break
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1006
                    pos += step
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1007
                    n += 1
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1008
                l = prelines[n]
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1009
20242
2dad90bdf29d check-code: drop now unused check-code-ignore
Simon Heimberg <simohe@besonet.ch>
parents: 20241
diff changeset
  1010
                if ignore and re.search(ignore, l, re.MULTILINE):
20243
cc09cfea3dd4 check-code: print debug output when an ignore pattern matches
Simon Heimberg <simohe@besonet.ch>
parents: 20242
diff changeset
  1011
                    if debug:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1012
                        print(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1013
                            "Skipping %s for %s:%s (ignore pattern)"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1014
                            % (name, f, (n + lineoffset))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1015
                        )
16705
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16704
diff changeset
  1016
                    continue
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1017
                bd = ""
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1018
                if blame:
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1019
                    bd = 'working directory'
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
  1020
                    if blamecache is None:
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1021
                        blamecache = getblame(f)
41823
7a139fc60eb0 contrib: factor out actual error check for file data of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41822
diff changeset
  1022
                        context['blamecache'] = blamecache
41825
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1023
                    if (n + lineoffset) < len(blamecache):
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1024
                        bl, bu, br = blamecache[(n + lineoffset)]
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1025
                        if offset is None and bl == l:
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1026
                            bd = '%s@%s' % (bu, br)
41825
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1027
                        elif offset is not None and bl.endswith(l):
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1028
                            # "offset is not None" means "checking
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1029
                            # embedded code fragment". In this case,
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1030
                            # "l" does not have information about the
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1031
                            # beginning of an *original* line in the
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1032
                            # file (e.g. '  > ').
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1033
                            # Therefore, use "str.endswith()", and
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1034
                            # show "maybe" for a little loose
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1035
                            # examination.
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1036
                            bd = '%s@%s, maybe' % (bu, br)
20005
22154ec6fb8b check-code: prepend warning prefix only once, but for each warning
Simon Heimberg <simohe@besonet.ch>
parents: 19999
diff changeset
  1037
41825
6d6bd9039ecd contrib: add line offset information to file check function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41824
diff changeset
  1038
                errors.append((f, lineno and (n + lineoffset + 1), l, msg, bd))
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1039
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1040
        errors.sort()
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1041
        for e in errors:
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1042
            logfunc(*e)
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1043
            fc += 1
15873
a153a86a472c tests: keep track of all check-code.py warnings
Mads Kiilerich <mads@kiilerich.com>
parents: 15611
diff changeset
  1044
            if maxerr and fc >= maxerr:
28509
9e3ecb6f4995 check-code: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28399
diff changeset
  1045
                print(" (too many errors, giving up)")
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1046
                break
15281
aeeb2afcdc25 check-code: support multiline matches like try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 14978
diff changeset
  1047
41824
519b2faea261 contrib: change return value of file checking function of check-code.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 41823
diff changeset
  1048
    return fc
10717
b1f4fcef99b3 check-code: Add a ``checkfile`` function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10716
diff changeset
  1049
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1050
29568
7825f6154a65 check-code: factor out boot procedure into main
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29566
diff changeset
  1051
def main():
31824
4804644489cf check-code: use "-" to specify a list of files from stdin
Jun Wu <quark@fb.com>
parents: 31816
diff changeset
  1052
    parser = optparse.OptionParser("%prog [options] [files | -]")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1053
    parser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1054
        "-w",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1055
        "--warnings",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1056
        action="store_true",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1057
        help="include warning-level checks",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1058
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1059
    parser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1060
        "-p", "--per-file", type="int", help="max warnings per file"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1061
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1062
    parser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1063
        "-b",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1064
        "--blame",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1065
        action="store_true",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1066
        help="use annotate to generate blame info",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1067
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1068
    parser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1069
        "", "--debug", action="store_true", help="show debug information"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1070
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1071
    parser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1072
        "",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1073
        "--nolineno",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1074
        action="store_false",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1075
        dest='lineno',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1076
        help="don't show line numbers",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1077
    )
10895
217557b26bc7 check-code: add a warnings level
Matt Mackall <mpm@selenic.com>
parents: 10814
diff changeset
  1078
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1079
    parser.set_defaults(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1080
        per_file=15, warnings=False, blame=False, debug=False, lineno=True
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1081
    )
10895
217557b26bc7 check-code: add a warnings level
Matt Mackall <mpm@selenic.com>
parents: 10814
diff changeset
  1082
    (options, args) = parser.parse_args()
217557b26bc7 check-code: add a warnings level
Matt Mackall <mpm@selenic.com>
parents: 10814
diff changeset
  1083
217557b26bc7 check-code: add a warnings level
Matt Mackall <mpm@selenic.com>
parents: 10814
diff changeset
  1084
    if len(args) == 0:
10716
5f92bde72eef check-code: Only call check-code if __name__ = "__main__".
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10707
diff changeset
  1085
        check = glob.glob("*")
31824
4804644489cf check-code: use "-" to specify a list of files from stdin
Jun Wu <quark@fb.com>
parents: 31816
diff changeset
  1086
    elif args == ['-']:
4804644489cf check-code: use "-" to specify a list of files from stdin
Jun Wu <quark@fb.com>
parents: 31816
diff changeset
  1087
        # read file list from stdin
4804644489cf check-code: use "-" to specify a list of files from stdin
Jun Wu <quark@fb.com>
parents: 31816
diff changeset
  1088
        check = sys.stdin.read().splitlines()
10716
5f92bde72eef check-code: Only call check-code if __name__ = "__main__".
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10707
diff changeset
  1089
    else:
10895
217557b26bc7 check-code: add a warnings level
Matt Mackall <mpm@selenic.com>
parents: 10814
diff changeset
  1090
        check = args
10281
e7d3b509af8b Introduce check-code.py
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1091
29569
3d52e7c78a6b check-code: move fixing up regexp into main procedure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29568
diff changeset
  1092
    _preparepats()
3d52e7c78a6b check-code: move fixing up regexp into main procedure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29568
diff changeset
  1093
15544
53ef627cda30 check-code: fix return code initialization
Mads Kiilerich <mads@kiilerich.com>
parents: 15502
diff changeset
  1094
    ret = 0
10716
5f92bde72eef check-code: Only call check-code if __name__ = "__main__".
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 10707
diff changeset
  1095
    for f in check:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1096
        if not checkfile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1097
            f,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1098
            maxerr=options.per_file,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1099
            warnings=options.warnings,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1100
            blame=options.blame,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1101
            debug=options.debug,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1102
            lineno=options.lineno,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1103
        ):
11816
e1359ad582f6 check-code: add exit status
Alecs King <alecsk@gmail.com>
parents: 11764
diff changeset
  1104
            ret = 1
29568
7825f6154a65 check-code: factor out boot procedure into main
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29566
diff changeset
  1105
    return ret
7825f6154a65 check-code: factor out boot procedure into main
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29566
diff changeset
  1106
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43063
diff changeset
  1107
29568
7825f6154a65 check-code: factor out boot procedure into main
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29566
diff changeset
  1108
if __name__ == "__main__":
7825f6154a65 check-code: factor out boot procedure into main
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29566
diff changeset
  1109
    sys.exit(main())