Commit 6af18e45 authored by Martín Ferrari's avatar Martín Ferrari

High level interface to subprocess

parent 75cb3572
...@@ -78,19 +78,14 @@ stats = link0.get_stats() ...@@ -78,19 +78,14 @@ stats = link0.get_stats()
# IDEA: implement Node.popen and build the others upon it. # IDEA: implement Node.popen and build the others upon it.
# IDEA: use SCM_RIGHTS to pass filedescriptors instead of using pipes/sockets # IDEA: use SCM_RIGHTS to pass filedescriptors instead of using pipes/sockets
# Run a process in background, associate its stdio to three named pipes # Run a process in background
app0 = a.start_process("ping -c 3 10.0.0.2") import subprocess
print "ping command PIPES at (%s, %s, %s)" % app0.pipes app0 = a.Popen("ping -c 3 10.0.0.2", shell = True, stdout = subprocess.PIPE)
app0.kill(15) print app0.stdout.readline()
app0.wait()
# The same, but directly as python file objects
app1 = a.start_process(["ping", "-c", "3", "10.0.0.2"])
buf = app1.stdout.read()
app1.wait()
# Run, capture output and wait() # Run, capture output and wait()
(stdout, stderr) = a.run_process(["ping", "-c", "3", "10.0.0.2"]) stdout = a.backticks(["ping", "-c", "3", "10.0.0.2"])
# stdout, stderr are strings
# Run an process with a pseudo-tty associated to it; provide a UNIX socket to # Run an process with a pseudo-tty associated to it; provide a UNIX socket to
# interact with the process # interact with the process
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# vim:ts=4:sw=4:et:ai:sts=4 # vim:ts=4:sw=4:et:ai:sts=4
import os, socket, sys, traceback, unshare, weakref import os, socket, sys, traceback, unshare, weakref
import netns.protocol import netns.protocol, netns.subprocess_
class Node(object): class Node(object):
_nodes = weakref.WeakValueDictionary() _nodes = weakref.WeakValueDictionary()
...@@ -37,11 +37,25 @@ class Node(object): ...@@ -37,11 +37,25 @@ class Node(object):
self._slave.shutdown() self._slave.shutdown()
del self._slave del self._slave
# Subprocesses
def _add_subprocess(self, subprocess): def _add_subprocess(self, subprocess):
self._processes[subprocess.pid] = subprocess self._processes[subprocess.pid] = subprocess
def Subprocess(self, *kargs, **kwargs):
return netns.subprocess_.Subprocess(self, *kargs, **kwargs)
def Popen(self, *kargs, **kwargs):
return netns.subprocess_.Popen(self, *kargs, **kwargs)
def system(self, *kargs, **kwargs):
return netns.subprocess_.system(self, *kargs, **kwargs)
def backticks(self, *kargs, **kwargs):
return netns.subprocess_.backticks(self, *kargs, **kwargs)
def backticks_raise(self, *kargs, **kwargs):
return netns.subprocess_.backticks_raise(self, *kargs, **kwargs)
@property @property
def pid(self): def pid(self):
return self._pid return self._pid
def add_if(self, mac_address = None, mtu = None): def add_if(self, mac_address = None, mtu = None):
return Interface(mac_address, mtu) return Interface(mac_address, mtu)
def add_route(self, prefix, prefix_len, nexthop = None, interface = None): def add_route(self, prefix, prefix_len, nexthop = None, interface = None):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment