author | Taapas Agrawal <taapas2897@gmail.com> |
Thu, 11 Jul 2019 03:08:28 +0530 | |
changeset 42604 | 209f2b8a50dc |
parent 42391 | c2deb2512823 |
child 43076 | 2372284d9457 |
permissions | -rw-r--r-- |
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 |
def bytestr(s): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
42 |
# 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
|
43 |
return s.encode('latin1') |
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 sysstr(s): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
46 |
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
|
47 |
return s |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
48 |
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
|
49 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
50 |
def opentext(f): |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
51 |
return open(f, 'r') |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
52 |
else: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
53 |
bytestr = str |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
54 |
sysstr = identity |
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 |
opentext = open |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
57 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
58 |
def b2s(x): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
59 |
# 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
|
60 |
return rapply(sysstr, x) |
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 |
def writeout(data): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
63 |
# 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
|
64 |
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
|
65 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
66 |
def writeerr(data): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
67 |
# 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
|
68 |
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
|
69 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
70 |
#################### |
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 |
class embeddedmatcher(object): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
73 |
"""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
|
74 |
""" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
75 |
__metaclass__ = abc.ABCMeta |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
76 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
77 |
def __init__(self, desc): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
78 |
self.desc = desc |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
79 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
80 |
@abc.abstractmethod |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
81 |
def startsat(self, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
82 |
"""Examine whether embedded code starts at line |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
83 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
84 |
This can return arbitrary object, and it is used as 'ctx' for |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
85 |
subsequent method invocations. |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
86 |
""" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
87 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
88 |
@abc.abstractmethod |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
89 |
def endsat(self, ctx, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
90 |
"""Examine whether embedded code ends at line""" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
91 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
92 |
@abc.abstractmethod |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
93 |
def isinside(self, ctx, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
94 |
"""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
|
95 |
""" |
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 |
@abc.abstractmethod |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
98 |
def ignores(self, ctx): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
99 |
"""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
|
100 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
101 |
@abc.abstractmethod |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
102 |
def filename(self, ctx): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
103 |
"""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
|
104 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
105 |
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
|
106 |
returns None. |
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 codeatstart(self, ctx, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
111 |
"""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
|
112 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
113 |
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
|
114 |
actual code. |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
115 |
""" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
116 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
117 |
@abc.abstractmethod |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
118 |
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
|
119 |
"""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
|
120 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
121 |
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
|
122 |
code. |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
123 |
""" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
124 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
125 |
@abc.abstractmethod |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
126 |
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
|
127 |
"""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
|
128 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
129 |
def embedded(basefile, lines, errors, matchers): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
130 |
"""pick embedded code fragments up from given lines |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
131 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
132 |
This is common parsing logic, which examines specified matchers on |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
133 |
given lines. |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
134 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
135 |
:basefile: a name of a file, from which lines to be parsed come. |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
136 |
:lines: to be parsed (might be a value returned by "open(basefile)") |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
137 |
:errors: an array, into which messages for detected error are stored |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
138 |
:matchers: an array of embeddedmatcher objects |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
139 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
140 |
This function yields '(filename, starts, ends, code)' tuple. |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
141 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
142 |
:filename: a name of embedded code, if it is explicitly specified |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
143 |
(e.g. "foobar" of "cat >> foobar <<EOF"). |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
144 |
Otherwise, this is None |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
145 |
:starts: line number (1-origin), at which embedded code starts (inclusive) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
146 |
:ends: line number (1-origin), at which embedded code ends (exclusive) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
147 |
:code: extracted embedded code, which is single-stringified |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
148 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
149 |
>>> class ambigmatcher(object): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
150 |
... # mock matcher class to examine implementation of |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
151 |
... # "ambiguous matching" corner case |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
152 |
... def __init__(self, desc, matchfunc): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
153 |
... self.desc = desc |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
154 |
... self.matchfunc = matchfunc |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
155 |
... def startsat(self, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
156 |
... return self.matchfunc(line) |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
157 |
>>> ambig1 = ambigmatcher('ambiguous #1', |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
158 |
... lambda l: l.startswith(' $ cat ')) |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
159 |
>>> ambig2 = ambigmatcher('ambiguous #2', |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
160 |
... lambda l: l.endswith('<< EOF\\n')) |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
161 |
>>> lines = [' $ cat > foo.py << EOF\\n'] |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
162 |
>>> errors = [] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
163 |
>>> matchers = [ambig1, ambig2] |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
164 |
>>> list(t for t in embedded('<dummy>', lines, errors, matchers)) |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
165 |
[] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
166 |
>>> b2s(errors) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
167 |
['<dummy>:1: ambiguous line for "ambiguous #1", "ambiguous #2"'] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
168 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
169 |
""" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
170 |
matcher = None |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
171 |
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
|
172 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
173 |
for lineno, line in enumerate(lines, 1): |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
174 |
if not line.endswith('\n'): |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
175 |
line += '\n' # to normalize EOF line |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
176 |
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
|
177 |
if matcher.endsat(ctx, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
178 |
codeatend = matcher.codeatend(ctx, line) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
179 |
if codeatend is not None: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
180 |
code.append(codeatend) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
181 |
if not matcher.ignores(ctx): |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
182 |
yield (filename, startline, lineno, ''.join(code)) |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
183 |
matcher = None |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
184 |
# DO NOT "continue", because line might start next fragment |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
185 |
elif not matcher.isinside(ctx, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
186 |
# this is an error of basefile |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
187 |
# (if matchers are implemented correctly) |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
188 |
errors.append('%s:%d: unexpected line for "%s"' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
189 |
% (basefile, lineno, matcher.desc)) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
190 |
# 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
|
191 |
# 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
|
192 |
# 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
|
193 |
# appear |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
194 |
matcher = None |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
195 |
# DO NOT "continue", because line might start next fragment |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
196 |
else: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
197 |
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
|
198 |
continue |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
199 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
200 |
# 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
|
201 |
assert not matcher |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
202 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
203 |
matched = [] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
204 |
for m in matchers: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
205 |
ctx = m.startsat(line) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
206 |
if ctx: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
207 |
matched.append((m, ctx)) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
208 |
if matched: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
209 |
if len(matched) > 1: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
210 |
# this is an error of matchers, maybe |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
211 |
errors.append('%s:%d: ambiguous line for %s' % |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
212 |
(basefile, lineno, |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
213 |
', '.join(['"%s"' % m.desc |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
214 |
for m, c in matched]))) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
215 |
# 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
|
216 |
# 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
|
217 |
# 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
|
218 |
continue |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
219 |
matcher, ctx = matched[0] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
220 |
filename = matcher.filename(ctx) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
221 |
code = [] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
222 |
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
|
223 |
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
|
224 |
code.append(codeatstart) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
225 |
startline = lineno |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
226 |
else: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
227 |
startline = lineno + 1 |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
228 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
229 |
if matcher: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
230 |
# 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
|
231 |
# 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
|
232 |
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
|
233 |
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
|
234 |
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
|
235 |
code.append(codeatend) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
236 |
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
|
237 |
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
|
238 |
else: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
239 |
# 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
|
240 |
# (if matchers are implemented correctly) |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
241 |
errors.append('%s:%d: unexpected end of file for "%s"' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
242 |
% (basefile, lineno, matcher.desc)) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
243 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
244 |
# 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
|
245 |
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
|
246 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
247 |
# 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
|
248 |
# string as 'lname' group |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
249 |
# |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
250 |
# - << LIMITMARK |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
251 |
# - << "LIMITMARK" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
252 |
# - << 'LIMITMARK' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
253 |
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
|
254 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
255 |
class fileheredocmatcher(embeddedmatcher): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
256 |
"""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
|
257 |
|
42391
c2deb2512823
testparseutil: fix doctest to use str instead of bytes
Augie Fackler <augie@google.com>
parents:
42390
diff
changeset
|
258 |
>>> 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
|
259 |
>>> 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
|
260 |
('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
|
261 |
>>> 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
|
262 |
('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
|
263 |
>>> 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
|
264 |
('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
|
265 |
>>> 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
|
266 |
('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
|
267 |
>>> 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
|
268 |
('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
|
269 |
>>> 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
|
270 |
>>> ctx = matcher.startsat(start) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
271 |
>>> matcher.codeatstart(ctx, start) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
272 |
>>> b2s(matcher.filename(ctx)) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
273 |
'file.py' |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
274 |
>>> matcher.ignores(ctx) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
275 |
False |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
276 |
>>> 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
|
277 |
>>> matcher.endsat(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
278 |
False |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
279 |
>>> matcher.isinside(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
280 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
281 |
>>> 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
|
282 |
'foo = 1\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
283 |
>>> end = ' > EOF\\n' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
284 |
>>> matcher.endsat(ctx, end) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
285 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
286 |
>>> 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
|
287 |
>>> 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
|
288 |
False |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
289 |
>>> 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
|
290 |
>>> matcher.ignores(ctx) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
291 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
292 |
""" |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
293 |
_prefix = ' > ' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
294 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
295 |
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
|
296 |
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
|
297 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
298 |
# 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
|
299 |
# 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
|
300 |
# group |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
301 |
# |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
302 |
# - > NAMEPAT |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
303 |
# - > "NAMEPAT" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
304 |
# - > 'NAMEPAT' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
305 |
namepat = (r'\s*>>?\s*(?P<nquote>["\']?)(?P<name>%s)(?P=nquote)' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
306 |
% namepat) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
307 |
self._fileres = [ |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
308 |
# "cat > NAME << LIMIT" case |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
309 |
re.compile(r' \$ \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
|
310 |
# "cat << LIMIT > NAME" case |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
311 |
re.compile(r' \$ \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
|
312 |
] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
313 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
314 |
def startsat(self, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
315 |
# 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
|
316 |
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
|
317 |
matched = filere.match(line) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
318 |
if matched: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
319 |
return (matched.group('name'), |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
320 |
' > %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
|
321 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
322 |
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
|
323 |
return ctx[1] == line |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
324 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
325 |
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
|
326 |
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
|
327 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
328 |
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
|
329 |
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
|
330 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
331 |
def filename(self, ctx): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
332 |
return ctx[0] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
333 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
334 |
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
|
335 |
return None # no embedded code at start line |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
336 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
337 |
def codeatend(self, ctx, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
338 |
return None # no embedded code at end line |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
339 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
340 |
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
|
341 |
return line[len(self._prefix):] # strip prefix |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
342 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
343 |
#### |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
344 |
# for embedded python script |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
345 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
346 |
class pydoctestmatcher(embeddedmatcher): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
347 |
"""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
|
348 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
349 |
>>> matcher = pydoctestmatcher() |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
350 |
>>> 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
|
351 |
>>> matcher.startsat(startline) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
352 |
True |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
353 |
>>> 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
|
354 |
False |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
355 |
>>> ctx = matcher.startsat(startline) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
356 |
>>> matcher.filename(ctx) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
357 |
>>> matcher.ignores(ctx) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
358 |
False |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
359 |
>>> 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
|
360 |
'foo = 1\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
361 |
>>> 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
|
362 |
>>> matcher.endsat(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
363 |
False |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
364 |
>>> matcher.isinside(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
365 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
366 |
>>> 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
|
367 |
'foo = 1\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
368 |
>>> 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
|
369 |
>>> matcher.endsat(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
370 |
False |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
371 |
>>> matcher.isinside(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
372 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
373 |
>>> 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
|
374 |
'foo = 1\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
375 |
>>> 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
|
376 |
>>> matcher.endsat(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
377 |
False |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
378 |
>>> matcher.isinside(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
379 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
380 |
>>> 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
|
381 |
'\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
382 |
>>> inside = ' \\n' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
383 |
>>> matcher.endsat(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
384 |
False |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
385 |
>>> matcher.isinside(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
386 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
387 |
>>> 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
|
388 |
'\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
389 |
>>> 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
|
390 |
>>> matcher.endsat(ctx, end) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
391 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
392 |
>>> 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
|
393 |
>>> end = '\\n' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
394 |
>>> matcher.endsat(ctx, end) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
395 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
396 |
>>> matcher.codeatend(ctx, end) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
397 |
""" |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
398 |
_prefix = ' >>> ' |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
399 |
_prefixre = re.compile(r' (>>>|\.\.\.) ') |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
400 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
401 |
# 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
|
402 |
# 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
|
403 |
# |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
404 |
# 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
|
405 |
# 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
|
406 |
# 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
|
407 |
# 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
|
408 |
# regexp does not matche against such directive lines. |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
409 |
_outputre = re.compile(r' $| [^$]') |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
410 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
411 |
def __init__(self): |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
412 |
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
|
413 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
414 |
def startsat(self, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
415 |
# ctx is "True" |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
416 |
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
|
417 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
418 |
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
|
419 |
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
|
420 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
421 |
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
|
422 |
return True # always true, if not yet ended |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
423 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
424 |
def ignores(self, ctx): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
425 |
return False # should be checked always |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
426 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
427 |
def filename(self, ctx): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
428 |
return None # no filename |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
429 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
430 |
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
|
431 |
return line[len(self._prefix):] # strip prefix ' >>> '/' ... ' |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
432 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
433 |
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
|
434 |
return None # no embedded code at end line |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
435 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
436 |
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
|
437 |
if self._prefixre.match(line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
438 |
return line[len(self._prefix):] # strip prefix ' >>> '/' ... ' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
439 |
return '\n' # an expected output line is treated as an empty line |
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 |
class pyheredocmatcher(embeddedmatcher): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
442 |
"""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
|
443 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
444 |
>>> matcher = pyheredocmatcher() |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
445 |
>>> 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
|
446 |
' > EOF\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
447 |
>>> 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
|
448 |
' > EOF\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
449 |
>>> 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
|
450 |
' > EOF\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
451 |
>>> 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
|
452 |
' > ANYLIMIT\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
453 |
>>> 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
|
454 |
>>> 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
|
455 |
>>> ctx = matcher.startsat(start) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
456 |
>>> matcher.codeatstart(ctx, start) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
457 |
>>> matcher.filename(ctx) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
458 |
>>> matcher.ignores(ctx) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
459 |
False |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
460 |
>>> 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
|
461 |
>>> matcher.endsat(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
462 |
False |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
463 |
>>> matcher.isinside(ctx, inside) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
464 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
465 |
>>> 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
|
466 |
'foo = 1\\n' |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
467 |
>>> end = ' > EOF\\n' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
468 |
>>> matcher.endsat(ctx, end) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
469 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
470 |
>>> 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
|
471 |
>>> 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
|
472 |
False |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
473 |
>>> 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
|
474 |
>>> matcher.ignores(ctx) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
475 |
True |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
476 |
""" |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
477 |
_prefix = ' > ' |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
478 |
|
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
479 |
_startre = re.compile(r' \$ (\$PYTHON|"\$PYTHON"|python).*' + |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
480 |
heredoclimitpat) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
481 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
482 |
def __init__(self): |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
483 |
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
|
484 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
485 |
def startsat(self, line): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
486 |
# 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
|
487 |
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
|
488 |
if matched: |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
489 |
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
|
490 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
491 |
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
|
492 |
return ctx == line |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
493 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
494 |
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
|
495 |
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
|
496 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
497 |
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
|
498 |
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
|
499 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
500 |
def filename(self, ctx): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
501 |
return None # no filename |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
502 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
503 |
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
|
504 |
return None # no embedded code at start line |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
505 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
506 |
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
|
507 |
return None # no embedded code at end line |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
508 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
509 |
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
|
510 |
return line[len(self._prefix):] # strip prefix |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
511 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
512 |
_pymatchers = [ |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
513 |
pydoctestmatcher(), |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
514 |
pyheredocmatcher(), |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
515 |
# 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
|
516 |
# paths including whitespaces |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
517 |
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
|
518 |
] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
519 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
520 |
def pyembedded(basefile, lines, errors): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
521 |
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
|
522 |
|
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 |
# for embedded shell script |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
525 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
526 |
_shmatchers = [ |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
527 |
# 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
|
528 |
# paths including whitespaces |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
529 |
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
|
530 |
] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
531 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
532 |
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
|
533 |
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
|
534 |
|
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 |
# for embedded hgrc configuration |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
537 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
538 |
_hgrcmatchers = [ |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
539 |
# 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
|
540 |
# paths including whitespaces |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
541 |
fileheredocmatcher('heredoc hgrc file', |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
542 |
r'(([^/<]+/)+hgrc|\$HGRCPATH|\${HGRCPATH})'), |
40093
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
543 |
] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
544 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
545 |
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
|
546 |
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
|
547 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
548 |
#### |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
549 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
550 |
if __name__ == "__main__": |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
551 |
import optparse |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
552 |
import sys |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
553 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
554 |
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
|
555 |
errors = [] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
556 |
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
|
557 |
if not name: |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
558 |
name = '<anonymous>' |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
559 |
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
|
560 |
if opts.verbose and code: |
42330
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
561 |
writeout(" |%s\n" % |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
562 |
"\n |".join(l for l in code.splitlines())) |
5364ba1f796f
py3: make contrib/testparseutil.py to work on str(unicodes)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41552
diff
changeset
|
563 |
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
|
564 |
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
|
565 |
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
|
566 |
return len(errors) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
567 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
568 |
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
|
569 |
ret = 0 |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
570 |
if args: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
571 |
for f in args: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
572 |
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
|
573 |
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
|
574 |
ret = 1 |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
575 |
else: |
42390
37f38e1dea44
testparseutil: stop extracting using std* streams as bytes on py3
Augie Fackler <augie@google.com>
parents:
42330
diff
changeset
|
576 |
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
|
577 |
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
|
578 |
ret = 1 |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
579 |
return ret |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
580 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
581 |
commands = {} |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
582 |
def command(name, desc): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
583 |
def wrap(func): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
584 |
commands[name] = (desc, func) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
585 |
return wrap |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
586 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
587 |
@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
|
588 |
def pyembeddedcmd(args, opts): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
589 |
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
|
590 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
591 |
@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
|
592 |
def shembeddedcmd(args, opts): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
593 |
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
|
594 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
595 |
@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
|
596 |
def hgrcembeddedcmd(args, opts): |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
597 |
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
|
598 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
599 |
availablecommands = "\n".join([" - %s: %s" % (key, value[0]) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
600 |
for key, value in commands.items()]) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
601 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
602 |
parser = optparse.OptionParser("""%prog COMMAND [file ...] |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
603 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
604 |
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
|
605 |
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
|
606 |
("FILENAME:LINENO:"). |
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 |
Available commands are: |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
609 |
""" + availablecommands + """ |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
610 |
""") |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
611 |
parser.add_option("-v", "--verbose", |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
612 |
help="enable additional output (e.g. actual code)", |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
613 |
action="store_true") |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
614 |
(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
|
615 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
616 |
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
|
617 |
parser.print_help() |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
618 |
sys.exit(255) |
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
619 |
|
726cfc47f17a
contrib: add an utility module to parse test scripts
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff
changeset
|
620 |
sys.exit(commands[args[0]][1](args[1:], opts)) |