Пример #1
0
    def test_write_ordereddict(self):
        d = OrderedDict([("the", "a"), ("order", "z"), ("is", "b"), ("important", "y")])
        yaml.dump(d, open(os.path.join(self.dir_path, "output.yaml"), "w"))

        loaded = yaml.load(open(os.path.join(self.dir_path, "output.yaml"), "r"))
        assert type(loaded) == OrderedDict
        assert loaded.keys() == ["the", "order", "is", "important"]
Пример #2
0
def _api_convert_output(status_code, return_value, response=None):
    if not response:
        response = Response()
        response.status_code = status_code
    """ Convert the output to what the client asks """
    content_type = flask.request.environ.get('CONTENT_TYPE', 'text/json')

    if "text/json" in content_type:
        response.content_type = 'text/json; charset=utf-8'
        response.response = [json.dumps(return_value)]
        return response
    if "text/html" in content_type:
        response.content_type = 'text/html; charset=utf-8'
        dump = yaml.dump(return_value)
        response.response = ["<pre>" + dump + "</pre>"]
        return response
    if "text/yaml" in content_type or \
                    "text/x-yaml" in content_type or \
                    "application/yaml" in content_type or \
                    "application/x-yaml" in content_type:
        response.content_type = 'text/yaml; charset=utf-8'
        response.response = [yaml.dump(return_value)]
        return response
    response.content_type = 'text/json; charset=utf-8'
    response.response = [json.dumps(return_value)]
    return response
Пример #3
0
    def run(self):
        """ Run the installator """
        self._display_header("DOCKER CONFIGURATION")
        def_backend, def_remote_host, def_remote_docker_port, def_use_tls = self.generate_docker_default()
        options = {}
        while True:
            options = {}
            backend = self.ask_docker_backend(def_backend)
            if backend == "docker_machine":
                self._display_info("Backend chosen: docker machine. Let's configure the agents.")
                options = self.configure_backend_docker_machine(def_remote_host, def_remote_docker_port, def_use_tls)
                if options is not None:
                    break
            elif backend == "remote":
                self._display_info("Backend chosen: remote. Let's configure the agents.")
                options = self.configure_backend_remote(def_remote_host, def_remote_docker_port, def_use_tls)
                if options is not None:
                    break
            elif backend == "local":
                self._display_info("Backend chosen: local. Testing the configuration.")
                options = {"backend": "local"}
                if not self.test_basic_docker_conf("local"):
                    self._display_error("An error occured while testing the configuration.")
                    if self._ask_boolean("Would you like to continue anyway?", False):
                        break
                else:
                    break
            else:
                self._display_warning("Backend chosen: remote_manual. As it is a really advanced feature, you will have to configure it yourself in "
                                      "the configuration file, at the end of the setup process.")
                options = {"backend": "remote_manual"}
                break

        self._display_header("MONGODB CONFIGURATION")
        mongo_opt = self.configure_mongodb()
        options.update(mongo_opt)

        self._display_header("TASK DIRECTORY")
        task_directory_opt = self.configure_task_directory()
        options.update(task_directory_opt)

        self._display_header("CONTAINERS")
        containers_opt = self.configure_containers(options)
        options.update(containers_opt)

        self._display_header("MISC")
        misc_opt = self.configure_misc()
        options.update(misc_opt)

        options = self.frontend_specific_configuration(options)

        self._display_header("END")
        file_dir = self._config_path or os.path.join(os.getcwd(), self.configuration_filename())
        try:
            yaml.dump(options, open(file_dir, "w"))
            self._display_info("Successfully written the configuration file")
        except:
            self._display_error("Cannot write the configuration file on disk. Here is the content of the file")
            print yaml.dump(options)
Пример #4
0
def edit_configuration_file(path, data):
    """ Edit the configuration file with the new configuration """
    config_file_path = os.path.join(path, CONFIG_FILE_NAME)
    if os.path.isfile(config_file_path):
        stream = open(config_file_path, 'w')
        custom_yaml.dump(data, stream)
        stream.close()
    else:
        get_configuration_file(path)
Пример #5
0
    def test_write_ordereddict(self):
        d = OrderedDict([("the", "a"), ("order", "z"), ("is", "b"),
                         ("important", "y")])
        yaml.dump(d, open(os.path.join(self.dir_path, "output.yaml"), "w"))

        loaded = yaml.load(
            open(os.path.join(self.dir_path, "output.yaml"), "r"))
        assert type(loaded) == OrderedDict
        assert loaded.keys() == ["the", "order", "is", "important"]
