<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import code <EOL> import logging <EOL> import types <EOL> import re <EOL> import optparse <EOL> import glob <EOL> import traceback <EOL> import gluon . fileutils as fileutils <EOL> from gluon . settings import global_settings <EOL> from gluon . utils import web2py_uuid <EOL> from gluon . compileapp import build_environment , read_pyc , run_models_in <EOL> from gluon . restricted import RestrictedError <EOL> from gluon . globals import Request , Response , Session <EOL> from gluon . storage import Storage , List <EOL> from gluon . admin import w2p_unpack <EOL> from gluon . dal import BaseAdapter <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def enable_autocomplete_and_history ( adir , env ) : <EOL> try : <EOL> import rlcompleter <EOL> import atexit <EOL> import readline <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> readline . parse_and_bind ( "<STR_LIT>" <EOL> if sys . platform == '<STR_LIT>' <EOL> else "<STR_LIT>" ) <EOL> history_file = os . path . join ( adir , '<STR_LIT>' ) <EOL> try : <EOL> readline . read_history_file ( history_file ) <EOL> except IOError : <EOL> open ( history_file , '<STR_LIT:a>' ) . close ( ) <EOL> atexit . register ( readline . write_history_file , history_file ) <EOL> readline . set_completer ( rlcompleter . Completer ( env ) . complete ) <EOL> def exec_environment ( <EOL> pyfile = '<STR_LIT>' , <EOL> request = None , <EOL> response = None , <EOL> session = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> if request is None : <EOL> request = Request ( { } ) <EOL> if response is None : <EOL> response = Response ( ) <EOL> if session is None : <EOL> session = Session ( ) <EOL> if request . folder is None : <EOL> mo = re . match ( r'<STR_LIT>' , pyfile ) <EOL> if mo : <EOL> appname = mo . group ( '<STR_LIT>' ) <EOL> request . folder = os . path . join ( '<STR_LIT>' , appname ) <EOL> else : <EOL> request . folder = '<STR_LIT>' <EOL> env = build_environment ( request , response , session , store_current = False ) <EOL> if pyfile : <EOL> pycfile = pyfile + '<STR_LIT:c>' <EOL> if os . path . isfile ( pycfile ) : <EOL> exec read_pyc ( pycfile ) in env <EOL> else : <EOL> execfile ( pyfile , env ) <EOL> return Storage ( env ) <EOL> def env ( <EOL> a , <EOL> import_models = False , <EOL> c = None , <EOL> f = None , <EOL> dir = '<STR_LIT>' , <EOL> extra_request = { } , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> request = Request ( { } ) <EOL> response = Response ( ) <EOL> session = Session ( ) <EOL> request . application = a <EOL> if not dir : <EOL> request . folder = os . path . join ( '<STR_LIT>' , a ) <EOL> else : <EOL> request . folder = dir <EOL> request . controller = c or '<STR_LIT:default>' <EOL> request . function = f or '<STR_LIT:index>' <EOL> response . view = '<STR_LIT>' % ( request . controller , <EOL> request . function ) <EOL> if global_settings . cmd_options : <EOL> ip = global_settings . cmd_options . ip <EOL> port = global_settings . cmd_options . port <EOL> else : <EOL> ip , port = '<STR_LIT:127.0.0.1>' , '<STR_LIT>' <EOL> request . env . http_host = '<STR_LIT>' % ( ip , port ) <EOL> request . env . remote_addr = '<STR_LIT:127.0.0.1>' <EOL> request . env . web2py_runtime_gae = global_settings . web2py_runtime_gae <EOL> for k , v in extra_request . items ( ) : <EOL> request [ k ] = v <EOL> path_info = '<STR_LIT>' % ( a , c , f ) <EOL> if request . args : <EOL> path_info = '<STR_LIT>' % ( path_info , '<STR_LIT:/>' . join ( request . args ) ) <EOL> if request . vars : <EOL> vars = [ '<STR_LIT>' % ( k , v ) if v else '<STR_LIT:%s>' % k <EOL> for ( k , v ) in request . vars . iteritems ( ) ] <EOL> path_info = '<STR_LIT>' % ( path_info , '<STR_LIT:&>' . join ( vars ) ) <EOL> request . env . path_info = path_info <EOL> def check_credentials ( request , other_application = '<STR_LIT>' ) : <EOL> return True <EOL> fileutils . check_credentials = check_credentials <EOL> environment = build_environment ( request , response , session ) <EOL> if import_models : <EOL> try : <EOL> run_models_in ( environment ) <EOL> except RestrictedError , e : <EOL> sys . stderr . write ( e . traceback + '<STR_LIT:\n>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> environment [ '<STR_LIT>' ] = '<STR_LIT:__main__>' <EOL> return environment <EOL> def exec_pythonrc ( ) : <EOL> pythonrc = os . environ . get ( '<STR_LIT>' ) <EOL> if pythonrc and os . path . isfile ( pythonrc ) : <EOL> def execfile_getlocals ( file ) : <EOL> execfile ( file ) <EOL> return locals ( ) <EOL> try : <EOL> return execfile_getlocals ( pythonrc ) <EOL> except NameError : <EOL> pass <EOL> return dict ( ) <EOL> def run ( <EOL> appname , <EOL> plain = False , <EOL> import_models = False , <EOL> startfile = None , <EOL> bpython = False , <EOL> python_code = False , <EOL> cronjob = False ) : <EOL> """<STR_LIT>""" <EOL> ( a , c , f , args , vars ) = parse_path_info ( appname , av = True ) <EOL> errmsg = '<STR_LIT>' % appname <EOL> if not a : <EOL> die ( errmsg ) <EOL> adir = os . path . join ( '<STR_LIT>' , a ) <EOL> if not os . path . exists ( adir ) : <EOL> if sys . stdin and not sys . stdin . name == '<STR_LIT>' : <EOL> confirm = raw_input ( <EOL> '<STR_LIT>' % a ) <EOL> else : <EOL> logging . warn ( '<STR_LIT>' ) <EOL> return <EOL> if confirm . lower ( ) in [ '<STR_LIT:y>' , '<STR_LIT:yes>' ] : <EOL> os . mkdir ( adir ) <EOL> w2p_unpack ( '<STR_LIT>' , adir ) <EOL> for subfolder in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> subpath = os . path . join ( adir , subfolder ) <EOL> if not os . path . exists ( subpath ) : <EOL> os . mkdir ( subpath ) <EOL> db = os . path . join ( adir , '<STR_LIT>' ) <EOL> if os . path . exists ( db ) : <EOL> data = fileutils . read_file ( db ) <EOL> data = data . replace ( <EOL> '<STR_LIT>' , '<STR_LIT>' + web2py_uuid ( ) ) <EOL> fileutils . write_file ( db , data ) <EOL> if c : <EOL> import_models = True <EOL> extra_request = { } <EOL> if args : <EOL> extra_request [ '<STR_LIT:args>' ] = args <EOL> if vars : <EOL> extra_request [ '<STR_LIT>' ] = vars <EOL> _env = env ( a , c = c , f = f , import_models = import_models , extra_request = extra_request ) <EOL> if c : <EOL> pyfile = os . path . join ( '<STR_LIT>' , a , '<STR_LIT>' , c + '<STR_LIT>' ) <EOL> pycfile = os . path . join ( '<STR_LIT>' , a , '<STR_LIT>' , <EOL> "<STR_LIT>" % ( c , f ) ) <EOL> if ( ( cronjob and os . path . isfile ( pycfile ) ) <EOL> or not os . path . isfile ( pyfile ) ) : <EOL> exec read_pyc ( pycfile ) in _env <EOL> elif os . path . isfile ( pyfile ) : <EOL> execfile ( pyfile , _env ) <EOL> else : <EOL> die ( errmsg ) <EOL> if f : <EOL> exec ( '<STR_LIT>' % f , _env ) <EOL> return <EOL> _env . update ( exec_pythonrc ( ) ) <EOL> if startfile : <EOL> try : <EOL> ccode = None <EOL> if startfile . endswith ( '<STR_LIT>' ) : <EOL> ccode = read_pyc ( startfile ) <EOL> exec ccode in _env <EOL> else : <EOL> execfile ( startfile , _env ) <EOL> if import_models : <EOL> BaseAdapter . close_all_instances ( '<STR_LIT>' ) <EOL> except Exception , e : <EOL> print traceback . format_exc ( ) <EOL> if import_models : <EOL> BaseAdapter . close_all_instances ( '<STR_LIT>' ) <EOL> elif python_code : <EOL> try : <EOL> exec ( python_code , _env ) <EOL> if import_models : <EOL> BaseAdapter . close_all_instances ( '<STR_LIT>' ) <EOL> except Exception , e : <EOL> print traceback . format_exc ( ) <EOL> if import_models : <EOL> BaseAdapter . close_all_instances ( '<STR_LIT>' ) <EOL> else : <EOL> if not plain : <EOL> if bpython : <EOL> try : <EOL> import bpython <EOL> bpython . embed ( locals_ = _env ) <EOL> return <EOL> except : <EOL> logger . warning ( <EOL> '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> import IPython <EOL> if IPython . __version__ > '<STR_LIT>' : <EOL> IPython . start_ipython ( user_ns = _env ) <EOL> return <EOL> elif IPython . __version__ == '<STR_LIT>' : <EOL> from IPython . terminal . embed import InteractiveShellEmbed <EOL> shell = InteractiveShellEmbed ( user_ns = _env ) <EOL> shell ( ) <EOL> return <EOL> elif IPython . __version__ >= '<STR_LIT>' : <EOL> from IPython . frontend . terminal . embed import InteractiveShellEmbed <EOL> shell = InteractiveShellEmbed ( user_ns = _env ) <EOL> shell ( ) <EOL> return <EOL> else : <EOL> if '<STR_LIT>' in _env : <EOL> del _env [ '<STR_LIT>' ] <EOL> shell = IPython . Shell . IPShell ( argv = [ ] , user_ns = _env ) <EOL> shell . mainloop ( ) <EOL> return <EOL> except : <EOL> logger . warning ( <EOL> '<STR_LIT>' ) <EOL> enable_autocomplete_and_history ( adir , _env ) <EOL> code . interact ( local = _env ) <EOL> def parse_path_info ( path_info , av = False ) : <EOL> """<STR_LIT>""" <EOL> if av : <EOL> vars = None <EOL> if '<STR_LIT:?>' in path_info : <EOL> path_info , query = path_info . split ( '<STR_LIT:?>' , <NUM_LIT:2> ) <EOL> vars = Storage ( ) <EOL> for var in query . split ( '<STR_LIT:&>' ) : <EOL> ( var , val ) = var . split ( '<STR_LIT:=>' , <NUM_LIT:2> ) if '<STR_LIT:=>' in var else ( var , None ) <EOL> vars [ var ] = val <EOL> items = List ( path_info . split ( '<STR_LIT:/>' ) ) <EOL> args = List ( items [ <NUM_LIT:3> : ] ) if len ( items ) > <NUM_LIT:3> else None <EOL> return ( items ( <NUM_LIT:0> ) , items ( <NUM_LIT:1> ) , items ( <NUM_LIT:2> ) , args , vars ) <EOL> mo = re . match ( r'<STR_LIT>' , <EOL> path_info ) <EOL> if mo : <EOL> return ( mo . group ( '<STR_LIT:a>' ) , mo . group ( '<STR_LIT:c>' ) , mo . group ( '<STR_LIT:f>' ) ) <EOL> else : <EOL> return ( None , None , None ) <EOL> def die ( msg ) : <EOL> print >> sys . stderr , msg <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def test ( testpath , import_models = True , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> import doctest <EOL> if os . path . isfile ( testpath ) : <EOL> mo = re . match ( r'<STR_LIT>' , testpath ) <EOL> if not mo : <EOL> die ( '<STR_LIT>' <EOL> % testpath ) <EOL> a = mo . group ( '<STR_LIT:a>' ) <EOL> c = f = None <EOL> files = [ testpath ] <EOL> else : <EOL> ( a , c , f ) = parse_path_info ( testpath ) <EOL> errmsg = '<STR_LIT>' % testpath <EOL> if not a : <EOL> die ( errmsg ) <EOL> cdir = os . path . join ( '<STR_LIT>' , a , '<STR_LIT>' ) <EOL> if not os . path . isdir ( cdir ) : <EOL> die ( errmsg ) <EOL> if c : <EOL> cfile = os . path . join ( cdir , c + '<STR_LIT>' ) <EOL> if not os . path . isfile ( cfile ) : <EOL> die ( errmsg ) <EOL> files = [ cfile ] <EOL> else : <EOL> files = glob . glob ( os . path . join ( cdir , '<STR_LIT>' ) ) <EOL> for testfile in files : <EOL> globs = env ( a , import_models ) <EOL> ignores = globs . keys ( ) <EOL> execfile ( testfile , globs ) <EOL> def doctest_object ( name , obj ) : <EOL> """<STR_LIT>""" <EOL> if type ( obj ) in ( types . FunctionType , types . TypeType , <EOL> types . ClassType , types . MethodType , <EOL> types . UnboundMethodType ) : <EOL> globs = env ( a , c = c , f = f , import_models = import_models ) <EOL> execfile ( testfile , globs ) <EOL> doctest . run_docstring_examples ( <EOL> obj , globs = globs , <EOL> name = '<STR_LIT>' % ( os . path . basename ( testfile ) , <EOL> name ) , verbose = verbose ) <EOL> if type ( obj ) in ( types . TypeType , types . ClassType ) : <EOL> for attr_name in dir ( obj ) : <EOL> o = eval ( '<STR_LIT>' % ( name , attr_name ) , globs ) <EOL> doctest_object ( attr_name , o ) <EOL> for ( name , obj ) in globs . items ( ) : <EOL> if name not in ignores and ( f is None or f == name ) : <EOL> doctest_object ( name , obj ) <EOL> def get_usage ( ) : <EOL> usage = """<STR_LIT>""" <EOL> return usage <EOL> def execute_from_command_line ( argv = None ) : <EOL> if argv is None : <EOL> argv = sys . argv <EOL> parser = optparse . OptionParser ( usage = get_usage ( ) ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> msg = '<STR_LIT>' <EOL> msg += '<STR_LIT>' <EOL> msg += '<STR_LIT>' <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> dest = '<STR_LIT>' , <EOL> help = msg , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' + <EOL> '<STR_LIT>' , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' + <EOL> '<STR_LIT>' , <EOL> ) <EOL> ( options , args ) = parser . parse_args ( argv [ <NUM_LIT:1> : ] ) <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> startfile = args [ <NUM_LIT:0> ] <EOL> else : <EOL> startfile = '<STR_LIT>' <EOL> run ( options . shell , options . plain , startfile = startfile , <EOL> bpython = options . bpython ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> execute_from_command_line ( ) </s>
