Ejemplo n.º 1
0
 def test_should_return_the_compiled_file(self, reset_folder_structure):
     """Compile from template should return the compiled file."""
     target = "TITLE\nThis is a DESC."
     settings = {"title": "TITLE", "description": "DESC"}
     compile_from_template(self.template_file_content, self.compiled_file,
                           settings)
     with open(self.compiled_file, "r") as f:
         assert f.read() == target
Ejemplo n.º 2
0
 def _compile_config_file(self) -> None:
     """Compile an instance specific cfg file that will be passed as -config flag to the server."""
     # recover template file and prepare composed bat file path
     if self.S.config_settings.config_template == "":
         template_file_content = self._read_resource_file('templates/server_cfg_template.txt')
     else:
         with open(self.S.config_settings.config_template, "r") as template:
             template_file_content = template.read()
     compiled_config_path = join(self.get_server_instance_path(), self.S.bat_settings.server_config_file_name)
     # prepare settings
     settings = self.S.config_settings.to_dict()
     # compose and save the bat
     compile_from_template(template_file_content, compiled_config_path, settings)
Ejemplo n.º 3
0
 def _compile_bat_file(self) -> None:
     """Compile an instance specific bat file to run the server."""
     # recover template file and prepare composed bat file path
     if self.S.bat_settings.bat_template == "":
         template_file_content = self._read_resource_file('templates/run_server_template.txt')
     else:
         with open(self.S.bat_settings.bat_template, "r") as template:
             template_file_content = template.read()
     compiled_bat_path = join(self.get_server_instance_path(), "run_server.bat")
     # prepare settings
     settings = self.S.bat_settings.copy()
     settings.user_mods = self._compose_relative_path_mods(self.S.user_mods_list)
     settings.server_mods = self._compose_relative_path_mods(self.S.server_mods_list)
     settings.server_drive = self.S.server_drive
     settings.server_root = self.get_server_instance_path()
     settings.instance_name = self.S.server_instance_name
     # compose and save the bat
     compile_from_template(template_file_content, compiled_bat_path, settings)
Ejemplo n.º 4
0
 def test_should_output_the_file(self, reset_folder_structure):
     """Compile from template should output the file."""
     compile_from_template(self.template_file_content, self.compiled_file,
                           {})
     assert isfile(self.compiled_file)
Ejemplo n.º 5
0
 def bootstrap(self, default_config_file: str = None) -> None:
     """Interactive UI to start building a new server instance."""
     print("\n ======[ WELCOME TO ODKSM! ]======\n")
     try:
         # try and read the config
         data = ConfigIni.read_file(default_config_file, bootstrap=True)
         # check for needed fields
         if data["bootstrap"].get("instances_root", "") == "":
             raise ValueError(
                 "'instances_root' field can't be empty in the [bootstrap] section!"
             )
         if data["bootstrap"].get("odksm_folder_path", "") == "":
             raise ValueError(
                 "'odksm_folder_path' field can't be empty in the [bootstrap] section!"
             )
     except Exception as err:
         self._ui_abort(
             "\n [ERR] Error while reading the default config file!\n\n {}\n\n "
             "Check the documentation in the wiki, in the bootstrap.ini example file, in the README.md or"
             " in the odksm_servermanager/settings.py.\n Bye!\n".format(
                 err))
     try:
         # Check the instances_root exists
         instances_root = data["bootstrap"]["instances_root"]
         if not isdir(instances_root):
             raise Exception(
                 "Could not find the instances_folder '{}'".format(
                     instances_root))
         print(
             "\n This utility will help you start creating your server instance.\n"
         )
         instance_name = input(" Choose a unique name for the instance: ")
         # check if custom templates are needed
         custom_bat_template_needed = False
         custom_config_template_needed = False
         custom_templates_needed_string = input(
             " Will you need custom templates? (y/n) ")
         if custom_templates_needed_string == "y":
             print("\n OK! Now.. \n")
             custom_bat_template_needed_string = input(
                 " ... will you need a custom BAT template? (y/n) ")
             if custom_bat_template_needed_string == "y":
                 custom_bat_template_needed = True
             custom_config_template_needed_string = input(
                 " ... will you need a custom CONFIG template? (y/n) ")
             if custom_config_template_needed_string == "y":
                 custom_config_template_needed = True
         # create the folder
         instance_dir = join(instances_root, instance_name)
         mkdir(instance_dir)
         # copy templates if needed
         if custom_bat_template_needed:
             bat_template_file_name = "run_server_template.txt"
             bat_template_file = join(instance_dir, bat_template_file_name)
             template_file = self._get_resource_file(
                 "templates/{}".format(bat_template_file_name))
             copy(template_file, bat_template_file)
             data["bat"]["bat_template"] = abspath(bat_template_file)
         if custom_config_template_needed:
             config_template_file_name = "server_cfg_template.txt"
             config_template_file = join(instance_dir,
                                         config_template_file_name)
             template_file = self._get_resource_file(
                 "templates/{}".format(config_template_file_name))
             copy(template_file, config_template_file)
             data["config"]["config_template"] = abspath(
                 config_template_file)
         # prepare some fields for the config file
         data["ODKSM"]["server_instance_name"] = instance_name
         data["ODKSM"]["server_instance_root"] = abspath(instance_dir)
         # generate the config.ini file
         ConfigIni().create_file(join(instance_dir, "config.ini"), data)
         # compile the ODKSM.bat file
         bat_template = pkg_resources.resource_string(
             "odk_servermanager",
             "templates/ODKSM_bat_template.txt").decode("UTF-8")
         bat_file = join(instance_dir, "ODKSM.bat")
         compile_from_template(
             bat_template, bat_file,
             {"odksm_folder_path": data["bootstrap"]["odksm_folder_path"]})
         print(
             "\n Instance folder created!\n\n [WARNING] IMPORTANT! YOU ARE NOT DONE! You still need to edit the\n"
             " config.ini file in the folder and to run the actual ODKSM.bat tool.\n Bye!\n"
         )
     except Exception as err:
         self._ui_abort(
             "\n [ERR] Error while bootstrapping the instance!\n\n {}\n\n "
             "Check the documentation in the wiki, in the bootstrap.ini example file, in the README.md or"
             " in the odksm_servermanager/settings.py.\n Bye!\n".format(
                 err))