def executor(queue,task, out): """ the background process """ logging.debug(' task started') class LogOutput(object): """Facility to log output at intervals""" def __init__(self, out_queue): self.out_queue = out_queue self.stdout = sys.stdout sys.stdout = self self.istr = "" def __del__(self): sys.stdout = self.stdout def write(self,data): self.out_queue.put(data) self.istr += data def getvalue(self): return self.istr #stdout, sys.stdout = sys.stdout, cStringIO.StringIO() stdout = LogOutput(out) try: if task.app: os.chdir(os.environ['WEB2PY_PATH']) from gluon.shell import env, parse_path_info from gluon.dal import BaseAdapter from gluon import current level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARN) # Get controller-specific subdirectory if task.app is of # form 'app/controller' (a,c,f) = parse_path_info(task.app) _env = env(a=a,c=c,import_models=True) logging.getLogger().setLevel(level) scheduler = current._scheduler f = task.function functions = current._scheduler.tasks if not functions: #look into env _function = _env.get(f) else: _function = functions.get(f) if not isinstance(_function, CALLABLETYPES): raise NameError("name '%s' not found in scheduler's environment" % f) globals().update(_env) args = loads(task.args) vars = loads(task.vars, object_hook=_decode_dict) result = dumps(_function(*args,**vars)) else: ### for testing purpose only result = eval(task.function)( *loads(task.args, object_hook=_decode_dict), **loads(task.vars, object_hook=_decode_dict)) #stdout, sys.stdout = sys.stdout, stdout sys.stdout = stdout.stdout queue.put(TaskReport(COMPLETED, result,stdout.getvalue())) except BaseException,e: sys.stdout = stdout.stdout tb = traceback.format_exc() queue.put(TaskReport(FAILED,tb=tb, output=stdout.getvalue()))
def web2py(appname, fixture_create_testfile_for_application): ''' Create a Web2py environment similar to that achieved by Web2py shell. It allows you to use global Web2py objects like db, request, response, session, etc. Concerning tests, it is usually used to check if your database is an expected state, avoiding creating controllers and functions to help tests. ''' from gluon.shell import env from gluon.storage import Storage web2py_env = env(appname, import_models=True, extra_request=dict(is_local=True)) # Uncomment next 3 lines to allow using global Web2py objects directly # in your test scripts. if hasattr(web2py_env, '__file__'): del web2py_env['__file__'] # avoid py.test import error globals().update(web2py_env) return Storage(web2py_env)
def executor(queue, task): """ the background process """ logging.debug(' task started') stdout, sys.stdout = sys.stdout, cStringIO.StringIO() try: if task.app: os.chdir(os.environ['WEB2PY_PATH']) from gluon.shell import env from gluon.dal import BaseAdapter from gluon import current level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARN) _env = env(task.app, import_models=True) logging.getLogger().setLevel(level) scheduler = current._scheduler scheduler_tasks = current._scheduler.tasks _function = scheduler_tasks[task.function] globals().update(_env) args = loads(task.args) vars = loads(task.vars, object_hook=_decode_dict) result = dumps(_function(*args, **vars)) else: ### for testing purpose only result = eval(task.function)(*loads(task.args, object_hook=_decode_dict), **loads(task.vars, object_hook=_decode_dict)) stdout, sys.stdout = sys.stdout, stdout queue.put(TaskReport(COMPLETED, result, stdout.getvalue())) except BaseException, e: sys.stdout = stdout tb = traceback.format_exc() queue.put(TaskReport(FAILED, tb=tb))
def executor(queue, task, out): """ the background process """ logger.debug(' task started') class LogOutput(object): """Facility to log output at intervals""" def __init__(self, out_queue): self.out_queue = out_queue self.stdout = sys.stdout sys.stdout = self def __del__(self): sys.stdout = self.stdout def flush(self): pass def write(self, data): self.out_queue.put(data) W2P_TASK = Storage({'id': task.task_id, 'uuid': task.uuid}) stdout = LogOutput(out) try: if task.app: os.chdir(os.environ['WEB2PY_PATH']) from gluon.shell import env, parse_path_info from gluon import current level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARN) # Get controller-specific subdirectory if task.app is of # form 'app/controller' (a, c, f) = parse_path_info(task.app) _env = env(a=a, c=c, import_models=True) logging.getLogger().setLevel(level) f = task.function functions = current._scheduler.tasks if not functions: #look into env _function = _env.get(f) else: _function = functions.get(f) if not isinstance(_function, CALLABLETYPES): raise NameError( "name '%s' not found in scheduler's environment" % f) #Inject W2P_TASK into environment _env.update({'W2P_TASK': W2P_TASK}) globals().update(_env) args = loads(task.args) vars = loads(task.vars, object_hook=_decode_dict) result = dumps(_function(*args, **vars)) else: ### for testing purpose only result = eval(task.function)(*loads(task.args, object_hook=_decode_dict), **loads(task.vars, object_hook=_decode_dict)) queue.put(TaskReport(COMPLETED, result=result)) except BaseException, e: tb = traceback.format_exc() queue.put(TaskReport(FAILED, tb=tb))
def new_env(app='init', controller=None): try: from gluon import shell except ImportError: raise ImportError(""" No module named `gluon`, unable to find `shell` Make sure you have the correct URI that is the main web2py directory. This should include your `applications` and `gluon` directories.""") dir = os.path.join(_PATH, 'applications', app) _env = shell.env(app, c=controller, import_models=True, dir=dir, extra_request={ 'wsgi': { 'environ': { 'TEST_APP': _TEST_APP_KEY, 'TEST_METHOD': 'FakeEnv' } } }) if controller: execfile(os.path.join(dir, 'controllers', controller + '.py'), _env) return _env
def set_globals(web2py_path,app): """ This function sets the environment variables like database , dbengine etc . @param web2py_path : The path to the web2py congaing the Eden app (i.e "/home/web2py") @param app : The name of the eden application of whose database needs to be migrated (i.e "eden") It loads the models all the models to get the database and also sets the other global variable which are required by all of the functions for database operations """ global WEB2PY_PATH,APP,dbengine WEB2PY_PATH = web2py_path APP = app os.chdir(WEB2PY_PATH) sys.path.append(WEB2PY_PATH) from gluon.custom_import import custom_import_install custom_import_install(WEB2PY_PATH) from gluon.shell import env from gluon import DAL, Field old_env = env(APP, c=None, import_models=True) old_str =''' try: s3db.load_all_models() except NameError: print "s3db not defined" ''' globals().update(**old_env) exec old_str in globals(), locals() if settings.database.db_type: dbengine = settings.database.db_type else: dbengine = "sqlite"
def web2py(appname, fixture_create_testfile_for_application): '''Create a Web2py environment similar to that achieved by Web2py shell. It allows you to use global Web2py objects like db, request, response, session, etc. Concerning tests, it is usually used to check if your database is an expected state, avoiding creating controllers and functions to help tests. ''' from gluon.shell import env from gluon.storage import Storage web2py_env = env(appname, import_models=True, extra_request=dict(is_local=True)) # Uncomment next 2 lines to allow using global Web2py objects directly # in your test scripts. #del web2py_env['__file__'] # avoid py.test import error globals().update(web2py_env) return Storage(web2py_env)
def new_env(app='init', controller=None): try: from gluon import shell except ImportError: raise ImportError(""" No module named `gluon`, unable to find `shell` Make sure you have the correct URI that is the main web2py directory. This should include your `applications` and `gluon` directories.""") dir = os.path.join(_PATH, 'applications', app) _env = shell.env(app, c=controller, import_models=True, dir=dir, extra_request = { 'wsgi': {'environ': { 'TEST_APP': _TEST_APP_KEY, 'TEST_METHOD': 'FakeEnv' }} } ) if controller: execfile(os.path.join(dir, 'controllers', controller + '.py'), _env) return _env
def executor(queue, task, out): """ the background process """ logger.debug(" task started") class LogOutput(object): """Facility to log output at intervals""" def __init__(self, out_queue): self.out_queue = out_queue self.stdout = sys.stdout sys.stdout = self def __del__(self): sys.stdout = self.stdout def flush(self): pass def write(self, data): self.out_queue.put(data) W2P_TASK = Storage({"id": task.task_id, "uuid": task.uuid}) stdout = LogOutput(out) try: if task.app: os.chdir(os.environ["WEB2PY_PATH"]) from gluon.shell import env, parse_path_info from gluon import current level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARN) # Get controller-specific subdirectory if task.app is of # form 'app/controller' (a, c, f) = parse_path_info(task.app) _env = env(a=a, c=c, import_models=True) logging.getLogger().setLevel(level) f = task.function functions = current._scheduler.tasks if not functions: # look into env _function = _env.get(f) else: _function = functions.get(f) if not isinstance(_function, CALLABLETYPES): raise NameError("name '%s' not found in scheduler's environment" % f) # Inject W2P_TASK into environment _env.update({"W2P_TASK": W2P_TASK}) globals().update(_env) args = loads(task.args) vars = loads(task.vars, object_hook=_decode_dict) result = dumps(_function(*args, **vars)) else: ### for testing purpose only result = eval(task.function)( *loads(task.args, object_hook=_decode_dict), **loads(task.vars, object_hook=_decode_dict) ) queue.put(TaskReport(COMPLETED, result=result)) except BaseException, e: tb = traceback.format_exc() queue.put(TaskReport(FAILED, tb=tb))
def executor(queue,task): """ the background process """ logging.debug(' task started') stdout, sys.stdout = sys.stdout, cStringIO.StringIO() try: if task.app: os.chdir(os.environ['WEB2PY_PATH']) from gluon.shell import env from gluon.dal import BaseAdapter from gluon import current level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARN) _env = env(task.app,import_models=True) logging.getLogger().setLevel(level) scheduler = current._scheduler scheduler_tasks = current._scheduler.tasks _function = scheduler_tasks[task.function] globals().update(_env) args = loads(task.args) vars = loads(task.vars, object_hook=_decode_dict) result = dumps(_function(*args,**vars)) else: ### for testing purpose only result = eval(task.function)( *loads(task.args, object_hook=_decode_dict), **loads(task.vars, object_hook=_decode_dict)) stdout, sys.stdout = sys.stdout, stdout queue.put(TaskReport(COMPLETED, result,stdout.getvalue())) except BaseException,e: sys.stdout = stdout tb = traceback.format_exc() queue.put(TaskReport(FAILED,tb=tb))
def __shell(app, response): import code, thread from gluon.shell import env (shell, lock) = (code.InteractiveInterpreter(), thread.allocate_lock()) shell.locals = env(app, True) response._custom_commit = lambda: None response._custom_rollback = lambda: None return (shell, lock)
def __shell(app, response): import code, thread from gluon.shell import env (shell, lock) = (code.InteractiveInterpreter(), thread.allocate_lock()) shell.locals = env(app,True) response._custom_commit = lambda: None response._custom_rollback = lambda: None return (shell, lock)
def create_env(context): """ creates a web2py env, like the shell one """ from gluon.shell import env from gluon.storage import Storage app_path = os.path.join(context.web2py_path, "applications", context.appname) env_ = env(a=context.appname, import_models=True, dir=app_path, extra_request=dict(is_local=True)) context.web2py = Storage(env_) def bogus_url( a=None, c=None, f=None, r=None, args=None, vars=None, anchor="", extension=None, env=None, hmac_key=None, hash_vars=True, salt=None, user_signature=None, scheme=None, host=None, port=None, encode_embedded_slash=False, url_encode=True, ): return env_["URL"]( a=a, c=c, f=f, r=r, args=args, vars=vars, anchor=anchor, extension=extension, env=env, hmac_key=hmac_key, hash_vars=hash_vars, salt=salt, user_signature=user_signature, scheme=scheme, host=host if host is not None else context.host, port=port, encode_embedded_slash=encode_embedded_slash, url_encode=url_encode, ) context.web2py.URL = bogus_url
def web2py(appname, fixture_create_testfile_for_application): """Create a Web2py environment similar to that achieved by Web2py shell. It allows you to use global Web2py objects like db, request, response, session, etc. Concerning tests, it is usually used to check if your database is an expected state, avoiding creating controllers and functions to help tests. """ from gluon.shell import env from gluon.storage import Storage web2py_env = env(appname, import_models=True, extra_request=dict(is_local=True)) # Uncomment next 2 lines to allow using global Web2py objects directly # in your test scripts. del web2py_env["__file__"] # avoid py.test import error globals().update(web2py_env) # FIXME hack for fixing migrations # count = 0 # for row in db(db.path_log.id > 0).select(): # if row.path: # row.update_record(in_path=row.path) # db.commit() # count += 1 # print count # for row in db(db.attempt_log.id > 0).select(): # if row.path: # row.update_record(in_path=row.path) # db.commit() # count += 1 # print count # for row in db(db.tag_records.id > 0).select(): # if row.path: # row.update_record(in_path=row.path) # db.commit() # count += 1 # print count # print 'updated', count, 'records' return Storage(web2py_env)
def run(fname, application, *args, **kwargs): logging.info("\tfunction %r in model", fname) import sys sys.path.insert(0, ".") logging.debug("len(sys.path): %r", len(sys.path)) from gluon.shell import env environment = env(application, import_models=True) if fname in environment: fn = environment[fname] else: instance_name, _, attribute = fname.rpartition(".") fn = getattr(environment[instance_name], attribute) return fn(*args, **kwargs)
def executor(queue, task): """ the background process """ logging.debug(' task started') stdout, sys.stdout = sys.stdout, cStringIO.StringIO() try: if task.app: os.chdir(os.environ['WEB2PY_PATH']) from gluon.shell import env, parse_path_info from gluon.dal import BaseAdapter from gluon import current level = logging.getLogger().getEffectiveLevel() logging.getLogger().setLevel(logging.WARN) # Get controller-specific subdirectory if task.app is of # form 'app/controller' (a, c, f) = parse_path_info(task.app) _env = env(a=a, c=c, import_models=True) logging.getLogger().setLevel(level) scheduler = current._scheduler f = task.function functions = current._scheduler.tasks if not functions: #look into env _function = _env.get(f) else: _function = functions.get(f) if not isinstance(_function, CALLABLETYPES): raise NameError( "name '%s' not found in scheduler's environment" % f) globals().update(_env) args = loads(task.args) vars = loads(task.vars, object_hook=_decode_dict) result = dumps(_function(*args, **vars)) else: ### for testing purpose only result = eval(task.function)(*loads(task.args, object_hook=_decode_dict), **loads(task.vars, object_hook=_decode_dict)) stdout, sys.stdout = sys.stdout, stdout queue.put(TaskReport(COMPLETED, result, stdout.getvalue())) except BaseException, e: sys.stdout = stdout tb = traceback.format_exc() queue.put(TaskReport(FAILED, tb=tb))
def callback(): app = request.args[0] command = request.vars.statement escape = command[:1]!='!' history = session['history:'+app] = session.get('history:'+app,gluon.contrib.shell.History()) if not escape: command = command[1:] if command == '%reset': reset() return '*** reset ***' elif command[0] == '%': try: command=session['commands:'+app][int(command[1:])] except ValueError: return '' session['commands:'+app].append(command) environ=env(app,True) output = gluon.contrib.shell.run(history,command,environ) k = len(session['commands:'+app]) - 1 #output = PRE(output) #return TABLE(TR('In[%i]:'%k,PRE(command)),TR('Out[%i]:'%k,output)) return 'In [%i] : %s%s\n' % (k + 1, command, output)
def get_old_db(): """ This function let up view how the database was before the migration scripts were called , the relevant data is displayed during the tests """ os.chdir(WEB2PY_PATH) sys.path.append(WEB2PY_PATH) from gluon.custom_import import custom_import_install custom_import_install(WEB2PY_PATH) from gluon.shell import env from gluon import DAL, Field old_env = env(APP, c=None, import_models=True) old_str =''' try: s3db.load_all_models() except NameError: print "s3db not defined" ''' globals().update(**old_env) exec old_str in globals(), locals() return db
def get_old_db(): """ This function let up view how the database was before the migration scripts were called , the relevant data is displayed during the tests """ os.chdir(WEB2PY_PATH) sys.path.append(WEB2PY_PATH) from gluon.custom_import import custom_import_install custom_import_install(WEB2PY_PATH) from gluon.shell import env from gluon import DAL, Field old_env = env(APP, c=None, import_models=True) old_str = ''' try: s3db.load_all_models() except NameError: print "s3db not defined" ''' globals().update(**old_env) exec old_str in globals(), locals() return db
def callback(): app = request.args[0] command = request.vars.statement escape = command[:1] != '!' history = session['history:' + app] = session.get( 'history:' + app, gluon.contrib.shell.History()) if not escape: command = command[1:] if command == '%reset': reset() return '*** reset ***' elif command[0] == '%': try: command = session['commands:' + app][int(command[1:])] except ValueError: return '' session['commands:' + app].append(command) environ = env(app, True, extra_request=dict(is_https=request.is_https)) output = gluon.contrib.shell.run(history, command, environ) k = len(session['commands:' + app]) - 1 #output = PRE(output) #return TABLE(TR('In[%i]:'%k,PRE(command)),TR('Out[%i]:'%k,output)) return cgi.escape('In [%i] : %s%s\n' % (k + 1, command, output))
allargs = args["allargs"] adir = os.path.join('applications', app) if not os.path.exists(adir): print >> sys.stderr, "Application not found: %s" % adir sys.exit(1) from gluon.custom_import import custom_import_install try: # Web2py post revision b8afce7 2012-10-11 custom_import_install() except: # Web2py pre revision b8afce7 2012-10-11 custom_import_install(os.environ['WEB2PY_PATH']) from gluon.shell import env _env = env(app, c=None, import_models=True) globals().update(**_env) # This is present in case this is a first run of the models. db.commit() from gluon import current # Get tasks from the scheduler_task table. if task: query = (db.scheduler_task.task_name == task) else: query = (db.scheduler_task.id > 0) scheduled_tasks = db(query).select(orderby=db.scheduler_task.task_name) # Pick up the associated function objects from the scheduler's task list. # These are also stored in the S3Task instance -- both lists should be the # same.
APP = sys.argv[2] changed_table = "org_organisation" new_field = "type_id" new_table = "org_organisation_type" old_field = "type" new_table_field = "name" os.chdir(WEB2PY_PATH) sys.path.append(WEB2PY_PATH) from gluon.custom_import import custom_import_install custom_import_install(WEB2PY_PATH) from gluon.shell import env from gluon import DAL, Field old_env = env(APP, c=None, import_models=True) old_str =''' try: s3db.load_all_models() except NameError: print "s3db not defined" ''' globals().update(**old_env) exec old_str in globals(), locals() database_string = "sqlite://storage.db" old_database_folder = "%s/applications/%s/databases" % (WEB2PY_PATH, APP) temp_db = DAL( database_string, folder = old_database_folder, migrate_enabled=True ,migrate = True) #MIGRATION SCRIPT
APP = sys.argv[2] changed_table = "org_organisation" new_field = "type_id" new_table = "org_organisation_type" old_field = "type" new_table_field = "name" os.chdir(WEB2PY_PATH) sys.path.append(WEB2PY_PATH) from gluon.custom_import import custom_import_install custom_import_install(WEB2PY_PATH) from gluon.shell import env from gluon import DAL, Field old_env = env(APP, c=None, import_models=True) old_str = ''' try: s3db.load_all_models() except NameError: print "s3db not defined" ''' globals().update(**old_env) exec old_str in globals(), locals() database_string = "sqlite://storage.db" old_database_folder = "%s/applications/%s/databases" % (WEB2PY_PATH, APP) temp_db = DAL(database_string, folder=old_database_folder, migrate_enabled=True, migrate=True)
""" import os import unittest from ConfigParser import NoSectionError from gluon.shell import env from gluon.storage import Storage from applications.zcomix.modules.stickon.tools import \ ModelDb, \ SettingsLoader from applications.zcomix.modules.test_runner import LocalTestCase # R0904: Too many public methods # pylint: disable=C0111,R0904 APPLICATION = __file__.split(os.sep)[-4] APP_ENV = env(APPLICATION, import_models=False) class TestModelDb(LocalTestCase): def test____init__(self): # W0212: *Access to a protected member %s of a client class* # pylint: disable=W0212 # # Test with default config file. # model_db = ModelDb(APP_ENV, init_all=False) self.assertTrue(model_db) # returns object # response.static_version is set
def main(): """ Main routine. """ usage = '%prog [options] schema_file' + '\nVersion: %s' % VERSION parser = OptionParser(usage=usage) parser.add_option('-c', '--config-file', dest='config_file', help='Name of configuration file.') parser.add_option('-f', '--full-help', action='store_true', dest='full_help', help=' '.join(['Print full help and exit.', 'Full help includes examples and notes.']), ) parser.add_option('-i', '--include-headers', action='store_true', dest='headers', help='Print table documentation headers.') parser.add_option('-s', '--ignore-signature', action='store_true', dest='ignore_signature', help='Do not print signature fields.') parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='Print messages to stdout', ) (options, args) = parser.parse_args() if options.verbose: # Add a stream handler to print messages to stderr. ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter('%(levelname)s - %(message)s') ch.setFormatter(formatter) LOG.addHandler(ch) if options.full_help: parser.print_help() print print usage_full() return if len(args) == 0: parser.print_help() exit(1) ignore_columns = [] if options.ignore_signature: APP_ENV = env('shared', import_models=True) for field in ModelDb(APP_ENV).auth.signature.fields: if not field == 'id': ignore_columns.append(field) f = None if args[0] == '-': f = sys.stdin else: f = open(args[0], 'r') schema_file = SchemaFile(file_object=f) schema_file.parse() tables = [] for line in schema_file.filter(['create', 'create_close', 'column']): LOG.debug('line: %s' % line.text) if line.type == 'create': create_line = CreateLine(text=line.text) create_line.parse() LOG.debug('create_line.table_name: %s' % create_line.table_name) if create_line.table_name: table = MySQLTable(name=create_line.table_name) tables.append(table) if line.type == 'create_close': pass if line.type == 'column': if not len(tables) > 0: raise ValueError('Column without table: {text}'.format( text=line.text)) column_line = ColumnLine(text=line.text) column_line.parse() column = MySQLColumn( name=column_line.name, data_type=column_line.type, length=column_line.length, decimals=column_line.decimals, table=tables[-1], attributes=column_line.attributes, ) if column.name not in ignore_columns: tables[-1].columns.append(column) f.close() # Set tables references # This step must be done after all MySQLTable objects are defined and # added to tables list since so the tables are available for columns that # reference it. for t in tables: for c in t.columns: c.set_referenced_table(tables=tables) if c.referenced_table: c.set_referenced_column() c.set_descriptor_column() defaults_set = None if options.config_file: defaults_set = FieldPropertyDefaultsSet() defaults_set.load(options.config_file) for t in tables: print '' if options.headers: print '"""' print t.documentation() print '"""' print define_table_code(t, defaults_set=defaults_set) first = True for t in sorted(tables, cmp=lambda x, y: cmp(x.name, y.name)): for c in sorted(t.columns, cmp=lambda x, y: cmp(x.name, y.name)): if c.referenced_table: if first: print '' first = False print c.requires_statement()
Test suite for shared/modules/settings.py """ from applications.shared.modules.test_runner import LocalTestSuite, \ ModuleTestSuite from applications.shared.modules.database import Collection from applications.shared.modules.settings import Setting from gluon.shell import env import gluon.main import sys import unittest APP_ENV = env(__file__.split('/')[-3], import_models=True) DB = APP_ENV['db'] # C0111: Missing docstring # R0904: Too many public methods # pylint: disable=C0111,R0904 class TestSetting(unittest.TestCase): _objects = [] # C0103: *Invalid name "%s" (should match %s)* # pylint: disable=C0103 @classmethod def tearDownClass(cls):
Script to create and maintain images and their sizes. """ import datetime import logging import os import sys import traceback from gluon import * from gluon.shell import env from optparse import OptionParser from applications.zcomix.modules.images import \ UploadImage, \ set_thumb_dimensions VERSION = 'Version 0.1' APP_ENV = env(__file__.split(os.sep)[-3], import_models=True) # C0103: *Invalid name "%%s" (should match %%s)* # pylint: disable=C0103 db = APP_ENV['db'] LOG = logging.getLogger('cli') FIELDS = [ 'creator.image', 'book_page.image', ] class ImageHandler(object): """Class representing a handler for image resizing.""" def __init__(self,
def web2py(appname): '''Create a Web2py environment similar to that achieved by Web2py shell. It allows you to use global Web2py objects like db, request, response, session, etc. Concerning tests, it is usually used to check if your database is an expected state, avoiding creating controllers and functions to help tests. ''' def run(controller, function, env): """Injects request.controller and request.function into web2py environment. """ from gluon.compileapp import run_controller_in env.request.controller = controller env.request.function = function r = None try: r = run_controller_in(controller, function, env) except HTTP as e: if str(e.status).startswith("2") or str(e.status).startswith("3"): env.db.commit() raise else: env.db.commit() finally: env.db.rollback() return r def submit(controller, action, env, data=None, formname=None): """Submits a form, setting _formkey and _formname accordingly. env must be the web2py environment fixture. """ formname = formname or "default" hidden = dict( _formkey=action, _formname=formname ) if data: env.request.post_vars.update(data) env.request.post_vars.update(hidden) env.session["_formkey[%s]" % formname] = [action] return env.run(controller, action, env) from gluon.shell import env from gluon.storage import Storage web2py_env = env(appname, import_models=True, extra_request=dict(is_local=True, _running_under_test=True)) del web2py_env['__file__'] # avoid py.test import error web2py_env['run'] = run web2py_env['submit'] = submit globals().update(web2py_env) return Storage(web2py_env)
NEW_APP = sys.argv[3] OLD_APP = sys.argv[2] NEW_PATH = "%s/applications/%s" % (os.environ["WEB2PY_PATH"], NEW_APP) OLD_PATH = "%s/applications/%s" % (os.environ["WEB2PY_PATH"], OLD_APP) #WE ARE LOADING THE 2 ENVIRONMENTS WITH ALL THERE MODELS os.chdir(os.environ["WEB2PY_PATH"]) sys.path.append(os.environ["WEB2PY_PATH"]) from gluon.custom_import import custom_import_install custom_import_install(os.environ["WEB2PY_PATH"]) from gluon.shell import env from gluon import DAL, Field new_env = env(NEW_APP, c=None, import_models=True) d = globals().copy() d.update(**new_env) new_str = ''' try: s3db.load_all_models() except NameError: print "s3db not defined" new_db = db ''' exec new_str in d, locals() d.clear() old_env = env(OLD_APP, c=None, import_models=True) old_str = '''
reset_password.py Script reset the password of a auth_user record. """ from __future__ import print_function import getpass import os from optparse import OptionParser from gluon import * from gluon.shell import env from gluon.validators import CRYPT from applications.zcomx.modules.creators import AuthUser from applications.zcomx.modules.logger import set_cli_logging VERSION = 'Version 0.1' APP_ENV = env(__file__.split(os.sep)[-3], import_models=True) # C0103: *Invalid name "%%s" (should match %%s)* # pylint: disable=C0103 db = APP_ENV['db'] # line-too-long (C0301): *Line too long (%%s/%%s)* # pylint: disable=C0301 def man_page(): """Print manual page-like help""" print(""" USAGE reset_password.py [OPTIONS] email [password]
) args = vars(parser.parse_args()) app = args["app"] task = args["task"] allargs = args["allargs"] adir = os.path.join("applications", app) if not os.path.exists(adir): sys.stderr.write("Application not found: %s\n" % adir) sys.exit(1) from gluon.custom_import import custom_import_install custom_import_install() from gluon.shell import env _env = env(app, c=None, import_models=True) globals().update(**_env) # This is present in case this is a first run of the models. db.commit() from gluon import current # Get tasks from the scheduler_task table. if task: query = (db.scheduler_task.task_name == task) else: query = (db.scheduler_task.id > 0) scheduled_tasks = db(query).select(orderby=db.scheduler_task.task_name) # Pick up the associated function objects from the scheduler's task list. # These are also stored in the S3Task instance -- both lists should be the # same.
""" WE ARE LOADING THE 2 ENVIRONMENTS WITH ALL THERE MODELS """ os.chdir(os.environ['WEB2PY_PATH']) sys.path.append(os.environ['WEB2PY_PATH']) from gluon.custom_import import custom_import_install custom_import_install(os.environ['WEB2PY_PATH']) from gluon.shell import env from gluon import DAL, Field new_database_folder = "%s/databases" % (NEW_PATH) new_env = env(NEW_APP, c=None, import_models=True) database_string = "sqlite://storage.sqlite" new_db = DAL( database_string, folder = new_database_folder, auto_import = True, migrate = False ,fake_migrate = True) old_database_folder = "%s/databases" % (OLD_PATH) old_env = env(OLD_APP, c=None, import_models=True) old_db = DAL( database_string, folder = old_database_folder,\ auto_import=True, migrate = False ,fake_migrate = True) print sys.argv """ this is specific to eden thus commented """ #subprocess.call("python web2py.py -S %s -M -R applications/eden2/static/scripts/tools/noop.py" % (NEW_APP),shell=True) #subprocess.call("python web2py.py -S %s -M -R applications/eden2/static/scripts/tools/noop.py" % (OLD_APP),shell=True)
def test____init__(self): # W0212: *Access to a protected member %s of a client class* # pylint: disable=W0212 # # Test with default config file. # app_env = env(APPLICATION, import_models=False) model_db = ModelDb(app_env) self.assertTrue(model_db) # returns object self.assertTrue(model_db.db) # sets db property self.assertEqual(model_db.db._dbname, 'mysql') # db is mysql db self.assertEqual(model_db.db._dbname, 'mysql') # db is mysql db m = \ re.compile(r""" ^(?P<user>[^:@]+)(\:(?P<passwd>[^@]*))? @(?P<host>[^\:/]+) (\:(?P<port>[0-9]+))? / (?P<db>[^?]+) (\?set_encoding=(?P<charset>\w+))? $""", re.VERBOSE).match(model_db.db._uri[8:]) # db has correct application self.assertTrue(m and m.group('db') == APPLICATION) self.assertTrue(model_db.mail) # sets mail property # sender is an email email_re = re.compile(r'[a-zA-Z0-9][email protected]') self.assertTrue(email_re.match(model_db.mail.settings.sender)) self.assertTrue(model_db.auth) # sets auth property # admin_email is an email self.assertTrue(email_re.match(model_db.auth.settings.admin_email)) # mailer setting correct self.assertEqual(model_db.auth.settings.mailer, model_db.mail) self.assertTrue(model_db.crud) # sets crud property # has log_event attr self.assertTrue(hasattr(model_db.crud, 'log_event')) self.assertTrue(model_db.service) # sets service property # service has xmlrpc self.assertTrue(hasattr(model_db.service, 'xmlrpc')) # # Test with custom config file. # config_text = """ [web2py] mail.server = smtp.mymailserver.com:587 mail.sender = [email protected] mail.login = myusername:fakepassword auth.registration_requires_verification = True auth.registration_requires_approval = False auth.admin_email = [email protected] [shared] auth.registration_requires_verification = False auth.registration_requires_approval = True database = shared hmac_key = 226bca51c2121b magento_api_username = api_user magento_api_password = fake_api_password mysql_user = shared mysql_password = {mysql_pw} version = '0.1' """.format(mysql_pw=model_db.local_settings.mysql_password) f_text = '/tmp/TestModelDb_test__init__.txt' _config_file_from_text(f_text, config_text) model_db = ModelDb(app_env, config_file=f_text) self.assertTrue(model_db) self.assertEqual(model_db.mail.settings.server, 'smtp.mymailserver.com:587') self.assertEqual(model_db.mail.settings.sender, '*****@*****.**') self.assertEqual( model_db.auth.settings.registration_requires_verification, False) self.assertEqual(model_db.auth.settings.registration_requires_approval, True) os.unlink(f_text) return