Better domain control and validation

This commit is contained in:
Cyrille L 2023-09-23 02:39:19 +02:00
parent 6a17add248
commit 5f45caebe4
26 changed files with 100 additions and 71 deletions

View File

@ -7,7 +7,10 @@ Tyto - Littérateur
- License: - License:
- Documentation: - Documentation:
## [1.9.0] ## [1.9.1]
- Auto update configuration domain
- Better managing configuration domain values
- Better checking if valid domain

View File

@ -12,3 +12,5 @@ tyto
- - needs more checks to be validated - - needs more checks to be validated
- english logs translation file (French only, for now) - english logs translation file (French only, for now)
# Next
- Create all directories for valid domain

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Version: 1.9.0 # Version: 1.9.1
# Updated: 2023-09-23 1695429475
# Tyto - Littérateur # Tyto - Littérateur
# Copyright (C) 2023 Cyrille Louarn <echolib+tyto@a-lec.org> # Copyright (C) 2023 Cyrille Louarn <echolib+tyto@a-lec.org>
@ -40,9 +41,9 @@
# file program : # file program :
#-------------------------- #--------------------------
#======#
# MAIN #
#======#======================================================================= #======#=======================================================================
# MAIN #
#======#
import sys import sys
if not __name__ == "__main__": if not __name__ == "__main__":
print("! Error: '%s' not '%s'"%(__name__, "__main__")) print("! Error: '%s' not '%s'"%(__name__, "__main__"))

View File

@ -45,7 +45,6 @@ def get_action():
except: action = "" except: action = ""
#==================# #==================#
# Target arguments # # Target arguments #
#------------------# #------------------#
@ -63,9 +62,9 @@ def get_options():
dlogs = force = erron = False dlogs = force = erron = False
for arg in range(1, len(sys.argv)): for arg in range(1, len(sys.argv)):
if sys.argv[arg] in tyto.debug_options: dlogs = True dlogs = sys.argv[arg] in tyto.debug_options
if sys.argv[arg] in tyto.force_options: force = True force = sys.argv[arg] in tyto.force_options
if sys.argv[arg] in tyto.debug_errors: erron = True erron = sys.argv[arg] in tyto.debug_errors
#===========# #===========#
@ -74,7 +73,7 @@ def get_options():
def valid_action(): def valid_action():
global action global action
if not action in tyto.actions: if not action in tyto.actions:
debug.out(1, "[action]", action, dlogs,2, False) debug.out(1, "[action]", action, False, 2, False)
action = "help" action = "help"

View File

@ -33,11 +33,11 @@
#-------------------------- #--------------------------
import sys import sys
import domain import domain, langs
def manage(action, target): def manage(action, target):
# Load domain configuration, update if needed, check if valid # Load domain configuration, update if needed, check if valid
domain.valid_conf() domain.valid_conf()
langs.load_website_lang()
print("check:", action, target, domain.dcf.get("DOMAIN", "name")) print("check:", action, target, domain.dcf.get("DOMAIN", "name"))

View File

@ -37,8 +37,8 @@ import langs, args
#===================================# #===================================#
# Show logs if "show" is True # # Show all logs if "show" is True #
# -D argument for specific ones # # or with -D argument #
# color is for "*" in message # # color is for "*" in message #
# - 3 colors levels: # # - 3 colors levels: #
# - - 0 = Green # # - - 0 = Green #
@ -48,8 +48,11 @@ import langs, args
#-----------------------------------# #-----------------------------------#
def out(nbr, var, val, show, color, stop): def out(nbr, var, val, show, color, stop):
args.get_options() args.get_options()
if not show and not args.dlogs: return # Show Very less logs if not show:
if args.erron and color == 0: return # Show only warn and error logs show = args.dlogs or args.erron and color > 0 # Show only warn and error logs
if not show:
return
# COlors # COlors
CS = '\033[0;0m' # Unset CS = '\033[0;0m' # Unset

View File

