예제 #1
0
def compile_controllers(folder):
    """
    compiles all the controllers in the applicaiton specified by the
    current folder
    """

    path = os.path.join(folder, "controllers/")
    for file in listdir(path, ".+\.py$"):
        save_pyc(os.path.join(path, file))
        data = open(path + file, "r").read()
        exposed = regex_expose.findall(data)
        for function in exposed:
            command = (
                data
                + """

response._vars=response._caller(%s)"""
                % function
            )
            filename = os.path.join(
                folder, "compiled/", ("controllers/" + file[:-3]).replace("/", "_") + "_" + function + ".py"
            )
            open(filename, "w").write(command)
            save_pyc(filename)
            os.unlink(filename)
예제 #2
0
파일: compileapp.py 프로젝트: mariuz/web2py
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!

    folder = environment["request"].folder
    path = pjoin(folder, "compiled")
    badc = "invalid controller (%s/%s)" % (controller, function)
    badf = "invalid function (%s/%s)" % (controller, function)
    if os.path.exists(path):
        filename = pjoin(path, "controllers_%s_%s.pyc" % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(404, rewrite.thread.routes.error_message % badf, web2py_error=badf)
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == "_TEST":
        # TESTING: adjust the path to include site packages
        from settings import global_settings
        from admin import abspath, add_path_first

        paths = (global_settings.gluon_parent, abspath("site-packages", gluon=True), abspath("gluon", gluon=True), "")
        [add_path_first(path) for path in paths]
        # TESTING END

        filename = pjoin(folder, "controllers/%s.py" % controller)
        if not os.path.exists(filename):
            raise HTTP(404, rewrite.thread.routes.error_message % badc, web2py_error=badc)
        environment["__symbols__"] = environment.keys()
        code = read_file(filename)
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = pjoin(folder, "controllers/%s.py" % controller)
        if not os.path.exists(filename):
            raise HTTP(404, rewrite.thread.routes.error_message % badc, web2py_error=badc)
        code = read_file(filename)
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(404, rewrite.thread.routes.error_message % badf, web2py_error=badf)
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ":" + function
            code = getcfs(layer, filename, lambda: compile2(code, layer))
        restricted(code, environment, filename)
    response = environment["response"]
    vars = response._vars
    if response.postprocessing:
        for p in response.postprocessing:
            vars = p(vars)
    if isinstance(vars, unicode):
        vars = vars.encode("utf8")
    elif hasattr(vars, "xml") and callable(vars.xml):
        vars = vars.xml()
    return vars
예제 #3
0
def run_controller_in(controller, function, environment):
    """
    runs the controller.function() (for the app specified by 
    the current folder)  in the environment. 
    it tries precompiled controller_function.pyc first.
    """

    # if compiled should run compiled!

    folder = environment["request"].folder
    path = os.path.join(folder, "compiled/")
    if os.path.exists(path):
        filename = os.path.join(path, "controllers_%s_%s.pyc" % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(400, error_message_custom % "invalid function", web2py_error="invalid function")
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == "_TEST":
        filename = os.path.join(folder, "controllers/%s.py" % controller)
        if not os.path.exists(filename):
            raise HTTP(400, error_message_custom % "invalid controller", web2py_error="invalid controller")
        environment["__symbols__"] = environment.keys()
        code = open(filename, "r").read()
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, "controllers/%s.py" % controller)
        if not os.path.exists(filename):
            raise HTTP(400, error_message_custom % "invalid controller", web2py_error="invalid controller")
        code = open(filename, "r").read()
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(400, error_message_custom % "invalid function", web2py_error="invalid function")
        code = """%s

response._vars=response._caller(%s)
""" % (
            code,
            function,
        )
        if is_gae:
            layer = filename + ":" + function
            code = getcfs(layer, filename, lambda: compile(code.replace("\r\n", "\n"), layer, "exec"))
        restricted(code, environment, filename)
    response = environment["response"]
    if response.postprocessing:
        for p in response.postprocessing:
            response._vars = p(response._vars)
    if type(response._vars) == types.StringType:
        response.body = response._vars
    elif type(response._vars) == types.GeneratorType:
        response.body = response._vars
    elif type(response._vars) != types.DictType:
        response.body = str(response._vars)
예제 #4
0
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)
예제 #5
0
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)
예제 #6
0
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)
예제 #7
0
def compile_controllers(folder):
    """
    Compiles all the controllers in the application specified by `folder`
    """

    path = os.path.join(folder, 'controllers/')
    for file in listdir(path, '.+\.py$'):
        save_pyc(os.path.join(path, file))
        fp = open(path + file, 'r')
        data = fp.read()
        fp.close()
        exposed = regex_expose.findall(data)
        for function in exposed:
            command = data + "\nresponse._vars=response._caller(%s)\n" % \
                function
            filename = os.path.join(folder, 'compiled/', ('controllers/'
                                     + file[:-3]).replace('/', '_')
                                     + '_' + function + '.py')
            fp = open(filename, 'w')
            fp.write(command)
            fp.close()
            save_pyc(filename)
            os.unlink(filename)
