<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import time <EOL> import threading <EOL> import Bcfg2 . Options <EOL> from Bcfg2 . Reporting . Transport . base import TransportBase , TransportError <EOL> from Bcfg2 . Compat import Queue , Full , Empty , cPickle <EOL> class DirectStore ( TransportBase , threading . Thread ) : <EOL> options = TransportBase . options + [ Bcfg2 . Options . Common . reporting_storage ] <EOL> def __init__ ( self ) : <EOL> TransportBase . __init__ ( self ) <EOL> threading . Thread . __init__ ( self ) <EOL> self . save_file = os . path . join ( self . data , "<STR_LIT>" ) <EOL> self . storage = Bcfg2 . Options . setup . reporting_storage ( ) <EOL> self . storage . validate ( ) <EOL> self . queue = Queue ( <NUM_LIT> ) <EOL> self . terminate = threading . Event ( ) <EOL> self . debug_log ( "<STR_LIT>" % <EOL> self . __class__ . __name__ ) <EOL> self . start ( ) <EOL> def shutdown ( self ) : <EOL> self . terminate . set ( ) <EOL> def store ( self , hostname , metadata , stats ) : <EOL> try : <EOL> self . queue . put_nowait ( dict ( hostname = hostname , <EOL> metadata = metadata , <EOL> stats = stats ) ) <EOL> except Full : <EOL> self . logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def run ( self ) : <EOL> if not self . _load ( ) : <EOL> self . logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> while not self . terminate . isSet ( ) and self . queue is not None : <EOL> try : <EOL> interaction = self . queue . get ( block = True , <EOL> timeout = self . timeout ) <EOL> start = time . time ( ) <EOL> self . storage . import_interaction ( interaction ) <EOL> self . logger . info ( "<STR_LIT>" % <EOL> ( interaction . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> time . time ( ) - start ) ) <EOL> except Empty : <EOL> self . debug_log ( "<STR_LIT>" ) <EOL> continue <EOL> except : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . logger . error ( "<STR_LIT>" <EOL> % err ) <EOL> continue <EOL> self . debug_log ( "<STR_LIT>" % <EOL> self . __class__ . __name__ ) <EOL> if self . queue is not None and not self . queue . empty ( ) : <EOL> self . _save ( ) <EOL> def fetch ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def start_monitor ( self , collector ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def rpc ( self , method , * args , ** kwargs ) : <EOL> try : <EOL> return getattr ( self . storage , method ) ( * args , ** kwargs ) <EOL> except : <EOL> msg = "<STR_LIT>" % ( method , <EOL> sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> self . logger . error ( msg ) <EOL> raise TransportError ( msg ) <EOL> def _save ( self ) : <EOL> """<STR_LIT>""" <EOL> self . debug_log ( "<STR_LIT>" % <EOL> self . save_file ) <EOL> saved_data = [ ] <EOL> try : <EOL> while not self . queue . empty ( ) : <EOL> saved_data . append ( self . queue . get_nowait ( ) ) <EOL> except Empty : <EOL> pass <EOL> try : <EOL> savefile = open ( self . save_file , '<STR_LIT:w>' ) <EOL> cPickle . dump ( saved_data , savefile ) <EOL> savefile . close ( ) <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> except ( IOError , TypeError ) : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . logger . warning ( "<STR_LIT>" % err ) <EOL> def _load ( self ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( self . save_file ) : <EOL> self . debug_log ( "<STR_LIT>" ) <EOL> return True <EOL> saved_data = [ ] <EOL> try : <EOL> savefile = open ( self . save_file , '<STR_LIT:r>' ) <EOL> saved_data = cPickle . load ( savefile ) <EOL> savefile . close ( ) <EOL> except ( IOError , cPickle . UnpicklingError ) : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . logger . warning ( "<STR_LIT>" % err ) <EOL> return False <EOL> for interaction in saved_data : <EOL> if self . terminate . isSet ( ) : <EOL> self . logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return False <EOL> try : <EOL> self . queue . put_nowait ( interaction ) <EOL> except Full : <EOL> self . logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> break <EOL> try : <EOL> os . unlink ( self . save_file ) <EOL> except OSError : <EOL> self . logger . error ( "<STR_LIT>" % <EOL> self . save_file ) <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> return True </s>
