contrib/testparseutil.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Mon, 25 Jan 2021 23:13:01 +0100
changeset 46387 c41ac8985fe4
parent 45942 89a2afe31e82
child 48875 6000f5b25c9b
permissions -rw-r--r--
perf: test the formatting of a command help Since we are about to addd ':' in command name I want this covered. Differential Revision: https://phab.mercurial-scm.org/D9871
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
from __future__ import absolute_import, print_function
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     9
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    10
import abc
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
import sys
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
####################
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    15
# 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
    16
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    17
ispy3 = sys.version_info[0] >= 3
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    18
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    19
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    20
def identity(a):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    21
    return a
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    22
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    23
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    24
def _rapply(f, xs):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    25
    if xs is None:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    26
        # 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
    27
        return xs
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    28
    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
    29
        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
    30
    if isinstance(xs, dict):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    31
        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
    32
    return f(xs)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    33
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    34
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    35
def rapply(f, xs):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    36
    if f is identity:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    37
        # 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
    38
        return xs
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    39
    return _rapply(f, xs)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    40
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    41
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    42
if ispy3:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    43
    import builtins
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    44
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    45
    def bytestr(s):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    46
        # tiny version of pycompat.bytestr
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    47
        return s.encode('latin1')
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    48
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    49
    def sysstr(s):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    50
        if isinstance(s, builtins.str):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    51
            return s
43091
127cc1f72e70 py3: stop normalizing .encode()/.decode() arguments to unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    52
        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
    53
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    54
    def opentext(f):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
    55
        return open(f, 'r')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    56
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    57
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    58
else:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    59
    bytestr = str
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    60
    sysstr = identity
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    61
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    62
    opentext = open
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    63
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    64
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    65
def b2s(x):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    66
    # 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
    67
    return rapply(sysstr, x)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    68
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    69
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    70
def writeout(data):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    71
    # 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
    72
    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
    73
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
def writeerr(data):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    76
    # 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
    77
    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
    78
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    79
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    80
####################
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    81
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
    82
43474
70d42e2ad9b4 pytype: don't warn us about ignored-on-py3 metaclasses
Augie Fackler <augie@google.com>
parents: 43091
diff changeset
    83