Пример #6
0
    def run(self):
        """ Run the installator """
        self._display_header("BACKEND CONFIGURATION")
        options = {}
        while True:
            options = {}
            backend = self.ask_backend()
            if backend == "local":
                self._display_info(
                    "Backend chosen: local. Testing the configuration.")
                options = self._ask_local_config()
                if not self.test_local_docker_conf():
                    self._display_error(
                        "An error occurred while testing the configuration. Please make sure you are able do run `docker info` in "
                        "your command line, and environment parameters like DOCKER_HOST are correctly set."
                    )
                    if self._ask_boolean("Would you like to continue anyway?",
                                         False):
                        break
                else:
                    break
            else:
                self._display_warning(
                    "Backend chosen: manual. As it is a really advanced feature, you will have to configure it yourself in "
                    "the configuration file, at the end of the setup process.")
                options = {"backend": backend}
                break

        self._display_header("MONGODB CONFIGURATION")
        mongo_opt = self.configure_mongodb()
        options.update(mongo_opt)

        self._display_header("TASK DIRECTORY")
        task_directory_opt = self.configure_task_directory()
        options.update(task_directory_opt)

        self._display_header("CONTAINERS")
        self.configure_containers(options)

        self._display_header("MISC")
        misc_opt = self.configure_misc()
        options.update(misc_opt)

        options = self.frontend_specific_configuration(options)

        self._display_header("END")
        file_dir = self._config_path or os.path.join(
            os.getcwd(), self.configuration_filename())
        try:
            yaml.dump(options, open(file_dir, "w"))
            self._display_info("Successfully written the configuration file")
        except:
            self._display_error(
                "Cannot write the configuration file on disk. Here is the content of the file"
            )
            print((yaml.dump(options)))
Пример #7
0
def get_configuration_file(path):
    """ Get the actual configuration from the configuration file"""
    config_file_path = os.path.join(path, CONFIG_FILE_NAME)
    if os.path.exists(config_file_path):
        stream = open(config_file_path, 'r')
        data = custom_yaml.load(stream)
        stream.close()
        return data
    else:
        stream = open(config_file_path, 'w')
        custom_yaml.dump(DEFAULT_CONFIG, stream)
        stream.close()
        return DEFAULT_CONFIG
Пример #8
0
    def test_write_string(self):
        d = OrderedDict([("the", "a"), ("order", "z"), ("is", "b"), ("important", "y")])
        string = yaml.dump(d)

        loaded = yaml.load(string)
        assert type(loaded) == OrderedDict
        assert loaded.keys() == ["the", "order", "is", "important"]
Пример #9
0
    def GET_AUTH(self, courseid):  # pylint: disable=arguments-differ
        """ GET request """
        course, __ = self.get_course_and_check_rights(courseid)

        if course.is_lti():
            raise web.notfound()

        if "download" in web.input():
            web.header('Content-Type', 'text/x-yaml', unique=True)
            web.header('Content-Disposition', 'attachment; filename="aggregations.yaml"', unique=True)
            if course.use_classrooms():
                aggregations = [{"default": aggregation["default"],
                               "description": aggregation["description"],
                               "groups": aggregation["groups"],
                               "students": aggregation["students"],
                               "tutors": aggregation["tutors"]} for aggregation in
                              self.user_manager.get_course_aggregations(course)]
            else:
                aggregations = [{"default": aggregation["default"],
                               "description": aggregation["description"],
                               "groups": aggregation["groups"],
                               "students": aggregation["students"],
                               "tutors": aggregation["tutors"]} for aggregation in
                              self.user_manager.get_course_aggregations(course) if len(aggregation["groups"]) > 0]

            return yaml.dump(aggregations)

        return self.page(course)
Пример #10
0
    def GET_AUTH(self, courseid):  # pylint: disable=arguments-differ
        """ GET request """
        course, __ = self.get_course_and_check_rights(courseid)

        if course.is_lti():
            raise web.notfound()

        if "download" in web.input():
            web.header('Content-Type', 'text/x-yaml', unique=True)
            web.header('Content-Disposition', 'attachment; filename="aggregations.yaml"', unique=True)
            if course.use_classrooms():
                aggregations = [{"default": aggregation["default"],
                               "description": aggregation["description"],
                               "groups": aggregation["groups"],
                               "students": aggregation["students"],
                               "tutors": aggregation["tutors"]} for aggregation in
                              self.user_manager.get_course_aggregations(course)]
            else:
                aggregations = [{"default": aggregation["default"],
                               "description": aggregation["description"],
                               "groups": aggregation["groups"],
                               "students": aggregation["students"],
                               "tutors": aggregation["tutors"]} for aggregation in
                              self.user_manager.get_course_aggregations(course) if len(aggregation["groups"]) > 0]

            return yaml.dump(aggregations)

        return self.page(course)
