1 |
mcinquil |
1.1 |
from WMCore.Services.Service import Service
|
2 |
|
|
import common
|
3 |
|
|
|
4 |
|
|
class Downloader:
|
5 |
|
|
|
6 |
spiga |
1.4 |
def __init__(self, endpoint, cachepath='', cacheduration = 0.5, timeout = 20, \
|
7 |
spiga |
1.7 |
type = "txt/csv", logger = None ):
|
8 |
spiga |
1.4 |
## if not already defined set default CachePath to $HOME/.cms_crab
|
9 |
|
|
if cachepath =='':
|
10 |
|
|
import os
|
11 |
spiga |
1.5 |
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 |
belforte |
1.9 |
import pwd
|
17 |
spiga |
1.5 |
cachepath = '/tmp/crab_cache_' + pwd.getpwuid(os.getuid())[0]
|
18 |
|
|
|
19 |
spiga |
1.4 |
if not os.path.isdir(cachepath):
|
20 |
|
|
try:
|
21 |
|
|
os.mkdir(cachepath)
|
22 |
|
|
except:
|
23 |
spiga |
1.5 |
common.logger.info('Warning cannot create %s. Using current directory'%cachepath)
|
24 |
spiga |
1.4 |
cachepath=os.getcwd()
|
25 |
|
|
|
26 |
spiga |
1.7 |
if not logger: logger = common.logger()
|
27 |
spiga |
1.4 |
|
28 |
mcinquil |
1.1 |
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 |
farinafa |
1.6 |
def downloadConfig(self, cacheFile, type = "txt/csv",openf=True, useVerb='GET'):
|
36 |
mcinquil |
1.1 |
self.wmcorecache['type'] = type
|
37 |
mcinquil |
1.2 |
common.logger.debug("Downloading file [%s] to [%s]." %(str(self.wmcorecache['endpoint']),(str(self.wmcorecache['cachepath'])+"/"+cacheFile)))
|
38 |
mcinquil |
1.1 |
servo = Service( self.wmcorecache )
|
39 |
farinafa |
1.8 |
servo['usestalecache'] = True
|
40 |
farinafa |
1.6 |
return servo.refreshCache( cacheFile, cacheFile, openfile=openf, verb=useVerb )
|
41 |
mcinquil |
1.1 |
|
42 |
spiga |
1.4 |
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 |
mcinquil |
1.1 |
def config(self, fileName = "prova"):
|
56 |
spiga |
1.4 |
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 |
mcinquil |
1.1 |
|
64 |
spiga |
1.3 |
def filePath(self, fileName = "prova"):
|
65 |
|
|
return self.downloadConfig(fileName, openf=False)
|