-
Notifications
You must be signed in to change notification settings - Fork 4
/
fcc_execution_module.py
192 lines (124 loc) · 5.8 KB
/
fcc_execution_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#************************** libraries importation **************************#
#DIRAC libraries
__RCSID__ = "$Id$"
from DIRAC.Core.Utilities.Subprocess import shellCall
from ILCDIRAC.Workflow.Modules.ModuleBase import ModuleBase, generateRandomString
from ILCDIRAC.Core.Utilities.CombinedSoftwareInstallation import getSoftwareFolder
from ILCDIRAC.Core.Utilities.PrepareOptionFiles import getNewLDLibs
from ILCDIRAC.Core.Utilities.ResolveDependencies import resolveDeps
from ILCDIRAC.Core.Utilities.resolvePathsAndNames import getProdFilename
from DIRAC import gLogger, S_OK, S_ERROR
#standard libraries
import os
import stat
from os import listdir
import subprocess
import tarfile
import shutil
#************************************* Classes - Definition ******************************#
#*****************************************#
# Class : ExecutionModule() #
# role : Uncompress compressed folders #
# needed by the application, generate #
# a script that will run the job #
#*****************************************#
class ExecutionModule(ModuleBase):
""" Run FCC softs """
def __init__(self):
super(ExecutionModule, self).__init__()
self.enable = True
self.STEP_NUMBER = ''
self.script_to_source = ''
self.fcc_executable = ''
self.fcc_conf_file = ''
self.fcc_input_files = ''
self.fcc_output_file = ''
self.fccsw = ''
self.number_of_events = ''
self.debug = True
self.log = gLogger.getSubLogger( "ExecutionModule" )
self.sourcingFCCStack = ''
self.script_name = os.path.join(os.getcwd(),'user_temp_job.sh')
#******************************#
# Function name : uncompress #
# role : uncompress .tgz files #
#******************************#
def uncompress(self,compressed_folder):
tar = tarfile.open(compressed_folder)
output_dir, file_extension = os.path.splitext(compressed_folder)
#print 'OUTPUT'
#print output_dir
for item in tar:
tar.extract(item,output_dir) # extract
#*******************************************************#
# Function name : execute #
# role : main method called by the installation module #
#*******************************************************#
def execute(self):
""" Run the module
"""
print 'EXECUTABLE'
print self.fcc_executable
print '\n'.join(os.listdir(os.getcwd()))
if '' != self.fccsw:
for file in os.listdir(os.getcwd()):
if file.endswith('.tgz'):
self.uncompress(file)
#print '\n'.join(os.listdir(os.getcwd()))
if not self.fcc_conf_file.startswith('/cvmfs/'):
self.fcc_conf_file = os.path.abspath(os.path.basename(self.fcc_conf_file))
bash_commands = [self.fcc_executable + " " + self.fcc_output_file + " " + self.fcc_conf_file + " " + self.number_of_events]
self.generate_bash_script(bash_commands,self.script_name)
#set environnement + execute job
p = subprocess.call(self.script_name)
status = 0
return self.finalStatusReport(status)
#****************************************************#
# Function name : chmod #
# input : permission #
# role : change permission #
#****************************************************#
def chmod(self,file,permission):
#reflet chmod a+permission
#make the file x,r, or w for everyone
USER_PERMISSION = eval('stat.S_I'+permission+'USR')
GROUP_PERMISSION = eval('stat.S_I'+permission+'GRP')
OTHER_PERMISSION = eval('stat.S_I'+permission+'OTH')
PERMISSION = USER_PERMISSION | GROUP_PERMISSION | OTHER_PERMISSION
#get actual mode of the file
mode = os.stat(file).st_mode
#append the new permission to the existing one
os.chmod(file,mode | PERMISSION)
#*************************************************#
# Function name : generate_bash_script #
# input : bash commands and script name #
# role : generate bash script #
#*************************************************#
def generate_bash_script(self,commands,script_name):
#in addition to the job command, we set the environnement for FCC,HEPPY and EOS
shebang = "#!/bin/bash"
test1 = "echo $LD_LIBRARY_PATH"
test3 = "unset LD_LIBRARY_PATH"
if not self.script_to_source.startswith('/cvmfs/'):
self.script_to_source = os.path.abspath(os.path.basename(self.script_to_source))
self.sourcingFCCStack = 'source ' + self.script_to_source
bash_script_text = [shebang, test1,test3,self.sourcingFCCStack,test1] + commands
print '\n'.join(bash_script_text)
#write the temporary job
self.write2file('w',script_name,'\n'.join(bash_script_text) + '\n')
#make the job executable and readable for all
self.chmod(script_name,'R')
self.chmod(script_name,'X')
#************************************#
# Function name : write2file #
# input : file_name and its content #
# role : write the content in a file #
#************************************#
def write2file(self,operation,file_name,filetext):
try:
#create file with w permission
with open(file_name,operation) as text_file:
text_file.write(filetext)
except:
print 'Error in writting file'
quit()