import sys
import fishdescriptor.fish
+import re
+
+donor = None
+
+usage =
+'''
+fishdescriptor <pid> <action>... [-p|--pid <pid> <action>...]
+
+<action>s
+ <there-fd>[=<here>]
+ fish the openfile referenced by descriptor <there-fd> in
+ (the most recent) <pid> and keep a descriptor onto it;
+ and, optionally, give it the number <here> for exec
+ exec <program> [<arg>...]
+ execute a process with each specified numeric <here>
+ as an actual fd
+ sockinfo
+ calls getsockname/getpeername on the most recent
+ <there-fd>
+
+ -p|-pid <pid>
+ now attach to <pid>, detaching from previous pid
+'''
+
+def set_donor(pid_arg):
+ pid = int(pid_arg)
+ if donor is not None: donor.detach()
+ donor = fishdescriptor.fish.Donor(pid)
+
+def process_args():
+ def arg_matches(regexp):
+ nonlocal m
+ m = re.search(regexp, arg)
+ return m
+
+ arg_ix = 0
+ def next_arg():
+ sys.argv[arg_ix] =
+
+ while arg_ix < len(sys.argv):
+ arg = next_arg()
+ if arg_matches(r'^-p(\d+)') or
+ arg_matches(r'^--pid=(\d+)$')
+ set_donor(m.groups(1)[0])
+
+ pid =
+(?:-p|^--pid(?:=|$))(\d*)$'):
+
+ pid = int(m.groups(1)[0])
+ elif arg_matches(r'^(?:-p|^--pid=)(\d+)$'):
+
+ if expecting_pid:
+ set_donor(arg)
+ el
+ elif arg_matches(r'\d+
+
+ parser = OptionParser(usage=usage, )
+
+ parser.add_option("-p", "--pid", type='int', callback=set_donor)
+ parser.add_option(
pid = int(sys.argv[1])
fds = [int(x) for x in sys.argv[2:]]