ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ServerConfig.py
Revision: 1.20
Committed: Tue Apr 28 17:34:23 2009 UTC (16 years ago) by slacapra
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_6_0_pre2, CRAB_2_6_0_pre1
Changes since 1.19: +2 -3 lines
Log Message:
change test URL

File Contents

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