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

# User Rev Content
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     cachepath = '/tmp/crab_cache_' + pwd.getpwuid(os.getuid())[0]
17    
18 spiga 1.4 if not os.path.isdir(cachepath):
19     try:
20     os.mkdir(cachepath)
21     except:
22 spiga 1.5 common.logger.info('Warning cannot create %s. Using current directory'%cachepath)
23 spiga 1.4 cachepath=os.getcwd()
24    
25 spiga 1.7 if not logger: logger = common.logger()
26 spiga 1.4
27 mcinquil 1.1 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 farinafa 1.6 def downloadConfig(self, cacheFile, type = "txt/csv",openf=True, useVerb='GET'):
35 mcinquil 1.1 self.wmcorecache['type'] = type
36 mcinquil 1.2 common.logger.debug("Downloading file [%s] to [%s]." %(str(self.wmcorecache['endpoint']),(str(self.wmcorecache['cachepath'])+"/"+cacheFile)))
37 mcinquil 1.1 servo = Service( self.wmcorecache )
38 farinafa 1.6 return servo.refreshCache( cacheFile, cacheFile, openfile=openf, verb=useVerb )
39 mcinquil 1.1
40 spiga 1.4 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 mcinquil 1.1 def config(self, fileName = "prova"):
54 spiga 1.4 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 mcinquil 1.1
62 spiga 1.3 def filePath(self, fileName = "prova"):
63     return self.downloadConfig(fileName, openf=False)