1"""For starting up remote processes"""
2import sys, pickle, os
3import importlib
4
5if __name__ == '__main__':
6    if hasattr(os, 'setpgrp'):
7        os.setpgrp()  ## prevents signals (notably keyboard interrupt) being forwarded from parent to this process
8    if sys.version[0] == '3':
9        #name, port, authkey, ppid, targetStr, path, pyside = pickle.load(sys.stdin.buffer)
10        opts = pickle.load(sys.stdin.buffer)
11    else:
12        #name, port, authkey, ppid, targetStr, path, pyside = pickle.load(sys.stdin)
13        opts = pickle.load(sys.stdin)
14    #print "key:",  ' '.join([str(ord(x)) for x in authkey])
15    path = opts.pop('path', None)
16    if path is not None:
17        if isinstance(path, str):
18            # if string, just insert this into the path
19            sys.path.insert(0, path)
20        else:
21            # if list, then replace the entire sys.path
22            ## modify sys.path in place--no idea who already has a reference to the existing list.
23            while len(sys.path) > 0:
24                sys.path.pop()
25            sys.path.extend(path)
26
27    pyqtapis = opts.pop('pyqtapis', None)
28    if pyqtapis is not None:
29        try:
30            from PyQt5 import sip
31        except ImportError:
32            import sip
33            for k,v in pyqtapis.items():
34                sip.setapi(k, v)
35
36    qt_lib = opts.pop('qt_lib', None)
37    if qt_lib is not None:
38        globals()[qt_lib] = importlib.import_module(qt_lib)
39
40    targetStr = opts.pop('targetStr')
41    try:
42        target = pickle.loads(targetStr)  ## unpickling the target should import everything we need
43    except:
44        print("Current sys.path:", sys.path)
45        raise
46    target(**opts)  ## Send all other options to the target function
47    sys.exit(0)
48