<s> """<STR_LIT>""" <EOL> import types , os , copy , cStringIO <EOL> try : <EOL> import pwd <EOL> except ImportError : <EOL> pwd = None <EOL> from xml . dom . minidom import Element , Text <EOL> from twisted . spread import pb <EOL> from twisted . spread . banana import SIZE_LIMIT <EOL> from twisted . web import http , resource , server , util , static <EOL> from twisted . web . http_headers import Headers <EOL> from twisted . python import log <EOL> from twisted . persisted import styles <EOL> from twisted . internet import address , reactor <EOL> class _ReferenceableProducerWrapper ( pb . Referenceable ) : <EOL> def __init__ ( self , producer ) : <EOL> self . producer = producer <EOL> def remote_resumeProducing ( self ) : <EOL> self . producer . resumeProducing ( ) <EOL> def remote_pauseProducing ( self ) : <EOL> self . producer . pauseProducing ( ) <EOL> def remote_stopProducing ( self ) : <EOL> self . producer . stopProducing ( ) <EOL> class Request ( pb . RemoteCopy , server . Request ) : <EOL> """<STR_LIT>""" <EOL> def setCopyableState ( self , state ) : <EOL> """<STR_LIT>""" <EOL> for k in '<STR_LIT:host>' , '<STR_LIT>' : <EOL> tup = state [ k ] <EOL> addrdesc = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } [ tup [ <NUM_LIT:0> ] ] <EOL> addr = { '<STR_LIT>' : lambda : address . IPv4Address ( addrdesc , <EOL> tup [ <NUM_LIT:1> ] , tup [ <NUM_LIT:2> ] ) , <EOL> '<STR_LIT>' : lambda : address . UNIXAddress ( tup [ <NUM_LIT:1> ] ) } [ addrdesc ] ( ) <EOL> state [ k ] = addr <EOL> state [ '<STR_LIT>' ] = Headers ( dict ( state [ '<STR_LIT>' ] ) ) <EOL> pb . RemoteCopy . setCopyableState ( self , state ) <EOL> self . content = cStringIO . StringIO ( self . content_data ) <EOL> self . finish = self . remote . remoteMethod ( '<STR_LIT>' ) <EOL> self . setHeader = self . remote . remoteMethod ( '<STR_LIT>' ) <EOL> self . addCookie = self . remote . remoteMethod ( '<STR_LIT>' ) <EOL> self . setETag = self . remote . remoteMethod ( '<STR_LIT>' ) <EOL> self . setResponseCode = self . remote . remoteMethod ( '<STR_LIT>' ) <EOL> self . setLastModified = self . remote . remoteMethod ( '<STR_LIT>' ) <EOL> self . _write = self . remote . remoteMethod ( '<STR_LIT>' ) <EOL> def write ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> start = <NUM_LIT:0> <EOL> end = SIZE_LIMIT <EOL> while True : <EOL> self . _write ( bytes [ start : end ] ) <EOL> start += SIZE_LIMIT <EOL> end += SIZE_LIMIT <EOL> if start >= len ( bytes ) : <EOL> break <EOL> def registerProducer ( self , producer , streaming ) : <EOL> self . remote . callRemote ( "<STR_LIT>" , <EOL> _ReferenceableProducerWrapper ( producer ) , <EOL> streaming ) . addErrback ( self . fail ) <EOL> def unregisterProducer ( self ) : <EOL> self . remote . callRemote ( "<STR_LIT>" ) . addErrback ( self . fail ) <EOL> def fail ( self , failure ) : <EOL> log . err ( failure ) <EOL> pb . setUnjellyableForClass ( server . Request , Request ) <EOL> class Issue : <EOL> def __init__ ( self , request ) : <EOL> self . request = request <EOL> def finished ( self , result ) : <EOL> if result != server . NOT_DONE_YET : <EOL> assert isinstance ( result , types . StringType ) , "<STR_LIT>" <EOL> self . request . write ( result ) <EOL> self . request . finish ( ) <EOL> def failed ( self , failure ) : <EOL> failure = str ( failure ) <EOL> self . request . write ( <EOL> resource . ErrorPage ( http . INTERNAL_SERVER_ERROR , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" + <EOL> util . _PRE ( failure ) ) . <EOL> render ( self . request ) ) <EOL> self . request . finish ( ) <EOL> log . msg ( failure ) <EOL> class ResourceSubscription ( resource . Resource ) : <EOL> isLeaf = <NUM_LIT:1> <EOL> waiting = <NUM_LIT:0> <EOL> def __init__ ( self , host , port ) : <EOL> resource . Resource . __init__ ( self ) <EOL> self . host = host <EOL> self . port = port <EOL> self . pending = [ ] <EOL> self . publisher = None <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = copy . copy ( self . __dict__ ) <EOL> state [ '<STR_LIT>' ] = None <EOL> state [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> state [ '<STR_LIT>' ] = [ ] <EOL> return state <EOL> def connected ( self , publisher ) : <EOL> """<STR_LIT>""" <EOL> log . msg ( '<STR_LIT>' ) <EOL> publisher . broker . notifyOnDisconnect ( self . booted ) <EOL> self . publisher = publisher <EOL> self . waiting = <NUM_LIT:0> <EOL> for request in self . pending : <EOL> self . render ( request ) <EOL> self . pending = [ ] <EOL> def notConnected ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> log . msg ( "<STR_LIT>" % msg ) <EOL> self . waiting = <NUM_LIT:0> <EOL> self . publisher = None <EOL> for request in self . pending : <EOL> request . write ( "<STR_LIT>" ) <EOL> request . finish ( ) <EOL> self . pending = [ ] <EOL> def booted ( self ) : <EOL> self . notConnected ( "<STR_LIT>" ) <EOL> def render ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not self . publisher : <EOL> self . pending . append ( request ) <EOL> if not self . waiting : <EOL> self . waiting = <NUM_LIT:1> <EOL> bf = pb . PBClientFactory ( ) <EOL> timeout = <NUM_LIT:10> <EOL> if self . host == "<STR_LIT>" : <EOL> reactor . connectUNIX ( self . port , bf , timeout ) <EOL> else : <EOL> reactor . connectTCP ( self . host , self . port , bf , timeout ) <EOL> d = bf . getRootObject ( ) <EOL> d . addCallbacks ( self . connected , self . notConnected ) <EOL> else : <EOL> i = Issue ( request ) <EOL> self . publisher . callRemote ( '<STR_LIT>' , request ) . addCallbacks ( i . finished , i . failed ) <EOL> return server . NOT_DONE_YET <EOL> class ResourcePublisher ( pb . Root , styles . Versioned ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , site ) : <EOL> self . site = site <EOL> persistenceVersion = <NUM_LIT:2> <EOL> def upgradeToVersion2 ( self ) : <EOL> self . application . authorizer . removeIdentity ( "<STR_LIT>" ) <EOL> del self . application . services [ self . serviceName ] <EOL> del self . serviceName <EOL> del self . application <EOL> del self . perspectiveName <EOL> def getPerspectiveNamed ( self , name ) : <EOL> return self <EOL> def remote_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> res = self . site . getResourceFor ( request ) <EOL> log . msg ( request ) <EOL> result = res . render ( request ) <EOL> if result is not server . NOT_DONE_YET : <EOL> request . write ( result ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> class UserDirectory ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> userDirName = '<STR_LIT>' <EOL> userSocketName = '<STR_LIT>' <EOL> template = """<STR_LIT>""" <EOL> def __init__ ( self , userDatabase = None ) : <EOL> resource . Resource . __init__ ( self ) <EOL> if userDatabase is None : <EOL> userDatabase = pwd <EOL> self . _pwd = userDatabase <EOL> def _users ( self ) : <EOL> """<STR_LIT>""" <EOL> users = [ ] <EOL> for user in self . _pwd . getpwall ( ) : <EOL> name , passwd , uid , gid , gecos , dir , shell = user <EOL> realname = gecos . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:0> ] <EOL> if not realname : <EOL> realname = name <EOL> if os . path . exists ( os . path . join ( dir , self . userDirName ) ) : <EOL> users . append ( ( name , realname + '<STR_LIT>' ) ) <EOL> twistdsock = os . path . join ( dir , self . userSocketName ) <EOL> if os . path . exists ( twistdsock ) : <EOL> linkName = name + '<STR_LIT>' <EOL> users . append ( ( linkName , realname + '<STR_LIT>' ) ) <EOL> return users <EOL> def render_GET ( self , request ) : <EOL> """<STR_LIT>""" <EOL> listing = Element ( '<STR_LIT>' ) <EOL> for link , text in self . _users ( ) : <EOL> linkElement = Element ( '<STR_LIT:a>' ) <EOL> linkElement . setAttribute ( '<STR_LIT>' , link + '<STR_LIT:/>' ) <EOL> textNode = Text ( ) <EOL> textNode . data = text <EOL> linkElement . appendChild ( textNode ) <EOL> item = Element ( '<STR_LIT>' ) <EOL> item . appendChild ( linkElement ) <EOL> listing . appendChild ( item ) <EOL> return self . template % { '<STR_LIT>' : listing . toxml ( ) } <EOL> def getChild ( self , name , request ) : <EOL> if name == '<STR_LIT>' : <EOL> return self <EOL> td = '<STR_LIT>' <EOL> if name [ - len ( td ) : ] == td : <EOL> username = name [ : - len ( td ) ] <EOL> sub = <NUM_LIT:1> <EOL> else : <EOL> username = name <EOL> sub = <NUM_LIT:0> <EOL> try : <EOL> pw_name , pw_passwd , pw_uid , pw_gid , pw_gecos , pw_dir , pw_shell = self . _pwd . getpwnam ( username ) <EOL> except KeyError : <EOL> return resource . NoResource ( ) <EOL> if sub : <EOL> twistdsock = os . path . join ( pw_dir , self . userSocketName ) <EOL> rs = ResourceSubscription ( '<STR_LIT>' , twistdsock ) <EOL> self . putChild ( name , rs ) <EOL> return rs <EOL> else : <EOL> path = os . path . join ( pw_dir , self . userDirName ) <EOL> if not os . path . exists ( path ) : <EOL> return resource . NoResource ( ) <EOL> return static . File ( path ) </s>
