<s> import tempfile <EOL> import os <EOL> import sys <EOL> from twisted . internet import defer , reactor <EOL> from twisted . internet . error import ProcessExitedAlready <EOL> from twisted . python import usage <EOL> from ooni . utils import log , net <EOL> from ooni . templates import process , httpt <EOL> class UsageOptions ( usage . Options ) : <EOL> optParameters = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:p>' , None , '<STR_LIT>' ] , <EOL> [ '<STR_LIT:url>' , '<STR_LIT:u>' , net . GOOGLE_HUMANS [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:e>' , net . GOOGLE_HUMANS [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' + net . GOOGLE_HUMANS [ <NUM_LIT:1> ] + '<STR_LIT:)>' ] <EOL> ] <EOL> class PsiphonTest ( httpt . HTTPTest , process . ProcessTest ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> description = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> author = "<STR_LIT>" <EOL> version = "<STR_LIT>" <EOL> timeout = <NUM_LIT> <EOL> usageOptions = UsageOptions <EOL> def _setUp ( self ) : <EOL> self . localOptions [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> super ( PsiphonTest , self ) . _setUp ( ) <EOL> def setUp ( self ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . report [ '<STR_LIT>' ] = None <EOL> self . report [ '<STR_LIT>' ] = None <EOL> self . report [ '<STR_LIT>' ] = None <EOL> self . report [ '<STR_LIT>' ] = True <EOL> self . bootstrapped = defer . Deferred ( ) <EOL> self . url = self . localOptions [ '<STR_LIT:url>' ] <EOL> if self . localOptions [ '<STR_LIT:url>' ] != net . GOOGLE_HUMANS [ <NUM_LIT:0> ] : <EOL> self . report [ '<STR_LIT>' ] = False <EOL> if self . localOptions [ '<STR_LIT>' ] != net . GOOGLE_HUMANS [ <NUM_LIT:1> ] : <EOL> self . report [ '<STR_LIT>' ] = False <EOL> if self . localOptions [ '<STR_LIT>' ] : <EOL> self . psiphonpath = self . localOptions [ '<STR_LIT>' ] <EOL> else : <EOL> from os import path , getenv <EOL> self . psiphonpath = path . join ( <EOL> getenv ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> log . debug ( '<STR_LIT>' % self . psiphonpath ) <EOL> def createCommand ( self ) : <EOL> x = """<STR_LIT>""" <EOL> f = tempfile . NamedTemporaryFile ( delete = False ) <EOL> f . write ( x ) <EOL> f . close ( ) <EOL> self . command = [ sys . executable , f . name ] <EOL> log . debug ( '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( self . command ) ) <EOL> def handleRead ( self , stdout , stderr ) : <EOL> if '<STR_LIT>' in self . processDirector . stdout : <EOL> if not self . bootstrapped . called : <EOL> self . report [ '<STR_LIT>' ] = True <EOL> log . debug ( "<STR_LIT>" ) <EOL> self . bootstrapped . callback ( None ) <EOL> def test_psiphon ( self ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . createCommand ( ) <EOL> if not os . path . exists ( self . psiphonpath ) : <EOL> log . err ( '<STR_LIT>' ) <EOL> self . report [ '<STR_LIT>' ] = False <EOL> log . debug ( "<STR_LIT>" % self . report ) <EOL> reactor . callLater ( <NUM_LIT:0.0> , self . bootstrapped . callback , None ) <EOL> return self . bootstrapped <EOL> self . report [ '<STR_LIT>' ] = True <EOL> log . debug ( "<STR_LIT>" % self . report ) <EOL> finished = self . run ( self . command , <EOL> env = dict ( PYTHONPATH = self . psiphonpath ) , <EOL> path = self . psiphonpath , <EOL> usePTY = <NUM_LIT:1> ) <EOL> self . report [ '<STR_LIT>' ] = False <EOL> def callDoRequest ( _ ) : <EOL> log . debug ( "<STR_LIT>" % ( _ , ) ) <EOL> d = self . doRequest ( self . url ) <EOL> def addSuccessToReport ( res ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> if res . body . startswith ( self . localOptions [ '<STR_LIT>' ] ) : <EOL> self . report [ '<STR_LIT>' ] = True <EOL> else : <EOL> self . report [ '<STR_LIT>' ] = False <EOL> return res <EOL> d . addCallback ( addSuccessToReport ) <EOL> def addFailureToReport ( res ) : <EOL> log . debug ( "<STR_LIT>" % ( res , ) ) <EOL> self . report [ '<STR_LIT>' ] = False <EOL> return res <EOL> d . addErrback ( addFailureToReport ) <EOL> return d <EOL> self . bootstrapped . addCallback ( callDoRequest ) <EOL> def cleanup ( _ ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> try : <EOL> self . processDirector . transport . signalProcess ( '<STR_LIT>' ) <EOL> except ProcessExitedAlready : <EOL> pass <EOL> os . remove ( self . command [ <NUM_LIT:1> ] ) <EOL> return finished <EOL> self . bootstrapped . addBoth ( cleanup ) <EOL> return self . bootstrapped </s>
