contrib/testparseutil.py
author Pulkit Goyal <pulkit@yandex-team.ru>
Mon, 25 Feb 2019 16:49:01 +0300
changeset 41797 68bbcc70e274
parent 41552 99b4c6d73a72
child 42330 5364ba1f796f
permissions -rw-r--r--
branchcache: move loading of branch names and nodes into it's own function This will help me in implementing lazy loading of the branchcache in upcoming patches. Differential Revision: https://phab.mercurial-scm.org/D6023
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
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    17
ispy3 = (sys.version_info[0] >= 3)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    18
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    19
def identity(a):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    20
    return a
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    21
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    22
def _rapply(f, xs):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    23
    if xs is None:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    24
        # 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
    25
        return xs
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    26
    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
    27
        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
    28
    if isinstance(xs, dict):
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, 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
    30
    return f(xs)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    31
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    32
def rapply(f, xs):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    33
    if f is identity:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    34
        # fast path mainly for py2
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    35
        return xs
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    36
    return _rapply(f, xs)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    37
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    38
if ispy3:
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    39
    import builtins
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    40
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    41
    # TODO: .buffer might not exist if std streams were replaced; we'll need
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    42
    # a silly wrapper to make a bytes stream backed by a unicode one.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    43
    stdin = sys.stdin.buffer
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    44
    stdout = sys.stdout.buffer
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    45
    stderr = sys.stderr.buffer
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    46
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    47
    def bytestr(s):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    48
        # 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
    49
        return s.encode('latin1')
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    50
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    51
    def sysstr(s):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    52
        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
    53
            return s
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    54
        return s.decode(u'latin-1')
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    55
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    56
    def opentext(f):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    57
        return open(f, 'rb')
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
    stdin = sys.stdin
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    60
    stdout = sys.stdout
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    61
    stderr = sys.stderr
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    62
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    63
    bytestr = str
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    64
    sysstr = identity
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    65
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    66
    opentext = open
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    67
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    68
def b2s(x):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    69
    # 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
    70
    return rapply(sysstr, x)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    71
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    72
def writeout(data):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    73
    # write "data" in BYTES into stdout
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    74
    stdout.write(data)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    75
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    76
def writeerr(data):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    77
    # write "data" in BYTES into stderr
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    78
    stderr.write(data)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    79
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    80
####################
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    81
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    82
class embeddedmatcher(object):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    83
    """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
    84
    """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    85
    __metaclass__ = abc.ABCMeta
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    86
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    87
    def __init__(self, desc):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    88
        self.desc = desc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    89
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    90
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    91
    def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    92
        """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
    93
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    94
        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
    95
        subsequent method invocations.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    96
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    97
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    98
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    99
    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
   100
        """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
   101
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   102
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   103
    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
   104
        """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
   105
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   106
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   107
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   108
    def ignores(self, ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   109
        """Examine whether detected embedded code should be ignored"""
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   110
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   111
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   112
    def filename(self, ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   113
        """Return filename of embedded code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   114
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   115
        If filename isn't specified for embedded code explicitly, this
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   116
        returns None.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   117
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   118
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   119
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   120
    def codeatstart(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   121
        """Return actual code at the start line of embedded code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   122
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   123
        This might return None, if the start line doesn't contain
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   124
        actual code.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   125
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   126
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   127
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   128
    def codeatend(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   129
        """Return actual code at the end line of embedded code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   130
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   131
        This might return None, if the end line doesn't contain actual
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   132
        code.
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   133
        """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   134
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   135
    @abc.abstractmethod
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   136
    def codeinside(self, ctx, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   137
        """Return actual code at line inside embedded code"""
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   138
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   139
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
   140
    """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
   141
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   142
    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
   143
    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
    :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
   146
    :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
   147
    :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
   148
    :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
   149
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   150
    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
   151
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   152
    :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
   153
               (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
   154
               Otherwise, this is None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   155
    :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
   156
    :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
   157
    :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
   158
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   159
    >>> class ambigmatcher(object):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   160
    ...     # 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
   161
    ...     # "ambiguous matching" corner case
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   162
    ...     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
   163
    ...         self.desc = desc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   164
    ...         self.matchfunc = matchfunc
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   165
    ...     def startsat(self, line):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   166
    ...         return self.matchfunc(line)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   167
    >>> ambig1 = ambigmatcher(b'ambiguous #1',
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   168
    ...                       lambda l: l.startswith(b'  $ cat '))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   169
    >>> ambig2 = ambigmatcher(b'ambiguous #2',
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   170
    ...                       lambda l: l.endswith(b'<< EOF\\n'))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   171
    >>> lines = [b'  $ cat > foo.py << EOF\\n']
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   172
    >>> errors = []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   173
    >>> matchers = [ambig1, ambig2]
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   174
    >>> list(t for t in embedded(b'<dummy>', lines, errors, matchers))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   175
    []
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   176
    >>> b2s(errors)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   177
    ['<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
   178
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   179
    """
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   180
    matcher = None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   181
    ctx = filename = code = startline = None # for pyflakes
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
    for lineno, line in enumerate(lines, 1):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   184
        if not line.endswith(b'\n'):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   185
            line += b'\n' # to normalize EOF line
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   186
        if matcher: # now, inside embedded code
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   187
            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
   188
                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
   189
                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
   190
                    code.append(codeatend)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   191
                if not matcher.ignores(ctx):
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   192
                    yield (filename, startline, lineno, b''.join(code))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   193
                matcher = None
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   194
                # 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
   195
            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
   196
                # 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
   197
                # (if matchers are implemented correctly)
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   198
                errors.append(b'%s:%d: unexpected line for "%s"'
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   199
                              % (basefile, lineno, matcher.desc))
726cfc47f17a contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   200
                # 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
   201
                # 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
   202
                # 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