@@ -15,19 +15,20 @@ import logging import types import re import optparse import glob - +import traceback import fileutils import settings from utils import web2py_uuid from compileapp import build_environment, read_pyc, run_models_in from restricted import RestrictedError from globals import Request, Response, Session from storage import Storage from admin import w2p_unpack +from dal import BaseAdapter logger = logging.getLogger("web2py") def exec_environment( @@ -48,13 +49,13 @@ A Storage dictionary containing the resulting environment is returned. The working directory must be web2py root -- this is the web2py default. """ - if request==None: request = Request() - if response==None: response = Response() - if session==None: session = Session() + if request is None: request = Request() + if response is None: response = Response() + if session is None: session = Session() if request.folder is None: mo = re.match(r'(|.*/)applications/(?P[^/]+)', pyfile) if mo: appname = mo.group('appname') @@ -148,11 +149,12 @@ def run( appname, plain=False, import_models=False, startfile=None, - bpython=False + bpython=False, + python_code=False ): """ Start interactive shell or run Python script (startfile) in web2py controller environment. appname is formatted like: @@ -200,12 +202,22 @@ exec ('print %s()' % f, _env) elif startfile: exec_pythonrc() try: execfile(startfile, _env) - except RestrictedError, e: - print e.traceback + if import_models: BaseAdapter.close_all_instances('commit') + except Exception, e: + print traceback.format_exc() + if import_models: BaseAdapter.close_all_instances('rollback') + elif python_code: + exec_pythonrc() + try: + exec(python_code, _env) + if import_models: BaseAdapter.close_all_instances('commit') + except Exception, e: + print traceback.format_exc() + if import_models: BaseAdapter.close_all_instances('rollback') else: if not plain: if bpython: try: import bpython @@ -215,16 +227,23 @@ logger.warning( 'import bpython error; trying ipython...') else: try: import IPython - # following 2 lines fix a problem with IPython; thanks Michael Toomim - if '__builtins__' in _env: - del _env['__builtins__'] - shell = IPython.Shell.IPShell(argv=[], user_ns=_env) - shell.mainloop() - return + if IPython.__version__ >= '0.11': + from IPython.frontend.terminal.embed import InteractiveShellEmbed + shell = InteractiveShellEmbed(user_ns=_env) + shell() + return + else: + # following 2 lines fix a problem with + # IPython; thanks Michael Toomim + if '__builtins__' in _env: + del _env['__builtins__'] + shell = IPython.Shell.IPShell(argv=[],user_ns=_env) + shell.mainloop() + return except: logger.warning( 'import IPython error; use default python shell') try: import readline @@ -395,6 +414,8 @@ run(options.shell, options.plain, startfile=startfile, bpython=options.bpython) if __name__ == '__main__': execute_from_command_line() + +