Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

sp belongs into evnet

  • Loading branch information...
commit c18564c0517ec73a38f1688a7cce5eebd11010bc 1 parent 317cfbb
Mark Schloesser authored October 11, 2012
1  py/pwrcall/__init__.py
@@ -4,4 +4,3 @@
4 4
 from .util import Referenced
5 5
 
6 6
 from .evloop import loop, unloop, schedule
7  
-from .sp import Process
178  py/pwrcall/sp.py
... ...
@@ -1,178 +0,0 @@
1  
-
2  
-from evnet import loop, unloop, pyev, default_loop, EVException, EventGen, later
3  
-
4  
-import sys
5  
-import os
6  
-import subprocess
7  
-import traceback
8  
-import fcntl
9  
-import errno
10  
-
11  
-def fdnonblock(fd):
12  
-	fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
13  
-
14  
-class Process(EventGen):
15  
-	def __init__(self, args):
16  
-		EventGen.__init__(self)
17  
-
18  
-		self.buf = bytearray()
19  
-		self._writing = False
20  
-		self._closed = False
21  
-		self.retval = None
22  
-		try:
23  
-			self.p = subprocess.Popen(args, stdin=subprocess.PIPE,
24  
-				stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
25  
-		except Exception, e:
26  
-			self._closed = True
27  
-			self.retval = 3
28  
-			self._event('close', e)
29  
-		else:
30  
-			self.cw = pyev.Child(self.p.pid, False, default_loop, self.cw_cb)
31  
-			self.orw = pyev.Io(self.p.stdout, pyev.EV_READ, default_loop, self.orw_cb, priority=pyev.EV_MINPRI)
32  
-			self.erw = pyev.Io(self.p.stderr, pyev.EV_READ, default_loop, self.erw_cb, priority=pyev.EV_MINPRI)
33  
-			self.iww = pyev.Io(self.p.stdin, pyev.EV_WRITE, default_loop, self.iww_cb, priority=pyev.EV_MINPRI)
34  
-			fdnonblock(self.p.stdin.fileno())
35  
-			fdnonblock(self.p.stdout.fileno())
36  
-			fdnonblock(self.p.stderr.fileno())
37  
-
38  
-			self.cw.start()
39  
-			self.orw.start()
40  
-			self.erw.start()
41  
-
42  
-	def kill(self):
43  
-		try: self.p.kill()
44  
-		except OSError, e: self._close(e)
45  
-	def terminate(self):
46  
-		try: self.p.terminate()
47  
-		except OSError, e: self._close(e)
48  
-
49  
-	def write(self, data):
50  
-		if self._closed: raise EVException('Already closed.')
51  
-		if not isinstance(data, bytes): data = bytes(data)
52  
-		self.buf.extend(data)
53  
-		if not self.iww.active and not self._writing: self._writeloop()
54  
-		
55  
-	def forward(self, fd, ed, watcher):
56  
-		count = 0
57  
-		while not self._closed and count < 5:
58  
-			count += 1
59  
-			try: data = fd.read(16384)
60  
-			except IOError as e:
61  
-				if e.errno == errno.EAGAIN: return
62  
-				else:
63  
-					watcher.stop()
64  
-					self._close(EVException('Exception {0}'.format(e)))
65  
-			except Exception as e:
66  
-				watcher.stop()
67  
-				self._close(EVException('Exception {0}'.format(e)))
68  
-			else:
69  
-				if not data: watcher.stop()
70  
-				else:
71  
-					try: self._event(ed, data)
72  
-					except:	traceback.print_exc()
73  
-
74  
-	def cw_cb(self, watcher, events):
75  
-		if os.WIFSIGNALED(watcher.rstatus): self.retval = -os.WTERMSIG(watcher.rstatus)
76  
-		elif os.WIFEXITED(watcher.rstatus): self.retval = os.WEXITSTATUS(watcher.rstatus)
77  
-		self._close(EVException('Child exit.'))
78  
-		
79  
-	def orw_cb(self, watcher, events):
80  
-		self.forward(self.p.stdout, 'read', watcher)
81  
-
82  
-	def erw_cb(self, watcher, events):
83  
-		self.forward(self.p.stderr, 'readerr', watcher)
84  
-
85  
-	def iww_cb(self, watcher, events):
86  
-		if self.iww.active: self.iww.stop()
87  
-
88  
-		try: self._writeloop()
89  
-		except:
90  
-			traceback.print_exc()
91  
-			self._close(EVException('DEBUGWRAP'))
92  
-
93  
-	def _writeloop(self):
94  
-		self._writing = True
95  
-		count = 0
96  
-		while not self._closed and len(self.buf)>0 and count < 5:
97  
-			count += 1
98  
-			try:
99  
-				self.p.stdin.write(self.buf[:16384])
100  
-			except IOError as e:
101  
-				if e.errno == errno.EAGAIN:
102  
-					self.iww.start()
103  
-					return
104  
-				else: self._close(EVException('Exception {0}'.format(e)))
105  
-			except Exception as e:
106  
-				self._close(EVException('Exception {0}'.format(e)))
107  
-			else:
108  
-				del self.buf[:16384]
109  
-				if len(self.buf) < 16384*2:
110  
-					self._event('writable')
111  
-
112  
-		if len(self.buf)>0 and not self.iww.active:
113  
-			self.iww.start()
114  
-
115  
-		self._writing = False
116  
-
117  
-	def _close(self, e):
118  
-		if self.orw.active:
119  
-			self.orw.invoke(pyev.EV_READ)
120  
-			self.orw.stop()
121  
-		if self.erw.active:
122  
-			self.erw.invoke(pyev.EV_READ)
123  
-			self.erw.stop()
124  
-		self.p.stdout.close()
125  
-		self.p.stderr.close()
126  
-		self.p.stdin.close()
127  
-		self._closed = True
128  
-		self._event('close', e)
129  
-
130  
-if __name__ == '__main__':
131  
-	def incoming(data):
132  
-		print 'stdout:', data
133  
-	def incominge(data):
134  
-		print 'stderr:', data
135  
-
136  
-	def writeto(p):
137  
-		p.write('test test test\n')
138  
-		p.write('test test test\n')
139  
-
140  
-	def killp(p):
141  
-		print 'sending TERM'
142  
-		p.p.terminate()
143  
-	def killp2(p):
144  
-		print 'sending KILL'
145  
-		p.p.kill()
146  
-
147  
-	def poll(p):
148  
-		print 'poll', p.p.poll(), p.p.returncode
149  
-		later(1.0, poll, p)
150  
-
151  
-	def closed(e):
152  
-		print 'subprocess closed', e
153  
-		#unloop()
154  
-
155  
-	def end():
156  
-		print 'end.'
157  
-		unloop()
158  
-
159  
-	def status(p):
160  
-		print 'status', p
161  
-
162  
-	p = Process(['/bin/nc', '-vvn', '127.0.0.1', '50000'])
163  
-	p._on('read', incoming)
164  
-	p._on('readerr', incominge)
165  
-	p._on('close', closed)
166  
-	p.write('abcdef\n')
167  
-	#later(5.0, status, p)
168  
-	later(1.0, poll, p)
169  
-
170  
-	#later(5.0, writeto, p)
171  
-	#later(5.0, killp, p)
172  
-	#later(7.0, killp, p)
173  
-	later(15.0, end)
174  
-	loop()
175  
-	print 'sys.exit'
176  
-	sys.exit(0)
177  
-
178  
-

0 notes on commit c18564c

Please sign in to comment.
Something went wrong with that request. Please try again.