refs #1059 add ogRaiseError()

pull/1/head
Natalia Serrano 2024-10-29 18:15:00 +01:00
parent 7cd6e22b0d
commit eb43e67b38
2 changed files with 126 additions and 113 deletions

View File

@ -9,6 +9,7 @@ import ogGlobals
#from engine.DiskLib import *
#from engine.CacheLib import *
#from engine.StringLib import *
from StringLib import *
#NODEBUGFUNCTIONS, OGIMG, OG_ERR_CACHESIZE, OG_ERR_NOTCACHE, OG_ERR_NOTWRITE, OG_ERR_FILESYS
#OG_ERR_REPO, OG_ERR_NOTOS, OG_ERR_NOGPT, OG_ERR_OUTOFLIMIT, OG_ERR_IMAGE, OG_ERR_CACHE
@ -169,123 +170,82 @@ def ogHelp (fname, fmt=None, examples=[]):
else: ## string
ogEcho([], "help", f" {ogGlobals.lang.MSG_EXAMPLE}: {examples}")
def ogRaiseError(*args):
# Variables locales
CONT = 1
LOGS = ""
MSG = ""
CODE = ""
FUNCS = ""
FUNCNAME = ogRaiseError.__name__
# Si se solicita, mostrar ayuda.
if len(args) > 0 and args[0] == "help":
ogHelp(f"{FUNCNAME}", f"{FUNCNAME} [str_logfile ...] int_errorcode str_errormessage")
#/**
# ogRaiseError [str_logtype ...] int_errcode ["str_errmessage" ...]
#@brief Devuelve el mensaje y el código de error correspondiente.
#@param str_logtype tipo de registro de incidencias.
#@param int_errcode código de error.
#@param str_errmessage mensajes complementarios de error.
#@return str_code - código de error
#*/
def ogRaiseError (logtypes, code, msg):
if type (logtypes) is str and 'help' == logtypes:
ogHelp (
'ogRaiseError',
'ogRaiseError ([str_logfile, ...], int_errorcode, str_errormessage)',
[
'ogRaiseError ("log", 42, "my error message")',
'ogRaiseError (["log", "session"], 43, "my other error message")',
]
)
return
# Selección de registros de incidencias.
while CONT:
arg = args.pop(0).lower()
if arg == "log" or arg == "command" or arg == "session":
LOGS += " " + arg
else:
CONT = 0
# Obtener código y mensaje de error.
CODE = args.pop(0)
if CODE == OG_ERR_FORMAT:
MSG = f"{MSG_ERR_FORMAT} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOTFOUND:
MSG = f"{MSG_ERR_NOTFOUND} \"{args.pop(0)}\""
elif CODE == OG_ERR_OUTOFLIMIT:
MSG = f"{MSG_ERR_OUTOFLIMIT} \"{args.pop(0)}\""
elif CODE == OG_ERR_PARTITION:
MSG = f"{MSG_ERR_PARTITION} \"{args.pop(0)}\""
elif CODE == OG_ERR_LOCKED:
MSG = f"{MSG_ERR_LOCKED} \"{args.pop(0)}\""
elif CODE == OG_ERR_CACHE:
MSG = f"{MSG_ERR_CACHE} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOGPT:
MSG = f"{MSG_ERR_NOGPT} \"{args.pop(0)}\""
elif CODE == OG_ERR_REPO:
MSG = f"{MSG_ERR_REPO} \"{args.pop(0)}\""
elif CODE == OG_ERR_FILESYS:
MSG = f"{MSG_ERR_FILESYS} \"{args.pop(0)}\""
elif CODE == OG_ERR_IMAGE:
MSG = f"{MSG_ERR_IMAGE} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOTOS:
MSG = f"{MSG_ERR_NOTOS} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOTEXEC:
MSG = f"{MSG_ERR_NOTEXEC} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOTWRITE:
MSG = f"{MSG_ERR_NOTWRITE} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOTCACHE:
MSG = f"{MSG_ERR_NOTCACHE} \"{args.pop(0)}\""
elif CODE == OG_ERR_CACHESIZE:
MSG = f"{MSG_ERR_CACHESIZE} \"{args.pop(0)}\""
elif CODE == OG_ERR_REDUCEFS:
MSG = f"{MSG_ERR_REDUCEFS} \"{args.pop(0)}\""
elif CODE == OG_ERR_EXTENDFS:
MSG = f"{MSG_ERR_EXTENDFS} \"{args.pop(0)}\""
elif CODE == OG_ERR_IMGSIZEPARTITION:
MSG = f"{MSG_ERR_IMGSIZEPARTITION} \"{args.pop(0)}\""
elif CODE == OG_ERR_UPDATECACHE:
MSG = f"{MSG_ERR_UPDATECACHE} \"{args.pop(0)}\""
elif CODE == OG_ERR_DONTFORMAT:
MSG = f"{MSG_ERR_DONTFORMAT} \"{args.pop(0)}\""
elif CODE == OG_ERR_IMAGEFILE:
MSG = f"{MSG_ERR_IMAGEFILE} \"{args.pop(0)}\""
elif CODE == OG_ERR_UCASTSYNTAXT:
MSG = f"{MSG_ERR_UCASTSYNTAXT} \"{args.pop(0)}\""
elif CODE == OG_ERR_UCASTSENDPARTITION:
MSG = f"{MSG_ERR_UCASTSENDPARTITION} \"{args.pop(0)}\""
elif CODE == OG_ERR_UCASTSENDFILE:
MSG = f"{MSG_ERR_UCASTSENDFILE} \"{args.pop(0)}\""
elif CODE == OG_ERR_UCASTRECEIVERPARTITION:
MSG = f"{MSG_ERR_UCASTRECEIVERPARTITION} \"{args.pop(0)}\""
elif CODE == OG_ERR_UCASTRECEIVERFILE:
MSG = f"{MSG_ERR_UCASTRECEIVERFILE} \"{args.pop(0)}\""
elif CODE == OG_ERR_MCASTSYNTAXT:
MSG = f"{MSG_ERR_MCASTSYNTAXT} \"{args.pop(0)}\""
elif CODE == OG_ERR_MCASTSENDFILE:
MSG = f"{MSG_ERR_MCASTSENDFILE} \"{args.pop(0)}\""
elif CODE == OG_ERR_MCASTRECEIVERFILE:
MSG = f"{MSG_ERR_MCASTRECEIVERFILE} \"{args.pop(0)}\""
elif CODE == OG_ERR_MCASTSENDPARTITION:
MSG = f"{MSG_ERR_MCASTSENDPARTITION} \"{args.pop(0)}\""
elif CODE == OG_ERR_MCASTRECEIVERPARTITION:
MSG = f"{MSG_ERR_MCASTRECEIVERPARTITION} \"{args.pop(0)}\""
elif CODE == OG_ERR_PROTOCOLJOINMASTER:
MSG = f"{MSG_ERR_PROTOCOLJOINMASTER} \"{args.pop(0)}\""
elif CODE == OG_ERR_DONTMOUNT_IMAGE:
MSG = f"{MSG_ERR_DONTMOUNT_IMAGE} \"{args.pop(0)}\""
elif CODE == OG_ERR_DONTUNMOUNT_IMAGE:
MSG = f"{MSG_ERR_DONTUNMOUNT_IMAGE} \"{args.pop(0)}\""
elif CODE == OG_ERR_DONTSYNC_IMAGE:
MSG = f"{MSG_ERR_DONTSYNC_IMAGE} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOTDIFFERENT:
MSG = f"{MSG_ERR_NOTDIFFERENT} \"{args.pop(0)}\""
elif CODE == OG_ERR_SYNCHRONIZING:
MSG = f"{MSG_ERR_SYNCHRONIZING} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOTUEFI:
MSG = f"{MSG_ERR_NOTUEFI} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOMSDOS:
MSG = f"{MSG_ERR_NOMSDOS} \"{args.pop(0)}\""
elif CODE == OG_ERR_NOTBIOS:
MSG = f"{MSG_ERR_NOTBIOS} \"{args.pop(0)}\""
if code == ogGlobals.OG_ERR_FORMAT: MSG = f'{ogGlobals.lang.MSG_ERR_FORMAT} "{msg}"'
elif code == ogGlobals.OG_ERR_NOTFOUND: MSG = f'{ogGlobals.lang.MSG_ERR_NOTFOUND} "{msg}"'
elif code == ogGlobals.OG_ERR_OUTOFLIMIT: MSG = f'{ogGlobals.lang.MSG_ERR_OUTOFLIMIT} "{msg}"'
elif code == ogGlobals.OG_ERR_PARTITION: MSG = f'{ogGlobals.lang.MSG_ERR_PARTITION} "{msg}"'
elif code == ogGlobals.OG_ERR_LOCKED: MSG = f'{ogGlobals.lang.MSG_ERR_LOCKED} "{msg}"'
elif code == ogGlobals.OG_ERR_CACHE: MSG = f'{ogGlobals.lang.MSG_ERR_CACHE} "{msg}"'
elif code == ogGlobals.OG_ERR_NOGPT: MSG = f'{ogGlobals.lang.MSG_ERR_NOGPT} "{msg}"'
elif code == ogGlobals.OG_ERR_REPO: MSG = f'{ogGlobals.lang.MSG_ERR_REPO} "{msg}"'
elif code == ogGlobals.OG_ERR_FILESYS: MSG = f'{ogGlobals.lang.MSG_ERR_FILESYS} "{msg}"'
elif code == ogGlobals.OG_ERR_IMAGE: MSG = f'{ogGlobals.lang.MSG_ERR_IMAGE} "{msg}"'
elif code == ogGlobals.OG_ERR_NOTOS: MSG = f'{ogGlobals.lang.MSG_ERR_NOTOS} "{msg}"'
elif code == ogGlobals.OG_ERR_NOTEXEC: MSG = f'{ogGlobals.lang.MSG_ERR_NOTEXEC} "{msg}"'
elif code == ogGlobals.OG_ERR_NOTWRITE: MSG = f'{ogGlobals.lang.MSG_ERR_NOTWRITE} "{msg}"'
elif code == ogGlobals.OG_ERR_NOTCACHE: MSG = f'{ogGlobals.lang.MSG_ERR_NOTCACHE} "{msg}"'
elif code == ogGlobals.OG_ERR_CACHESIZE: MSG = f'{ogGlobals.lang.MSG_ERR_CACHESIZE} "{msg}"'
elif code == ogGlobals.OG_ERR_REDUCEFS: MSG = f'{ogGlobals.lang.MSG_ERR_REDUCEFS} "{msg}"'
elif code == ogGlobals.OG_ERR_EXTENDFS: MSG = f'{ogGlobals.lang.MSG_ERR_EXTENDFS} "{msg}"'
elif code == ogGlobals.OG_ERR_IMGSIZEPARTITION: MSG = f'{ogGlobals.lang.MSG_ERR_IMGSIZEPARTITION} "{msg}"'
elif code == ogGlobals.OG_ERR_UPDATECACHE: MSG = f'{ogGlobals.lang.MSG_ERR_UPDATECACHE} "{msg}"'
elif code == ogGlobals.OG_ERR_DONTFORMAT: MSG = f'{ogGlobals.lang.MSG_ERR_DONTFORMAT} "{msg}"'
elif code == ogGlobals.OG_ERR_IMAGEFILE: MSG = f'{ogGlobals.lang.MSG_ERR_IMAGEFILE} "{msg}"'
elif code == ogGlobals.OG_ERR_UCASTSYNTAXT: MSG = f'{ogGlobals.lang.MSG_ERR_UCASTSYNTAXT} "{msg}"'
elif code == ogGlobals.OG_ERR_UCASTSENDPARTITION: MSG = f'{ogGlobals.lang.MSG_ERR_UCASTSENDPARTITION} "{msg}"'
elif code == ogGlobals.OG_ERR_UCASTSENDFILE: MSG = f'{ogGlobals.lang.MSG_ERR_UCASTSENDFILE} "{msg}"'
elif code == ogGlobals.OG_ERR_UCASTRECEIVERPARTITION: MSG = f'{ogGlobals.lang.MSG_ERR_UCASTRECEIVERPARTITION} "{msg}"'
elif code == ogGlobals.OG_ERR_UCASTRECEIVERFILE: MSG = f'{ogGlobals.lang.MSG_ERR_UCASTRECEIVERFILE} "{msg}"'
elif code == ogGlobals.OG_ERR_MCASTSYNTAXT: MSG = f'{ogGlobals.lang.MSG_ERR_MCASTSYNTAXT} "{msg}"'
elif code == ogGlobals.OG_ERR_MCASTSENDFILE: MSG = f'{ogGlobals.lang.MSG_ERR_MCASTSENDFILE} "{msg}"'
elif code == ogGlobals.OG_ERR_MCASTRECEIVERFILE: MSG = f'{ogGlobals.lang.MSG_ERR_MCASTRECEIVERFILE} "{msg}"'
elif code == ogGlobals.OG_ERR_MCASTSENDPARTITION: MSG = f'{ogGlobals.lang.MSG_ERR_MCASTSENDPARTITION} "{msg}"'
elif code == ogGlobals.OG_ERR_MCASTRECEIVERPARTITION: MSG = f'{ogGlobals.lang.MSG_ERR_MCASTRECEIVERPARTITION} "{msg}"'
elif code == ogGlobals.OG_ERR_PROTOCOLJOINMASTER: MSG = f'{ogGlobals.lang.MSG_ERR_PROTOCOLJOINMASTER} "{msg}"'
elif code == ogGlobals.OG_ERR_DONTMOUNT_IMAGE: MSG = f'{ogGlobals.lang.MSG_ERR_DONTMOUNT_IMAGE} "{msg}"'
elif code == ogGlobals.OG_ERR_DONTUNMOUNT_IMAGE: MSG = f'{ogGlobals.lang.MSG_ERR_DONTUNMOUNT_IMAGE} "{msg}"'
elif code == ogGlobals.OG_ERR_DONTSYNC_IMAGE: MSG = f'{ogGlobals.lang.MSG_ERR_DONTSYNC_IMAGE} "{msg}"'
elif code == ogGlobals.OG_ERR_NOTDIFFERENT: MSG = f'{ogGlobals.lang.MSG_ERR_NOTDIFFERENT} "{msg}"'
elif code == ogGlobals.OG_ERR_SYNCHRONIZING: MSG = f'{ogGlobals.lang.MSG_ERR_SYNCHRONIZING} "{msg}"'
elif code == ogGlobals.OG_ERR_NOTUEFI: MSG = f'{ogGlobals.lang.MSG_ERR_NOTUEFI} "{msg}"'
elif code == ogGlobals.OG_ERR_NOMSDOS: MSG = f'{ogGlobals.lang.MSG_ERR_NOMSDOS} "{msg}"'
elif code == ogGlobals.OG_ERR_NOTBIOS: MSG = f'{ogGlobals.lang.MSG_ERR_NOTBIOS} "{msg}"'
else:
MSG = MSG_ERR_GENERIC
CODE = OG_ERR_GENERIC
MSG = ogGlobals.lang.MSG_ERR_GENERIC
CODE = ogGlobals.OG_ERR_GENERIC
# Obtener lista de funciones afectadas, incluyendo el script que las llama.
FUNCS = " ".join(FUNCNAME[1:])
FUNCS = FUNCS.replace("main", os.path.basename(sys.argv[0]))
call_stack = [i[3] for i in inspect.stack()]
if len (call_stack) < 3: return ## shouldn't happen
call_stack.pop() ## remove '<module>'
call_stack.pop(0) ## remove 'ogRaiseError'
str_call_stack = ' '.join (call_stack)
# Mostrar mensaje de error si es función depurable y salir con el código indicado.
if CODE == OG_ERR_FORMAT or ogCheckStringInGroup(FUNCS, NODEBUGFUNCTIONS) or not ogCheckStringInGroup(FUNCS.split()[0], NODEBUGFUNCTIONS):
ogEcho(LOGS, "error", f"{FUNCS.replace(' ', '<-')}: {MSG}", file=sys.stderr)
if code == ogGlobals.OG_ERR_FORMAT or \
ogCheckStringInGroup (str_call_stack, ogGlobals.NODEBUGFUNCTIONS) or \
not ogCheckStringInGroup (call_stack[0], ogGlobals.NODEBUGFUNCTIONS):
ogEcho (logtypes, "error", f"{str_call_stack.replace(' ', '<-')}: {MSG}")
return CODE
return code
def ogIsRepoLocked():
# Variables locales

