ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ServerConfig.py
Revision: 1.17
Committed: Wed Jan 14 17:19:53 2009 UTC (16 years, 3 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_5_0_pre3, CRAB_2_5_0_pre2, CRAB_2_5_0_pre1, CRAB_2_4_4, CRAB_2_4_4_pre6, CRAB_2_4_4_pre5, CRAB_2_4_4_pre4, CRAB_2_4_4_pre3, CRAB_2_4_4_pre2, CRAB_2_4_4_pre1
Changes since 1.16: +2 -1 lines
Log Message:
keep in cache the randomly choosen server for next usage in the same task

File Contents

# Content
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 def __init__(self, serverName):
11 import string
12 serverName = string.lower(serverName)
13 common.logger.debug(5,'Calling ServerConfig '+serverName)
14 # self.url = 'http://cmsdoc.cern.ch/cms/ccs/wm/www/Crab/useful_script/'
15
16 self.url ='https://cmsweb.cern.ch/crabconf/files/'
17 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 # clean up empty lines and comments
31 serverList=[]
32 [serverList.append(string.strip(it)) for it in tmp if (it.strip() and not it.strip()[0]=="#")]
33
34 # if more than one, pick up a random one, waiting for something smarter (SiteDB)
35 import random
36 serverName = random.choice(serverList)
37 common.logger.debug(5,'Avaialble servers: '+str(serverList)+' choosen: '+serverName)
38 common.logger.write('Avaialble servers: '+str(serverList)+' choosen: '+serverName)
39 if 'server_' in serverName:
40 configFileName = '%s.conf'%serverName
41 else:
42 configFileName = 'server_%s.conf'%serverName
43
44 localCfg = self.getConfig_(configFileName)
45
46 # parse the localCfg file
47 f = open(localCfg, 'r')
48 l = ''.join( f.readlines() )
49 f.close()
50
51 if not l:
52 l = str('{}')
53 self.theConfig = eval(l)
54 self.theConfig['serverGenericName']=serverName
55 pass
56
57 def config(self):
58 return self.theConfig
59
60 def downloadFile(self, url, destination):
61 try:
62 f = urllib.urlopen(url)
63 data = f.read()
64 if '<!' in data[:2]:
65 raise IOError
66
67 ff = open(destination, 'w')
68 ff.write(data)
69 ff.close()
70 except IOError:
71 raise CrabException('Cannot download config file '+destination+' from '+self.url)
72
73 def getConfig_(self, configFileName):
74 url = self.url+configFileName
75 if not os.path.exists(configFileName):
76 common.logger.message('Downloading config files for '+url)
77 self.downloadFile( url, configFileName)
78 else:
79 statinfo = os.stat(configFileName)
80 ## if the file is older then 12 hours it is re-downloaded to update the configuration
81 oldness = 12*3600
82 if (time.time() - statinfo.st_ctime) > oldness:
83 common.logger.message('Downloading config files for '+url)
84 self.downloadFile( url, configFileName)
85 pass
86 return os.getcwd()+'/'+configFileName