1 |
import common
|
2 |
from crab_logger import Logger
|
3 |
from crab_exceptions import *
|
4 |
from crab_util import *
|
5 |
|
6 |
class codePreparator:
|
7 |
def __init__(self, cfg_params):
|
8 |
try:
|
9 |
self.build = cfg_params['USER.build_mode']
|
10 |
except KeyError:
|
11 |
self.build = 'tgz'
|
12 |
pass
|
13 |
|
14 |
self.tgz_name = 'default.tgz'
|
15 |
self.tgzNameWithPath = None
|
16 |
pass
|
17 |
|
18 |
|
19 |
def prepareTgz_(self, executable):
|
20 |
|
21 |
self.tgzNameWithPath = common.work_space.shareDir()+self.tgz_name
|
22 |
|
23 |
if os.path.exists(self.tgzNameWithPath):
|
24 |
scramArea = os.environ["LOCALRT"]
|
25 |
n = len(string.split(scramArea, '/'))
|
26 |
swVersion = self.findSwVersion_(scramArea)
|
27 |
common.analisys_common_info['sw_version'] = swVersion
|
28 |
return self.tgzNameWithPath
|
29 |
|
30 |
# Prepare a tar gzipped file with user binaries.
|
31 |
|
32 |
# First of all declare the user Scram area
|
33 |
try:
|
34 |
scramArea = os.environ["LOCALRT"]
|
35 |
#print scramArea,'\n'
|
36 |
n = len(string.split(scramArea, '/'))
|
37 |
swVersion = self.findSwVersion_(scramArea)
|
38 |
#print swVersion,'\n'
|
39 |
common.analisys_common_info['sw_version'] = swVersion
|
40 |
swArea = os.environ["CMS_PATH"]
|
41 |
#print swArea,'\n'
|
42 |
# swArea = "/opt/exp_software/cms"
|
43 |
if scramArea.find(swArea) == -1:
|
44 |
# Calculate length of scram dir to extract software version
|
45 |
crabArea = os.getcwd()
|
46 |
os.chdir(scramArea)
|
47 |
|
48 |
## SL use SCRAM_ARCH instead of Linux__2.4
|
49 |
if os.environ.has_key('SCRAM_ARCH'):
|
50 |
scramArch = os.environ["SCRAM_ARCH"]
|
51 |
else:
|
52 |
scramArch = 'Linux__2.4'
|
53 |
#print scramArch,'\n'
|
54 |
# libDir = 'lib/'+scramArch+'/'
|
55 |
libDir = ''
|
56 |
binDir = 'bin/'+scramArch+'/'
|
57 |
|
58 |
|
59 |
exe = binDir + executable
|
60 |
### here assume that executable is in user directory
|
61 |
#print '##'+exe+'##\n'
|
62 |
if os.path.isfile(exe):
|
63 |
cmd = 'ldd ' + exe + ' | grep ' + scramArea
|
64 |
myCmd = os.popen(cmd)
|
65 |
tarcmd = 'tar zcvf ' + self.tgzNameWithPath + ' '
|
66 |
for line in (myCmd):
|
67 |
line = string.split(line, '=>')
|
68 |
lib = 'lib/' + scramArch + '/' + string.strip(line[0]) + ' '
|
69 |
tarcmd = tarcmd + lib
|
70 |
status = myCmd.close()
|
71 |
tarcmd = tarcmd + ' ' + exe
|
72 |
cout = runCommand(tarcmd)
|
73 |
else:
|
74 |
### If not, check if it's standard
|
75 |
|
76 |
cmd = 'which ' + executable
|
77 |
myCmd = os.popen(cmd)
|
78 |
out=myCmd.readline()
|
79 |
status = myCmd.close()
|
80 |
#print int(out.find("no "+executable+" in"))!=-1
|
81 |
if int(out.find("no "+executable+" in"))!=-1:
|
82 |
raise CrabException('User executable '+executable+' not found')
|
83 |
exe = string.strip(out[0:-1])
|
84 |
#print '##'+exe+'##\n'
|
85 |
if not os.path.isfile(exe):
|
86 |
raise CrabException('User executable not found')
|
87 |
#os.system('touch '+common.tgzNameWithPath)
|
88 |
os.chdir(crabArea)
|
89 |
else:
|
90 |
cmd = 'which ' + executable
|
91 |
cout = runCommand(cmd)
|
92 |
if not cout:
|
93 |
raise CrabException('Executable not found')
|
94 |
except KeyError:
|
95 |
raise CrabException('Undefined SCRAM area')
|
96 |
pass
|
97 |
return self.tgzNameWithPath
|
98 |
|
99 |
def findSwVersion_(self, path):
|
100 |
""" Find Sw version using proper scram env """
|
101 |
scramEnvFile = path+"/.SCRAM/Environment"
|
102 |
reVer = re.compile(r'SCRAM_PROJECTVERSION')
|
103 |
|
104 |
try:
|
105 |
for line in open(scramEnvFile, "r").readlines():
|
106 |
if reVer.match(line):
|
107 |
ver = string.strip(string.split(line, '=')[1])
|
108 |
return ver
|
109 |
pass
|
110 |
pass
|
111 |
except IOError, e:
|
112 |
msg = 'Cannot find SCRAM project version:\n'
|
113 |
msg += str(e)
|
114 |
raise CrabException(msg)
|
115 |
return
|