Repository navigation
Expand file tree
/
Copy pathworkset_output.py
More file actions
137 lines (128 loc) · 4.59 KB
/
Copy pathworkset_output.py
File metadata and controls
137 lines (128 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""A bounded readonly output wrapper; never a shell or a subprocess executor."""
from dataclasses import dataclass
import shlex
@dataclass(frozen=True)
class WorksetOutputRequest:
argv: tuple[str, ...]
head_lines: int | None = None
merge_stderr: bool = False
def _tokens(command):
"""Keep operator provenance so quoted query text is never a pipeline."""
if not isinstance(command, str) or len(command) > 16384:
raise ValueError("ic-workset command exceeds its bounded input")
result = []
start = None
quote = None
i = 0
def word(end):
nonlocal start
if start is not None:
values = shlex.split(command[start:end], posix=True)
if len(values) != 1:
raise ValueError("ic-workset requires one quoted argument per word")
result.append((values[0], False, command[start:end], start, end))
start = None
while i < len(command):
char = command[i]
if quote is not None:
if quote == '"' and char in ("$", "`"):
raise ValueError("ic-workset does not expand shell expressions")
if char == quote:
quote = None
elif char == "\\" and quote == '"':
i += 1
if i >= len(command):
raise ValueError("ic-workset has an unfinished escape")
i += 1
continue
if char in "\r\n":
raise ValueError("ic-workset requires one command line")
if char.isspace():
word(i)
i += 1
continue
if char in ("$", "`"):
raise ValueError("ic-workset does not expand shell expressions")
if char in ("'", '"'):
if start is None:
start = i
quote = char
i += 1
continue
if char == "\\":
if start is None:
start = i
i += 2
if i > len(command):
raise ValueError("ic-workset has an unfinished escape")
continue
if char in "|&;<>":
word(i)
end = i + 1
while end < len(command) and command[end] in "|&;<>":
end += 1
result.append((command[i:end], True, command[i:end], i, end))
i = end
continue
if start is None:
start = i
i += 1
if quote is not None:
raise ValueError("ic-workset has an unfinished quote")
word(len(command))
return result
def parse_workset_output(command):
tokens = _tokens(command)
pipes = [i for i, (value, operator, *_) in enumerate(tokens) if operator and value == "|"]
if len(pipes) > 1:
raise ValueError("ic-workset supports only one readonly head output filter")
head = None
if pipes:
at = pipes[0]
suffix = tokens[at + 1 :]
tokens = tokens[:at]
if any(operator for _, operator, *_ in suffix):
raise ValueError("ic-workset output filter cannot contain shell operators")
args = [value for value, *_ in suffix]
if args == ["head"]:
head = 10
elif (
len(args) == 3
and args[:2] == ["head", "-n"]
and args[2].isascii()
and args[2].isdigit()
):
head = int(args[2])
elif len(args) == 2 and args[0] == "head":
value = (
args[1][2:]
if args[1].startswith("-n")
else args[1][1:]
if args[1].startswith("-")
else ""
)
if value.isascii() and value.isdigit():
head = int(value)
if head is None or not 1 <= head <= 1000:
raise ValueError(
"ic-workset supports head with1..1000 lines only;no files or byte filters"
)
merged = False
redirect = tokens[-3:]
if (
len(redirect) == 3
and [token[:3] for token in redirect]
== [("2", False, "2"), (">&", True, ">&"), ("1", False, "1")]
and redirect[0][4] == redirect[1][3]
and redirect[1][4] == redirect[2][3]
):
tokens = tokens[:-3]
merged = True
if any(operator for _, operator, *_ in tokens):
raise ValueError(
"ic-workset supports a single query,optional2>&1 and head;no other shell operations"
)
args = tuple(value for value, *_ in tokens)
if not 2 <= len(args) <= 32 or args[0] != "ic-workset":
raise ValueError("one complete ic-workset SUBCOMMAND invocation required")
return WorksetOutputRequest(args, head, merged)