View File

@ -23,12 +23,65 @@ if not l or not load_lang (f'lang_{l}'):
if not load_lang ('lang_en_GB'):
raise ModuleNotFoundError (f"can't find the default language module", name=name)
TZ='Europe/Madrid'
## engine.cfg
NODEBUGFUNCTIONS='ogCreateImageSyntax ogGetHivePath ogGetOsType ogRestoreImageSyntax ogUnmountAll ogUnmountCache'
## /engine.cfg
#OPENGNSYS='/opt/opengnsys'
OPENGNSYS='/tmp/opengnsys'
OPENGNSYS='/tmp/opengnsys' ## XXX
OGLOG=f'{OPENGNSYS}/log'
OGLOGFILE=f'{OGLOG}/192.168.42.42' ## TODO
OGLOGCOMMAND='/tmp/command.log'
OGLOGSESSION='/tmp/session.log'
DEBUG='yes'
TZ='Europe/Madrid'
# Declaración de códigos de error.
OG_ERR_FORMAT=1 # Formato de ejecución incorrecto.
OG_ERR_NOTFOUND=2 # Fichero o dispositivo no encontrado.
OG_ERR_PARTITION=3 # Error en partición de disco.
OG_ERR_LOCKED=4 # Partición o fichero bloqueado.
OG_ERR_IMAGE=5 # Error al crear o restaurar una imagen.
OG_ERR_NOTOS=6 # Sin sistema operativo.
OG_ERR_NOTEXEC=7 # Programa o función no ejecutable.
# Códigos 8-13 reservados por ogAdmClient.h
OG_ERR_NOTWRITE=14 # No hay acceso de escritura
OG_ERR_NOTCACHE=15 # No hay particion cache en cliente
OG_ERR_CACHESIZE=16 # No hay espacio en la cache para almacenar fichero-imagen
OG_ERR_REDUCEFS=17 # Error al reducir sistema archivos
OG_ERR_EXTENDFS=18 # Error al expandir el sistema de archivos
OG_ERR_OUTOFLIMIT=19 # Valor fuera de rango o no válido.
OG_ERR_FILESYS=20 # Sistema de archivos desconocido o no se puede montar
OG_ERR_CACHE=21 # Error en partición de caché local
OG_ERR_NOGPT=22 # El disco indicado no contiene una particion GPT
OG_ERR_REPO=23 # Error al montar el repositorio de imagenes
OG_ERR_NOMSDOS=24 # El disco indicado no contienen una particion MSDOS
OG_ERR_IMGSIZEPARTITION=30 # Error al restaurar partición más pequeña que la imagen
OG_ERR_UPDATECACHE=31 # Error al realizar el comando updateCache
OG_ERR_DONTFORMAT=32 # Error al formatear
OG_ERR_IMAGEFILE=33 # Archivo de imagen corrupto o de otra versión de $IMGPROG
OG_ERR_GENERIC=40 # Error imprevisto no definido
OG_ERR_UCASTSYNTAXT=50 # Error en la generación de sintaxis de transferenica UNICAST
OG_ERR_UCASTSENDPARTITION=51 # Error en envío UNICAST de partición
OG_ERR_UCASTSENDFILE=52 # Error en envío UNICAST de un fichero
OG_ERR_UCASTRECEIVERPARTITION=53 # Error en la recepcion UNICAST de una particion
OG_ERR_UCASTRECEIVERFILE=54 # Error en la recepcion UNICAST de un fichero
OG_ERR_MCASTSYNTAXT=55 # Error en la generacion de sintaxis de transferenica Multicast.
OG_ERR_MCASTSENDFILE=56 # Error en envio MULTICAST de un fichero
OG_ERR_MCASTRECEIVERFILE=57 # Error en la recepcion MULTICAST de un fichero
OG_ERR_MCASTSENDPARTITION=58 # Error en envio MULTICAST de una particion
OG_ERR_MCASTRECEIVERPARTITION=59 # Error en la recepcion MULTICAST de una particion
OG_ERR_PROTOCOLJOINMASTER=60 # Error en la conexion de una sesion UNICAST|MULTICAST con el MASTER
OG_ERR_DONTMOUNT_IMAGE=70 # Error al montar una imagen sincronizada.
OG_ERR_DONTSYNC_IMAGE=71 # Imagen no sincronizable (es monolitica)
OG_ERR_DONTUNMOUNT_IMAGE=72 # Error al desmontar la imagen
OG_ERR_NOTDIFFERENT=73 # No se detectan diferencias entre la imagen basica y la particion.
OG_ERR_SYNCHRONIZING=74 # Error al sincronizar, puede afectar la creacion/restauracion de la imagen
OG_ERR_NOTUEFI=80 # La interfaz UEFI no está activa
OG_ERR_NOTBIOS=81 # La interfaz BIOS legacy no está activa