-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
129 lines (103 loc) · 3.63 KB
/
script.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
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
# 1 reverse-engineer the protocol
# SO Post: https://stackoverflow.com/questions/6954116/ruby-s-method-missing-in-python
# A missing method was called.
# The object was <__main__.ProcessLike object at 0x7f31b98a2be0>, the method was 'popen'.
# It was called with () and {'stdin': <_io.BufferedReader name=3>} as arguments
import tempfile
from tempfile import NamedTemporaryFile
from plumbum import local
from io import StringIO
import io
import os
from collections import namedtuple
# ProcessReturn = namedtuple('ProcessReturn', ['code', 'stderr', 'stdout'])
# first tried namedtuple, but the problem is plumbum
# tries to assign these values crosswise based on the pipeline, and namedtuples
# are immutable, so you run into this:
# dstproc.stderr = srcproc.stderr
# AttributeError: can't set attribute
# Went to dummy ProcessReturn class to try to patch in the gaps
# class ProcessReturn(object):
# code = 0
# retcode = 0
# returncode = 0
# def wait(self, *args, **kwargs):
# pass
# def verify(self, *args, **kwargs):
# pass
# def communicate(self, *args, **kwargs):
# return (self.stdout, self.stderr)
class ProcessLike(object):
returncode = 0
# stdin = io.BufferedReader(StringIO())
# stderr = io.BufferedWriter(StringIO())
# stdout = io.BufferedWriter(StringIO())
stdin = io.BytesIO()
stderr = io.BytesIO()
stdout = io.BytesIO()
custom_encoding = None
run = None
wait = lambda *args, **kwargs: print('*** DEFAULT WAIT ***')
srcproc = None
verify = None
def __init__(self):
self.stdin = tempfile.TemporaryFile()
print('Init stdin:' + str(self.stdin))
self.stdout = tempfile.TemporaryFile()
self.stderr = tempfile.TemporaryFile()
def __setattr__(self, name, value):
if name in ['stdin', 'stderr', 'stdout', 'custom_encoding', 'run', 'wait', 'srcproc', 'verify']:
super(ProcessLike, self).__setattr__(name, value)
else:
print('*** Attempted to set unknown attr ***')
print('Attr: ' + name + ', value: ' + str(value))
def __enter__(self, *args, **kwargs):
print('*** __enter__ ***')
print(args)
print(kwargs)
def __getattr__(self, name):
def _missing(*args, **kwargs):
print("A missing method was called.")
print("The object was %r, the method was %r. " % (self, name))
print("It was called with %r and %r as arguments" % (args, kwargs))
return _missing
def communicate(self, *args, **kwargs):
print("tried to communicate with us")
print(str(args))
print(str(kwargs))
return (self.stdout, self.stderr)
def verify(self, retcode, timeout, stdout, stderr):
return True
def __del__(self, *args, **kwargs):
pass
# def wait(self, *args, **kwargs):
# print('wait' + str(args) + str(kwargs))
# print(self.srcproc)
# print(self.wait)
# # returns returncode
# print(self.stdin)
# # for line in self.stdin:
# # print(line)
# # print(' '.join(list(line.decode())).rstrip())
# # print(self.stdout)
# # self.stdout.write(' '.join(list(line.decode())).encode())
# return 0
def popen(self, *args, **kwargs):
print('*** POPEN ProcessLike')
print(str(args))
print(str(kwargs))
return self
def __or__(self, *args, **kwargs):
print('ORed ' + str(args) + str(kwargs))
class MyTask(object):
def __enter__(self):
self.tempfile = NamedTemporaryFile()
return self
def fileno(self):
return self.tempfile.fileno
# def __or__(self, other):
# def __exit__(self, exc_type, exc_value, traceback):
# self.tempfile.close()
cmd = local['cat']['/usr/share/dict/words'] | local['head']['-n 5'] | ProcessLike() | local['cat']
code, out, err = cmd.run()
print(out)