예제 #8
0
def compile_controllers(folder):
    """
    compiles all the controllers in the applicaiton specified by the
    current folder
    """

    path = os.path.join(folder, 'controllers/')
    for file in listdir(path, '.+\.py$'):
        save_pyc(os.path.join(path, file))
        data = open(path + file, 'r').read()
        exposed = regex_expose.findall(data)
        for function in exposed:
            command = data + '''

response._vars=response._caller(%s)'''\
                 % function
            filename = os.path.join(
                folder, 'compiled/',
                ('controllers/' + file[:-3]).replace('/', '_') + '_' +
                function + '.py')
            open(filename, 'w').write(command)
            save_pyc(filename)
            os.unlink(filename)
예제 #9
0
def compile_controllers(folder):
    """
    Compiles all the controllers in the application specified by `folder`
    """

    path = os.path.join(folder, 'controllers/')
    for file in listdir(path, '.+\.py$'):
        save_pyc(os.path.join(path, file))
        fp = open(path + file, 'r')
        data = fp.read()
        fp.close()
        exposed = regex_expose.findall(data)
        for function in exposed:
            command = data + "\nresponse._vars=response._caller(%s)\n" % \
                function
            filename = os.path.join(folder, 'compiled/', ('controllers/'
                                     + file[:-3]).replace('/', '_')
                                     + '_' + function + '.py')
            fp = open(filename, 'w')
            fp.write(command)
            fp.close()
            save_pyc(filename)
            os.unlink(filename)
예제 #10
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!
    folder = environment['request'].folder
    path = pjoin(folder, 'compiled')
    badc = 'invalid controller (%s/%s)' % (controller, function)
    badf = 'invalid function (%s/%s)' % (controller, function)
    if os.path.exists(path):
        filename = pjoin(path, 'controllers_%s_%s.pyc'
                         % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badf,
                       web2py_error=badf)
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        # TESTING: adjust the path to include site packages
        from settings import global_settings
        from admin import abspath, add_path_first
        paths = (global_settings.gluon_parent, abspath(
            'site-packages', gluon=True), abspath('gluon', gluon=True), '')
        [add_path_first(path) for path in paths]
        # TESTING END

        filename = pjoin(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badc,
                       web2py_error=badc)
        environment['__symbols__'] = environment.keys()
        code = read_file(filename)
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = pjoin(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badc,
                       web2py_error=badc)
        code = read_file(filename)
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badf,
                       web2py_error=badf)
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename, lambda: compile2(code, layer))
        restricted(code, environment, filename)
    response = environment['response']
    vars = response._vars
    if response.postprocessing:
        vars = reduce(lambda vars, p: p(vars), response.postprocessing, vars)
    if isinstance(vars, unicode):
        vars = vars.encode('utf8')
    elif hasattr(vars, 'xml') and callable(vars.xml):
        vars = vars.xml()
    return vars
예제 #11
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!
    folder = environment['request'].folder
    path = pjoin(folder, 'compiled')
    badc = 'invalid controller (%s/%s)' % (controller, function)
    badf = 'invalid function (%s/%s)' % (controller, function)
    if os.path.exists(path):
        filename = pjoin(path, 'controllers_%s_%s.pyc'
                                 % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badf,
                       web2py_error=badf)
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        # TESTING: adjust the path to include site packages
        from settings import global_settings
        from admin import abspath, add_path_first
        paths = (global_settings.gluon_parent, abspath('site-packages', gluon=True),  abspath('gluon', gluon=True), '')
        [add_path_first(path) for path in paths]
        # TESTING END

        filename = pjoin(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badc,
                       web2py_error=badc)
        environment['__symbols__'] = environment.keys()
        code = read_file(filename)
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = pjoin(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badc,
                       web2py_error=badc)
        code = read_file(filename)
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badf,
                       web2py_error=badf)
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename,  lambda: compile2(code,layer))
        restricted(code, environment, filename)
    response = environment['response']
    vars=response._vars
    if response.postprocessing:
        vars = reduce(lambda vars, p: p(vars), response.postprocessing, vars)
    if isinstance(vars,unicode):
        vars = vars.encode('utf8')
    elif hasattr(vars,'xml') and callable(vars.xml):
        vars = vars.xml()
    return vars
