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='LXB'):
|
11 |
common.logger.debug(5,'Calling ServerConfig')
|
12 |
self.url = 'http://cmsdoc.cern.ch/cms/ccs/wm/www/Crab/useful_script/Server_conf/'
|
13 |
self.configFileName = 'Server_%s.conf'%str(serverName)
|
14 |
localCfg = self.getConfig_()
|
15 |
|
16 |
# parse the localCfg file
|
17 |
f = open(localCfg, 'r')
|
18 |
l = ''.join( f.readlines() )
|
19 |
f.close()
|
20 |
|
21 |
if not l:
|
22 |
l = str('{}')
|
23 |
self.theConfig = eval(l)
|
24 |
pass
|
25 |
|
26 |
def config(self):
|
27 |
return self.theConfig
|
28 |
|
29 |
def downloadFile(self, url, destination):
|
30 |
try:
|
31 |
f = urllib.urlopen(url)
|
32 |
ff = open(destination, 'w')
|
33 |
ff.write(f.read())
|
34 |
ff.close()
|
35 |
except IOError:
|
36 |
raise CrabException('Cannot download config file '+destination+' from '+self.url)
|
37 |
|
38 |
def getConfig_(self):
|
39 |
if not os.path.exists(self.configFileName):
|
40 |
url = self.url+self.configFileName
|
41 |
common.logger.message('Downloading config files for '+url)
|
42 |
self.downloadFile( url, self.configFileName)
|
43 |
else:
|
44 |
statinfo = os.stat(self.configFileName)
|
45 |
## if the file is older then 12 hours it is re-downloaded to update the configuration
|
46 |
oldness = 12*3600
|
47 |
if (time.time() - statinfo.st_ctime) > oldness:
|
48 |
url = self.url+self.configFileName
|
49 |
common.logger.message('Downloading config files for '+url)
|
50 |
self.downloadFile( url, self.configFileName)
|
51 |
pass
|
52 |
return os.getcwd()+'/'+self.configFileName
|
53 |
|
54 |
|
55 |
|