Пример #11
0
    def test_write_string(self):
        d = OrderedDict([("the", "a"), ("order", "z"), ("is", "b"),
                         ("important", "y")])
        string = yaml.dump(d)

        loaded = yaml.load(string)
        assert type(loaded) == OrderedDict
        assert loaded.keys() == ["the", "order", "is", "important"]
Пример #12
0
    def test_write_long_str(self):
        d = {"key": """This is a very long string
        that should be multiline in the yaml!


        minimum 6 lines!"""}
        string = yaml.dump(d)
        assert len(string.splitlines()) == 6
Пример #13
0
def _api_convert_output(return_value):
    """ Convert the output to what the client asks """
    content_type = web.ctx.environ.get('CONTENT_TYPE', 'text/json')

    if "text/json" in content_type:
        web.header('Content-Type', 'text/json; charset=utf-8')
        return json.dumps(return_value)
    if "text/html" in content_type:
        web.header('Content-Type', 'text/html; charset=utf-8')
        dump = yaml.dump(return_value)
        return "<pre>" + web.websafe(dump) + "</pre>"
    if "text/yaml" in content_type or \
                    "text/x-yaml" in content_type or \
                    "application/yaml" in content_type or \
                    "application/x-yaml" in content_type:
        web.header('Content-Type', 'text/yaml; charset=utf-8')
        dump = yaml.dump(return_value)
        return dump
    web.header('Content-Type', 'text/json; charset=utf-8')
    return json.dumps(return_value)
Пример #14
0
    def test_write_long_str(self):
        d = {
            "key":
            """This is a very long string
        that should be multiline in the yaml!


        minimum 6 lines!"""
        }
        string = yaml.dump(d)
        assert len(string.splitlines()) == 6
Пример #15
0
def _api_convert_output(return_value):
    """ Convert the output to what the client asks """
    content_type = web.ctx.environ.get('CONTENT_TYPE', 'text/json')

    if "text/json" in content_type:
        web.header('Content-Type', 'text/json; charset=utf-8')
        return json.dumps(return_value)
    if "text/html" in content_type:
        web.header('Content-Type', 'text/html; charset=utf-8')
        dump = yaml.dump(return_value)
        return "<pre>" + web.websafe(dump) + "</pre>"
    if "text/yaml" in content_type or \
                    "text/x-yaml" in content_type or \
                    "application/yaml" in content_type or \
                    "application/x-yaml" in content_type:
        web.header('Content-Type', 'text/yaml; charset=utf-8')
        dump = yaml.dump(return_value)
        return dump
    web.header('Content-Type', 'text/json; charset=utf-8')
    return json.dumps(return_value)
Пример #16
0
    def test_write_long_str_obj(self):
        class strange_object(object):
            def __str__(self):
                return """This is a very long string
                    that should be multiline in the yaml!


                    minimum 6 lines!"""

        string = yaml.dump({"key": strange_object()})
        assert len(string.splitlines()) == 6
Пример #17
0
    def test_write_long_str_obj(self):
        class strange_object(object):
            def __str__(self):
                return """This is a very long string
                    that should be multiline in the yaml!


                    minimum 6 lines!"""

        string = yaml.dump({"key": strange_object()})
        assert len(string.splitlines()) == 6
Пример #18
0
    def export_elements(self, course, data):
        errors = []
        export_tasks, exported_tasks = [], []
        export_sections = []

        # get input and filter
        for id in data.keys():
            if id.startswith("task_"):
                export_tasks.append(id[5:])
            elif id.startswith("section_"):
                export_sections.append(id[8:])

        tmpfile = tempfile.TemporaryFile()
        with tarfile.open(fileobj=tmpfile, mode='w:gz') as tar:
            # export tasks
            for taskid in export_tasks:
                try:
                    task_path = course.get_task(taskid).get_fs().prefix.rstrip(
                        '/')
                    tar.add(task_path, arcname=os.path.basename(task_path))
                    exported_tasks.append(taskid)
                except:
                    errors.append(_("Could not export task ") + taskid)
            # export sections
            try:
                from inginious.frontend.plugins.course_structure.webapp_course import get_sections_tasks_ids, \
                    get_course_structure
                toc = get_course_structure(course)
                filtered_toc = filter_sections(toc, export_sections,
                                               exported_tasks)

                sections_yaml = io.BytesIO(dump(filtered_toc).encode('utf-8'))
                info = tarfile.TarInfo(name="sections.yaml")
                info.size = sections_yaml.getbuffer().nbytes
                tar.addfile(info, fileobj=sections_yaml)
            except:
                errors.append(
                    _("Error adding section description to the archive"))
        tmpfile.seek(0)

        if errors:
            return self.page(course, errors, None)
        else:
            web.header('Content-Type', 'application/x-gzip', unique=True)
            web.header('Content-Disposition',
                       'attachment; filename="inginious_tasks.tgz"',
                       unique=True)
            return tmpfile
