def cleanup(): """ Remove session, cache and error files """ app = request.args[0] # Remove error files files = listdir(apath('%s/errors/' % app), '^\d.*$', 0) for file in files: os.unlink(file) # Remove session files files = listdir(apath('%s/sessions/' % app), '\d.*', 0) for file in files: os.unlink(file) session.flash = T('cache, errors and sessions cleaned') # Remove cache files files = listdir(apath('%s/cache/' % app), 'cache.*', 0) for file in files: try: os.unlink(file) except: session.flash = T('some files could not be removed') redirect(URL(r=request, f='site'))
def clear(): app = request.application files = listdir('applications/%s/cache/' % app,'',0) for file in files: os.unlink(file) files=listdir('applications/%s/errors/' % app,'',0) for file in files: os.unlink(file) session.flash="cache cleaned" redirect(URL(c='manage', f='index'))
def cleanup(): app=request.application files=listdir('applications/%s/cache/' % app,'',0) for file in files: os.unlink(file) files=listdir('applications/%s/errors/' % app,'',0) for file in files: os.unlink(file) files=listdir('applications/%s/sessions/' % app,'',0) for file in files: os.unlink(file) session.flash="cache, errors and sessions cleaned" redirect(URL('index'))
def run_models_in(environment): """ Runs all models (in the app specified by the current folder) It tries pre-compiled models first before compiling them. """ request = current.request folder = request.folder c = request.controller #f = environment['request'].function response = current.response path = pjoin(folder, 'models') cpath = pjoin(folder, 'compiled') compiled = os.path.exists(cpath) if PY2: if compiled: models = sorted(listdir(cpath, '^models[_.][\w.]+\.pyc$', 0), model_cmp) else: models = sorted(listdir(path, '^\w+\.py$', 0, sort=False), model_cmp_sep) else: if compiled: models = sorted(listdir(cpath, '^models[_.][\w.]+\.pyc$', 0), key=lambda f: '{0:03d}'.format(f.count('.')) + f) else: models = sorted( listdir(path, '^\w+\.py$', 0, sort=False), key=lambda f: '{0:03d}'.format(f.count(os.path.sep)) + f) models_to_run = None for model in models: if response.models_to_run != models_to_run: regex = models_to_run = response.models_to_run[:] if isinstance(regex, list): regex = re_compile('|'.join(regex)) if models_to_run: if compiled: n = len(cpath) + 8 fname = model[n:-4].replace('.', '/') + '.py' else: n = len(path) + 1 fname = model[n:].replace(os.path.sep, '/') if not regex.search(fname) and c != 'appadmin': continue elif compiled: code = read_pyc(model) elif is_gae: code = getcfs(model, model, lambda: compile2(read_file(model), model)) else: code = getcfs(model, model, None) restricted(code, environment, layer=model)
def cleanup(): app = request.application files = listdir('applications/%s/cache/' % app, '', 0) for file in files: os.unlink(file) files = listdir('applications/%s/errors/' % app, '', 0) for file in files: os.unlink(file) files = listdir('applications/%s/sessions/' % app, '', 0) for file in files: os.unlink(file) session.flash = "cache, errors and sessions cleaned" redirect(URL('index'))
def run_models_in(environment): """ Runs all models (in the app specified by the current folder) It tries pre-compiled models first before compiling them. """ request = current.request folder = request.folder c = request.controller # f = environment['request'].function response = current.response path = pjoin(folder, 'models') cpath = pjoin(folder, 'compiled') compiled = exists(cpath) if PY2: if compiled: models = sorted(listdir(cpath, REGEX_COMPILED_MODEL, 0), model_cmp) else: models = sorted(listdir(path, REGEX_MODEL, 0, sort=False), model_cmp_sep) else: if compiled: models = sorted(listdir(cpath, REGEX_COMPILED_MODEL, 0), key=lambda f: '{0:03d}'.format(f.count('.')) + f) else: models = sorted(listdir(path, REGEX_MODEL, 0, sort=False), key=lambda f: '{0:03d}'.format(f.count(os.sep)) + f) models_to_run = None for model in models: if response.models_to_run != models_to_run: regex = models_to_run = response.models_to_run[:] if isinstance(regex, list): regex = re_compile('|'.join(regex)) if models_to_run: if compiled: n = len(cpath)+8 fname = model[n:-4].replace('.', '/')+'.py' else: n = len(path)+1 fname = model[n:].replace(os.sep, '/') if not regex.search(fname) and c != 'appadmin': continue elif compiled: f = lambda: read_pyc(model) else: f = lambda: compile2(read_file(model), model) ccode = getcfs(model, model, f) restricted(ccode, environment, layer=model)
def findT(path, language=DEFAULT_LANGUAGE): """ Note: Must be run by the admin app """ from gluon.tools import Auth, Crud lang_file = pjoin(path, 'languages', language + '.py') sentences = read_dict(lang_file) mp = pjoin(path, 'models') cp = pjoin(path, 'controllers') vp = pjoin(path, 'views') mop = pjoin(path, 'modules') def add_message(message): if not message.startswith('#') and not '\n' in message: tokens = message.rsplit('##', 1) else: # this allows markmin syntax in translations tokens = [message] if len(tokens) == 2: message = tokens[0].strip() + '##' + tokens[1].strip() if message and not message in sentences: sentences[message] = message.replace("@markmin\x01", "") for filename in \ listdir(mp, '^.+\.py$', 0) + listdir(cp, '^.+\.py$', 0)\ + listdir(vp, '^.+\.html$', 0) + listdir(mop, '^.+\.py$', 0): data = to_native(read_locked(filename)) items = regex_translate.findall(data) for x in regex_translate_m.findall(data): if x[0:3] in ["'''", '"""']: items.append("%s@markmin\x01%s" % (x[0:3], x[3:])) else: items.append("%s@markmin\x01%s" % (x[0], x[1:])) for item in items: try: message = safe_eval(item) except: continue # silently ignore inproperly formatted strings add_message(message) gluon_msg = [Auth.default_messages, Crud.default_messages] for item in [x for m in gluon_msg for x in m.values() if x is not None]: add_message(item) if not '!langcode!' in sentences: sentences['!langcode!'] = (DEFAULT_LANGUAGE if language in ('default', DEFAULT_LANGUAGE) else language) if not '!langname!' in sentences: sentences['!langname!'] = (DEFAULT_LANGUAGE_NAME if language in ( 'default', DEFAULT_LANGUAGE) else sentences['!langcode!']) write_dict(lang_file, sentences)
def findT(path, language=DEFAULT_LANGUAGE): """ Note: Must be run by the admin app """ from gluon.tools import Auth, Crud lang_file = pjoin(path, 'languages', language + '.py') sentences = read_dict(lang_file) mp = pjoin(path, 'models') cp = pjoin(path, 'controllers') vp = pjoin(path, 'views') mop = pjoin(path, 'modules') def add_message(message): if not message.startswith('#') and not '\n' in message: tokens = message.rsplit('##', 1) else: # this allows markmin syntax in translations tokens = [message] if len(tokens) == 2: message = tokens[0].strip() + '##' + tokens[1].strip() if message and not message in sentences: sentences[message] = message.replace("@markmin\x01", "") for filename in \ listdir(mp, '^.+\.py$', 0) + listdir(cp, '^.+\.py$', 0)\ + listdir(vp, '^.+\.html$', 0) + listdir(mop, '^.+\.py$', 0): data = to_native(read_locked(filename)) items = regex_translate.findall(data) for x in regex_translate_m.findall(data): if x[0:3] in ["'''", '"""']: items.append("%s@markmin\x01%s" %(x[0:3], x[3:])) else: items.append("%s@markmin\x01%s" %(x[0], x[1:])) for item in items: try: message = safe_eval(item) except: continue # silently ignore inproperly formatted strings add_message(message) gluon_msg = [Auth.default_messages, Crud.default_messages] for item in [x for m in gluon_msg for x in m.values() if x is not None]: add_message(item) if not '!langcode!' in sentences: sentences['!langcode!'] = ( DEFAULT_LANGUAGE if language in ('default', DEFAULT_LANGUAGE) else language) if not '!langname!' in sentences: sentences['!langname!'] = ( DEFAULT_LANGUAGE_NAME if language in ('default', DEFAULT_LANGUAGE) else sentences['!langcode!']) write_dict(lang_file, sentences)
def search_components(cls): """Функция поиска компонентов Vue в папке контроллера""" request = current.request views_folder = os.path.join(request.folder, 'views') controller_folder = os.path.join(views_folder, request.controller) components = listdir(controller_folder, '.+\.vue$') components = [os.path.abspath(os.path.join(controller_folder, x)) for x in components] components = [x.decode(locale.getpreferredencoding()).encode() for x in components] components = [(x.split(os.path.sep)[-1][:-4], os.path.relpath(x, views_folder)) for x in components] return dict(components)
def run_models_in(environment): """ Runs all models (in the app specified by the current folder) It tries pre-compiled models first before compiling them. """ request = current.request folder = request.folder c = request.controller #f = environment['request'].function response = current.response path = pjoin(folder, 'models') cpath = pjoin(folder, 'compiled') compiled = os.path.exists(cpath) if compiled: models = sorted(listdir(cpath, '^models[_.][\w.]+\.pyc$', 0), model_cmp) else: models = sorted(listdir(path, '^\w+\.py$', 0, sort=False), model_cmp_sep) models_to_run = None for model in models: if response.models_to_run != models_to_run: regex = models_to_run = response.models_to_run[:] if isinstance(regex, list): regex = re_compile('|'.join(regex)) if models_to_run: if compiled: n = len(cpath)+8 fname = model[n:-4].replace('.','/')+'.py' else: n = len(path)+1 fname = model[n:].replace(os.path.sep,'/') if not regex.search(fname) and c != 'appadmin': continue elif compiled: code = read_pyc(model) elif is_gae: code = getcfs(model, model, lambda: compile2(read_file(model), model)) else: code = getcfs(model, model, None) restricted(code, environment, layer=model)
def remove_compiled_application(folder): """ Deletes the folder `compiled` containing the compiled application. """ try: shutil.rmtree(pjoin(folder, 'compiled')) path = pjoin(folder, 'controllers') for file in listdir(path, '.*\.pyc$', drop=False): os.unlink(file) except OSError: pass
def remove_compiled_application(folder): """ Deletes the folder `compiled` containing the compiled application. """ try: shutil.rmtree(pjoin(folder, 'compiled')) path = pjoin(folder, 'controllers') for file in listdir(path, REGEX_COMPILED_CONTROLLER, drop=False): os.unlink(file) except OSError: pass
def findT(path, language=DEFAULT_LANGUAGE): """ Note: Must be run by the admin app """ lang_file = pjoin(path, 'languages', language + '.py') sentences = read_dict(lang_file) mp = pjoin(path, 'models') cp = pjoin(path, 'controllers') vp = pjoin(path, 'views') mop = pjoin(path, 'modules') for filename in \ listdir(mp, '^.+\.py$', 0) + listdir(cp, '^.+\.py$', 0)\ + listdir(vp, '^.+\.html$', 0) + listdir(mop, '^.+\.py$', 0): data = read_locked(filename) items = regex_translate.findall(data) items += regex_translate_m.findall(data) for item in items: try: message = safe_eval(item) except: continue # silently ignore inproperly formatted strings if not message.startswith('#') and not '\n' in message: tokens = message.rsplit('##', 1) else: # this allows markmin syntax in translations tokens = [message] if len(tokens) == 2: message = tokens[0].strip() + '##' + tokens[1].strip() if message and not message in sentences: sentences[message] = message if not '!langcode!' in sentences: sentences['!langcode!'] = ( DEFAULT_LANGUAGE if language in ('default', DEFAULT_LANGUAGE) else language) if not '!langname!' in sentences: sentences['!langname!'] = ( DEFAULT_LANGUAGE_NAME if language in ('default', DEFAULT_LANGUAGE) else sentences['!langcode!']) write_dict(lang_file, sentences)
def compile_models(folder): """ Compiles all the models in the application specified by `folder` """ path = pjoin(folder, 'models') for file in listdir(path, '.+\.py$'): data = read_file(pjoin(path, file)) filename = pjoin(folder, 'compiled', 'models', file) mktree(filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def compile_models(folder): """ Compiles all the models in the application specified by `folder` """ path = pjoin(folder, 'models') for fname in listdir(path, REGEX_MODEL_PATH): data = read_file(pjoin(path, fname)) modelfile = 'models.'+fname.replace(os.sep, '.') filename = pjoin(folder, 'compiled', modelfile) mktree(filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def test(): """ Execute controller tests """ app = request.args[0] if len(request.args) > 1: file = request.args[1] else: file = '.*\.py' controllers = listdir(apath('%s/controllers/' % app), file + '$') return dict(app=app, controllers=controllers)
def findT(path, language=DEFAULT_LANGUAGE): """ must be run by the admin app """ lang_file = pjoin(path, "languages", language + ".py") sentences = read_dict(lang_file) mp = pjoin(path, "models") cp = pjoin(path, "controllers") vp = pjoin(path, "views") mop = pjoin(path, "modules") for filename in ( listdir(mp, "^.+\.py$", 0) + listdir(cp, "^.+\.py$", 0) + listdir(vp, "^.+\.html$", 0) + listdir(mop, "^.+\.py$", 0) ): data = read_locked(filename) items = regex_translate.findall(data) for item in items: try: message = safe_eval(item) except: continue # silently ignore inproperly formatted strings if not message.startswith("#") and not "\n" in message: tokens = message.rsplit("##", 1) else: # this allows markmin syntax in translations tokens = [message] if len(tokens) == 2: message = tokens[0].strip() + "##" + tokens[1].strip() if message and not message in sentences: sentences[message] = message if not "!langcode!" in sentences: sentences["!langcode!"] = DEFAULT_LANGUAGE if language in ("default", DEFAULT_LANGUAGE) else language if not "!langname!" in sentences: sentences["!langname!"] = ( DEFAULT_LANGUAGE_NAME if language in ("default", DEFAULT_LANGUAGE) else sentences["!langcode!"] ) write_dict(lang_file, sentences)
def findT(path, language=DEFAULT_LANGUAGE): """ Note: Must be run by the admin app """ lang_file = pjoin(path, 'languages', language + '.py') sentences = read_dict(lang_file) mp = pjoin(path, 'models') cp = pjoin(path, 'controllers') vp = pjoin(path, 'views') mop = pjoin(path, 'modules') for filename in \ listdir(mp, '^.+\.py$', 0) + listdir(cp, '^.+\.py$', 0)\ + listdir(vp, '^.+\.html$', 0) + listdir(mop, '^.+\.py$', 0): data = read_locked(filename) items = regex_translate.findall(data) items += regex_translate_m.findall(data) for item in items: try: message = safe_eval(item) except: continue # silently ignore inproperly formatted strings if not message.startswith('#') and not '\n' in message: tokens = message.rsplit('##', 1) else: # this allows markmin syntax in translations tokens = [message] if len(tokens) == 2: message = tokens[0].strip() + '##' + tokens[1].strip() if message and not message in sentences: sentences[message] = message if not '!langcode!' in sentences: sentences['!langcode!'] = (DEFAULT_LANGUAGE if language in ('default', DEFAULT_LANGUAGE) else language) if not '!langname!' in sentences: sentences['!langname!'] = (DEFAULT_LANGUAGE_NAME if language in ( 'default', DEFAULT_LANGUAGE) else sentences['!langcode!']) write_dict(lang_file, sentences)
def compile_models(folder): """ Compiles all the models in the application specified by `folder` """ path = pjoin(folder, 'models') for fname in listdir(path, REGEX_MODEL_PATH, followlinks=True): data = read_file(pjoin(path, fname)) modelfile = 'models.' + fname.replace(os.sep, '.') filename = pjoin(folder, 'compiled', modelfile) mktree(filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def errors(): """ Error handler """ from gluon.admin import apath from gluon.fileutils import listdir for item in request.vars: if item[:7] == "delete_": os.unlink(apath("%s/errors/%s" % (appname, item[7:]), r=request)) func = lambda p: os.stat(apath("%s/errors/%s" % (appname, p), r=request)).st_mtime tickets = sorted(listdir(apath("%s/errors/" % appname, r=request), "^\w.*"), key=func, reverse=True) return dict(app=appname, tickets=tickets)
def compile_models(folder): """ Compiles all the models in the application specified by `folder` """ path = pjoin(folder, "models") for fname in listdir(path, ".+\.py$"): data = read_file(pjoin(path, fname)) modelfile = "models." + fname.replace(os.path.sep, ".") filename = pjoin(folder, "compiled", modelfile) mktree(filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def errors(): """ Error handler """ app = request.args[0] for item in request.vars: if item[:7] == 'delete_': os.unlink(apath('%s/errors/%s' % (app, item[7:]))) func = lambda p: os.stat(apath('%s/errors/%s' % (app, p))).st_mtime tickets = sorted(listdir(apath('%s/errors/' % app), '^\w.*'), key=func, reverse=True) return dict(app=app, tickets=tickets)
def run_models_in(environment): """ Runs all models (in the app specified by the current folder) It tries pre-compiled models first before compiling them. """ folder = environment['request'].folder c = environment['request'].controller f = environment['request'].function cpath = pjoin(folder, 'compiled') if os.path.exists(cpath): for model in listdir(cpath, '^models_\w+\.pyc$', 0): restricted(read_pyc(model), environment, layer=model) path = pjoin(cpath, 'models') models = listdir(path, '^\w+\.pyc$', 0, sort=True) compiled = True else: path = pjoin(folder, 'models') models = listdir(path, '^\w+\.py$', 0, sort=True) compiled = False n = len(path) + 1 for model in models: regex = environment['response'].models_to_run if isinstance(regex, list): regex = re_compile('|'.join(regex)) file = model[n:].replace(os.path.sep, '/').replace('.pyc', '.py') if not regex.search(file) and c != 'appadmin': continue elif compiled: code = read_pyc(model) elif is_gae: code = getcfs(model, model, lambda: compile2(read_file(model), model)) else: code = getcfs(model, model, None) restricted(code, environment, layer=model)
def run_models_in(environment): """ Runs all models (in the app specified by the current folder) It tries pre-compiled models first before compiling them. """ folder = environment['request'].folder c = environment['request'].controller f = environment['request'].function cpath = pjoin(folder, 'compiled') if os.path.exists(cpath): for model in listdir(cpath, '^models_\w+\.pyc$', 0): restricted(read_pyc(model), environment, layer=model) path = pjoin(cpath, 'models') models = listdir(path, '^\w+\.pyc$', 0, sort=False) compiled = True else: path = pjoin(folder, 'models') models = listdir(path, '^\w+\.py$', 0, sort=False) compiled = False n = len(path) + 1 for model in models: regex = environment['response'].models_to_run if isinstance(regex, list): regex = re_compile('|'.join(regex)) file = model[n:].replace(os.path.sep, '/').replace('.pyc', '.py') if not regex.search(file) and c != 'appadmin': continue elif compiled: code = read_pyc(model) elif is_gae: code = getcfs(model, model, lambda: compile2(read_file(model), model)) else: code = getcfs(model, model, None) restricted(code, environment, layer=model)
def errors(): """ Error handler """ app = request.application for item in request.vars: if item[:7] == "delete_": os.unlink(apath("%s/errors/%s" % (app, item[7:]), r=request)) func = lambda p: os.stat(apath("%s/errors/%s" % (app, p), r=request)).st_mtime tickets = sorted(listdir(apath("%s/errors/" % app, r=request), "^\w.*"), key=func, reverse=True) return dict(app=app, tickets=tickets)
def compile_views(folder): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') for fname in listdir(path, '^[\w/\-]+(\.\w+)*$'): try: data = parse_template(fname, path) except Exception, e: raise Exception("%s in %s" % (e, fname)) filename = 'views.%s.py' % fname.replace(os.path.sep, '.') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def compile_controllers(folder): """ Compiles all the controllers in the application specified by `folder` """ path = pjoin(folder, "controllers") for fname in listdir(path, ".+\.py$"): ### why is this here? save_pyc(pjoin(path, file)) data = read_file(pjoin(path, fname)) exposed = find_exposed_functions(data) for function in exposed: command = data + "\nresponse._vars=response._caller(%s)\n" % function filename = pjoin(folder, "compiled", "controllers.%s.%s.py" % (fname[:-3], function)) write_file(filename, command) save_pyc(filename) os.unlink(filename)
def compile_controllers(folder): """ Compiles all the controllers in the application specified by `folder` """ path = pjoin(folder, 'controllers') for fname in listdir(path, REGEX_CONTROLLER): data = read_file(pjoin(path, fname)) exposed = find_exposed_functions(data) for function in exposed: command = data + "\nresponse._vars=response._caller(%s)\n" % \ function filename = pjoin(folder, 'compiled', 'controllers.%s.%s.py' % (fname[:-3], function)) write_file(filename, command) save_pyc(filename) os.unlink(filename)
def compile_controllers(folder): """ Compiles all the controllers in the application specified by `folder` """ path = pjoin(folder, 'controllers') for fname in listdir(path, REGEX_CONTROLLER, followlinks=True): data = read_file(pjoin(path, fname)) exposed = find_exposed_functions(data) for function in exposed: command = data + "\nresponse._vars=response._caller(%s)\n" % \ function filename = pjoin(folder, 'compiled', 'controllers.%s.%s.py' % (fname[:-3], function)) write_file(filename, command) save_pyc(filename) os.unlink(filename)
def compile_controllers(folder): """ Compiles all the controllers in the application specified by `folder` """ path = pjoin(folder, 'controllers') for fname in listdir(path, '.+\.py$'): ### why is this here? save_pyc(pjoin(path, file)) data = read_file(pjoin(path, fname)) exposed = regex_expose.findall(data) for function in exposed: command = data + "\nresponse._vars=response._caller(%s)\n" % \ function filename = pjoin(folder, 'compiled', 'controllers.%s.%s.py' % (fname[:-3], function)) write_file(filename, command) save_pyc(filename) os.unlink(filename)
def compile_controllers(folder): """ Compiles all the controllers in the application specified by `folder` """ path = pjoin(folder, 'controllers') for file in listdir(path, '.+\.py$'): ### why is this here? save_pyc(pjoin(path, file)) data = read_file(pjoin(path, file)) exposed = regex_expose.findall(data) for function in exposed: command = data + "\nresponse._vars=response._caller(%s)\n" % \ function filename = pjoin(folder, 'compiled', ('controllers/' + file[:-3]).replace('/', '_') + '_' + function + '.py') write_file(filename, command) save_pyc(filename) os.unlink(filename)
def compile_views(folder, skip_failed_views=False): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') failed_views = [] for fname in listdir(path, '^[\w/\-]+(\.\w+)*$'): try: data = parse_template(fname, path) except Exception, e: if skip_failed_views: failed_views.append(file) else: raise Exception("%s in %s" % (e, file)) else: filename = ('views/%s.py' % file).replace('/', '_').replace('\\', '_') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def compile_views(folder, skip_failed_views=False): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') failed_views = [] for fname in listdir(path, '^[\w/\-]+(\.\w+)*$'): try: data = parse_template(fname, path) except Exception, e: if skip_failed_views: failed_views.append(fname) else: raise Exception("%s in %s" % (e, fname)) else: filename = ('views/%s.py' % fname).replace('/', '_').replace('\\', '_') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def compile_views(folder, skip_failed_views=False): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') failed_views = [] for fname in listdir(path, REGEX_VIEW_PATH, followlinks=True): try: data = parse_template(fname, path) except Exception as e: if skip_failed_views: failed_views.append(fname) else: # FIXME: should raise something more specific than Exception raise Exception("%s in %s" % (e, fname)) else: filename = 'views.%s.py' % fname.replace(os.sep, '.') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename) return failed_views or None
def compile_views(folder, skip_failed_views=False): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') failed_views = [] for fname in listdir(path, REGEX_VIEW_PATH): try: data = parse_template(fname, path) except Exception as e: if skip_failed_views: failed_views.append(fname) else: # FIXME: should raise something more specific than Exception raise Exception("%s in %s" % (e, fname)) else: filename = 'views.%s.py' % fname.replace(os.sep, '.') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename) return failed_views or None
update_check_needed = True if update_check_needed: # Run update checks -- these are the update_check() functions from each # Python file in private/update_check that has such a function. from gluon.fileutils import listdir update_check_path_parts = [ "applications", request.application, "private", "update_check"] update_check_path = os.path.join(*update_check_path_parts) update_check_import_path = ".".join(update_check_path_parts) errors = [] warnings = [] # Supply the current (Web2py) environment. Pick out only the items that are # safe for the check functions to combine with their own environments, i.e. # not anything of the form __x__. environment = dict((k, v) for (k, v) in globals().iteritems() if not k.startswith("__")) for filename in listdir(update_check_path, expression = ".*\.py$"): try: exec "from %s.%s import update_check" % \ (update_check_import_path, filename[0:-3]) except ImportError: continue messages = update_check(environment) errors.extend(messages.get("error_messages", [])) warnings.extend(messages.get("warning_messages", [])) # Catch-all check for dependency errors. # @ToDo: This does not satisfy the goal of calling out all the setup errors # at once -- it will die on the first fatal error encountered. try: import s3 as s3base except Exception, e:
def serverTask(accountId, appId, devPhase, region, kvCheck, pbPath=None): # TODO: it would be nice to store these in a nice protected dict # and then write out the key to disk only for running the playbook... # We could put all of them in and identify a default key. Then if we # have an entry for the ssh key identified by the host, use that.... sshKeyFilePath = "/home/ec2-user/.ssh/msca-devops.pem" # Directory to write out inventories and playbooks... runtimeDir = "/data/parapet/" grid = {} grid['validHosts'] = {} logger.debug("Task UUID: {0}".format(W2P_TASK.uuid)) logger.debug("Account ID: {0}".format(accountId)) if int(accountId) == 1: logger.debug("Setting account Query to all accounts") accountQuery = db.hostInfo.accountNumber > 1 else: accountQuery = db.hostInfo.accountNumber == accountId logger.debug("Application: '{0}'".format(appId)) if appId == "All Applications": appQuery = db.hostInfo.app.like('%') else: appQuery = db.hostInfo.app == appId logger.debug("DevPhase: {0}".format(devPhase)) if len(devPhase) > 1: devQuery = db.hostInfo.devPhase == devPhase else: logger.debug("Setting devPhase to %") devQuery = db.hostInfo.devPhase.like('%') logger.debug("Region: {0}".format(region)) if len(region) > 1: regionQuery = db.hostInfo.region == region else: logger.debug("Setting region to %") regionQuery = db.hostInfo.region.like('%') logger.debug("hostFilter: {0}".format(kvCheck)) hostFilter = json.loads(kvCheck) try: hostFilter['awsInfo']['ec2_state'] = 'running' except KeyError: hostFilter['awsInfo'] = {} hostFilter['awsInfo']['ec2_state'] = 'running' logger.debug("HF: {0}".format(hostFilter)) # Get the hosts that match the base query dbQuery = ((accountQuery)&(appQuery)&(devQuery)&(regionQuery)) s = db(dbQuery) rows = s.select() # Iterate through the core hosts and apply the hostFilter for row in rows: # Get the host data from the notes field hostNotes = json.loads(row['notes']) # Verify that all of the things in the hostFilter are true for key in hostFilter.keys(): if hostNotes.has_key(key): for check in hostFilter[key].keys(): try: if hostFilter[key][check] == hostNotes[key][check]: if grid['validHosts'].has_key(row['instance_id']) is False: # Passes the test, set the AWS instanceID to the databaseID grid['validHosts'][row['instance_id']] = row['id'] # If this host has already failed a prior test, don't add it now elif grid['validHosts'][row['instance_id']] is None: pass else: # Host fails the test, set it to None (clean it up later) grid['validHosts'][row['instance_id']] = None except KeyError: # If the host doesn't have a matching key, then it doesn't match the filter grid['validHosts'][row['instance_id']] = None # Get rid of the hosts that don't match the hostFilter for key in grid['validHosts'].keys(): if grid['validHosts'][key] is None: del grid['validHosts'][key] logger.debug("HostIDs: {0}".format(grid['validHosts'].values())) logger.debug("This search found {0} hosts".format(len(grid['validHosts']))) # Download and parse playbook file here... write it out as: # runtimeDir/Task_UUID.yml # use serializers.loads_yaml() if pbPath: pbData = serializers.loads_yaml(urllib2.urlopen(pbPath).read()) hostGroup = pbData[0]['hosts'] fileutils.write_file(os.path.join(runtimeDir,W2P_TASK.uuid+".yml"),serializers.yaml(pbData)) # Generate inventory file # runtimeDir/Task_UUID.inv # Need to parse out teh playbook file first to determine what group we should put the hosts in. invHosts = "[{0}]\n".format(hostGroup) for row in db(db.hostInfo.id.belongs(grid['validHosts'].values())).select(): hostNotes = serializers.loads_json(row.notes) if utils.is_valid_ip_address(hostNotes['awsInfo']['ec2_ip_address']): sshKeyFilePath = os.path.join(runtimeDir,W2P_TASK.uuid+hostNotes['awsInfo']['ec2_ip_address']+".key") try: thisHostKey = serverTask_config.keyData[hostNotes['awsInfo']['ec2_key_name']] sshKeyFilePath = os.path.join(runtimeDir,W2P_TASK.uuid+hostNotes['awsInfo']['ec2_ip_address']+".key") except KeyError: logger.debug("Unable to find a key named {0} using default".format(hostNotes['awsInfo']['ec2_key_name'])) thisHostKey = serverTask_config.keyData['default'] fileutils.write_file(sshKeyFilePath,thisHostKey) os.chmod(sshKeyFilePath,0600) thisHostString = "{0} ansible_ssh_host={1} ansible_ssh_private_key_file={2} ansible_ssh_user=ec2-user\n".format(row.instance_id,hostNotes['awsInfo']['ec2_ip_address'],sshKeyFilePath) invHosts = "{0} {1}".format(invHosts,thisHostString) else: logger.warn("{0} is not a valid IP address".format(hostNotes['awsInfo']['ec2_ip_address'])) fileutils.write_file(os.path.join(runtimeDir,W2P_TASK.uuid+".inv"),invHosts) # Run the task cmdLine = "/usr/bin/ansible-playbook -e 'parapetServer=true' -vvv -i {invFile} {pbFile}".format(invFile=os.path.join(runtimeDir,W2P_TASK.uuid+".inv"),pbFile=os.path.join(runtimeDir,W2P_TASK.uuid+".yml")) logger.debug("{0}".format(cmdLine)) #ansibleOut = subprocess.Popen(cmdLine, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] ansible = subprocess.Popen(cmdLine,shell=True,cwd=runtimeDir,stdout=subprocess.PIPE) while ansible.poll() is None: # Only 1 newline?? print ansible.stdout.readline(), #logger.info(ansibleOut) #print ansibleOut try: keyFiles = fileutils.listdir(runtimeDir,expression="^"+W2P_TASK.uuid+".*\.key$", drop=False) for keyFile in keyFiles: logger.debug("Removing: {0}".format(keyFile)) fileutils.recursive_unlink(keyFile) except: logger.error("Unable to remove key files") return 0
def design(): """ Application design handler """ app = request.args[0] if not response.flash and app == request.application: msg = T('ATTENTION: you cannot edit the running application!') response.flash = msg # If we have only pyc files it means that # we cannot design if os.path.exists(apath('%s/compiled' % app)): session.flash = \ T('application is compiled and cannot be designed') redirect(URL(r=request, f='site')) # Get all models models = listdir(apath('%s/models/' % app), '.*\.py$') defines = {} for m in models: data = open(apath('%s/models/%s' % (app, m)), 'r').read() defines[m] = regex_tables.findall(data) defines[m].sort() # Get all controllers controllers = sorted(listdir(apath('%s/controllers/' % app), '.*\.py$')) functions = {} for c in controllers: data = open(apath('%s/controllers/%s' % (app, c)), 'r').read() items = regex_expose.findall(data) functions[c] = items # Get all views views = sorted(listdir(apath('%s/views/' % app), '.*\.html$')) extend = {} include = {} for c in views: data = open(apath('%s/views/%s' % (app, c)), 'r').read() items = regex_extend.findall(data) if items: extend[c] = items[0][1] items = regex_include.findall(data) include[c] = [i[1] for i in items] # Get all modules modules = listdir(apath('%s/modules/' % app), '.*\.py$') modules.sort() # Get all static files statics = listdir(apath('%s/static/' % app), '[^\.#].*') statics.sort() # Get all languages languages = listdir(apath('%s/languages/' % app), '[\w-]*\.py') return dict(app=app, models=models, defines=defines, controllers=controllers, functions=functions, views=views, modules=modules, extend=extend, include=include, statics=statics, languages=languages,)
update_check_needed = True if update_check_needed: # Run update checks -- these are the update_check() functions from each # Python file in private/update_check that has such a function. from gluon.fileutils import listdir update_check_path_parts = [ "applications", request.application, "private", "update_check"] update_check_path = os.path.join(*update_check_path_parts) update_check_import_path = ".".join(update_check_path_parts) errors = [] warnings = [] # Supply the current (Web2py) environment. Pick out only the items that are # safe for the check functions to combine with their own environments, i.e. # not anything of the form __x__. environment = dict((k, v) for (k, v) in globals().iteritems() if not k.startswith("__")) for filename in listdir(update_check_path, expression = ".*\.py$"): try: exec "from %s.%s import update_check" % \ (update_check_import_path, filename[0:-3]) except ImportError: continue messages = update_check(environment) errors.extend(messages.get("error_messages", [])) warnings.extend(messages.get("warning_messages", [])) # Catch-all check for dependency errors. # @ToDo: This does not satisfy the goal of calling out all the setup errors # at once -- it will die on the first fatal error encountered. try: import s3 as s3base except Exception, errmsg:
def _get_statics(): statics = listdir(apath('sqlabs/static/%s/' % plugin_name, r=request), '[^\.#].*') statics = [x.replace('\\', '/') for x in statics] statics.sort() return statics
except TypeError: safe_file = open(a, b) try: return safe_file.read() finally: safe_file.close() def find_exposed_functions(dt): dt = regex_longcomments.sub('', dt) return regex_expose.findall(dt) def beauty_text(t): parts = t.split('_') return ' '.join([x.title() for x in parts]) response.tmpmenu = UL(_class='collapsible') controllers = sorted( listdir(apath('ecommerce/controllers/', r=request), '.*\.py$')) controllers = [x.replace('\\', '/') for x in controllers] for c in controllers: li = LI() _c = c[:-3] data = safe_read(apath('ecommerce/controllers/%s' % c, r=request)) items = find_exposed_functions(data) li.append(DIV(beauty_text(_c), _class='collapsible-header')) body = UL() for i in sorted(items): body.append(LI(A(beauty_text(i), _href=URL(_c, i)))) li.append(DIV( body, _class='collapsible-body')) response.tmpmenu.append(li)
def design(): """ Application design handler """ app = request.args[0] if not response.flash and app == request.application: msg = T('ATTENTION: you cannot edit the running application!') response.flash = msg # If we have only pyc files it means that # we cannot design if os.path.exists(apath('%s/compiled' % app)): session.flash = \ T('application is compiled and cannot be designed') redirect(URL(r=request, f='site')) # Get all models models = listdir(apath('%s/models/' % app), '.*\.py$') defines = {} for m in models: data = open(apath('%s/models/%s' % (app, m)), 'r').read() defines[m] = regex_tables.findall(data) defines[m].sort() # Get all controllers controllers = sorted(listdir(apath('%s/controllers/' % app), '.*\.py$')) functions = {} for c in controllers: data = open(apath('%s/controllers/%s' % (app, c)), 'r').read() items = regex_expose.findall(data) functions[c] = items # Get all views views = sorted(listdir(apath('%s/views/' % app), '.*\.html$')) extend = {} include = {} for c in views: data = open(apath('%s/views/%s' % (app, c)), 'r').read() items = regex_extend.findall(data) if items: extend[c] = items[0][1] items = regex_include.findall(data) include[c] = [i[1] for i in items] # Get all modules modules = listdir(apath('%s/modules/' % app), '.*\.py$') modules.sort() # Get all static files statics = listdir(apath('%s/static/' % app), '[^\.#].*') statics.sort() # Get all languages languages = listdir(apath('%s/languages/' % app), '[\w-]*\.py') return dict( app=app, models=models, defines=defines, controllers=controllers, functions=functions, views=views, modules=modules, extend=extend, include=include, statics=statics, languages=languages, )
def _get_statics(): statics = listdir(apath('sqlabs/static/%s/' % plugin_name, r=request), '[^\.#].*') statics = [x.replace('\\','/') for x in statics] statics.sort() return statics