1
2
3
4
5
6
7 from Ganga.Utility.logging import getLogger
8
9 logger = getLogger()
10
12 """
13 StandardJobConfig defines a standard input for many of the handlers: LSF, Localhost,LCG.
14 It corresponds to a simplified JDL definition: specification of executable, arguments and input sandbox.
15 Executable and arguments may be specified either as strings or File objects. In the second case they are
16 automatically added to the input sandbox list.
17
18 If you modify attributes of the StandardJobConfig object after the initialization, the you should
19 do processValues() which perfomes a validation of the attributes and updates internal cache which is
20 used by getter methods.
21
22 """
23
24 - def __init__(self,exe=None,inputbox=[],args=[],outputbox=[],env={}):
25 """
26 exe - executable string to be run on the worker node or a File object to be shipped as executable script to the worker node
27 args - list of strings which are passed as arguments to the executable string or File objects which are automatically added to the sandbox
28 inputbox - list of additional File or FileBuffer objects which go to the sandbox
29 outputbox - list of additional files which should be returned by the sandbox
30 env - environment to be set for execution of the job
31
32 The constructor does processValues() automatically so the construction of the object may failed with exceptions raised by that method.
33 Notes for derived classes:
34 - this constructor should be called at the end of the derived constructor.
35 - you may freely add new attributes as long as you they do not start with _
36 """
37 self.exe = exe
38 self.inputbox = inputbox[:]
39 self.args = args
40 self.outputbox = outputbox[:]
41 self.env = env
42
43 self.__all_inputbox = []
44 self.__args_strings = []
45 self.__exe_string = ""
46 self.__sandbox_check = {}
47
48 self.processValues()
49
51 '''Get all input sandbox files'''
52 return self.__all_inputbox
53
55 """Get all output sandbox files. The duplicates are removed. """
56 from Ganga.Utility.util import unique
57 return unique(self.outputbox)
58
60 '''Get a string which should be used at the worker node to invoke an executable.
61 Note that this string does not necesserily have to be a file name on the worker node'''
62 return self.__exe_string
63
65 '''Get a list of strings which correspond to the arguments to the executable on the worker node.'''
66 return self.__args_strings
67
69 '''Get a command string including the quoted arguments which may be passed to os.system().
70 This method is provided for the convenience'''
71
72 logger.warning('INTERNAL METHOD JobConfig.getExeCmdString() IS OBSOLETED, backend should be updated to use getExeString() and shell=False in a call to subprocess.Popen()')
73
74
75 def quote_arguments(args):
76 quoted = ""
77 for a in args:
78 quoted += '"'+a+'" '
79 return quoted
80
81 return self.__exe_string+' '+quote_arguments(self.__args_strings)
82
84 '''Process original exe,args and inputbox values and extract strings suitable for the further processing.
85 If the exe property is a File then this method will check if it has executable attributes.
86 You do not have to call this method unless you explicitly modify some of the original values.
87 '''
88
89
90 from Ganga.GPIDev.Lib.File import File
91
92 self.__all_inputbox = self.inputbox[:]
93 self.__args_strings = []
94 self.__exe_string = ""
95 self.__sandbox_check = {}
96
97
98
99 def _get_path_in_sandbox(f):
100 ''' A helper which checks conflicts in sandbox.
101 If you try to add twice a file object f with the same name, it shows a warning.
102 '''
103
104 fn = f.getPathInSandbox()
105 if self.__sandbox_check.has_key(fn):
106 logger.warning('File %s already in the sandbox (source=%s). Overriding from source=%s',fn,self.__sandbox_check[fn],f.name)
107 self.__sandbox_check[fn] = f.name
108 return fn
109
110
111
112 for f in self.inputbox:
113 fn = _get_path_in_sandbox(f)
114
115
116
117 for a in self.args:
118 if type(a) is type(''):
119 self.__args_strings.append(a)
120 else:
121 try:
122 fn = _get_path_in_sandbox(a)
123 self.__args_strings.append(fn)
124 self.__all_inputbox.append(a)
125 except AttributeError,x:
126 s = "cannot process argument %s, it is neither File nor string" % repr(a)
127 logger.error(s)
128 raise ValueError(s)
129
130 if type(self.exe) is type(''):
131 self.__exe_string = self.exe
132 else:
133 try:
134 self.__exe_string = _get_path_in_sandbox(self.exe)
135 if not self.exe.isExecutable():
136 logger.warning('file %s is not executable, overriding executable permissions in the input sandbox'%self.exe.name)
137 self.exe.executable = True
138 self.__all_inputbox.append(self.exe)
139 except AttributeError,x:
140 s = "cannot process exe property %s, it is neither File nor string (%s)" % (repr(self.exe),str(x))
141 logger.error(s)
142 raise ValueError(s)
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167