Пример #19
0
    def GET_AUTH(self, courseid):  # pylint: disable=arguments-differ
        """ GET request """
        course, __ = self.get_course_and_check_rights(courseid)

        if "download" in web.input():
            web.header('Content-Type', 'text/x-yaml', unique=True)
            web.header('Content-Disposition',
                       'attachment; filename="audiences.yaml"',
                       unique=True)
            audiences = [{
                "description": audience["description"],
                "students": audience["students"],
                "tutors": audience["tutors"]
            } for audience in self.user_manager.get_course_audiences(course)]

            return yaml.dump(audiences)

        return self.page(course)
Пример #20
0
    def run(self):
        """ Run the installator """
        self._display_header("BACKEND CONFIGURATION")
        options = {}
        while True:
            options = {}
            backend = self.ask_backend()
            if backend == "local":
                self._display_info("Backend chosen: local. Testing the configuration.")
                options = self._ask_local_config()
                if not self.test_local_docker_conf():
                    self._display_error(
                        "An error occurred while testing the configuration. Please make sure you are able do run `docker info` in "
                        "your command line, and environment parameters like DOCKER_HOST are correctly set.")
                    if self._ask_boolean("Would you like to continue anyway?", False):
                        break
                else:
                    break
            else:
                self._display_warning(
                    "Backend chosen: manual. As it is a really advanced feature, you will have to configure it yourself in "
                    "the configuration file, at the end of the setup process.")
                options = {"backend": backend}
                break

        self._display_header("MONGODB CONFIGURATION")
        mongo_opt = self.configure_mongodb()
        options.update(mongo_opt)

        self._display_header("TASK DIRECTORY")
        task_directory_opt = self.configure_task_directory()
        options.update(task_directory_opt)

        self._display_header("CONTAINERS")
        self.configure_containers(options)

        self._display_header("MISC")
        misc_opt = self.configure_misc()
        options.update(misc_opt)

        database = self.try_mongodb_opts(options["mongo_opt"]["host"], options["mongo_opt"]["database"])

        self._display_header("BACKUP DIRECTORY")
        backup_directory_opt = self.configure_backup_directory()
        options.update(backup_directory_opt)

        self._display_header("AUTHENTIFICATION")
        auth_opts = self.configure_authentication(database)
        options.update(auth_opts)

        self._display_info("You may want to add additional plugins to the configuration file.")

        self._display_header("REMOTE DEBUGGING - IN BROWSER")
        self._display_info(
            "If you want to activate the remote debugging of task in the users' browser, you have to install separately "
            "INGInious-xterm, which is available on Github, according to the parameters you have given for the hostname and the "
            "port range given in the configuration of the remote debugging.")
        self._display_info(
            "You can leave the following question empty to disable this feature; remote debugging will still be available, "
            "but not in the browser.")
        webterm = self._ask_with_default(
            "Please indicate the link to your installation of INGInious-xterm (for example: "
            "https://your-hostname.com:8080).", "")
        if webterm != "":
            options["webterm"] = webterm

        self._display_header("END")
        file_dir = self._config_path or os.path.join(os.getcwd(), self.configuration_filename())
        try:
            yaml.dump(options, open(file_dir, "w"))
            self._display_info("Successfully written the configuration file")
        except:
            self._display_error("Cannot write the configuration file on disk. Here is the content of the file")
            print(yaml.dump(options))