@ -100,43 +100,12 @@ def dcf_exists():
return True return True
else: else:
try: shown_no try: shown_no
except: debug.out(100, "False", dcf_uri, True, 1, False) except: debug.out(104, "False", dcf_uri, True, 1, False)
shown_no = True shown_no = True
compatible_name()
return False return False
#=============================================#
# Ensure domain is valid and ready to be used #
#---------------------------------------------#
def valid_conf():
dcf_load()
try:
dcf_name = dcf.get("DOMAIN", "name")
except:
debug.out(100, conf_name, dcf_uri, True, 1, True)
# Check some values in ult_dcf
ult_dcf_load()
try:
ult_dcf_name = ult_dcf.get("DOMAIN", "name")
ult_dcf_hash = ult_dcf.get("DOMAIN", "hash")
ult_dcf_conf = ult_dcf.get("DOMAIN", "conf")
ult_dcf_root = ult_dcf.get("DOMAIN", "root")
except:
debug.out(100, conf_name, ult_dcf_uri, True, 1, True)
# Compare values (exit if mismatch)
if ult_dcf_name != conf_name or \
ult_dcf_conf != dcf_uri or \
dcf_name != ult_dcf_name:
debug.out(100, conf_name, "?", True, 1, True)
# Check if dcf need to be updated
now_dcf_hash = tools.get_filesum(dcf_uri, True)
if now_dcf_hash != ult_dcf_hash:
dcf_update_values("")
#=========================================# #=========================================#
# Guess and return wip_url from conf_name # # Guess and return wip_url from conf_name #
#-----------------------------------------# #-----------------------------------------#
@ -204,28 +173,26 @@ def dcf_update_values(srv):
dcf_load() dcf_load()
# Test server directory, and exit if not exists # Test server directory, and exit if not exists
if not srv: srv = dcf.get("SERVER", "root") # Set from db if unknown srv = srv or dcf.get("SERVER", "root") # Set from db if unknown
if srv: tools.dir_exists(srv, True)
# Test registred website lang # Test registred website lang
# change to default lang sys, or "en" if no translation file # change to default lang sys, or "en" if no translation file
# Load website lang file
langs.load_website_lang() langs.load_website_lang()
srv_dom = os.path.join(srv, conf_name + "/") srv_dom = os.path.join(srv, conf_name + "/")
srv_wip = os.path.join(srv_dom, "wip/") srv_wip = os.path.join(srv_dom, "wip/")
srv_www = os.path.join(srv_dom, "www/") srv_www = os.path.join(srv_dom, "www/")
favicon = dcf.get("TEMPLATE_FILENAMES", "favicon") favicon = dcf.get("TEMPLATE_FILENAMES", "favicon") or "favicon.png"
logo = dcf.get("TEMPLATE_FILENAMES", "logo") logo = dcf.get("TEMPLATE_FILENAMES", "logo") or "logo.png"
styles = dcf.get("TEMPLATE_FILENAMES", "styles") styles = dcf.get("TEMPLATE_FILENAMES", "styles") or "styles.css"
rss = dcf.get("TEMPLATE_FILENAMES", "rss") rss = dcf.get("TEMPLATE_FILENAMES", "rss") or "rss.xml"
stats = dcf.get("TEMPLATE_FILENAMES", "stats") stats = dcf.get("TEMPLATE_FILENAMES", "stats") or "stats.ini"
www_url = dcf.get("WEBSITE", "www_url") www_url = dcf.get("WEBSITE", "www_url")
wip_url = dcf.get("WEBSITE", "wip_url") wip_url = dcf.get("WEBSITE", "wip_url")
sdb_title = dcf.get("WEBSITE_MODULES", "sidebar_title")
usr_mods = os.path.join(dcf_dir + "modules/") usr_mods = os.path.join(dcf_dir + "modules/")
usr_tpl = os.path.join(dcf_dir, "template/") usr_tpl = os.path.join(dcf_dir, "template/")
usr_favicon = os.path.join(usr_tpl, favicon) usr_favicon = os.path.join(usr_tpl, favicon)
@ -262,11 +229,13 @@ def dcf_update_values(srv):
dcf.set("USER_DIRS", "modules", usr_mods) dcf.set("USER_DIRS", "modules", usr_mods)
dcf.set("USER_DIRS", "database", dcf_dir + ".db/") dcf.set("USER_DIRS", "database", dcf_dir + ".db/")
dcf.set("USER_DIRS", "template", usr_tpl) dcf.set("USER_DIRS", "template", usr_tpl)
dcf.set("USER_MODULES_FILES", "metas", usr_mods + "tyto_metas.raw") dcf.set("USER_MODULES_FILES", "metas", usr_mods + "tyto_metas.raw")
dcf.set("USER_MODULES_FILES", "header", usr_mods + "tyto_header.raw") dcf.set("USER_MODULES_FILES", "header", usr_mods + "tyto_header.raw")
dcf.set("USER_MODULES_FILES", "navbar", usr_mods + "tyto_navbar.raw") dcf.set("USER_MODULES_FILES", "navbar", usr_mods + "tyto_navbar.raw")
dcf.set("USER_MODULES_FILES", "sidebar", usr_mods + "tyto_sidebar.raw") dcf.set("USER_MODULES_FILES", "sidebar", usr_mods + "tyto_sidebar.raw")
dcf.set("USER_MODULES_FILES", "footer", usr_mods + "tyto_footer.raw") dcf.set("USER_MODULES_FILES", "footer", usr_mods + "tyto_footer.raw")
dcf.set("USER_TEMPLATE_FILES", "favicon", usr_favicon) dcf.set("USER_TEMPLATE_FILES", "favicon", usr_favicon)
dcf.set("USER_TEMPLATE_FILES", "logo", usr_logo) dcf.set("USER_TEMPLATE_FILES", "logo", usr_logo)
dcf.set("USER_TEMPLATE_FILES", "styles", usr_styles) dcf.set("USER_TEMPLATE_FILES", "styles", usr_styles)
@ -275,8 +244,18 @@ def dcf_update_values(srv):
dcf.set("WEBSITE", "lang", langs.site_lang) dcf.set("WEBSITE", "lang", langs.site_lang)
www_url or dcf.set("WEBSITE", "www_url", "https://%s/"%conf_name) www_url or dcf.set("WEBSITE", "www_url", "https://%s/"%conf_name)
wip_url or dcf.set("WEBSITE", "wip_url", create_wip_url()) wip_url or dcf.set("WEBSITE", "wip_url", create_wip_url())
dcf.get("WEBSITE_MODULES", "sidebar_title") or \
dcf.set("WEBSITE_MODULES", "sidebar_title", langs.site.sidebar_title)
dcf.get("WEBSITE_MODULES", "sidebar_items").isdigit() or \
dcf.set("WEBSITE_MODULES", "sidebar_items", "0")
dcf.get("WEBSITE_MODULES", "rss_items").isdigit() or \
dcf.set("WEBSITE_MODULES", "rss_items", "0")
sdb_title or dcf.set("WEBSITE_MODULES", "sidebar_title", langs.site.sidebar_title) dcf.set("TEMPLATE_FILENAMES", "favicon", favicon)
dcf.set("TEMPLATE_FILENAMES", "logo", logo)
dcf.set("TEMPLATE_FILENAMES", "styles", styles)
dcf.set("TEMPLATE_FILENAMES", "rss", rss)
dcf.set("TEMPLATE_FILENAMES", "stats", stats)
# SERVER # SERVER
dcf.set("SERVER", "root", srv) dcf.set("SERVER", "root", srv)
@ -341,6 +320,53 @@ def dcf_update_values(srv):
debug.out(204, "True", dcf_uri, True, 0, False) debug.out(204, "True", dcf_uri, True, 0, False)
#=============================================#
# Ensure domain is valid and ready to be used #
#---------------------------------------------#
def valid_conf():
dcf_load()
# Most importants
# - dcf name must be config_name
# - root server must exists
try:
dcf_name = dcf.get("DOMAIN", "name")
if dcf_name != conf_name:
debug.out(100, conf_name, dcf_uri, True, 2, True)
except:
debug.out(100, conf_name, dcf_uri, True, 2, True)
# Server root
try: dcf_srv = dcf.get("SERVER", "root")
except: debug.out(100, "SERVER root", "?", True, 2, True)
if not dcf_srv or not tools.dir_exists(dcf_srv, True):
debug.out(100, "SERVER root", "? %s"%dcf_srv, True, 2, True)
# Check some values in ult_dcf
ult_dcf_load()
try:
ult_dcf_name = ult_dcf.get("DOMAIN", "name")
ult_dcf_hash = ult_dcf.get("DOMAIN", "hash")
ult_dcf_conf = ult_dcf.get("DOMAIN", "conf")
ult_dcf_root = ult_dcf.get("DOMAIN", "root")
except:
debug.out(100, conf_name, ult_dcf_uri, True, 1, True)
# Compare values (exit if mismatch)
if ult_dcf_name != conf_name or \
ult_dcf_conf != dcf_uri or \
dcf_name != ult_dcf_name:
debug.out(100, conf_name, "?", True, 1, True)
# Check if dcf need to be updated
now_dcf_hash = tools.get_filesum(dcf_uri, True)
if now_dcf_hash != ult_dcf_hash:
dcf_update_values("")
#======#======================================================================= #======#=======================================================================
# MAIN # # MAIN #
#------# #------#

