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