Пример #21
0
    def run(self):
        """ Run the installator """
        self._display_header("BACKEND CONFIGURATION")
        options = {}
        while True:
            options = {}
            backend = self.ask_backend()
            if backend == "local":
                self._display_info(
                    "Backend chosen: local. Testing the configuration.")
                options = self._ask_local_config()
                if not self.test_local_docker_conf():
                    self._display_error(
                        "An error occurred while testing the configuration. Please make sure you are able do run `docker info` in "
                        "your command line, and environment parameters like DOCKER_HOST are correctly set."
                    )
                    if self._ask_boolean("Would you like to continue anyway?",
                                         False):
                        break
                else:
                    break
            else:
                self._display_warning(
                    "Backend chosen: manual. As it is a really advanced feature, you will have to configure it yourself in "
                    "the configuration file, at the end of the setup process.")
                options = {"backend": backend}
                break

        self._display_header("MONGODB CONFIGURATION")
        mongo_opt = self.configure_mongodb()
        options.update(mongo_opt)

        self._display_header("TASK DIRECTORY")
        task_directory_opt = self.configure_task_directory()
        options.update(task_directory_opt)

        self._display_header("CONTAINERS")
        self.select_containers_to_build()

        self._display_header("MISC")
        misc_opt = self.configure_misc()
        options.update(misc_opt)

        database = self.try_mongodb_opts(options["mongo_opt"]["host"],
                                         options["mongo_opt"]["database"])

        self._display_header("BACKUP DIRECTORY")
        backup_directory_opt = self.configure_backup_directory()
        options.update(backup_directory_opt)

        self._display_header("AUTHENTIFICATION")
        auth_opts = self.configure_authentication(database)
        options.update(auth_opts)

        self._display_info(
            "You may want to add additional plugins to the configuration file."
        )

        self._display_header("REMOTE DEBUGGING - IN BROWSER")
        self._display_info(
            "If you want to activate the remote debugging of task in the users' browser, you have to install separately "
            "INGInious-xterm, which is available on Github, according to the parameters you have given for the hostname and the "
            "port range given in the configuration of the remote debugging.")
        self._display_info(
            "You can leave the following question empty to disable this feature; remote debugging will still be available, "
            "but not in the browser.")
        webterm = self._ask_with_default(
            "Please indicate the link to your installation of INGInious-xterm (for example: "
            "https://your-hostname.com:8080).", "")
        if webterm != "":
            options["webterm"] = webterm

        self._display_header("END")
        file_dir = self._config_path or os.path.join(
            os.getcwd(), self.configuration_filename())
        try:
            yaml.dump(options, open(file_dir, "w"))
            self._display_info("Successfully written the configuration file")
        except:
            self._display_error(
                "Cannot write the configuration file on disk. Here is the content of the file"
            )
            print(yaml.dump(options))
Пример #22
0
    def run(self):
        """ Run the installator """
        self._display_header("DOCKER CONFIGURATION")
        def_backend, def_remote_host, def_remote_docker_port, def_use_tls = self.generate_docker_default(
        )
        options = {}
        while True:
            options = {}
            backend = self.ask_docker_backend(def_backend)
            if backend == "docker_machine":
                self._display_info(
                    "Backend chosen: docker machine. Let's configure the agents."
                )
                options = self.configure_backend_docker_machine(
                    def_remote_host, def_remote_docker_port, def_use_tls)
                if options is not None:
                    break
            elif backend == "remote":
                self._display_info(
                    "Backend chosen: remote. Let's configure the agents.")
                options = self.configure_backend_remote(
                    def_remote_host, def_remote_docker_port, def_use_tls)
                if options is not None:
                    break
            elif backend == "local":
                self._display_info(
                    "Backend chosen: local. Testing the configuration.")
                options = {"backend": "local"}
                if not self.test_basic_docker_conf("local"):
                    self._display_error(
                        "An error occured while testing the configuration.")
                    if self._ask_boolean("Would you like to continue anyway?",
                                         False):
                        break
                else:
                    break
            else:
                self._display_warning(
                    "Backend chosen: remote_manual. As it is a really advanced feature, you will have to configure it yourself in "
                    "the configuration file, at the end of the setup process.")
                options = {"backend": "remote_manual"}
                break

        self._display_header("MONGODB CONFIGURATION")
        mongo_opt = self.configure_mongodb()
        options.update(mongo_opt)

        self._display_header("TASK DIRECTORY")
        task_directory_opt = self.configure_task_directory()
        options.update(task_directory_opt)

        self._display_header("CONTAINERS")
        containers_opt = self.configure_containers(options)
        options.update(containers_opt)

        self._display_header("MISC")
        misc_opt = self.configure_misc()
        options.update(misc_opt)

        options = self.frontend_specific_configuration(options)

        self._display_header("END")
        file_dir = self._config_path or os.path.join(
            os.getcwd(), self.configuration_filename())
        try:
            yaml.dump(options, open(file_dir, "w"))
            self._display_info("Successfully written the configuration file")
        except:
            self._display_error(
                "Cannot write the configuration file on disk. Here is the content of the file"
            )
            print yaml.dump(options)