#!/usr/bin/python3 import sys import fishdescriptor.fish import re donor = None usage = ''' fishdescriptor ... [-p|--pid ...] s [=] fish the openfile referenced by descriptor in (the most recent) and keep a descriptor onto it; and, optionally, give it the number for exec exec [...] execute a process with each specified numeric as an actual fd sockinfo calls getsockname/getpeername on the most recent -p|-pid now attach to , 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:]] d = fishdescriptor.fish.Donor(pid) r = d.fish(fds) print(repr(r))