ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ServerConfig.py
Revision: 1.21
Committed: Tue May 26 10:23:01 2009 UTC (15 years, 11 months ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.20: +7 -9 lines
Log Message:
adapting code to logging usage. (crab_logger removed)

File Contents

# User Rev Content
1 farinafa 1.1 from crab_exceptions import *
2     from crab_util import *
3     import common
4    
5     import urllib
6     import os, time
7    
8     class ServerConfig:
9 spiga 1.2 def __init__(self, serverName):
10 slacapra 1.15 import string
11     serverName = string.lower(serverName)
12 spiga 1.21 common.logger.debug('Calling ServerConfig '+serverName)
13 slacapra 1.15
14 slacapra 1.20 self.url ='https://cmsweb.cern.ch/crabconf/'
15     # self.url ='http://www.pd.infn.it/~lacaprar/Computing/'
16 slacapra 1.15 if 'default' in serverName:
17 spiga 1.21 common.logger.debug('getting serverlist from web')
18 slacapra 1.15 # get a list of available servers
19     serverListFileName ='AvalableServerList'
20     serverListFile = self.getConfig_(serverListFileName)
21     # parse the localCfg file
22     f = open(serverListFile, 'r')
23     tmp = f.readlines()
24     f.close()
25     if not tmp:
26     msg = 'List of avalable Server '+serverListFileName+' from '+self.url+' is empty\n'
27     msg += 'Please report to CRAB feedback hypernews'
28     raise CrabException(msg)
29 slacapra 1.16 # clean up empty lines and comments
30 slacapra 1.15 serverList=[]
31 slacapra 1.19 [serverList.append(string.split(string.strip(it))) for it in tmp if (it.strip() and not it.strip()[0]=="#")]
32 spiga 1.21 common.logger.debug('All avaialble servers: '+str(serverList))
33 slacapra 1.15
34 slacapra 1.19 # select servers from client version
35     compatibleServerList=[]
36     for s in serverList:
37     vv=string.split(s[1],'-')
38     if len(vv[0])==0: vv[0]='0.0.0'
39     if len(vv[1])==0: vv[1]='99.99.99'
40     for i in 0,1:
41     tmp=[]
42     [tmp.append(int(t)) for t in vv[i].split('.')]
43     vv[i]=tuple(tmp)
44     #[vv.append(tuple(t.split('.'))) for t in string.split(s[1],'-')]
45    
46     common.prog_version
47    
48     #print vv[0],common.prog_version,vv[1]
49     if vv[0]<=common.prog_version and common.prog_version<=vv[1]: compatibleServerList.append(s[0])
50 spiga 1.21 common.logger.debug('All avaialble servers compatible with %s: '%common.prog_version_str +str(serverList))
51 slacapra 1.19 if len(compatibleServerList)==0:
52     msg = "No compatible server available with client version %s\n"%common.prog_version_str
53     msg += "Exiting"
54     raise CrabException(msg)
55 slacapra 1.15 # if more than one, pick up a random one, waiting for something smarter (SiteDB)
56     import random
57 slacapra 1.19 serverName = random.choice(compatibleServerList)
58 spiga 1.21 common.logger.debug('Avaialble servers: '+str(compatibleServerList)+' choosen: '+serverName)
59 slacapra 1.15 if 'server_' in serverName:
60     configFileName = '%s.conf'%serverName
61 farinafa 1.6 else:
62 slacapra 1.15 configFileName = 'server_%s.conf'%serverName
63 farinafa 1.6
64 slacapra 1.15 localCfg = self.getConfig_(configFileName)
65 farinafa 1.1
66     # parse the localCfg file
67     f = open(localCfg, 'r')
68     l = ''.join( f.readlines() )
69     f.close()
70    
71     if not l:
72     l = str('{}')
73     self.theConfig = eval(l)
74 slacapra 1.17 self.theConfig['serverGenericName']=serverName
75 farinafa 1.1 pass
76    
77     def config(self):
78     return self.theConfig
79    
80     def downloadFile(self, url, destination):
81     try:
82     f = urllib.urlopen(url)
83 farinafa 1.6 data = f.read()
84     if '<!' in data[:2]:
85     raise IOError
86    
87 farinafa 1.1 ff = open(destination, 'w')
88 farinafa 1.6 ff.write(data)
89 farinafa 1.1 ff.close()
90     except IOError:
91     raise CrabException('Cannot download config file '+destination+' from '+self.url)
92    
93 slacapra 1.15 def getConfig_(self, configFileName):
94     url = self.url+configFileName
95     if not os.path.exists(configFileName):
96 spiga 1.21 common.logger.info('Downloading config files for '+url)
97 slacapra 1.15 self.downloadFile( url, configFileName)
98 farinafa 1.1 else:
99 slacapra 1.15 statinfo = os.stat(configFileName)
100 farinafa 1.1 ## if the file is older then 12 hours it is re-downloaded to update the configuration
101     oldness = 12*3600
102     if (time.time() - statinfo.st_ctime) > oldness:
103 spiga 1.21 common.logger.info('Downloading config files for '+url)
104 slacapra 1.15 self.downloadFile( url, configFileName)
105 farinafa 1.1 pass
106 slacapra 1.15 return os.getcwd()+'/'+configFileName