<s> """<STR_LIT>""" <EOL> import sys , os , string , datetime <EOL> import types , re <EOL> from contextlib import contextmanager <EOL> class P4Exception ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return str ( self . value ) <EOL> class P4Error ( P4Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , errors ) : <EOL> P4Exception . __init__ ( self , value ) <EOL> self . errors = errors <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + str ( self . value ) + "<STR_LIT:U+0020>" + str ( self . errors ) <EOL> class P4Warning ( P4Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , warnings ) : <EOL> P4Exception . __init__ ( self , value ) <EOL> self . warnings = warnings <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + str ( self . value ) + "<STR_LIT:U+0020>" + str ( self . warnings ) <EOL> class Spec ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fieldmap = None ) : <EOL> self . __dict__ [ '<STR_LIT>' ] = fieldmap <EOL> def permitted_fields ( self ) : <EOL> return self . __fields <EOL> def __setitem__ ( self , key , value ) : <EOL> if key in self or self . __fields == None : <EOL> dict . __setitem__ ( self , key , value ) <EOL> elif str ( key ) . lower ( ) in self . __fields : <EOL> dict . __setitem__ ( self , self . __fields [ key . lower ( ) ] , value ) <EOL> else : <EOL> raise P4Error , ( "<STR_LIT>" + str ( key ) , [ ] ) <EOL> def __getattr__ ( self , attr ) : <EOL> key = str ( attr ) . lower ( ) <EOL> if key [ <NUM_LIT:0> ] != '<STR_LIT:_>' : <EOL> raise AttributeError , attr <EOL> key = key [ <NUM_LIT:1> : ] <EOL> if key in self : <EOL> return self [ key ] <EOL> elif key in self . __fields : <EOL> return self [ self . __fields [ key ] ] <EOL> def __setattr__ ( self , attr , value ) : <EOL> key = str ( attr ) . lower ( ) <EOL> if key [ <NUM_LIT:0> ] != '<STR_LIT:_>' : <EOL> raise AttributeError , attr <EOL> key = key [ <NUM_LIT:1> : ] <EOL> self [ key ] = value <EOL> class Integration : <EOL> def __init__ ( self , how , file , srev , erev ) : <EOL> self . how = how <EOL> self . file = file <EOL> self . srev = srev <EOL> self . erev = erev <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . how , self . file , self . srev , self . erev ) <EOL> class Revision : <EOL> def __init__ ( self , depotFile ) : <EOL> self . depotFile = depotFile <EOL> self . integrations = [ ] <EOL> self . rev = None <EOL> self . change = None <EOL> self . action = None <EOL> self . type = None <EOL> self . time = None <EOL> self . user = None <EOL> self . client = None <EOL> self . desc = None <EOL> self . digest = None <EOL> self . fileSize = None <EOL> def integration ( self , how , file , srev , erev ) : <EOL> rec = Integration ( how , file , srev , erev ) <EOL> self . integrations . append ( rec ) <EOL> return rec <EOL> def each_integration ( self ) : <EOL> for i in self . integrations : <EOL> yield i <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . depotFile , self . rev , self . change , self . action , self . type , self . time , self . user , self . client ) <EOL> class DepotFile : <EOL> def __init__ ( self , name ) : <EOL> self . depotFile = name <EOL> self . revisions = [ ] <EOL> def new_revision ( self ) : <EOL> r = Revision ( self . depotFile ) <EOL> self . revisions . append ( r ) <EOL> return r <EOL> def each_revision ( self ) : <EOL> for r in self . revisions : <EOL> yield r <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . depotFile , len ( self . revisions ) ) <EOL> class Resolver : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def resolve ( self , mergeInfo ) : <EOL> if mergeInfo . merge_hint == "<STR_LIT:e>" : <EOL> print "<STR_LIT>" <EOL> return "<STR_LIT:s>" <EOL> else : <EOL> return mergeInfo . merge_hint <EOL> import P4API <EOL> class P4 ( P4API . P4Adapter ) : <EOL> """<STR_LIT>""" <EOL> RAISE_ALL = <NUM_LIT:2> <EOL> RAISE_ERROR = <NUM_LIT:1> <EOL> RAISE_NONE = <NUM_LIT:0> <EOL> def __init__ ( self , * args , ** kwlist ) : <EOL> P4API . P4Adapter . __init__ ( self , * args , ** kwlist ) <EOL> def __del__ ( self ) : <EOL> if self . debug > <NUM_LIT:3> : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> def __getattr__ ( self , name ) : <EOL> if name . startswith ( "<STR_LIT>" ) : <EOL> cmd = name [ len ( "<STR_LIT>" ) : ] <EOL> return lambda * args : self . run ( cmd , * args ) <EOL> elif name . startswith ( "<STR_LIT>" ) : <EOL> cmd = name [ len ( "<STR_LIT>" ) : ] <EOL> return lambda * args : self . run ( cmd , "<STR_LIT>" , * args ) <EOL> elif name . startswith ( "<STR_LIT>" ) : <EOL> cmd = name [ len ( "<STR_LIT>" ) : ] <EOL> return lambda * args : self . run ( cmd , "<STR_LIT>" , * args ) [ <NUM_LIT:0> ] <EOL> elif name . startswith ( "<STR_LIT>" ) : <EOL> cmd = name [ len ( "<STR_LIT>" ) : ] <EOL> return lambda * args : self . __save ( cmd , * args ) <EOL> elif name . startswith ( "<STR_LIT>" ) : <EOL> cmd = name [ len ( "<STR_LIT>" ) : ] <EOL> return lambda * args : self . parse_spec ( cmd , * args ) <EOL> elif name . startswith ( "<STR_LIT>" ) : <EOL> cmd = name [ len ( "<STR_LIT>" ) : ] <EOL> return lambda * args : self . format_spec ( cmd , * args ) <EOL> else : <EOL> raise AttributeError , name <EOL> def __save ( self , cmd , * args ) : <EOL> self . input = args [ <NUM_LIT:0> ] <EOL> return self . run ( cmd , "<STR_LIT>" , args [ <NUM_LIT:1> : ] ) <EOL> def __repr__ ( self ) : <EOL> state = "<STR_LIT>" <EOL> if self . connected ( ) : <EOL> state = "<STR_LIT>" <EOL> return "<STR_LIT>" % ( self . user , self . client , self . port , state ) <EOL> def identify ( cls ) : <EOL> return P4API . identify ( ) <EOL> identify = classmethod ( identify ) <EOL> def run ( self , * args ) : <EOL> "<STR_LIT>" <EOL> return P4API . P4Adapter . run ( self , * self . __flatten ( args ) ) <EOL> def run_submit ( self , * args ) : <EOL> "<STR_LIT>" <EOL> nargs = list ( args ) <EOL> form = None <EOL> for n , arg in enumerate ( nargs ) : <EOL> if isinstance ( arg , dict ) : <EOL> self . input = arg <EOL> nargs . pop ( n ) <EOL> nargs . append ( "<STR_LIT>" ) <EOL> break <EOL> return self . run ( "<STR_LIT>" , * nargs ) <EOL> def run_login ( self , * args ) : <EOL> "<STR_LIT>" <EOL> self . input = self . password <EOL> return self . run ( "<STR_LIT>" , * args ) <EOL> def run_password ( self , oldpass , newpass ) : <EOL> "<STR_LIT>" <EOL> if ( oldpass and len ( oldpass ) > <NUM_LIT:0> ) : <EOL> self . input = [ oldpass , newpass , newpass ] <EOL> else : <EOL> self . input = [ newpass , newpass ] <EOL> return self . run ( "<STR_LIT:password>" ) <EOL> def run_filelog ( self , * args ) : <EOL> raw = self . run ( '<STR_LIT>' , args ) <EOL> if ( not self . tagged ) : <EOL> return raw <EOL> result = [ ] <EOL> for h in raw : <EOL> r = None <EOL> if isinstance ( h , dict ) : <EOL> df = DepotFile ( h [ "<STR_LIT>" ] ) <EOL> for n , rev in enumerate ( h [ "<STR_LIT>" ] ) : <EOL> r = df . new_revision ( ) <EOL> r . rev = int ( rev ) <EOL> r . change = int ( h [ "<STR_LIT>" ] [ n ] ) <EOL> r . action = h [ "<STR_LIT:action>" ] [ n ] <EOL> r . type = h [ "<STR_LIT:type>" ] [ n ] <EOL> r . time = datetime . datetime . utcfromtimestamp ( int ( h [ "<STR_LIT:time>" ] [ n ] ) ) <EOL> r . user = h [ "<STR_LIT:user>" ] [ n ] <EOL> r . client = h [ "<STR_LIT>" ] [ n ] <EOL> r . desc = h [ "<STR_LIT>" ] [ n ] <EOL> if "<STR_LIT>" in h : <EOL> r . digest = h [ "<STR_LIT>" ] [ n ] <EOL> if "<STR_LIT>" in h : <EOL> r . fileSize = h [ "<STR_LIT>" ] [ n ] <EOL> if ( not "<STR_LIT>" in h ) or ( n >= len ( h [ "<STR_LIT>" ] ) or h [ "<STR_LIT>" ] [ n ] == None ) : <EOL> continue <EOL> else : <EOL> for m , how in enumerate ( h [ "<STR_LIT>" ] [ n ] ) : <EOL> file = h [ "<STR_LIT:file>" ] [ n ] [ m ] <EOL> srev = string . lstrip ( h [ "<STR_LIT>" ] [ n ] [ m ] , '<STR_LIT:#>' ) <EOL> erev = string . lstrip ( h [ "<STR_LIT>" ] [ n ] [ m ] , '<STR_LIT:#>' ) <EOL> if srev == "<STR_LIT:none>" : <EOL> srev = <NUM_LIT:0> <EOL> else : <EOL> srev = int ( srev ) <EOL> if erev == "<STR_LIT:none>" : <EOL> erev = <NUM_LIT:0> <EOL> else : <EOL> erev = int ( erev ) <EOL> r . integration ( how , file , srev , erev ) <EOL> else : <EOL> r = h <EOL> result . append ( df ) <EOL> return result <EOL> def run_print ( self , * args ) : <EOL> raw = self . run ( '<STR_LIT>' , args ) <EOL> result = [ raw . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ] <EOL> for line in raw : <EOL> result [ - <NUM_LIT:1> ] += line <EOL> return result <EOL> def run_resolve ( self , * args , ** kargs ) : <EOL> myResolver = Resolver ( ) <EOL> if "<STR_LIT>" in kargs : <EOL> myResolver = kargs [ "<STR_LIT>" ] <EOL> savedResolver = self . resolver <EOL> self . resolver = myResolver <EOL> result = self . run ( "<STR_LIT>" , args ) <EOL> self . resolver = savedResolver <EOL> return result <EOL> def __flatten ( self , args ) : <EOL> result = [ ] <EOL> if isinstance ( args , tuple ) or isinstance ( args , list ) : <EOL> for i in args : <EOL> result . extend ( self . __flatten ( i ) ) <EOL> else : <EOL> result . append ( args ) <EOL> return tuple ( result ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if self . connected ( ) : <EOL> self . disconnect ( ) <EOL> return True <EOL> def connect ( self ) : <EOL> P4API . P4Adapter . connect ( self ) <EOL> return self <EOL> @ contextmanager <EOL> def while_tagged ( self , t ) : <EOL> old = self . tagged <EOL> self . tagged = t <EOL> yield <EOL> self . tagged = old <EOL> @ contextmanager <EOL> def at_exception_level ( self , e ) : <EOL> old = self . exception_level <EOL> self . exception_level = e <EOL> yield <EOL> self . exception_level = old <EOL> @ contextmanager <EOL> def saved_context ( self , ** kargs ) : <EOL> """<STR_LIT>""" <EOL> saved_context = { } <EOL> for attr in self . __members__ : <EOL> saved_context [ attr ] = getattr ( self , attr ) <EOL> for ( k , v ) in kargs . items ( ) : <EOL> setattr ( self , k , v ) <EOL> yield <EOL> for ( k , v ) in saved_context . items ( ) : <EOL> try : <EOL> setattr ( self , k , v ) <EOL> except AttributeError : <EOL> pass <EOL> class Map ( P4API . P4Map ) : <EOL> def __init__ ( self , * args ) : <EOL> P4API . P4Map . __init__ ( self , * args ) <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> self . insert ( * args ) <EOL> LEFT2RIGHT = True <EOL> RIGHT2LEFT = False <EOL> def __str__ ( self ) : <EOL> result = "<STR_LIT>" <EOL> for a in self . as_array ( ) : <EOL> result += a + "<STR_LIT:\n>" <EOL> return result <EOL> def is_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . count ( ) == <NUM_LIT:0> <EOL> def includes ( self , * args ) : <EOL> return self . translate ( * args ) != None <EOL> def insert ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> arg = args [ <NUM_LIT:0> ] <EOL> if isinstance ( arg , str ) : <EOL> P4API . P4Map . insert ( self , arg ) <EOL> elif isinstance ( arg , list ) : <EOL> for s in arg : <EOL> P4API . P4Map . insert ( self , s ) <EOL> else : <EOL> left = args [ <NUM_LIT:0> ] . strip ( ) <EOL> right = args [ <NUM_LIT:1> ] . strip ( ) <EOL> P4API . P4Map . insert ( self , left , right ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> p4 = P4 ( ) <EOL> p4 . connect ( ) <EOL> try : <EOL> ret = p4 . run ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> for line in ret : <EOL> if isinstance ( line , dict ) : <EOL> print "<STR_LIT>" <EOL> for k in line . keys ( ) : <EOL> print k , "<STR_LIT:=>" , line [ k ] <EOL> else : <EOL> print line <EOL> except : <EOL> for e in p4 . errors : <EOL> print e </s>
