-
Notifications
You must be signed in to change notification settings - Fork 1
/
ops.py
66 lines (62 loc) · 2.07 KB
/
ops.py
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
from subprocess import Popen, PIPE
import tempfile
from scooter_types import *
from parser import *
import os
def update(e, n):
if (n.op.type == 'setfile' and e.op.type == 'getfile'
and e.op.args[0] not in n.op.args):
return Edge(n.output, e.op, e.output)
return None
def popen(inputs, op):
args = op.args
d = tempfile.mkdtemp(prefix='scooter')
dirvalue = False
if args[1:] and args[1] == '$@D':
dirvalue = True
output = os.path.join(d, 'output')
os.mkdir(output)
args = args[0:1] + args[2:]
for (filename, contents) in zip(args[1:], inputs[1:]):
f = open(d + '/' + filename, 'w')
f.write(contents)
f.close()
o = Popen(args[0], cwd=d, shell=True, stdin=PIPE,
stdout=PIPE).communicate(inputs[0])[0]
if dirvalue:
result, fs = [], []
for triple in os.walk(output):
d, _, filenames = triple
for fname in filenames:
full = os.path.join(d, fname)
f = os.path.relpath(full, output)
result.append(edit(0, append(open(full).read()), f))
fs.append(f)
result.append(Stmt([0] + fs, setfile(*fs), ["_"]))
return result
else:
return [edit(0, append(o))]
def eval_op(inputs, op):
args, type = op.args, op.type
input, arg = (inputs or [None])[0], (op.args or [None])[0]
if type == 'noop':
return input()
if type == 'append':
return input() + arg
if type == 'trim':
return input()[arg:]
if type == 'limit':
return input()[:arg]
if type == 'insert':
before = input().ljust(arg)
return before[:arg] + args[1] + before[arg:]
if type == 'put':
before = input().ljust(arg)
return before[:arg] + args[1] + before[arg + len(args[1]):]
if type == 'cat':
return ''.join([i() for i in inputs])
if type == 'setfile':
return dict(zip(args, inputs[1:]) + (input() or {}).items())
if type == 'getfile':
return input()[arg]()
raise NotImplementedError