ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/Downloader.py
Revision: 1.7
Committed: Wed May 19 16:16:20 2010 UTC (14 years, 11 months ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.6: +2 -2 lines
Log Message:
fix logger issue [first part]

File Contents

# Content
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 cachepath = '/tmp/crab_cache_' + pwd.getpwuid(os.getuid())[0]
17
18 if not os.path.isdir(cachepath):
19 try:
20 os.mkdir(cachepath)
21 except:
22 common.logger.info('Warning cannot create %s. Using current directory'%cachepath)
23 cachepath=os.getcwd()
24
25 if not logger: logger = common.logger()
26
27 self.wmcorecache = {}
28 self.wmcorecache['logger'] = logger
29 self.wmcorecache['cachepath'] = cachepath ## cache area
30 self.wmcorecache['cacheduration'] = 0.5 ## half an hour
31 self.wmcorecache['timeout'] = 20 ## seconds
32 self.wmcorecache['endpoint'] = endpoint
33
34 def downloadConfig(self, cacheFile, type = "txt/csv",openf=True, useVerb='GET'):
35 self.wmcorecache['type'] = type
36 common.logger.debug("Downloading file [%s] to [%s]." %(str(self.wmcorecache['endpoint']),(str(self.wmcorecache['cachepath'])+"/"+cacheFile)))
37 servo = Service( self.wmcorecache )
38 return servo.refreshCache( cacheFile, cacheFile, openfile=openf, verb=useVerb )
39
40 def aconfig(self, fileName = "prova"):
41 f = self.downloadConfig(fileName)
42 l = ''.join( f.readlines() )
43 f.close()
44
45 value = None
46 try:
47 result = eval(l)
48 except SyntaxError, se:
49 common.logger.debug("Problem reading downloaded file %s "%str(fileName))
50
51 return result
52
53 def config(self, fileName = "prova"):
54 f = self.downloadConfig(fileName)
55 try:
56 result = f.read()
57 f.close()
58 except IOError:
59 raise RuntimeError("URL not available: %s" % self.wmcorecache['endpoint'] )
60 return result
61
62 def filePath(self, fileName = "prova"):
63 return self.downloadConfig(fileName, openf=False)