Пример #1
0
    def create_default_file(self):
        """
        Creates the default file in the default configuration
        file in case it's necessary.
        """

        # retrieves the plugin manager
        plugin_manager = self.plugin.manager

        # retrieves the plugin id
        plugin_id = self.plugin.id

        # resolves the configuration file path
        configuration_file_path = plugin_manager.resolve_file_path("%configuration:" + plugin_id + "%/authentication.py", True)

        # retrieves the authentication plugin path
        plugin_path = plugin_manager.get_plugin_path_by_id(plugin_id)

        # creates the authentication configuration file path
        authentication_configuration_file_path = plugin_path + "/" + CONFIGURATION_PATH + "/authentication_configuration.py"

        # ensures that the configuration file path exists and contains the default contents
        colony.ensure_file_path(configuration_file_path, authentication_configuration_file_path)
Пример #2
0
    def init(self, request):
        """
        Handles the given initialization request.
        This request should start an execution instance and
        return the identifier to the caller.

        :type request: Request
        :param request: The request to be handled.
        """

        # retrieves the reference to the plugin manager running
        # in the current context
        plugin_manager = self.plugin.manager

        # retrieves the json plugin for the encoding of the
        # response value (serialized value)
        json_plugin = self.plugin.json_plugin

        # retrieves the id of the interpreter instance to be used
        # in case the instance id exists none is created
        instance = request.field("instance", None)

        # in case no instance (identifier) is found a new randomly generated
        # value is created for it (secure generation)
        instance = instance or str(uuid.uuid4())

        # creates the memory buffer that will hold the contents
        # resulting from the execution of the python code
        buffer_out = colony.legacy.StringIO()
        buffer_err = colony.legacy.StringIO()

        # creates the map containing the various local names to be used
        # in the interpreter, these are the values that will be made available
        # as entrance points to the end user
        locals = dict(
            manager = plugin_manager,
            plugins = plugin_manager.plugins
        )

        # tries to retrieve the correct interpreter from the interpreters
        # map in case it does not exists creates a new one, then sets it
        # back in the interpreters map for latter usage
        interpreter = self.interpreters.get(instance, None)
        interpreter = interpreter or code.InteractiveInterpreter(locals = locals)
        self.interpreters[instance] = interpreter

        # resolves the configuration init file path using the plugins manager
        # then ensures that it exists falling back to the local resources init
        # file that is contained in the bundle
        configuration_file_path = plugin_manager.resolve_file_path("%configuration:" + self.plugin.id + "%/initrc", True)
        plugin_path = plugin_manager.get_plugin_path_by_id(self.plugin.id)
        init_file_path = plugin_path + "/nanger/resources/default/initrc"
        colony.ensure_file_path(configuration_file_path, init_file_path)

        # opens the configuration (init) file and reads the complete set of
        # contents in it (to be executed in the current instance)
        file = open(configuration_file_path, "rb")
        try: contents = file.read()
        finally: file.close()

        # replaces the windows styled newlines with the normalized unix like
        # newline styled values (compatibility issues)
        contents = colony.legacy.str(contents)
        contents = contents.replace("\r\n", "\n")

        # updates the standard output and error buffer files to the new buffer
        # and then runs the command at the interpreter after the execution restore
        # the standard output and error back to the original values
        sys.stdout = buffer_out
        sys.stderr = buffer_err

        try:
            # tries to run the source code extracted from the execution
            # file under the "exec" mode (this should print the results
            # to the standard output)
            interpreter.runsource(contents, configuration_file_path, symbol = "exec")
        finally:
            # restores both the standard output and the standard error streams
            # into the original values (further writes will be handled normally)
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__

        # retrieves the values from the standard output and error and checks if
        # the resulting string values should be the error or the output
        result_out = buffer_out.getvalue()
        result_err = buffer_err.getvalue()
        result = result_err or result_out

        # creates the response map and serializes it with json to create the
        # final result contents, should retrieve the appropriate mime type
        response = dict(
            result = result,
            instance = instance
        )
        self.serialize(request, response, serializer = json_plugin)