33###### COPYRIGHT NOTICE ########################################################
44#
55# Copyright (C) 2007-2011, Cycle Computing, LLC.
6- #
6+ #
77# Licensed under the Apache License, Version 2.0 (the "License"); you
88# may not use this file except in compliance with the License. You may
99# obtain a copy of the License at
10- #
10+ #
1111# http://www.apache.org/licenses/LICENSE-2.0.txt
12- #
12+ #
1313# Unless required by applicable law or agreed to in writing, software
1414# distributed under the License is distributed on an "AS IS" BASIS,
1515# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3434#
3535# The cache_config script will attempt to pull an updated configuration for a machine
3636# from the URL list, stopping when it successfully pulls config from a URL. It will
37- # store the config on disk, with a time-to-live, and use the cached copy of the
37+ # store the config on disk, with a time-to-live, and use the cached copy of the
3838# config if the time-to-live has not expired. It will keep using the cached copy if
3939# no new copy can be successfully fetched from any source on the URL list.
4040
6565
6666
6767# LOGGING CONFIGURATION
68- log_level_map = dict ()
69- log_level_map ['1' ] = logging .DEBUG
70- log_level_map ['2' ] = logging .INFO
71- log_level_map ['3' ] = logging .WARNING
72- log_level_map ['4' ] = logging .ERROR
73- log_level_map ['5' ] = logging .CRITICAL
68+ log_level_map = dict ()
69+ log_level_map ['1' ] = logging .DEBUG
70+ log_level_map ['2' ] = logging .INFO
71+ log_level_map ['3' ] = logging .WARNING
72+ log_level_map ['4' ] = logging .ERROR
73+ log_level_map ['5' ] = logging .CRITICAL
7474
75- env_var = '_CACHE_TOOL_DEBUG'
76- should_log = os .environ .has_key (env_var ) and log_level_map .has_key (os .environ [env_var ])
75+ env_var = '_CACHE_TOOL_DEBUG'
76+ should_log = os .environ .has_key (env_var ) and log_level_map .has_key (os .environ [env_var ])
7777
7878if should_log :
79- logLevel = log_level_map [os .environ [env_var ]]
79+ logLevel = log_level_map [os .environ [env_var ]]
8080else :
81- logLevel = logging .CRITICAL
82-
81+ logLevel = logging .CRITICAL
82+
8383logging .basicConfig (level = logLevel ,
8484 format = '%(asctime)s %(levelname)-8s %(message)s' ,
8585 datefmt = '%a, %d %b %Y %H:%M:%S' ,
8989# Python http_proxy incompatibility for http_proxy:
9090# handle the case where it does not start with http://
9191for http_proxy in ['http_proxy' , 'HTTP_PROXY' ]:
92- if os .environ .has_key (http_proxy ) and os .environ [http_proxy ][:7 ] != "http://" :
93- os .environ [http_proxy ]= "http://" + os .environ [http_proxy ]
92+ if os .environ .has_key (http_proxy ) and os .environ [http_proxy ][:7 ] != "http://" :
93+ os .environ [http_proxy ] = "http://" + os .environ [http_proxy ]
9494 break
9595
9696# SEED CONFIGURATION
@@ -118,25 +118,26 @@ def __init__(self, directory_name, step_base=1, step_random_coeff=0.2):
118118 self .isStepRandom = step_random_coeff == 0.0
119119 self .timeStep = step_base * (1 + (step_random_coeff * (1.0 - 2 * random .random ())))
120120 logging .info ('Created a directory lock with timeStep %s seconds.' % str (self .timeStep ))
121-
122-
121+
122+
123123 def acquire (self , acquire_by_force = True , lock_timeout = 30 ):
124124 '''Attempt to acquire the lock, with a configuration timeout value. Return True if
125125 acquired. False if acquie was forced. Raises DirectoryLockError if directory is
126126 already locked.'''
127127 if lock_timeout <= 0 :
128- logmsg = "Error Acquiring DirectoryLock: '%s' with invalid timeout of '%d' seconds" % (self .dirName , lock_timeout )
128+ logmsg = "Error Acquiring DirectoryLock: '%s' with invalid timeout of '%d' seconds" % \
129+ (self .dirName , lock_timeout )
129130 logging .error (logmsg )
130131 raise DirectoryLockError (logmsg )
131-
132+
132133 if self .isLocked == True :
133- logmsg = "Error Acquiring DirectoryLock: '%s' is already locked!" % self .dirName
134+ logmsg = "Error Acquiring DirectoryLock: '%s' is already locked!" % self .dirName
134135 logging .error (logmsg )
135136 raise DirectoryLockError (logmsg )
136-
137- wait_duration = 0
137+
138+ wait_duration = 0
138139 logging .info ("Acquiring lock" )
139-
140+
140141 while wait_duration < lock_timeout :
141142 wait_duration += self .timeStep
142143 try :
@@ -162,30 +163,30 @@ def acquire(self, acquire_by_force=True, lock_timeout=30):
162163 logmsg = "Error acquiring DirectoryLock on '%s'" % self .dirName
163164 logging .error (logmsg )
164165 raise DirectoryLockError (logmsg )
165-
166-
166+
167+
167168 def __del__ (self ):
168169 '''Automatically destroy the lock when the object is deleted.'''
169170 if self .isLocked :
170171 self .release (True )
171-
172-
172+
173+
173174 def release (self , raise_remove_error = False ):
174175 '''Release the lock. Raises DirectoryLockError if lock cannot be removed
175176 or does not exist.'''
176177 if not self .isLocked :
177178 logmsg = "Error releasing DirectoryLock: '%s' is not locked yet!" % self .dirName
178179 logging .error (logmsg )
179180 raise DirectoryLockError (logmsg )
180- self .isLocked = False
181+ self .isLocked = False
181182 try :
182183 os .rmdir (self .dirName )
183184 except os .error , err :
184185 if raise_remove_error :
185- logmsg = "Error releasing DirectoryLock: '%s' remove appeared to fail!" % self .dirName
186+ logmsg = "Error releasing DirectoryLock: '%s' remove appeared to fail!" % self .dirName
186187 logging .error (logmsg )
187188 raise DirectoryLockError (logmsg )
188-
189+
189190
190191class CacheConfigFile :
191192 '''An object representation of a config cache file. Provides some utility
@@ -197,45 +198,44 @@ def __init__(self, filename, ttl=30):
197198 randStr = '.' + hex (int (random .random ()* 256 * 256 * 256 * 256 ))[2 :10 ]
198199 self .tempFileName = filename + randStr
199200 logging .info ("CacheConfigFile created with tempFileName: %s" % self .tempFileName )
200-
201+
201202 def __del__ (self ):
202203 '''Clean up any temporary files that were created.'''
203204 if os .path .isfile (self .tempFileName ):
204205 os .remove (self .tempFileName )
205-
206+
206207 def temporaryFileName (self ):
207208 '''Return the name of a unique, temporary file we can use.'''
208209 return self .tempFileName
209-
210+
210211 def exists (self ):
211212 '''Returns True if the cache file exists on disk, otherwise False.'''
212213 return os .path .exists (self .fileName )
213-
214-
214+
215215 def shouldUpdate (self ):
216216 '''Check the cache file\' s timestamp against the TTL value for this file
217217 set when the object was created. Return True if the TTL has expired.
218218 Otherwise False.'''
219- currentTime = time .time ()
219+ currentTime = time .time ()
220220 if self .exists ():
221- lastModified = os .path .getmtime (self .fileName )
221+ lastModified = os .path .getmtime (self .fileName )
222222
223223 logging .info ("CacheConfigFile last modified: %s" % lastModified )
224- cacheAge = currentTime - lastModified
224+ cacheAge = currentTime - lastModified
225225 logging .info ("CacheConfigFile age: %s" % cacheAge )
226226 if cacheAge < float (self .fileTTL ):
227227 logging .info ("CacheConfigFile can be reused!" )
228228 return False
229229 logging .info ("CacheConfigFile should be updated!" )
230230 return True
231-
231+
232232
233233class CustomHttpHandler (urllib2 .HTTPHandler ):
234234 '''Handler helper class for dealing with URL requests.'''
235235
236236 def http_error_304 (self , req , fp , code , msg , hdrs ):
237237 return open (self .cache_file )
238-
238+
239239
240240
241241################################################################################
@@ -250,7 +250,7 @@ def writeToFile(in_fp, out_fp, error):
250250 stream named CONFIG_FILE_ERROR.'''
251251 # Track errors encountered
252252 config_lines = []
253-
253+
254254 try :
255255 if error :
256256 config_lines .append (error )
@@ -265,14 +265,14 @@ def writeToFile(in_fp, out_fp, error):
265265 skip_next = True
266266 else :
267267 config_lines .append (current_line )
268- current_line = in_fp .readline ()
269-
268+ current_line = in_fp .readline ()
269+
270270 config = '' .join (config_lines )
271271 out_fp .write (config )
272272 return config
273273 finally :
274274 in_fp .close ()
275-
275+
276276
277277
278278def downloadConfig (url , cache_file , temp_cache_file_fp , lastAttempt ):
@@ -285,7 +285,7 @@ def downloadConfig(url, cache_file, temp_cache_file_fp, lastAttempt):
285285 opener = urllib2 .build_opener (handler )
286286 opener .addheaders = [('User-agent' , 'CacheConfig/%s' % __version__ )]
287287 urllib2 .install_opener (opener )
288-
288+
289289 try :
290290 req = urllib2 .Request (url = url )
291291 if os .path .exists (cache_file ):
@@ -322,44 +322,48 @@ def main():
322322 cache_lock_timeout = int (sys .argv [3 ])
323323 cache_config_file = CacheConfigFile (cache_file_name , cache_file_timeout )
324324 config_urls = sys .argv [4 :]
325- logging .debug ("CacheFile Name: %s\n CacheFile TTL: %d\n Lock TTL: %d\n " % (cache_file_name , cache_file_timeout , cache_lock_timeout ))
325+ logging .debug ("CacheFile Name: %s\n CacheFile TTL: %d\n Lock TTL: %d\n " % \
326+ (cache_file_name , cache_file_timeout , cache_lock_timeout ))
326327 for u in config_urls :
327328 logging .debug ("Config URL: %s" % u )
328329 except :
329330 logging .error ("Error parsing arguments..." )
330331 return 1
331-
332+
332333 try :
333334 # Generate an app-specific directory name for our lock and then
334335 # attempt to get a lock on it.
335336 directoryName = cache_config_file .fileName + '_'
336337 dlock = DirectoryLock (directoryName )
337338 dlock .acquire (True , cache_lock_timeout )
338339 except DirectoryLockError , error :
339- logging .error ("Error acquiring directory lock!" )
340+ logging .error ("Error acquiring directory lock: %s" % error )
340341 pass
341-
342+
342343 config = None
343344 should_print = False
344345 error_occurred = False
345346 error_messages = []
346-
347+
347348 # Once acquired, if cachefile doesn't exist or it is beyond its time to live (TTL),
348349 # request the configuration file from the URL given. One the configuration has been
349350 # fetched withou error, write it to temporary file and then move it in to place .
350351 should_update = cache_config_file .shouldUpdate ()
351352 if should_update :
352353 url_counter = 0
353354 # Keep moving through the URLs in the list until we can pull a configuration
354- while should_print == False and url_counter < len (config_urls ):
355+ while should_print == False and url_counter < len (config_urls ):
355356 try :
356357 error_occurred = False
357- logging .info ("Opening temp cache file: %s" % cache_config_file .temporaryFileName ())
358+ logging .info ("Opening temp cache file: %s" % \
359+ cache_config_file .temporaryFileName ())
358360 temp_cache_file_fp = open (cache_config_file .temporaryFileName (), 'w' )
359361 try :
360- logging .info ("Opening URL #%d: %s" % (url_counter + 1 , config_urls [url_counter ]))
362+ logging .info ("Opening URL #%d: %s" % \
363+ (url_counter + 1 , config_urls [url_counter ]))
361364 lastAttempt = url_counter == len (config_urls ) - 1
362- config = downloadConfig (config_urls [url_counter ], cache_config_file .fileName , temp_cache_file_fp , lastAttempt )
365+ config = downloadConfig (config_urls [url_counter ], \
366+ cache_config_file .fileName , temp_cache_file_fp , lastAttempt )
363367 finally :
364368 temp_cache_file_fp .close ()
365369 logging .info ("Copying tempCacheConfig file to cacheFile" )
@@ -395,11 +399,11 @@ def main():
395399 should_print = True
396400 except :
397401 error_occurred = True
398-
402+
399403 if should_log :
400404 # If we are logging, give the user a chance to read the output.
401405 time .sleep (5 )
402-
406+
403407 if should_print and not error_occurred :
404408 print config
405409 else :
0 commit comments