<s> import warnings , sys , os <EOL> from twisted . application import service , app <EOL> from twisted . persisted import sob <EOL> from twisted . python import usage , util <EOL> from twisted import plugin <EOL> from twisted . python . util import uidFromString , gidFromString <EOL> IServiceMaker = service . IServiceMaker <EOL> _tapHelper = service . ServiceMaker <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> def getid ( uid , gid ) : <EOL> """<STR_LIT>""" <EOL> if uid is not None : <EOL> uid = uidFromString ( uid ) <EOL> if gid is not None : <EOL> gid = gidFromString ( gid ) <EOL> return ( uid , gid ) <EOL> def loadPlugins ( debug = None , progress = None ) : <EOL> tapLookup = { } <EOL> plugins = plugin . getPlugins ( IServiceMaker ) <EOL> for plug in plugins : <EOL> tapLookup [ plug . tapname ] = plug <EOL> return tapLookup <EOL> def addToApplication ( ser , name , append , procname , type , encrypted , uid , gid ) : <EOL> if append and os . path . exists ( append ) : <EOL> a = service . loadApplication ( append , '<STR_LIT>' , None ) <EOL> else : <EOL> a = service . Application ( name , uid , gid ) <EOL> if procname : <EOL> service . IProcess ( a ) . processName = procname <EOL> ser . setServiceParent ( service . IServiceCollection ( a ) ) <EOL> sob . IPersistable ( a ) . setStyle ( type ) <EOL> passphrase = app . getSavePassphrase ( encrypted ) <EOL> if passphrase : <EOL> append = None <EOL> sob . IPersistable ( a ) . save ( filename = append , passphrase = passphrase ) <EOL> class FirstPassOptions ( usage . Options ) : <EOL> synopsis = """<STR_LIT>""" <EOL> recursing = <NUM_LIT:0> <EOL> params = ( ) <EOL> optParameters = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:u>' , None , "<STR_LIT>" , uidFromString ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:g>' , None , "<STR_LIT>" , gidFromString ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:a>' , None , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT:type>' , '<STR_LIT:t>' , '<STR_LIT>' , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:n>' , None , "<STR_LIT>" ] <EOL> ] <EOL> optFlags = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:e>' , "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:d>' , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:p>' , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:h>' , "<STR_LIT>" ] , <EOL> ] <EOL> zsh_actions = { "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT:type>" : "<STR_LIT>" } <EOL> zsh_actionDescr = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:type>" : "<STR_LIT>" } <EOL> def init ( self , tapLookup ) : <EOL> sc = [ ] <EOL> for ( name , module ) in tapLookup . iteritems ( ) : <EOL> if IServiceMaker . providedBy ( module ) : <EOL> sc . append ( ( <EOL> name , None , lambda m = module : m . options ( ) , module . description ) ) <EOL> else : <EOL> sc . append ( ( <EOL> name , None , lambda obj = module : obj . load ( ) . Options ( ) , <EOL> getattr ( module , '<STR_LIT:description>' , '<STR_LIT>' ) ) ) <EOL> sc . sort ( ) <EOL> self . subCommands = sc <EOL> def parseArgs ( self , * rest ) : <EOL> self . params += rest <EOL> def _reportDebug ( self , info ) : <EOL> print '<STR_LIT>' , info <EOL> def _reportProgress ( self , info ) : <EOL> s = self . pb ( info ) <EOL> if s : <EOL> print '<STR_LIT>' , s , <EOL> if info == <NUM_LIT:1.0> : <EOL> print '<STR_LIT:\r>' + ( '<STR_LIT:U+0020>' * <NUM_LIT> ) + '<STR_LIT:\r>' , <EOL> def postOptions ( self ) : <EOL> if self . recursing : <EOL> return <EOL> debug = progress = None <EOL> if self [ '<STR_LIT>' ] : <EOL> debug = self . _reportDebug <EOL> if self [ '<STR_LIT>' ] : <EOL> progress = self . _reportProgress <EOL> self . pb = util . makeStatBar ( <NUM_LIT> , <NUM_LIT:1.0> ) <EOL> try : <EOL> self . tapLookup = loadPlugins ( debug , progress ) <EOL> except IOError : <EOL> raise usage . UsageError ( "<STR_LIT>" ) <EOL> self . init ( self . tapLookup ) <EOL> self . recursing = <NUM_LIT:1> <EOL> self . parseOptions ( self . params ) <EOL> if not hasattr ( self , '<STR_LIT>' ) or self [ '<STR_LIT>' ] : <EOL> raise usage . UsageError ( str ( self ) ) <EOL> if hasattr ( self , '<STR_LIT>' ) and self . subOptions . get ( '<STR_LIT>' ) : <EOL> raise usage . UsageError ( str ( self . subOptions ) ) <EOL> if not self . tapLookup . has_key ( self . subCommand ) : <EOL> raise usage . UsageError ( "<STR_LIT>" + <EOL> '<STR_LIT:U+0020>' . join ( self . tapLookup ) ) <EOL> def run ( ) : <EOL> options = FirstPassOptions ( ) <EOL> try : <EOL> options . parseOptions ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> except usage . UsageError , e : <EOL> print e <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except KeyboardInterrupt : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> plg = options . tapLookup [ options . subCommand ] <EOL> if not IServiceMaker . providedBy ( plg ) : <EOL> plg = plg . load ( ) <EOL> ser = plg . makeService ( options . subOptions ) <EOL> addToApplication ( ser , <EOL> options . subCommand , options [ '<STR_LIT>' ] , options [ '<STR_LIT>' ] , <EOL> options [ '<STR_LIT:type>' ] , options [ '<STR_LIT>' ] , <EOL> options [ '<STR_LIT>' ] , options [ '<STR_LIT>' ] ) </s>
