ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ServerConfig.py
Revision: 1.16
Committed: Tue Dec 16 11:43:18 2008 UTC (16 years, 4 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
Changes since 1.15: +1 -2 lines
Log Message:
cleanup

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')
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 pass
55
56 def config(self):
57 return self.theConfig
58
59 def downloadFile(self, url, destination):
60 try:
61 f = urllib.urlopen(url)
62 data = f.read()
63 if '<!' in data[:2]:
64 raise IOError
65
66 ff = open(destination, 'w')
67 ff.write(data)
68 ff.close()
69 except IOError:
70 raise CrabException('Cannot download config file '+destination+' from '+self.url)
71
72 def getConfig_(self, configFileName):
73 url = self.url+configFileName
74 if not os.path.exists(configFileName):
75 common.logger.message('Downloading config files for '+url)
76 self.downloadFile( url, configFileName)
77 else:
78 statinfo = os.stat(configFileName)
79 ## if the file is older then 12 hours it is re-downloaded to update the configuration
80 oldness = 12*3600
81 if (time.time() - statinfo.st_ctime) > oldness:
82 common.logger.message('Downloading config files for '+url)
83 self.downloadFile( url, configFileName)
84 pass
85 return os.getcwd()+'/'+configFileName