class embeddedmatcher(object):  # pytype: disable=ignored-metaclass
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44020
diff changeset
    84
    """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
    85
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    86
    __metaclass__ = abc.ABCMeta
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
    def __init__(self, desc):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    89
        self.desc = desc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    90
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    91
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    92
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    93
        """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
    94
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    95
        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
    96
        subsequent method invocations.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    97
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    98
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    99
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   100
    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
   101
        """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
   102
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   103
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   104
    def isinside(self, ctx, line):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44020
diff changeset
   105
        """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
   106
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   107
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   108
    def ignores(self, ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   109
        """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
   110
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   111
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   112
    def filename(self, ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   113
        """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
   114
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   115
        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
   116
        returns None.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   117
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   118
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   119
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   120
    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
   121
        """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
   122
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   123
        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
   124
        actual code.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   125
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   126
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   127
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   128
    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
   129
        """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
   130
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   131
        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
   132
        code.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   133
        """
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
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   136
    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
   137
        """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
   138
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   139
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   140
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
   141
    """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
   142
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   143
    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
   144
    given lines.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   145
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   146
    :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
   147
    :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
   148
    :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
   149
    :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
   150
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   151
    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
   152
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   153
    :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
   154
               (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
   155
               Otherwise, this is None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   156
    :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
   157
    :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
   158
    :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
   159
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   160
    >>> class ambigmatcher(object):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   161
    ...     # 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
   162
    ...     # "ambiguous matching" corner case
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   163
    ...     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
   164
    ...         self.desc = desc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   165
    ...         self.matchfunc = matchfunc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   166
    ...     def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   167
    ...         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
   168
    >>> ambig1 = ambigmatcher('ambiguous #1',
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   169
    ...                       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
   170
    >>> ambig2 = ambigmatcher('ambiguous #2',
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   171
    ...                       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
   172
    >>> 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
   173
    >>> errors = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   174
    >>> matchers = [ambig1, ambig2]
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   175
    >>> 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
   176
    []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   177
    >>> b2s(errors)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   178
    ['<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
   179
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   180
    """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   181
    matcher = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   182
    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
   183
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   184
    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
   185
        if not line.endswith('\n'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   186
            line += '\n'  # to normalize EOF line
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   187
        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
   188
            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
   189
                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
   190
                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
   191
                    code.append(codeatend)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   192
                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
   193
                    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
   194
                matcher = None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   195
                # 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
   196
            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
   197
                # 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
   198
                # (if matchers are implemented correctly)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   199
                errors.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   200
                    '%s:%d: unexpected line for "%s"'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   201
                    % (basefile, lineno, matcher.desc)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   202
                )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   203
                # 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
   204
                # 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
   205
                # 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
   206
                # appear
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   207
                matcher = None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   208
                # 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
   209
            else:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   210
                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
   211
                continue
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   212
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   213
        # 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
   214
        assert not matcher
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   215
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   216
        matched = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   217
        for m in matchers:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   218
            ctx = m.startsat(line)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   219
            if ctx:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   220
                matched.append((m, ctx))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   221
        if matched:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   222
            if len(matched) > 1:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   223
                # this is an error of matchers, maybe
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   224
                errors.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   225
                    '%s:%d: ambiguous line for %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   226
                    % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   227
                        basefile,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   228
                        lineno,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   229
                        ', '.join(['"%s"' % m.desc for m, c in matched]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   230
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   231
                )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   232
                # 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
   233
                # 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
   234
                # 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
   235
                continue
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   236
            matcher, ctx = matched[0]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   237
            filename = matcher.filename(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   238
            code = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   239
            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
   240
            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
   241
                code.append(codeatstart)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   242
                startline = lineno
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   243
            else:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   244
                startline = lineno + 1
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   245
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   246
    if matcher:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   247
        # 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
   248
        # 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
   249
        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
   250
            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
   251
            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
   252
                code.append(codeatend)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   253
            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
   254
                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
   255
        else:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   256
            # 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
   257
            # (if matchers are implemented correctly)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   258
            errors.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   259
                '%s:%d: unexpected end of file for "%s"'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   260
                % (basefile, lineno, matcher.desc)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   261
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   262
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   263
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   264
# 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
   265
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
   266
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   267
# 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
   268
# string as 'lname' group
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   269
#
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   270
# - << LIMITMARK
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   271
# - << "LIMITMARK"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   272
# - << 'LIMITMARK'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   273
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
   274
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   275
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   276
class fileheredocmatcher(embeddedmatcher):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   277
    """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
   278
42391
c2deb2512823 testparseutil: fix doctest to use str instead of bytes
Augie Fackler <augie@google.com>
parents: 42390
diff changeset
   279
    >>> 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
   280
    >>> 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
   281
    ('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
   282
    >>> 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
   283
    ('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
   284
    >>> 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
   285
    ('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
   286
    >>> 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
   287
    ('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
   288
    >>> 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
   289
    ('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
   290
    >>> 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
   291
    >>> ctx = matcher.startsat(start)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   292
    >>> matcher.codeatstart(ctx, start)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   293
    >>> b2s(matcher.filename(ctx))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   294
    'file.py'
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   295
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   296
    False
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   297
    >>> 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
   298
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   299
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   300
    >>> matcher.isinside(ctx, inside)
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
    >>> 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
   303
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   304
    >>> end = '  > EOF\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   305
    >>> matcher.endsat(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   306
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   307
    >>> 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
   308
    >>> 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
   309
    False
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   310
    >>> 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
   311
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   312
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   313
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   314
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   315
    _prefix = '  > '
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   316
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   317
    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
   318
        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
   319
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   320
        # 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
   321
        # 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
   322
        # group
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   323
        #
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   324
        # - > NAMEPAT
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   325
        # - > "NAMEPAT"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   326
        # - > 'NAMEPAT'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   327
        namepat = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   328
            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
   329
        )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   330
        self._fileres = [
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   331
            # "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
   332
            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
   333
            # "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
   334
            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
   335
        ]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   336
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   337
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   338
        # 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
   339
        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
   340
            matched = filere.match(line)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   341
            if matched:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   342
                return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   343
                    matched.group('name'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   344
                    '  > %s\n' % matched.group('limit'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   345
                )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   346
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   347
    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
   348
        return ctx[1] == line
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   349
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   350
    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
   351
        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
   352
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   353
    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
   354
        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
   355
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   356
    def filename(self, ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   357
        return ctx[0]
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
    def codeatstart(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   360
        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
   361
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   362
    def codeatend(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   363
        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
   364
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   365
    def codeinside(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   366
        return line[len(self._prefix) :]  # strip prefix
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   367
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   368
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   369
####
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   370
# for embedded python script
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   371
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   372
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   373
class pydoctestmatcher(embeddedmatcher):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   374
    """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
   375
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   376
    >>> matcher = pydoctestmatcher()
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   377
    >>> 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
   378
    >>> matcher.startsat(startline)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   379
    True
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   380
    >>> 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
   381
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   382
    >>> ctx = matcher.startsat(startline)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   383
    >>> matcher.filename(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   384
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   385
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   386
    >>> 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
   387
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   388
    >>> 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
   389
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   390
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   391
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   392
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   393
    >>> 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
   394
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   395
    >>> 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
   396
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   397
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   398
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   399
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   400
    >>> 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
   401
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   402
    >>> 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
   403
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   404
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   405
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   406
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   407
    >>> 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
   408
    '\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   409
    >>> inside = '  \\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, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   411
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   412
    >>> matcher.isinside(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   413
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   414
    >>> 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
   415
    '\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   416
    >>> 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
   417
    >>> matcher.endsat(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   418
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   419
    >>> 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
   420
    >>> end = '\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   421
    >>> matcher.endsat(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   422
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   423
    >>> matcher.codeatend(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   424
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   425
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   426
    _prefix = '  >>> '
44020
ac3cb5e05a38 cleanup: replace contiguous spaces in regex patterns with an explicit count
Matt Harbison <matt_harbison@yahoo.com>
parents: 43474
diff changeset
   427
    _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
   428
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   429
    # 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
   430
    # 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
   431
    #
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   432
    # 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
   433
    # 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
   434
    # 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
   435
    # 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
   436
    # 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
   437
    _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
   438
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   439
    def __init__(self):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   440
        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
   441
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   442
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   443
        # ctx is "True"
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   444
        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
   445
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   446
    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
   447
        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
   448
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   449
    def isinside(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   450
        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
   451
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   452
    def ignores(self, ctx):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   453
        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
   454
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   455
    def filename(self, ctx):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   456
        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
   457
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   458
    def codeatstart(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   459
        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
   460
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   461
    def codeatend(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   462
        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
   463
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   464
    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
   465
        if self._prefixre.match(line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   466
            return line[len(self._prefix) :]  # strip prefix '  >>> '/'  ... '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   467
        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
   468
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   469
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   470
class pyheredocmatcher(embeddedmatcher):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   471
    """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
   472
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   473
    >>> matcher = pyheredocmatcher()
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   474
    >>> 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
   475
    '  > EOF\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   476
    >>> 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
   477
    '  > EOF\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   478
    >>> 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
   479
    '  > EOF\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   480
    >>> 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
   481
    '  > ANYLIMIT\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   482
    >>> 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
   483
    >>> 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
   484
    >>> ctx = matcher.startsat(start)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   485
    >>> matcher.codeatstart(ctx, start)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   486
    >>> matcher.filename(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   487
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   488
    False
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   489
    >>> 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
   490
    >>> matcher.endsat(ctx, inside)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   491
    False
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   492
    >>> matcher.isinside(ctx, inside)
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
    >>> 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
   495
    'foo = 1\\n'
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   496
    >>> end = '  > EOF\\n'
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   497
    >>> matcher.endsat(ctx, end)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   498
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   499
    >>> 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
   500
    >>> 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
   501
    False
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   502
    >>> 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
   503
    >>> matcher.ignores(ctx)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   504
    True
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   505
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   506
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   507
    _prefix = '  > '
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   508
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   509
    _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
   510
        r' {2}\$ (\$PYTHON|"\$PYTHON"|python).*' + heredoclimitpat
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   511
    )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   512
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   513
    def __init__(self):
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   514
        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
   515
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   516
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   517
        # 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
   518
        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
   519
        if matched:
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   520
            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
   521
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   522
    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
   523
        return ctx == line
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   524
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   525
    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
   526
        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
   527
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   528
    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
   529
        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
   530
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   531
    def filename(self, ctx):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   532
        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
   533
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   534
    def codeatstart(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   535
        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
   536
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   537
    def codeatend(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   538
        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
   539
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   540
    def codeinside(self, ctx, line):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   541
        return line[len(self._prefix) :]  # strip prefix
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   542
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   543
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   544
_pymatchers = [
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   545
    pydoctestmatcher(),
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   546
    pyheredocmatcher(),
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   547
    # 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
   548
    # paths including whitespaces
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   549
    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
   550
]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   551
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   552
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   553
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
   554
    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
   555
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   556
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   557
####
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   558
# for embedded shell script
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   559
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   560
_shmatchers = [
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   561
    # 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
   562
    # paths including whitespaces
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   563
    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
   564
]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   565
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   566
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   567
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
   568
    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
   569
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   570
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   571
####
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   572
# for embedded hgrc configuration
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   573
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   574
_hgrcmatchers = [
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   575
    # 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
   576
    # paths including whitespaces
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   577
    fileheredocmatcher(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   578
        'heredoc hgrc file', r'(([^/<]+/)+hgrc|\$HGRCPATH|\${HGRCPATH})'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   579
    ),
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   580
]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   581
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   582
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   583
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
   584
    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
   585
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   586
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   587
####
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   588
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   589
if __name__ == "__main__":
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   590
    import optparse
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   591
    import sys
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   592
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   593
    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
   594
        errors = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   595
        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
   596
            if not name:
42330
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   597
                name = '<anonymous>'
5364ba1f796f py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 41552
diff changeset
   598
            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
   599
            if opts.verbose and code:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   600
                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
   601
            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
   602
        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
   603
            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
   604
        return len(errors)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   605
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   606
    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
   607
        ret = 0
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   608
        if args:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   609
            for f in args:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   610
                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
   611
                    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
   612
                        ret = 1
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   613
        else:
42390
37f38e1dea44 testparseutil: stop extracting using std* streams as bytes on py3
Augie Fackler <augie@google.com>
parents: 42330
diff changeset
   614
            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
   615
            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
   616
                ret = 1
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   617
        return ret
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   618
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   619
    commands = {}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   620
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   621
    def command(name, desc):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   622
        def wrap(func):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   623
            commands[name] = (desc, func)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   624
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   625
        return wrap
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   626
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   627
    @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
   628
    def pyembeddedcmd(args, opts):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   629
        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
   630
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   631
    @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
   632
    def shembeddedcmd(args, opts):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   633
        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
   634
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   635
    @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
   636
    def hgrcembeddedcmd(args, opts):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   637
        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
   638
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   639
    availablecommands = "\n".join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   640
        ["  - %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
   641
    )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   642
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   643
    parser = optparse.OptionParser(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   644
        """%prog COMMAND [file ...]
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   645
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   646
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
   647
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
   648
("FILENAME:LINENO:").
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   649
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   650
Available commands are:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   651
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   652
        + availablecommands
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   653
        + """
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   654
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   655
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   656
    parser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   657
        "-v",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   658
        "--verbose",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   659
        help="enable additional output (e.g. actual code)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   660
        action="store_true",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42391
diff changeset
   661
    )
40093
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   662
    (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
   663
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   664
    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
   665
        parser.print_help()
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   666
        sys.exit(255)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   667
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   668
    sys.exit(commands[args[0]][1](args[1:], opts))