<s> import time <EOL> import re <EOL> import os <EOL> import sys <EOL> import pprint <EOL> import inspect <EOL> from twisted . internet import protocol , reactor , defer <EOL> from twisted . spread import pb <EOL> from twisted . internet import abstract , fdesc , threads <EOL> from twisted . python import log , failure <EOL> from collections import defaultdict <EOL> from twisted . web . client import Agent <EOL> from twisted . web . http_headers import Headers <EOL> import urllib <EOL> from twisted . web . iweb import IBodyProducer <EOL> from zope . interface import implements <EOL> from twisted . internet . defer import succeed <EOL> class StringProducer ( object ) : <EOL> implements ( IBodyProducer ) <EOL> def __init__ ( self , body ) : <EOL> self . body = body <EOL> self . length = len ( body ) <EOL> def startProducing ( self , consumer ) : <EOL> consumer . write ( self . body ) <EOL> return succeed ( None ) <EOL> def pauseProducing ( self ) : <EOL> pass <EOL> def stopProducing ( self ) : <EOL> pass <EOL> def httpRequest ( url , values = { } , headers = { } , method = '<STR_LIT:POST>' ) : <EOL> agent = Agent ( reactor ) <EOL> data = urllib . urlencode ( values ) <EOL> d = agent . request ( method , <EOL> url , <EOL> Headers ( headers ) , <EOL> StringProducer ( data ) if data else None ) <EOL> def handle_response ( response ) : <EOL> if response . code == <NUM_LIT> : <EOL> d = defer . succeed ( '<STR_LIT>' ) <EOL> else : <EOL> class SimpleReceiver ( protocol . Protocol ) : <EOL> def __init__ ( s , d ) : <EOL> s . buf = '<STR_LIT>' ; s . d = d <EOL> def dataReceived ( s , data ) : <EOL> s . buf += data <EOL> def connectionLost ( s , reason ) : <EOL> s . d . callback ( s . buf ) <EOL> d = defer . Deferred ( ) <EOL> response . deliverBody ( SimpleReceiver ( d ) ) <EOL> return d <EOL> d . addCallback ( handle_response ) <EOL> return d <EOL> def send_email ( to , subject , body , from_addr = '<STR_LIT>' ) : <EOL> from twisted . mail . smtp import sendmail <EOL> from email . mime . text import MIMEText <EOL> host = '<STR_LIT:localhost>' <EOL> to_addrs = list ( set ( [ to , '<STR_LIT>' ] ) ) <EOL> msg = MIMEText ( body ) <EOL> msg [ '<STR_LIT>' ] = subject <EOL> msg [ '<STR_LIT>' ] = from_addr <EOL> msg [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( to_addrs ) <EOL> d = sendmail ( host , from_addr , to_addrs , msg . as_string ( ) ) <EOL> def success ( r ) : <EOL> print "<STR_LIT>" % str ( r ) <EOL> def error ( e ) : <EOL> print "<STR_LIT>" % str ( e ) <EOL> d . addCallback ( success ) <EOL> d . addErrback ( error ) <EOL> return d <EOL> def undefaulted ( x ) : <EOL> return dict ( <EOL> ( k , undefaulted ( v ) ) <EOL> for ( k , v ) in x . iteritems ( ) <EOL> ) if isinstance ( x , defaultdict ) else x <EOL> class SimpleListener ( ) : <EOL> def __init__ ( self , noisy = False ) : <EOL> self . noisy = noisy <EOL> if self . noisy : <EOL> print "<STR_LIT>" % ( time . ctime ( ) , ) <EOL> self . deferred = defer . Deferred ( ) <EOL> def startCmd ( self , cmd ) : <EOL> if self . noisy : <EOL> print "<STR_LIT>" % ( cmd , time . ctime ( ) ) <EOL> def outReceived ( self , data ) : <EOL> if self . noisy : <EOL> print "<STR_LIT>" + data . strip ( ) <EOL> def errReceived ( self , data ) : <EOL> if self . noisy : <EOL> print "<STR_LIT>" + data . strip ( ) <EOL> def processEnded ( self , status ) : <EOL> if self . noisy : <EOL> print "<STR_LIT>" % ( time . ctime ( ) , status ) <EOL> def lastProcessEnded ( self ) : <EOL> if self . noisy : <EOL> print "<STR_LIT>" % time . ctime ( ) <EOL> self . deferred . callback ( True ) <EOL> class SpecialLock : <EOL> locks = { } <EOL> run_again = { } <EOL> def handleLock ( self , lock_name , function ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % lock_name <EOL> if not self . locks . has_key ( lock_name ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> self . locks [ lock_name ] = True <EOL> return True <EOL> else : <EOL> self . run_again [ lock_name ] = function <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> return False <EOL> def removeLock ( self , lock_name ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % lock_name <EOL> if self . locks . has_key ( lock_name ) : <EOL> del self . locks [ lock_name ] <EOL> if self . run_again . has_key ( lock_name ) : <EOL> fn = self . run_again [ lock_name ] <EOL> del self . run_again [ lock_name ] <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> fn ( ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> class ExceptionLogger ( log . FileLogObserver ) : <EOL> def emit ( self , eventDict ) : <EOL> is_exception = eventDict . has_key ( '<STR_LIT>' ) <EOL> connection_error = False <EOL> if is_exception : <EOL> connection_error = '<STR_LIT>' in repr ( eventDict [ '<STR_LIT>' ] ) or '<STR_LIT>' in repr ( eventDict [ '<STR_LIT>' ] ) or '<STR_LIT>' in repr ( eventDict [ '<STR_LIT>' ] ) <EOL> other_loggable = '<STR_LIT>' in repr ( eventDict ) <EOL> if ( is_exception and not connection_error ) or other_loggable : <EOL> log . FileLogObserver . emit ( self , eventDict ) <EOL> def all_in ( phrases , list ) : <EOL> """<STR_LIT>""" <EOL> combined = '<STR_LIT>' . join ( list ) <EOL> for phrase in phrases : <EOL> if phrase not in combined : <EOL> return False <EOL> return True <EOL> def sleep ( secs , data = None ) : <EOL> d = defer . Deferred ( ) <EOL> reactor . callLater ( secs , d . callback , data ) <EOL> return d <EOL> def add_delay ( d , secs ) : <EOL> "<STR_LIT>" <EOL> def delay ( data ) : <EOL> print "<STR_LIT>" % secs <EOL> d = sleep ( secs , data ) <EOL> return d <EOL> d . addCallback ( delay ) <EOL> return d <EOL> class TooManyAttempts ( Exception ) : <EOL> pass <EOL> class BackOffUtil ( object ) : <EOL> verbose = False <EOL> def __init__ ( self , max_retries = <NUM_LIT:10> , scale = <NUM_LIT> , tag = None ) : <EOL> self . attempt = <NUM_LIT:0> <EOL> self . max_retries = max ( max_retries , <NUM_LIT:0> ) <EOL> self . scale = scale <EOL> self . tag = tag <EOL> if self . verbose : <EOL> print "<STR_LIT>" % tag <EOL> def backoff ( self , result ) : <EOL> d = defer . Deferred ( ) <EOL> if self . attempt < self . max_retries : <EOL> delay = self . scale ** self . attempt <EOL> self . attempt += <NUM_LIT:1> <EOL> if self . verbose : <EOL> if self . attempt < <NUM_LIT:2> : <EOL> if self . tag is not None : <EOL> print ( '<STR_LIT>' % ( self . tag , ) ) <EOL> print ( '<STR_LIT>' % ( delay , ) ) <EOL> else : <EOL> print ( '<STR_LIT>' % ( delay , ) ) <EOL> reactor . callLater ( delay , d . callback , result ) <EOL> else : <EOL> if self . verbose : <EOL> if self . tag is not None : <EOL> print ( '<STR_LIT>' % ( self . tag , ) ) <EOL> print ( '<STR_LIT>' % ( self . attempt + <NUM_LIT:1> , self . max_retries ) ) <EOL> d . errback ( failure . Failure ( TooManyAttempts ( '<STR_LIT>' % ( self . attempt , ) ) ) ) <EOL> return d <EOL> def is_database ( filesystem ) : <EOL> if filesystem [ : <NUM_LIT:6> ] == '<STR_LIT>' : <EOL> return filesystem [ <NUM_LIT:6> : ] <EOL> else : <EOL> return False <EOL> def is_site ( filesystem ) : <EOL> if filesystem [ : <NUM_LIT:5> ] == '<STR_LIT>' : <EOL> return filesystem [ <NUM_LIT:5> : ] <EOL> else : <EOL> return False <EOL> def is_mail ( filesystem ) : <EOL> if filesystem [ : <NUM_LIT:5> ] == '<STR_LIT>' : <EOL> return filesystem [ <NUM_LIT:5> : ] <EOL> else : <EOL> return False <EOL> def mysql_set ( n , s = "<STR_LIT:%s>" ) : <EOL> return "<STR_LIT:(>" + ( "<STR_LIT:U+002C>" . join ( [ "<STR_LIT:%s>" for i in range ( n ) ] ) ) + "<STR_LIT:)>" <EOL> def format_datetime ( x ) : <EOL> return time . strftime ( '<STR_LIT>' , time . gmtime ( float ( x ) ) ) <EOL> def format_time ( x ) : <EOL> return time . strftime ( '<STR_LIT>' , time . gmtime ( float ( x ) ) ) <EOL> def clearLogs ( type ) : <EOL> os . system ( "<STR_LIT>" % type ) <EOL> def email_error ( subject , body ) : <EOL> print "<STR_LIT>" + body <EOL> class SelectableFile ( abstract . FileDescriptor ) : <EOL> def __init__ ( self , fname , protocol ) : <EOL> abstract . FileDescriptor . __init__ ( self ) <EOL> self . fname = fname <EOL> self . protocol = protocol <EOL> self . _openFile ( ) <EOL> def _openFile ( self ) : <EOL> self . fp = open ( self . fname , '<STR_LIT>' ) <EOL> self . fileno = self . fp . fileno <EOL> fdesc . setNonBlocking ( self . fp ) <EOL> self . protocol . makeConnection ( self ) <EOL> def doRead ( self ) : <EOL> buf = self . fp . read ( <NUM_LIT> ) <EOL> if buf : <EOL> self . protocol . dataReceived ( buf ) <EOL> else : <EOL> print "<STR_LIT>" % self . fname <EOL> def write ( self , data ) : <EOL> pass <EOL> def loseConnection ( self ) : <EOL> self . fp . close ( ) <EOL> class BaseUITalker ( pb . Root ) : <EOL> def remote_setRemoteUIConnection ( self , ui_connection ) : <EOL> def callback ( conn ) : <EOL> self . ui_connections . remove ( conn ) <EOL> ui_connection . notifyOnDisconnect ( callback ) <EOL> self . ui_connections . append ( ui_connection ) <EOL> class ConnectionDispatcher : <EOL> def __init__ ( s , context ) : <EOL> s . context = context <EOL> def callRemote ( s , * args , ** kw ) : <EOL> d = DeferredDispatcher ( s . context ) <EOL> for conn in s . context . ui_connections : <EOL> d . deferreds . append ( conn . callRemote ( * args , ** kw ) ) <EOL> return d <EOL> class DeferredDispatcher : <EOL> def __init__ ( s , context ) : <EOL> s . context = context <EOL> s . deferreds = [ ] <EOL> def addCallback ( s , * args , ** kw ) : <EOL> d = DeferredDispatcher ( s . context ) <EOL> for deferred in s . deferreds : <EOL> d . deferreds . append ( deferred . addCallback ( * args , ** kw ) ) <EOL> return d <EOL> def addErrback ( s , * args , ** kw ) : <EOL> d = DeferredDispatcher ( s . context ) <EOL> for deferred in s . deferreds : <EOL> d . deferreds . append ( deferred . addErrback ( * args , ** kw ) ) <EOL> return d <EOL> def mktup ( item ) : <EOL> if type ( item ) == tuple : <EOL> return item <EOL> else : <EOL> return ( item , ) <EOL> def run_return ( cmd ) : <EOL> import popen2 <EOL> pipe = popen2 . Popen4 ( cmd ) <EOL> ret = ( pipe . fromchild . readlines ( ) , pipe . poll ( ) ) <EOL> pipe . fromchild . close ( ) <EOL> pipe . tochild . close ( ) <EOL> return ret <EOL> def lookup_ip_mapping_in_config ( my_ip ) : <EOL> import platform <EOL> config = read_config ( ) <EOL> node = platform . node ( ) <EOL> if '<STR_LIT:.>' in node : <EOL> node = node . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> return config [ '<STR_LIT>' ] [ node ] <EOL> def read_config ( ) : <EOL> import yaml <EOL> return yaml . load ( open ( '<STR_LIT>' , '<STR_LIT:r>' ) . read ( ) ) <EOL> def get_my_ip ( get_local = False ) : <EOL> my_ip = False <EOL> for line in run_return ( "<STR_LIT>" ) [ <NUM_LIT:0> ] : <EOL> m = re . match ( '<STR_LIT>' , line ) <EOL> if m is not None and m . group ( <NUM_LIT:2> ) != '<STR_LIT:127.0.0.1>' and '<STR_LIT>' not in m . group ( <NUM_LIT:2> ) : <EOL> my_ip = m . group ( <NUM_LIT:2> ) <EOL> break <EOL> if my_ip : <EOL> if my_ip . startswith ( '<STR_LIT>' ) and not get_local : <EOL> try : <EOL> my_ip = lookup_ip_mapping_in_config ( my_ip ) <EOL> except ( KeyError , IOError ) : <EOL> print "<STR_LIT>" <EOL> pass <EOL> return my_ip <EOL> else : <EOL> return '<STR_LIT>' <EOL> import platform <EOL> if platform . win32_ver ( ) [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> ip = get_my_ip ( ) <EOL> local_ip = get_my_ip ( get_local = True ) <EOL> def ip_to_alpha ( ip ) : <EOL> alpha = map ( lambda x : str ( x ) , range ( <NUM_LIT> ) ) <EOL> lastdigit = alpha [ ( int ( re . search ( "<STR_LIT>" , ip ) . group ( <NUM_LIT:1> ) ) % <NUM_LIT:100> ) % len ( alpha ) ] <EOL> return lastdigit <EOL> class curry : <EOL> def __init__ ( self , fun , * args , ** kwargs ) : <EOL> self . fun = fun <EOL> self . pending = args [ : ] <EOL> self . kwargs = kwargs . copy ( ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if kwargs and self . kwargs : <EOL> kw = self . kwargs . copy ( ) <EOL> kw . update ( kwargs ) <EOL> else : <EOL> kw = kwargs or self . kwargs <EOL> return self . fun ( * ( self . pending + args ) , ** kw ) <EOL> class CallbackProcessProtocol ( protocol . ProcessProtocol ) : <EOL> collector = '<STR_LIT>' <EOL> def __init__ ( self , viewer = None ) : <EOL> self . viewer = viewer <EOL> def outReceived ( self , data ) : <EOL> if self . viewer : <EOL> self . viewer . outReceived ( data ) <EOL> self . collector += data <EOL> def errReceived ( self , data ) : <EOL> if self . viewer : <EOL> self . viewer . errReceived ( data ) <EOL> self . collector += data <EOL> def processEnded ( self , status ) : <EOL> if self . viewer : <EOL> self . viewer . processEnded ( status ) <EOL> self . callback ( status , self . collector ) <EOL> class AlreadyQueuedError ( Exception ) : <EOL> pass <EOL> class SlotManager : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_slots = <NUM_LIT:4> , ui = None , target = '<STR_LIT>' , timeout = None ) : <EOL> self . n_slots = n_slots <EOL> self . queue = [ ] <EOL> self . slots = { } <EOL> self . ui = ui <EOL> self . target = target <EOL> self . timeout = timeout <EOL> for x in range ( n_slots ) : <EOL> self . slots [ x ] = None <EOL> def checkQueue ( self ) : <EOL> if not None in self . slots . values ( ) : <EOL> return <EOL> negative_new_queue = [ ] <EOL> for task in self . queue : <EOL> running_tags = [ t . tag for t in filter ( lambda x : x is not None , self . slots . values ( ) ) ] <EOL> if task [ '<STR_LIT>' ] is None or task [ '<STR_LIT>' ] not in running_tags : <EOL> for n , slot in self . slots . items ( ) : <EOL> if slot is None : <EOL> self . startTask ( task , n ) <EOL> negative_new_queue . append ( task ) <EOL> break <EOL> else : <EOL> print "<STR_LIT>" + task [ '<STR_LIT>' ] + "<STR_LIT>" <EOL> self . queue = [ x for x in self . queue if x not in negative_new_queue ] <EOL> """<STR_LIT>""" <EOL> def callback ( ) : <EOL> pass <EOL> def startTask ( self , task , n ) : <EOL> """<STR_LIT>""" <EOL> task_callback = task [ '<STR_LIT>' ] <EOL> print "<STR_LIT>" % ( task [ '<STR_LIT:description>' ] ) <EOL> def custom_callback ( statuses , outputs = [ ] , parameters = { } ) : <EOL> if self . ui is not None : <EOL> try : <EOL> self . ui . callRemote ( '<STR_LIT>' , item = ( task [ '<STR_LIT:id>' ] , ip , task [ '<STR_LIT:description>' ] ) , column = <NUM_LIT:3> , type = self . target , value = "<STR_LIT>" + str ( statuses ) , outputs = outputs ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" + str ( e ) <EOL> if task_callback is not None : <EOL> args_for_callback = { } <EOL> if '<STR_LIT>' in inspect . getargspec ( task_callback ) . args : <EOL> args_for_callback [ '<STR_LIT>' ] = parameters <EOL> if '<STR_LIT>' in inspect . getargspec ( task_callback ) . args : <EOL> args_for_callback [ '<STR_LIT>' ] = outputs <EOL> if '<STR_LIT>' in inspect . getargspec ( task_callback ) . args : <EOL> args_for_callback [ '<STR_LIT>' ] = task [ '<STR_LIT>' ] <EOL> task_callback ( statuses , ** args_for_callback ) <EOL> self . slots [ n ] = None <EOL> try : <EOL> self . checkQueue ( ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" + str ( e ) <EOL> p = { "<STR_LIT>" : str ( time . time ( ) ) } <EOL> if self . ui is not None : <EOL> try : <EOL> self . ui . callRemote ( '<STR_LIT>' , item = ( task [ '<STR_LIT:id>' ] , ip , task [ '<STR_LIT:description>' ] ) , column = <NUM_LIT:3> , type = '<STR_LIT>' , value = "<STR_LIT>" + str ( n ) ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" + str ( e ) <EOL> for i , cmd in enumerate ( task [ '<STR_LIT>' ] ) : <EOL> if type ( cmd ) == str or type ( cmd ) == unicode : <EOL> task [ '<STR_LIT>' ] [ i ] = task [ '<STR_LIT>' ] [ i ] % p <EOL> print "<STR_LIT:!>" * <NUM_LIT> <EOL> pprint . pprint ( task ) <EOL> print "<STR_LIT:!>" * <NUM_LIT> <EOL> self . slots [ n ] = AsyncExecCmds ( <EOL> task [ '<STR_LIT>' ] , <EOL> callback = curry ( custom_callback , parameters = p ) , <EOL> verbose = task [ '<STR_LIT>' ] , <EOL> ui = self . ui , <EOL> description = task [ '<STR_LIT:description>' ] , <EOL> tag = task [ '<STR_LIT>' ] , <EOL> id = task [ '<STR_LIT:id>' ] ) <EOL> def queueTask ( self , cmds , callback = None , verbose = <NUM_LIT:0> , ui = None , description = '<STR_LIT>' , tag = None , id = None , skip_uniqueness_check = False , errback = None ) : <EOL> if not skip_uniqueness_check : <EOL> for task in self . queue : <EOL> if task [ '<STR_LIT>' ] == cmds : <EOL> print "<STR_LIT>" + str ( cmds ) <EOL> if errback is not None : <EOL> errback ( ) <EOL> return False <EOL> """<STR_LIT>""" <EOL> id = int ( long ( time . time ( ) * <NUM_LIT:1000> ) % <NUM_LIT> ) <EOL> self . queue . append ( { '<STR_LIT>' : cmds , '<STR_LIT>' : callback , '<STR_LIT>' : verbose , '<STR_LIT:description>' : description , '<STR_LIT>' : tag , '<STR_LIT:id>' : id } ) <EOL> if self . ui is not None : <EOL> def callback ( * args ) : <EOL> self . ui . callRemote ( '<STR_LIT>' , item = ( id , ip , description ) , column = <NUM_LIT:3> , type = '<STR_LIT>' , value = "<STR_LIT>" ) <EOL> try : <EOL> self . ui . callRemote ( '<STR_LIT>' , { ( id , ip , description ) : map ( lambda cmd : ( id , '<STR_LIT>' , cmd ) , cmds ) } ) . addCallback ( callback ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" + str ( e ) + "<STR_LIT>" <EOL> self . checkQueue ( ) <EOL> class AsyncExecCmds : <EOL> def __init__ ( self , cmds , callback = None , verbose = <NUM_LIT:0> , ui = None , description = '<STR_LIT>' , tag = None , id = None , auto_run = True , cmd_prefix = None , viewer = None ) : <EOL> "<STR_LIT>" <EOL> self . cmds = cmds <EOL> self . callback = callback <EOL> self . statuses = [ ] <EOL> self . outputs = [ ] <EOL> self . verbose = verbose <EOL> self . ui = ui <EOL> self . description = description <EOL> self . tag = tag <EOL> self . id = id <EOL> self . cmd_prefix = cmd_prefix <EOL> self . viewer = viewer <EOL> if auto_run : <EOL> self . runNextCmd ( ) <EOL> def getDeferred ( self ) : <EOL> "<STR_LIT>" <EOL> d = defer . Deferred ( ) <EOL> def callback ( statuses , outputs ) : <EOL> try : <EOL> d . callback ( ( statuses , outputs ) ) <EOL> except failure . NoCurrentExceptionError , e : <EOL> print "<STR_LIT:*>" * <NUM_LIT> <EOL> print "<STR_LIT>" <EOL> print statuses , outputs <EOL> print "<STR_LIT:*>" * <NUM_LIT> <EOL> print e <EOL> print "<STR_LIT:*>" * <NUM_LIT> <EOL> self . callback = callback <EOL> return d <EOL> def deferredOutputLines ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> def callback ( statuses , outputs ) : <EOL> d . callback ( [ x . strip ( ) . split ( '<STR_LIT:\n>' ) for x in outputs ] ) <EOL> self . callback = callback <EOL> return d <EOL> def runNextCmd ( self ) : <EOL> "<STR_LIT>" <EOL> if self . cmds == [ ] : <EOL> if self . viewer : <EOL> self . viewer . lastProcessEnded ( ) <EOL> def to_string ( status ) : <EOL> if type ( status ) in [ int , str , unicode , bool ] : <EOL> return status <EOL> if "<STR_LIT>" in repr ( status ) : <EOL> return "<STR_LIT>" <EOL> if "<STR_LIT>" in repr ( status ) : <EOL> return "<STR_LIT>" <EOL> return repr ( status ) <EOL> if self . callback is not None : <EOL> self . callback ( map ( to_string , self . statuses ) , self . outputs ) <EOL> return <EOL> cmd = self . cmds . pop ( <NUM_LIT:0> ) <EOL> if self . viewer : <EOL> self . viewer . startCmd ( cmd ) <EOL> cpp = CallbackProcessProtocol ( self . viewer ) <EOL> def cmd_callback ( status , output ) : <EOL> self . statuses . append ( status ) <EOL> self . outputs . append ( output ) <EOL> self . runNextCmd ( ) <EOL> cpp . callback = cmd_callback <EOL> if type ( cmd ) in [ str , unicode ] : <EOL> if cmd [ <NUM_LIT:0> : <NUM_LIT:16> ] in [ '<STR_LIT>' ] : <EOL> prefix = '<STR_LIT>' <EOL> else : <EOL> prefix = '<STR_LIT>' <EOL> if self . cmd_prefix is not None : <EOL> prefix = self . cmd_prefix <EOL> if self . verbose >= <NUM_LIT:0> : <EOL> print "<STR_LIT>" + prefix + cmd <EOL> reactor . callLater ( <NUM_LIT:0> , reactor . spawnProcess , cpp , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:-c>' , ( prefix + cmd ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) ] ) <EOL> else : <EOL> def generic_callback ( x = '<STR_LIT>' ) : <EOL> cmd_callback ( x ) <EOL> returns_deferred = False <EOL> if hasattr ( cmd , '<STR_LIT>' ) : <EOL> returns_deferred = cmd . returns_deferred <EOL> if returns_deferred : <EOL> d = cmd ( ) <EOL> else : <EOL> d = threads . deferToThread ( cmd ) <EOL> d . addCallback ( generic_callback ) <EOL> def run ( self ) : <EOL> self . runNextCmd ( ) <EOL> class AsyncExecCmd ( AsyncExecCmds ) : <EOL> def __init__ ( self , cmd , callback = None , verbose = <NUM_LIT:0> , ui = None , description = '<STR_LIT>' ) : <EOL> AsyncExecCmds . __init__ ( self , [ cmd ] , callback , verbose , ui , description ) <EOL> """<STR_LIT>""" <EOL> def ntp ( ) : <EOL> import struct , telnetlib , time , socket <EOL> EPOCH = <NUM_LIT> L <EOL> try : <EOL> fromNTP = telnetlib . Telnet ( '<STR_LIT>' , <NUM_LIT> ) . read_all ( ) <EOL> remoteTime = struct . unpack ( '<STR_LIT>' , fromNTP ) [ <NUM_LIT:0> ] - EPOCH <EOL> return time . gmtime ( remoteTime ) <EOL> except socket . gaierror : <EOL> print "<STR_LIT>" <EOL> return False <EOL> def nice_time ( t ) : <EOL> """<STR_LIT>""" <EOL> return time . ctime ( t ) + "<STR_LIT:U+0020>" + str ( t ) <EOL> def shellquote ( s ) : <EOL> return "<STR_LIT:'>" + s . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) + "<STR_LIT:'>" <EOL> @ defer . inlineCallbacks <EOL> def safe_write ( file , contents , perms = None ) : <EOL> tmpname = '<STR_LIT>' + file . replace ( '<STR_LIT:/>' , '<STR_LIT:%>' ) + "<STR_LIT>" <EOL> fp = open ( tmpname , "<STR_LIT:w>" ) <EOL> fp . write ( contents ) <EOL> fp . close ( ) <EOL> yield AsyncExecCmds ( [ "<STR_LIT>" % ( shellquote ( tmpname ) , shellquote ( file ) ) ] ) . getDeferred ( ) <EOL> if perms is not None : <EOL> yield AsyncExecCmds ( [ "<STR_LIT>" % ( perms , shellquote ( file ) ) ] ) . getDeferred ( ) <EOL> def hex2ip ( sender ) : <EOL> _ , hex , priv = sender . split ( '<STR_LIT:#>' ) <EOL> quads = [ hex [ <NUM_LIT:0> : <NUM_LIT:2> ] , hex [ <NUM_LIT:2> : <NUM_LIT:4> ] , hex [ <NUM_LIT:4> : <NUM_LIT:6> ] , hex [ <NUM_LIT:6> : <NUM_LIT:8> ] ] <EOL> ip = '<STR_LIT:.>' . join ( map ( lambda x : str ( int ( x , <NUM_LIT:16> ) ) , quads ) ) <EOL> return ip <EOL> def natsort ( list_ ) : <EOL> try : <EOL> tmp = [ ( int ( re . search ( '<STR_LIT>' , i ) . group ( <NUM_LIT:0> ) ) , i ) for i in list_ ] <EOL> tmp . sort ( ) <EOL> return [ i [ <NUM_LIT:1> ] for i in tmp ] <EOL> except AttributeError : <EOL> list_ . sort ( ) <EOL> return list_ <EOL> def setproctitle ( name ) : <EOL> try : <EOL> from ctypes import cdll , byref , create_string_buffer <EOL> libc = cdll . LoadLibrary ( '<STR_LIT>' ) <EOL> libc . setproctitle ( name + '<STR_LIT>' ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> def parse_zfs_list ( data , zpool ) : <EOL> zfs_data = { } <EOL> for line in data . split ( '<STR_LIT:\n>' ) : <EOL> match = re . match ( zpool + '<STR_LIT>' , line . strip ( ) ) <EOL> if match is not None : <EOL> site , snapshot = match . groups ( ) <EOL> if site not in zfs_data . keys ( ) : <EOL> zfs_data [ site ] = [ ] <EOL> if snapshot != '<STR_LIT>' : <EOL> zfs_data [ site ] . append ( snapshot ) <EOL> return zfs_data <EOL> def remote_prefix ( username , ip_to , sudo = '<STR_LIT>' ) : <EOL> return '<STR_LIT>' + username + '<STR_LIT:@>' + ip_to + '<STR_LIT>' + sudo + '<STR_LIT:U+0020>' <EOL> class Spinner : <EOL> def __init__ ( self , type = <NUM_LIT:0> ) : <EOL> if type == <NUM_LIT:0> : <EOL> self . char = [ '<STR_LIT:.>' , '<STR_LIT:o>' , '<STR_LIT:O>' , '<STR_LIT:o>' ] <EOL> else : <EOL> self . char = [ '<STR_LIT:|>' , '<STR_LIT:/>' , '<STR_LIT:->' , '<STR_LIT:\\>' , '<STR_LIT:->' ] <EOL> self . len = len ( self . char ) <EOL> self . curr = <NUM_LIT:0> <EOL> def Get ( self ) : <EOL> self . curr = ( self . curr + <NUM_LIT:1> ) % self . len <EOL> str = self . char [ self . curr ] <EOL> return str <EOL> def Print ( self ) : <EOL> self . curr = ( self . curr + <NUM_LIT:1> ) % self . len <EOL> str = self . char [ self . curr ] <EOL> sys . stdout . write ( "<STR_LIT>" % str ) <EOL> sys . stdout . flush ( ) <EOL> def Done ( self ) : <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> sys . stdout . flush ( ) </s>
