contrib/testparseutil.py
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 24 Oct 2024 22:47:31 -0400
changeset 52127 fd200f5bcaea
parent 49874 cd3b8fd1d3eb
permissions -rw-r--r--
wireprototypes: make `baseprotocolhandler` methods abstract The documentation says it's an abstract base class, so let's enforce it. The `typing.Protocol` class is already an ABC, but it only prevents instantiation if there are abstract attrs that are missing. For example, from `hg debugshell`: >>> from mercurial import wireprototypes >>> x = wireprototypes.baseprotocolhandler() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class baseprotocolhandler with abstract method name >>> class fake(wireprototypes.baseprotocolhandler): ... pass ... >>> x = fake() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class fake with abstract method name That's great, but it doesn't protect against calling non-abstract methods at runtime, rather it depends on the protocol type hint being added to method signatures or class attrs, and then running a type checker to notice when an instance is assigned that doesn't conform to the protocol. We don't widely use type hints yet, and do have a lot of class hierarchy in the repository area, which could lead to surprises like this: >>> class fake(wireprototypes.baseprotocolhandler): ... @property ... def name(self) -> bytes: ... return b'name' ... >>> z = fake() >>> z.client() >>> print(z.client()) None Oops. That was supposed to return `bytes`. So not only is a bad/unexpected value returned, but it's one that violates the type hints (since the base client() method will be annotated to return bytes). With this change, we get: >>> from mercurial import wireprototypes >>> class fake(wireprototypes.baseprotocolhandler): ... @property ... def name(self) -> bytes: ... return b'name' ... >>> x = fake() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class fake with abstract methods addcapabilities, checkperm, client, getargs, getpayload, getprotocaps, mayberedirectstdio So this looks like a reasonable safety harness to me, and lets us catch problems by running the standard tests while the type hints are being added, and pytype is improved. We should probably do this for all Protocol class methods that don't supply a method implementation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     1
# testparseutil.py - utilities to parse test script for check tools
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     2
#
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     3
#  Copyright 2018 FUJIWARA Katsunori <foozy@lares.dti.ne.jp> and others
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     4
#
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     7
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     8
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     9
import abc
49874
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    10
import builtins
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    11
import re
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    12
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    13
####################
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    14
# for Python3 compatibility (almost comes from mercurial/pycompat.py)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    15
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    16
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    17
def identity(a):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    18
    return a
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    19
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    20
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    21
def _rapply(f, xs):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    22
    if xs is None:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    23
        # assume None means non-value of optional data
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    24
        return xs
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    25
    if isinstance(xs, (list, set, tuple)):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    26
        return type(xs)(_rapply(f, x) for x in xs)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    27
    if isinstance(xs, dict):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    28
        return type(xs)((_rapply(f, k), _rapply(f, v)) for k, v in xs.items())
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    29
    return f(xs)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    30
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    31
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    32
def rapply(f, xs):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    33
    if f is identity:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    34
        # fast path mainly for py2
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    35
        return xs
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    36
    return _rapply(f, xs)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    37
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    38
49874
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    39
def bytestr(s):
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    40
    # tiny version of pycompat.bytestr
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    41
    return s.encode('latin1')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    42
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    43
49874
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    44
def sysstr(s):
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    45
    if isinstance(s, builtins.str):
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    46
        return s
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    47
    return s.decode('latin-1')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    48
49874
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    49
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    50
def opentext(f):
cd3b8fd1d3eb contrib: drop py2 support from testparseutil.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    51
    return open(f, 'r')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    52
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    53
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    54
def b2s(x):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    55
    # convert BYTES elements in "x" to SYSSTR recursively
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    56
    return rapply(sysstr, x)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    57
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    58
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    59
def writeout(data):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    60
    # write "data" in BYTES into stdout
42390
37f38e1dea44 testparseutil: stop extracting using std* streams as bytes on py3
Augie Fackler <augie@google.com>
parents: 42330
diff changeset
    61
    sys.stdout.write(data)
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    62
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    63
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    64
def writeerr(data):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    65
    # write "data" in BYTES into stderr
