1 '''
2 A class that allows for a dynamic allocation model for VRED
3 '''
4
5
6
7
8
9 import os
10 import sys
11 import re
12 import tempfile
13
14 import qb.backend.pythonChildBackEnd
15 import qb.backend.utils as backendUtils
16
17
18 -class VRED_BackEnd(qb.backend.pythonChildBackEnd.PythonChildBackEnd):
19 """
20 A backend for a jobtype that runs an instance of VRED in a child process.
21
22 This will allow the VRED session to persist for the duration of the subjob.
23 """
24
26 """
27 Get the arguments to start the child python process.
28
29 @param port: the port on which the PyCmdDispatcher's backchannel instance is listening, usually
30 passed as a parameter to the child_bootstrapper.py script which starts up the pyCmdExecutor
31 inside child process started by the PyCmdDispatcher
32
33 @type port: C{int}
34
35 @return: Return a tuple of a list of args to start the python interpreter,
36 and a list of python commands to initialize the python working environment.
37
38 @rtype: C{tuple} C{([childArgs], [pyInitCmds])}
39 """
40 args = []
41 if sys.platform != 'win32':
42 args.extend(['/bin/tcsh', '-c'])
43
44 vred_args = self.job['package'].get('vred_args', '')
45 if vred_args is None:
46 vred_args = ''
47
48 pyCmdLine = '''"%s" %s -postpython "load('%s')"''' % (self.pyExecutable, vred_args, self.childBootstrapper)
49
50 args.append(pyCmdLine)
51
52 pyInitCmds = [
53 'sys.stderr = sys.__stderr__',
54 'sys.stdout = sys.__stdout__'
55 ]
56
57 return args, pyInitCmds
58
60 """
61 @param port: the port on which the PyCmdDispatcher's backchannel instance is listening, usually
62 passed as a parameter to the child_bootstrapper.py script which starts up the pyCmdExecutor
63 inside child process started by the PyCmdDispatcher
64
65 @type port: C{int}
66
67 @return: Return the path to the newly-created file
68 @rtype: C{str}
69 """
70 tmpdir = tempfile.gettempdir().replace('\\', '/')
71 this_dir = os.path.dirname(backendUtils.getModulePath()).replace('\\', '/')
72 template_script = '%s/%s' % (this_dir, 'child_bootstrapper_template.py')
73
74 fh = open(template_script, 'r')
75 bootstrapper_code = fh.readlines()
76 fh.close()
77
78 token = 'PORT_TOKEN'
79 PORT_TOKEN_RGX = re.compile(token)
80
81 for i in range(len(bootstrapper_code)):
82 line = bootstrapper_code[i]
83 if line.count(token):
84 m = PORT_TOKEN_RGX.search(line)
85 if m:
86 bootstrapper_code[i] = PORT_TOKEN_RGX.sub(str(port), line)
87 break
88
89 bootstrapper_file = '%s/bootstrap_%s.%s.py' % (tmpdir, os.environ.get('QBJOBID', 0), os.environ.get('QBSUBID', 0))
90
91 fh = open(bootstrapper_file, 'w')
92 fh.writelines(bootstrapper_code)
93 fh.close()
94
95 return bootstrapper_file
96