View File

@ -225,9 +225,7 @@ def ask_domain_server():
domain.dcf_load() domain.dcf_load()
srv = domain.dcf.get("SERVER", "root") srv = domain.dcf.get("SERVER", "root")
if srv: if srv and not not tools.dir_exists(srv, False):
if not tools.dir_exists(srv, False):
tested = True
srv = "" srv = ""
q = "> %s (%s)%s "%(langs.logs.domain_srv, srv, langs.logs.q) q = "> %s (%s)%s "%(langs.logs.domain_srv, srv, langs.logs.q)

View File

@ -114,7 +114,7 @@ def get_website_lang():
site_lang = domain.dcf.get("WEBSITE", "lang") site_lang = domain.dcf.get("WEBSITE", "lang")
if not translation_exists("website", site_lang, False): if not translation_exists("website", site_lang, False):
site_lang = get_sys_lang() site_lang = get_sys_lang() # or default "en"
tr_website_uri = tr_website_uri%(trfs, site_lang) tr_website_uri = tr_website_uri%(trfs, site_lang)

View File

@ -56,11 +56,8 @@ def get_filesum(path, src):
# Mainly used to check domain server dir # # Mainly used to check domain server dir #
#----------------------------------------# #----------------------------------------#
def dir_exists(dir_path, out): def dir_exists(dir_path, out):
exists = bool(os.path.exists(dir_path)) if not bool(os.path.exists(dir_path)):
debug.out(6, "False", dir_path, out, 2, out)
if not exists:
if out: debug.out(6, "False", dir_path, True, 2, True)
else: debug.out(6, "False", dir_path, True, 2, False)
return False return False
return True return True

View File

@ -60,5 +60,5 @@ tyto [action] [target] [options]
[options] ; multi-set [options] ; multi-set
--force, -F : force doing things... --force, -F : force doing things...
--debug, -D : show more logs --debug, -D : show more logs
--errors, -E : Show only warn and error logs --errors, -E : Show mainly warnings and errors logs
""" """

View File

@ -73,7 +73,7 @@ domain_new = "Domaine créé"
created_dir = "Dossier créé" created_dir = "Dossier créé"
created_file = "Fichier créé" created_file = "Fichier créé"
updated_file = "Fichier mis à jour" updated_file = "Fichier mis à jour"
later = "Peut-être plus targ..." later = "Peut-être plus tard..."
@ -104,5 +104,5 @@ tyto [action] [target] [options]
[options] ; multiples [options] ; multiples
--force, -F : forcer à faire quelque chose... --force, -F : forcer à faire quelque chose...
--debug, -D : montrer plus de logs --debug, -D : montrer plus de logs
--errors, -E : montrer uniquement les avertissement et erreurs --errors, -E : montrer surtout les avertissements et erreurs
""" """