42390
37f38e1dea44 testparseutil: stop extracting using std* streams as bytes on py3
Augie Fackler <augie@google.com>
parents: 42330
diff changeset
    66
    sys.stderr.write(data)
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    67
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    68
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    69
####################
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    70
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    71
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    72
class embeddedmatcher:  # pytype: disable=ignored-metaclass
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44020
diff changeset
    73
    """Base class to detect embedded code fragments in *.t test script"""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    74
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    75
    __metaclass__ = abc.ABCMeta
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    76
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    77
    def __init__(self, desc):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    78
        self.desc = desc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    79
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    80
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    81
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    82
        """Examine whether embedded code starts at line
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    83
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    84
        This can return arbitrary object, and it is used as 'ctx' for
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    85
        subsequent method invocations.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    86
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    87
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    88
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    89
    def endsat(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    90
        """Examine whether embedded code ends at line"""
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    91
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    92
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    93
    def isinside(self, ctx, line):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44020
diff changeset
    94
        """Examine whether line is inside embedded code, if not yet endsat"""
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    95
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    96
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    97
    def ignores(self, ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    98
        """Examine whether detected embedded code should be ignored"""
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    99
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   100
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   101
    def filename(self, ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   102
        """Return filename of embedded code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   103
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   104
        If filename isn't specified for embedded code explicitly, this
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   105
        returns None.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   106
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   107
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   108
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   109
    def codeatstart(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   110
        """Return actual code at the start line of embedded code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   111
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   112
        This might return None, if the start line doesn't contain
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   113
        actual code.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   114
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   115
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   116
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   117
    def codeatend(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   118
        """Return actual code at the end line of embedded code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   119
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   120
        This might return None, if the end line doesn't contain actual
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   121
        code.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   122
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   123
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   124
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   125
    def codeinside(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   126
        """Return actual code at line inside embedded code"""
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   127
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   128
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   129
def embedded(basefile, lines, errors, matchers):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   130
    """pick embedded code fragments up from given lines
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   131
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   132
    This is common parsing logic, which examines specified matchers on
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   133
    given lines.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   134
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   135
    :basefile: a name of a file, from which lines to be parsed come.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   136
    :lines: to be parsed (might be a value returned by "open(basefile)")
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   137
    :errors: an array, into which messages for detected error are stored
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   138
    :matchers: an array of embeddedmatcher objects
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   139
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   140
    This function yields '(filename, starts, ends, code)' tuple.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   141
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   142
    :filename: a name of embedded code, if it is explicitly specified
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   143
               (e.g.  "foobar" of "cat >> foobar <<EOF").
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   144
               Otherwise, this is None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   145
    :starts: line number (1-origin), at which embedded code starts (inclusive)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   146
    :ends: line number (1-origin), at which embedded code ends (exclusive)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   147
    :code: extracted embedded code, which is single-stringified
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   148
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
   149
    >>> class ambigmatcher:
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   150
    ...     # mock matcher class to examine implementation of
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   151
    ...     # "ambiguous matching" corner case
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   152
    ...     def __init__(self, desc, matchfunc):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   153
    ...         self.desc = desc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   154
    ...         self.matchfunc = matchfunc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   155
    ...     def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   156
    ...         return self.matchfunc(line)
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   157
    >>> ambig1 = ambigmatcher('ambiguous #1',
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   158
    ...                       lambda l: l.startswith('  $ cat '))
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   159
    >>> ambig2 = ambigmatcher('ambiguous #2',
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   160
    ...                       lambda l: l.endswith('<< EOF\\n'))
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   161
    >>> lines = ['  $ cat > foo.py << EOF\\n']
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   162
    >>> errors = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   163
    >>> matchers = [ambig1, ambig2]
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   164
    >>> list(t for t in embedded('<dummy>', lines, errors, matchers))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   165
    []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   166
    >>> b2s(errors)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   167
    ['<dummy>:1: ambiguous line for "ambiguous #1", "ambiguous #2"']
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   168
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   169
    """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   170
    matcher = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   171
    ctx = filename = code = startline = None  # for pyflakes
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   172
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   173
    for lineno, line in enumerate(lines, 1):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   174
        if not line.endswith('\n'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   175
            line += '\n'  # to normalize EOF line
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   176
        if matcher:  # now, inside embedded code
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   177
            if matcher.endsat(ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   178
                codeatend = matcher.codeatend(ctx, line)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   179
                if codeatend is not None:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   180
                    code.append(codeatend)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   181
                if not matcher.ignores(ctx):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   182
                    yield (filename, startline, lineno, ''.join(code))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   183
                matcher = None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   184
                # DO NOT "continue", because line might start next fragment
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   185
            elif not matcher.isinside(ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   186
                # this is an error of basefile
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   187
                # (if matchers are implemented correctly)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   188
                errors.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   189
                    '%s:%d: unexpected line for "%s"'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   190
                    % (basefile, lineno, matcher.desc)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   191
                )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   192
                # stop extracting embedded code by current 'matcher',
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   193
                # because appearance of unexpected line might mean
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   194
                # that expected end-of-embedded-code line might never
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   195
                # appear
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   196
                matcher = None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   197
                # DO NOT "continue", because line might start next fragment
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   198
            else:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   199
                code.append(matcher.codeinside(ctx, line))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   200
                continue
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   201
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   202
        # examine whether current line starts embedded code or not
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   203
        assert not matcher
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   204
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   205
        matched = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   206
        for m in matchers:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   207
            ctx = m.startsat(line)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   208
            if ctx:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   209
                matched.append((m, ctx))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   210
        if matched:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   211
            if len(matched) > 1:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   212
                # this is an error of matchers, maybe
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   213
                errors.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   214
                    '%s:%d: ambiguous line for %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   215
                    % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   216
                        basefile,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   217
                        lineno,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   218
                        ', '.join(['"%s"' % m.desc for m, c in matched]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   219
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   220
                )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   221
                # omit extracting embedded code, because choosing
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   222
                # arbitrary matcher from matched ones might fail to
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   223
                # detect the end of embedded code as expected.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   224
                continue
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   225
            matcher, ctx = matched[0]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   226
            filename = matcher.filename(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   227
            code = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   228
            codeatstart = matcher.codeatstart(ctx, line)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   229
            if codeatstart is not None:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   230
                code.append(codeatstart)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   231
                startline = lineno
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   232
            else:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   233
                startline = lineno + 1
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   234
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   235
    if matcher:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   236
        # examine whether EOF ends embedded code, because embedded
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   237
        # code isn't yet ended explicitly
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   238
        if matcher.endsat(ctx, '\n'):
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   239
            codeatend = matcher.codeatend(ctx, '\n')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   240
            if codeatend is not None:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   241
                code.append(codeatend)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   242
            if not matcher.ignores(ctx):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   243
                yield (filename, startline, lineno + 1, ''.join(code))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   244
        else:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   245
            # this is an error of basefile
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   246
            # (if matchers are implemented correctly)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   247
            errors.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   248
                '%s:%d: unexpected end of file for "%s"'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   249
                % (basefile, lineno, matcher.desc)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   250
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   251
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   252
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   253
# heredoc limit mark to ignore embedded code at check-code.py or so
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   254
heredocignorelimit = 'NO_CHECK_EOF'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   255
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   256
# the pattern to match against cases below, and to return a limit mark
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   257
# string as 'lname' group
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   258
#
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   259
# - << LIMITMARK
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   260
# - << "LIMITMARK"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   261
# - << 'LIMITMARK'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   262
heredoclimitpat = r'\s*<<\s*(?P<lquote>["\']?)(?P<limit>\w+)(?P=lquote)'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   263
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   264
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   265
class fileheredocmatcher(embeddedmatcher):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   266
    """Detect "cat > FILE << LIMIT" style embedded code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   267
42391
c2deb2512823 testparseutil: fix doctest to use str instead of bytes
Augie Fackler <augie@google.com>
parents: 42390
diff changeset
   268
    >>> matcher = fileheredocmatcher('heredoc .py file', r'[^<]+\\.py')
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   269
    >>> b2s(matcher.startsat('  $ cat > file.py << EOF\\n'))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   270
    ('file.py', '  > EOF\\n')
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   271
    >>> b2s(matcher.startsat('  $ cat   >>file.py   <<EOF\\n'))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   272
    ('file.py', '  > EOF\\n')
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   273
    >>> b2s(matcher.startsat('  $ cat>  \\x27any file.py\\x27<<  "EOF"\\n'))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   274
    ('any file.py', '  > EOF\\n')
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   275
    >>> b2s(matcher.startsat("  $ cat > file.py << 'ANYLIMIT'\\n"))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   276
    ('file.py', '  > ANYLIMIT\\n')
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   277
    >>> b2s(matcher.startsat('  $ cat<<ANYLIMIT>"file.py"\\n'))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   278
    ('file.py', '  > ANYLIMIT\\n')
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   279
    >>> start = '  $ cat > file.py << EOF\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   280
    >>> ctx = matcher.startsat(start)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   281
    >>> matcher.codeatstart(ctx, start)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   282
    >>> b2s(matcher.filename(ctx))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   283
    'file.py'
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   284
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   285
    False
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   286
    >>> inside = '  > foo = 1\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   287
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   288
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   289
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   290
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   291
    >>> b2s(matcher.codeinside(ctx, inside))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   292
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   293
    >>> end = '  > EOF\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   294
    >>> matcher.endsat(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   295
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   296
    >>> matcher.codeatend(ctx, end)
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   297
    >>> matcher.endsat(ctx, '  > EOFEOF\\n')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   298
    False
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   299
    >>> ctx = matcher.startsat('  $ cat > file.py << NO_CHECK_EOF\\n')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   300
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   301
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   302
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   303
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   304
    _prefix = '  > '
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   305
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   306
    def __init__(self, desc, namepat):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   307
        super(fileheredocmatcher, self).__init__(desc)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   308
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   309
        # build the pattern to match against cases below (and ">>"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   310
        # variants), and to return a target filename string as 'name'
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   311
        # group
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   312
        #
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   313
        # - > NAMEPAT
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   314
        # - > "NAMEPAT"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   315
        # - > 'NAMEPAT'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   316
        namepat = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   317
            r'\s*>>?\s*(?P<nquote>["\']?)(?P<name>%s)(?P=nquote)' % namepat
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   318
        )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   319
        self._fileres = [
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   320
            # "cat > NAME << LIMIT" case
44020
ac3cb5e05a38 cleanup: replace contiguous spaces in regex patterns with an explicit count
Matt Harbison <matt_harbison@yahoo.com>
parents: 43474
diff changeset
   321
            re.compile(r' {2}\$ \s*cat' + namepat + heredoclimitpat),
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   322
            # "cat << LIMIT > NAME" case
44020
ac3cb5e05a38 cleanup: replace contiguous spaces in regex patterns with an explicit count
Matt Harbison <matt_harbison@yahoo.com>
parents: 43474
diff changeset
   323
            re.compile(r' {2}\$ \s*cat' + heredoclimitpat + namepat),
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   324
        ]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   325
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   326
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   327
        # ctx is (filename, END-LINE-OF-EMBEDDED-CODE) tuple
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   328
        for filere in self._fileres:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   329
            matched = filere.match(line)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   330
            if matched:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   331
                return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   332
                    matched.group('name'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   333
                    '  > %s\n' % matched.group('limit'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   334
                )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   335
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   336
    def endsat(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   337
        return ctx[1] == line
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   338
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   339
    def isinside(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   340
        return line.startswith(self._prefix)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   341
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   342
    def ignores(self, ctx):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   343
        return '  > %s\n' % heredocignorelimit == ctx[1]
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   344
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   345
    def filename(self, ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   346
        return ctx[0]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   347
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   348
    def codeatstart(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   349
        return None  # no embedded code at start line
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   350
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   351
    def codeatend(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   352
        return None  # no embedded code at end line
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   353
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   354
    def codeinside(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   355
        return line[len(self._prefix) :]  # strip prefix
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   356
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   357
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   358
####
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   359
# for embedded python script
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   360
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   361
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   362
class pydoctestmatcher(embeddedmatcher):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   363
    """Detect ">>> code" style embedded python code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   364
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   365
    >>> matcher = pydoctestmatcher()
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   366
    >>> startline = '  >>> foo = 1\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   367
    >>> matcher.startsat(startline)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   368
    True
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   369
    >>> matcher.startsat('  ... foo = 1\\n')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   370
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   371
    >>> ctx = matcher.startsat(startline)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   372
    >>> matcher.filename(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   373
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   374
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   375
    >>> b2s(matcher.codeatstart(ctx, startline))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   376
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   377
    >>> inside = '  >>> foo = 1\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   378
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   379
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   380
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   381
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   382
    >>> b2s(matcher.codeinside(ctx, inside))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   383
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   384
    >>> inside = '  ... foo = 1\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   385
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   386
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   387
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   388
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   389
    >>> b2s(matcher.codeinside(ctx, inside))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   390
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   391
    >>> inside = '  expected output\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   392
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   393
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   394
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   395
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   396
    >>> b2s(matcher.codeinside(ctx, inside))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   397
    '\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   398
    >>> inside = '  \\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   399
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   400
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   401
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   402
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   403
    >>> b2s(matcher.codeinside(ctx, inside))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   404
    '\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   405
    >>> end = '  $ foo bar\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   406
    >>> matcher.endsat(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   407
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   408
    >>> matcher.codeatend(ctx, end)
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   409
    >>> end = '\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   410
    >>> matcher.endsat(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   411
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   412
    >>> matcher.codeatend(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   413
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   414
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   415
    _prefix = '  >>> '
44020
ac3cb5e05a38 cleanup: replace contiguous spaces in regex patterns with an explicit count
Matt Harbison <matt_harbison@yahoo.com>
parents: 43474
diff changeset
   416
    _prefixre = re.compile(r' {2}(>>>|\.\.\.) ')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   417
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   418
    # If a line matches against not _prefixre but _outputre, that line
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   419
    # is "an expected output line" (= not a part of code fragment).
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   420
    #
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   421
    # Strictly speaking, a line matching against "(#if|#else|#endif)"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   422
    # is also treated similarly in "inline python code" semantics by
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   423
    # run-tests.py. But "directive line inside inline python code"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   424
    # should be rejected by Mercurial reviewers. Therefore, this
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   425
    # regexp does not matche against such directive lines.
44020
ac3cb5e05a38 cleanup: replace contiguous spaces in regex patterns with an explicit count
Matt Harbison <matt_harbison@yahoo.com>
parents: 43474
diff changeset
   426
    _outputre = re.compile(r' {2}$| {2}[^$]')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   427
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   428
    def __init__(self):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   429
        super(pydoctestmatcher, self).__init__("doctest style python code")
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   430
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   431
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   432
        # ctx is "True"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   433
        return line.startswith(self._prefix)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   434
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   435
    def endsat(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   436
        return not (self._prefixre.match(line) or self._outputre.match(line))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   437
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   438
    def isinside(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   439
        return True  # always true, if not yet ended
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   440
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   441
    def ignores(self, ctx):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   442
        return False  # should be checked always
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   443
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   444
    def filename(self, ctx):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   445
        return None  # no filename
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   446
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   447
    def codeatstart(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   448
        return line[len(self._prefix) :]  # strip prefix '  >>> '/'  ... '
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   449
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   450
    def codeatend(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   451
        return None  # no embedded code at end line
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   452
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   453
    def codeinside(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   454
        if self._prefixre.match(line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   455
            return line[len(self._prefix) :]  # strip prefix '  >>> '/'  ... '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   456
        return '\n'  # an expected output line is treated as an empty line
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   457
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   458
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   459
class pyheredocmatcher(embeddedmatcher):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   460
    """Detect "python << LIMIT" style embedded python code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   461
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   462
    >>> matcher = pyheredocmatcher()
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   463
    >>> b2s(matcher.startsat('  $ python << EOF\\n'))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   464
    '  > EOF\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   465
    >>> b2s(matcher.startsat('  $ $PYTHON   <<EOF\\n'))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   466
    '  > EOF\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   467
    >>> b2s(matcher.startsat('  $ "$PYTHON"<<  "EOF"\\n'))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   468
    '  > EOF\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   469
    >>> b2s(matcher.startsat("  $ $PYTHON << 'ANYLIMIT'\\n"))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   470
    '  > ANYLIMIT\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   471
    >>> matcher.startsat('  $ "$PYTHON" < EOF\\n')
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   472
    >>> start = '  $ python << EOF\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   473
    >>> ctx = matcher.startsat(start)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   474
    >>> matcher.codeatstart(ctx, start)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   475
    >>> matcher.filename(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   476
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   477
    False
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   478
    >>> inside = '  > foo = 1\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   479
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   480
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   481
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   482
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   483
    >>> b2s(matcher.codeinside(ctx, inside))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   484
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   485
    >>> end = '  > EOF\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   486
    >>> matcher.endsat(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   487
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   488
    >>> matcher.codeatend(ctx, end)
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   489
    >>> matcher.endsat(ctx, '  > EOFEOF\\n')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   490
    False
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   491
    >>> ctx = matcher.startsat('  $ python << NO_CHECK_EOF\\n')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   492
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   493
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   494
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   495
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   496
    _prefix = '  > '
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   497
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   498
    _startre = re.compile(
44020
ac3cb5e05a38 cleanup: replace contiguous spaces in regex patterns with an explicit count
Matt Harbison <matt_harbison@yahoo.com>
parents: 43474
diff changeset
   499
        r' {2}\$ (\$PYTHON|"\$PYTHON"|python).*' + heredoclimitpat
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   500
    )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   501
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   502
    def __init__(self):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   503
        super(pyheredocmatcher, self).__init__("heredoc python invocation")
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   504
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   505
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   506
        # ctx is END-LINE-OF-EMBEDDED-CODE
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   507
        matched = self._startre.match(line)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   508
        if matched:
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   509
            return '  > %s\n' % matched.group('limit')
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   510
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   511
    def endsat(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   512
        return ctx == line
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   513
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   514
    def isinside(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   515
        return line.startswith(self._prefix)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   516
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   517
    def ignores(self, ctx):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   518
        return '  > %s\n' % heredocignorelimit == ctx
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   519
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   520
    def filename(self, ctx):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   521
        return None  # no filename
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   522
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   523
    def codeatstart(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   524
        return None  # no embedded code at start line
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   525
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   526
    def codeatend(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   527
        return None  # no embedded code at end line
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   528
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   529
    def codeinside(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   530
        return line[len(self._prefix) :]  # strip prefix
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   531
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   532
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   533
_pymatchers = [
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   534
    pydoctestmatcher(),
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   535
    pyheredocmatcher(),
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   536
    # use '[^<]+' instead of '\S+', in order to match against
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   537
    # paths including whitespaces
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   538
    fileheredocmatcher('heredoc .py file', r'[^<]+\.py'),
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   539
]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   540
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   541
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   542
def pyembedded(basefile, lines, errors):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   543
    return embedded(basefile, lines, errors, _pymatchers)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   544
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   545
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   546
####
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   547
# for embedded shell script
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   548
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   549
_shmatchers = [
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   550
    # use '[^<]+' instead of '\S+', in order to match against
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   551
    # paths including whitespaces
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   552
    fileheredocmatcher('heredoc .sh file', r'[^<]+\.sh'),
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   553
]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   554
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   555
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   556
def shembedded(basefile, lines, errors):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   557
    return embedded(basefile, lines, errors, _shmatchers)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   558
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   559
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   560
####
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   561
# for embedded hgrc configuration
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   562
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   563
_hgrcmatchers = [
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   564
    # use '[^<]+' instead of '\S+', in order to match against
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   565
    # paths including whitespaces
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   566
    fileheredocmatcher(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   567
        'heredoc hgrc file', r'(([^/<]+/)+hgrc|\$HGRCPATH|\${HGRCPATH})'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   568
    ),
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   569
]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   570
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   571
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   572
def hgrcembedded(basefile, lines, errors):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   573
    return embedded(basefile, lines, errors, _hgrcmatchers)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   574
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   575
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   576
####
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   577
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   578
if __name__ == "__main__":
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   579
    import optparse
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   580
    import sys
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   581
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   582
    def showembedded(basefile, lines, embeddedfunc, opts):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   583
        errors = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   584
        for name, starts, ends, code in embeddedfunc(basefile, lines, errors):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   585
            if not name:
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   586
                name = '<anonymous>'
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   587
            writeout("%s:%d: %s starts\n" % (basefile, starts, name))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   588
            if opts.verbose and code:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   589
                writeout("  |%s\n" % "\n  |".join(l for l in code.splitlines()))
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   590
            writeout("%s:%d: %s ends\n" % (basefile, ends, name))
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   591
        for e in errors:
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   592
            writeerr("%s\n" % e)
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   593
        return len(errors)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   594
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   595
    def applyembedded(args, embeddedfunc, opts):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   596
        ret = 0
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   597
        if args:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   598
            for f in args:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   599
                with opentext(f) as fp:
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   600
                    if showembedded(f, fp, embeddedfunc, opts):
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   601
                        ret = 1
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   602
        else:
42390
37f38e1dea44 testparseutil: stop extracting using std* streams as bytes on py3
Augie Fackler <augie@google.com>
parents: 42330
diff changeset
   603
            lines = [l for l in sys.stdin.readlines()]
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   604
            if showembedded('<stdin>', lines, embeddedfunc, opts):
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   605
                ret = 1
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   606
        return ret
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   607
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   608
    commands = {}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   609
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   610
    def command(name, desc):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   611
        def wrap(func):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   612
            commands[name] = (desc, func)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   613
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   614
        return wrap
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   615
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   616
    @command("pyembedded", "detect embedded python script")
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   617
    def pyembeddedcmd(args, opts):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   618
        return applyembedded(args, pyembedded, opts)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   619
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   620
    @command("shembedded", "detect embedded shell script")
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   621
    def shembeddedcmd(args, opts):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   622
        return applyembedded(args, shembedded, opts)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   623
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   624
    @command("hgrcembedded", "detect embedded hgrc configuration")
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   625
    def hgrcembeddedcmd(args, opts):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   626
        return applyembedded(args, hgrcembedded, opts)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   627
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   628
    availablecommands = "\n".join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   629
        ["  - %s: %s" % (key, value[0]) for key, value in commands.items()]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   630
    )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   631
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   632
    parser = optparse.OptionParser(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   633
        """%prog COMMAND [file ...]
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   634
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   635
Pick up embedded code fragments from given file(s) or stdin, and list
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   636
up start/end lines of them in standard compiler format
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   637
("FILENAME:LINENO:").
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   638
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   639
Available commands are:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   640
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   641
        + availablecommands
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   642
        + """
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   643
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   644
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   645
    parser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   646
        "-v",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   647
        "--verbose",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   648
        help="enable additional output (e.g. actual code)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   649
        action="store_true",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   650
    )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   651
    (opts, args) = parser.parse_args()
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   652
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   653
    if not args or args[0] not in commands:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   654
        parser.print_help()
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   655
        sys.exit(255)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   656
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   657
    sys.exit(commands[args[0]][1](args[1:], opts))