예제 #12
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled')
    badc = 'invalid controller (%s/%s)' % (controller, function)
    badf = 'invalid function (%s/%s)' % (controller, function)
    if os.path.exists(path):
        filename = os.path.join(
            path, 'controllers_%s_%s.pyc' % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badf,
                       web2py_error=badf)
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        filename = os.path.join(folder, 'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badc,
                       web2py_error=badc)
        environment['__symbols__'] = environment.keys()
        fp = open(filename, 'r')
        code = fp.read()
        fp.close()
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, 'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badc,
                       web2py_error=badc)
        fp = open(filename, 'r')
        code = fp.read()
        fp.close()
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badf,
                       web2py_error=badf)
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename, lambda: compile2(code, layer))
        restricted(code, environment, filename)
    response = environment['response']
    vars = response._vars
    if response.postprocessing:
        for p in response.postprocessing:
            vars = p(vars)
    if isinstance(vars, unicode):
        vars = vars.encode('utf8')
    if hasattr(vars, 'xml'):
        vars = vars.xml()
    return vars
예제 #13
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled/')
    if os.path.exists(path):
        filename = os.path.join(path, 'controllers_%s_%s.pyc'
                                 % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(400,
                       rewrite.params.error_message_custom % 'invalid function',
                       web2py_error='invalid function')
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        filename = os.path.join(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(400,
                       rewrite.params.error_message_custom % 'invalid controller',
                       web2py_error='invalid controller')
        environment['__symbols__'] = environment.keys()
        fp = open(filename, 'r')
        code = fp.read()
        fp.close()
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(400,
                       rewrite.params.error_message_custom % 'invalid controller',
                       web2py_error='invalid controller')
        fp = open(filename, 'r')
        code = fp.read()
        fp.close()
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(400,
                       rewrite.params.error_message_custom % 'invalid function',
                       web2py_error='invalid function')
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename, lambda: \
                          compile(code.replace('\r\n', '\n'), layer,
                          'exec'))
        restricted(code, environment, filename)
    response = environment['response']
    vars=response._vars
    if response.postprocessing:
        for p in response.postprocessing:
            vars = p(vars)
    if isinstance(vars,unicode):
        vars = vars.encode('utf8')
    if hasattr(vars,'xml'):
        vars = vars.xml()
    return vars
예제 #14
0
def run_controller_in(controller, function, environment):
    """
    runs the controller.function() (for the app specified by 
    the current folder)  in the environment. 
    it tries precompiled controller_function.pyc first.
    """

    # if compiled should run compiled!

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled/')
    if os.path.exists(path):
        filename = os.path.join(
            path, 'controllers_%s_%s.pyc' % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(400,
                       error_message_custom % 'invalid function',
                       web2py_error='invalid function')
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        filename = os.path.join(folder, 'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            raise HTTP(400,
                       error_message_custom % 'invalid controller',
                       web2py_error='invalid controller')
        environment['__symbols__'] = environment.keys()
        code = open(filename, 'r').read()
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, 'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            raise HTTP(400,
                       error_message_custom % 'invalid controller',
                       web2py_error='invalid controller')
        code = open(filename, 'r').read()
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(400,
                       error_message_custom % 'invalid function',
                       web2py_error='invalid function')
        code = '''%s

response._vars=response._caller(%s)
''' % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename, lambda : \
                          compile(code.replace('\r\n', '\n'), layer,
                          'exec'))
        restricted(code, environment, filename)
    response = environment['response']
    if response.postprocessing:
        for p in response.postprocessing:
            response._vars = p(response._vars)
    if type(response._vars) == types.StringType:
        response.body = response._vars
    elif type(response._vars) == types.GeneratorType:
        response.body = response._vars
    elif type(response._vars) != types.DictType:
        response.body = str(response._vars)