1 |
from WMCore.Services.Service import Service
|
2 |
import common
|
3 |
|
4 |
class Downloader:
|
5 |
|
6 |
def __init__(self, endpoint, cachepath='', cacheduration = 0.5, timeout = 20, \
|
7 |
type = "txt/csv", logger = None ):
|
8 |
## if not already defined set default CachePath to $HOME/.cms_crab
|
9 |
if cachepath =='':
|
10 |
import os
|
11 |
if os.getenv('CMS_CRAB_CACHE_DIR'):
|
12 |
cachepath ='%s/.cms_crab'%os.getenv('CMS_CRAB_CACHE_DIR')
|
13 |
elif os.getenv('HOME'):
|
14 |
cachepath='%s/.cms_crab'%os.getenv('HOME')
|
15 |
else:
|
16 |
import pwd
|
17 |
cachepath = '/tmp/crab_cache_' + pwd.getpwuid(os.getuid())[0]
|
18 |
|
19 |
if not os.path.isdir(cachepath):
|
20 |
try:
|
21 |
os.mkdir(cachepath)
|
22 |
except:
|
23 |
common.logger.info('Warning cannot create %s. Using current directory'%cachepath)
|
24 |
cachepath=os.getcwd()
|
25 |
|
26 |
if not logger: logger = common.logger()
|
27 |
|
28 |
self.wmcorecache = {}
|
29 |
self.wmcorecache['logger'] = logger
|
30 |
self.wmcorecache['cachepath'] = cachepath ## cache area
|
31 |
self.wmcorecache['cacheduration'] = 0.5 ## half an hour
|
32 |
self.wmcorecache['timeout'] = 20 ## seconds
|
33 |
self.wmcorecache['endpoint'] = endpoint
|
34 |
|
35 |
def downloadConfig(self, cacheFile, type = "txt/csv",openf=True, useVerb='GET'):
|
36 |
self.wmcorecache['type'] = type
|
37 |
common.logger.debug("Downloading file [%s] to [%s]." %(str(self.wmcorecache['endpoint']),(str(self.wmcorecache['cachepath'])+"/"+cacheFile)))
|
38 |
servo = Service( self.wmcorecache )
|
39 |
servo['usestalecache'] = True
|
40 |
return servo.refreshCache( cacheFile, cacheFile, openfile=openf, verb=useVerb )
|
41 |
|
42 |
def aconfig(self, fileName = "prova"):
|
43 |
f = self.downloadConfig(fileName)
|
44 |
l = ''.join( f.readlines() )
|
45 |
f.close()
|
46 |
|
47 |
value = None
|
48 |
try:
|
49 |
result = eval(l)
|
50 |
except SyntaxError, se:
|
51 |
common.logger.debug("Problem reading downloaded file %s "%str(fileName))
|
52 |
|
53 |
return result
|
54 |
|
55 |
def config(self, fileName = "prova"):
|
56 |
f = self.downloadConfig(fileName)
|
57 |
try:
|
58 |
result = f.read()
|
59 |
f.close()
|
60 |
except IOError:
|
61 |
raise RuntimeError("URL not available: %s" % self.wmcorecache['endpoint'] )
|
62 |
return result
|
63 |
|
64 |
def filePath(self, fileName = "prova"):
|
65 |
return self.downloadConfig(fileName, openf=False)
|