[FIX] script new_project: validate addons path not exist before create it

- validate with relative path
This commit is contained in:
Mathieu Benoit 2022-03-19 19:34:48 -04:00
parent fd9b4123b0
commit cd89b95f79

View file

@ -1,5 +1,6 @@
#!./.venv/bin/python #!./.venv/bin/python
import argparse import argparse
import configparser
import logging import logging
import os import os
import sys import sys
@ -103,13 +104,13 @@ class ProjectManagement:
self.force = force self.force = force
self.keep_bd_alive = keep_bd_alive self.keep_bd_alive = keep_bd_alive
self.msg_error = "" self.msg_error = ""
self.origin_config_txt = ""
self.has_config_update = False self.has_config_update = False
self.module_directory = module_directory self.module_directory = module_directory
if not os.path.exists(self.module_directory): if not os.path.exists(self.module_directory):
self.msg_error = ( self.msg_error = (
f"Path directory '{self.module_directory}' not exist." f"Path directory '{self.module_directory}' not exist. You"
f" actual path is: '{os.getcwd()}'."
) )
_logger.error(self.msg_error) _logger.error(self.msg_error)
return return
@ -117,7 +118,8 @@ class ProjectManagement:
self.cg_directory = cg_directory if cg_directory else module_directory self.cg_directory = cg_directory if cg_directory else module_directory
if not os.path.exists(self.cg_directory): if not os.path.exists(self.cg_directory):
self.msg_error = ( self.msg_error = (
f"Path cg directory '{self.cg_directory}' not exist." f"Path cg directory '{self.cg_directory}' not exist. You"
f" actual path is: '{os.getcwd()}'."
) )
_logger.error(self.msg_error) _logger.error(self.msg_error)
return return
@ -428,30 +430,38 @@ class ProjectManagement:
return True return True
def update_config(self): def update_config(self):
# Backup config and restore it after, check if path exist or add it temporary config = configparser.ConfigParser()
with open("./config.conf") as config: config.read("./config.conf")
config_txt = config.read() addons_path = config.get("options", "addons_path")
self.origin_config_txt = config_txt lst_addons_path = addons_path.split(",")
lst_directory = list( lst_directory = list(
{ {
self.cg_directory, self.cg_directory,
self.module_directory, self.module_directory,
self.template_directory, self.template_directory,
} }
) )
lst_directory_to_add = [] has_change = False
for directory in lst_directory: for new_addons_path in lst_directory:
if directory not in config_txt: for actual_addons_path in lst_addons_path:
self.has_config_update = True if not actual_addons_path:
lst_directory_to_add.append(directory) continue
# Validate if not existing and valide is different path
if lst_directory_to_add: relative_actual_addons_path = os.path.relpath(
new_str = "addons_path = " + ",".join(lst_directory_to_add) + "," actual_addons_path
config_txt = config_txt.replace("addons_path = ", new_str) )
relative_new_addons_path = os.path.relpath(new_addons_path)
if relative_actual_addons_path == relative_new_addons_path:
break
else:
lst_addons_path.insert(0, new_addons_path)
has_change = True
if has_change:
config.set("options", "addons_path", ",".join(lst_addons_path))
temp_file = tempfile.mktemp() temp_file = tempfile.mktemp()
with open(temp_file, "w") as config: with open(temp_file, "w") as configfile:
config.write(config_txt) config.write(configfile)
print(f"mathben \n {temp_file} \n") _logger.info(f"Create temporary config file: {temp_file}")
return temp_file return temp_file