refs #1099 The syntax for calling libraries into InventoryLib.py has been modified

code-review
Antonio Guerrero 2024-11-06 04:46:28 +00:00
parent d46ab35bd6
commit 2d94e3742e
1 changed files with 27 additions and 29 deletions

View File

@ -8,20 +8,18 @@ import re
import json import json
import shutil import shutil
import sqlite3 import sqlite3
from SystemLib import ogMount, ogGetOsType, ogRaiseError, ogHelp, ogGetOsVersion, ogGetHivePath, ogListRegistryKeys, ogGetRegistryValue, ogGetIpAddress
sys.path.append('/opt/opengnsys/lib/engine/bin') import SystemLib
from SystemLib import ogEcho import ogGlobals
#from Disklib import ogDiskToDev import FileLib
#from engine.FileLib import * import RegistryLib
#from engine.RegistryLib import * import FileSystemLib
#from engine.FileSystemLib import *
MSG_HARDWAREINVENTORY = "Inventario de hardware de la máquina" MSG_HARDWAREINVENTORY = "Inventario de hardware de la máquina"
def ogGetArch(): def ogGetArch():
if len(sys.argv) > 1 and sys.argv[1] == "help": if len(sys.argv) > 1 and sys.argv[1] == "help":
ogHelp(sys.argv[0], sys.argv[0], sys.argv[0] + " => x86_64") SystemLib.ogHelp(sys.argv[0], sys.argv[0], sys.argv[0] + " => x86_64")
return return
if platform.machine().endswith("64"): if platform.machine().endswith("64"):
@ -32,23 +30,23 @@ def ogGetArch():
def ogGetOsType(): def ogGetOsType():
# Si se solicita, mostrar ayuda. # Si se solicita, mostrar ayuda.
if len(sys.argv) > 1 and sys.argv[1] == "help": if len(sys.argv) > 1 and sys.argv[1] == "help":
ogHelp(sys.argv[0], sys.argv[0] + " int_ndisk int_npartition", sys.argv[0] + " 1 2 => Linux") SystemLib.ogHelp(sys.argv[0], sys.argv[0] + " int_ndisk int_npartition", sys.argv[0] + " 1 2 => Linux")
return return
ogGetOsVersion(sys.argv[1:]).split(':')[0] ogGetOsVersion(sys.argv[1:]).split(':')[0]
def ogGetOsUuid(): def ogGetOsUuid():
# Si se solicita, mostrar ayuda. # Si se solicita, mostrar ayuda.
if len(sys.argv) > 1 and sys.argv[1] == "help": if len(sys.argv) > 1 and sys.argv[1] == "help":
ogHelp(sys.argv[0], sys.argv[0] + " int_ndisk int_nfilesys", sys.argv[0] + " 1 2 => 540e47c6-8e78-4178-aa46-042e4803fb16") SystemLib.ogHelp(sys.argv[0], sys.argv[0] + " int_ndisk int_nfilesys", sys.argv[0] + " 1 2 => 540e47c6-8e78-4178-aa46-042e4803fb16")
return return
# Error si no se reciben 2 parametros. # Error si no se reciben 2 parametros.
if len(sys.argv) != 3: if len(sys.argv) != 3:
ogRaiseError(OG_ERR_FORMAT) SystemLib.ogRaiseError(OG_ERR_FORMAT)
return return
# Montar la particion, si no lo estaba previamente. # Montar la particion, si no lo estaba previamente.
MNTDIR = ogMount(sys.argv[1], sys.argv[2]) MNTDIR = FileSystemLib.ogMount(sys.argv[1], sys.argv[2])
if not MNTDIR: if not MNTDIR:
return return
@ -60,13 +58,13 @@ def ogGetOsUuid():
print(uuid) print(uuid)
elif os_type == "Windows": elif os_type == "Windows":
# Leer identificador en clave de registro. # Leer identificador en clave de registro.
uuid = ogGetRegistryValue(MNTDIR, "SOFTWARE", "\\Microsoft\\Cryptography\\MachineGuid") uuid = Registry.ogGetRegistryValue(MNTDIR, "SOFTWARE", "\\Microsoft\\Cryptography\\MachineGuid")
print(uuid) print(uuid)
def ogGetSerialNumber(): def ogGetSerialNumber():
# Si se solicita, mostrar ayuda. # Si se solicita, mostrar ayuda.
if len(sys.argv) > 1 and sys.argv[1] == "help": if len(sys.argv) > 1 and sys.argv[1] == "help":
ogHelp(sys.argv[0], sys.argv[0], sys.argv[0] + " => 123456") SystemLib.ogHelp(sys.argv[0], sys.argv[0], sys.argv[0] + " => 123456")
return return
# Obtener nº de serie (ignorar los no especificados). # Obtener nº de serie (ignorar los no especificados).
@ -141,12 +139,12 @@ def ogListHardwareInfo():
def ogListSoftware(disk, partition): def ogListSoftware(disk, partition):
# Error si no se reciben 2 parametros # Error si no se reciben 2 parametros
if disk is None or partition is None: if disk is None or partition is None:
og_raise_error("OG_ERR_FORMAT") SystemLib.ogRaiseError("OG_ERR_FORMAT")
return return
# Obtener tipo de sistema de archivos y montarlo # Obtener tipo de sistema de archivos y montarlo
mnt_dir = og_mount(disk, partition) mnt_dir = FileSystemLib.ogMount(disk, partition)
os_type = og_get_os_type(disk, partition) os_type = ogGetOsType(disk, partition)
# Ficheros temporales # Ficheros temporales
apps_file = tempfile.NamedTemporaryFile(delete=False) apps_file = tempfile.NamedTemporaryFile(delete=False)
@ -174,17 +172,17 @@ def ogListSoftware(disk, partition):
pass pass
elif os_type == "Windows": elif os_type == "Windows":
# Procesar aplicaciones instaladas en Windows # Procesar aplicaciones instaladas en Windows
hive_path = og_get_hive_path(mnt_dir, "software") hive_path = Registry.ogGetHivePath(mnt_dir, "software")
if hive_path: if hive_path:
subprocess.run(["hivexregedit", "--unsafe-printable-strings", "--export", hive_path, '\Microsoft\Windows\CurrentVersion\Uninstall'], stdout=tmp_file) subprocess.run(["hivexregedit", "--unsafe-printable-strings", "--export", hive_path, '\Microsoft\Windows\CurrentVersion\Uninstall'], stdout=tmp_file)
elif os_type == "MacOS": elif osType == "MacOS":
# Procesar aplicaciones instaladas en MacOS # Procesar aplicaciones instaladas en MacOS
pass pass
elif os_type == "BSD": elif osType == "BSD":
# Procesar aplicaciones instaladas en FreeBSD # Procesar aplicaciones instaladas en FreeBSD
pass pass
else: else:
og_raise_error("OG_ERR_NOTOS", disk, partition) SystemLib.ogRaiseError("OG_ERR_NOTOS", disk, partition)
finally: finally:
# Eliminar archivos temporales # Eliminar archivos temporales
apps_file.close() apps_file.close()
@ -193,30 +191,30 @@ def ogListSoftware(disk, partition):
os.remove(tmp_file.name) os.remove(tmp_file.name)
# Mostrar sistema operativo y aplicaciones # Mostrar sistema operativo y aplicaciones
og_get_os_version(disk, partition) ogGetOsVersion(disk, partition)
with open(apps_file.name, "r") as apps: with open(apps_file.name, "r") as apps:
for line in sorted(set(apps.readlines())): for line in sorted(set(apps.readlines())):
print(line.strip()) print(line.strip())
def ogGetOsVersion(ndisk, nfilesys): def ogGetOsVersion(ndisk, nfilesys):
# Variables locales. # Variables locales.
MNTDIR = ogMount(ndisk, nfilesys) MNTDIR = FileSystemLib.ogMount(ndisk, nfilesys)
TYPE = "" TYPE = ""
VERSION = "" VERSION = ""
IS64BIT = "" IS64BIT = ""
# Si se solicita, mostrar ayuda. # Si se solicita, mostrar ayuda.
if len(sys.argv) > 1 and sys.argv[1] == "help": if len(sys.argv) > 1 and sys.argv[1] == "help":
ogHelp(sys.argv[0], sys.argv[0] + " int_ndisk int_nfilesys", sys.argv[0] + " 1 2 => Linux:Ubuntu precise (12.04 LTS) 64 bits") SystemLib.ogHelp(sys.argv[0], sys.argv[0] + " int_ndisk int_nfilesys", sys.argv[0] + " 1 2 => Linux:Ubuntu precise (12.04 LTS) 64 bits")
return return
# Error si no se reciben 2 parametros. # Error si no se reciben 2 parametros.
if len(sys.argv) != 3: if len(sys.argv) != 3:
ogRaiseError(OG_ERR_FORMAT) SystemLib.ogRaiseError(OG_ERR_FORMAT)
return return
# Montar la particion, si no lo estaba previamente. # Montar la particion, si no lo estaba previamente.
MNTDIR = ogMount(sys.argv[1], sys.argv[2]) MNTDIR = FileSystemLib.ogMount(sys.argv[1], sys.argv[2])
if not MNTDIR: if not MNTDIR:
return return
@ -301,7 +299,7 @@ def ogGetOsVersion(ndisk, nfilesys):
lines = output.split("\n") lines = output.split("\n")
if len(lines) == 2: if len(lines) == 2:
VERSION = lines[1].strip() VERSION = lines[1].strip()
if ogGetRegistryValue(MNTDIR, "SOFTWARE", "\\Microsoft\\Windows\\CurrentVersion\\ProgramW6432Dir"): if Registry.ogGetRegistryValue(MNTDIR, "SOFTWARE", "\\Microsoft\\Windows\\CurrentVersion\\ProgramW6432Dir"):
IS64BIT = "64 bits" IS64BIT = "64 bits"
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
pass pass
@ -309,9 +307,9 @@ def ogGetOsVersion(ndisk, nfilesys):
# Para cargador Windows: buscar versión en fichero BCD (basado en os-prober). # Para cargador Windows: buscar versión en fichero BCD (basado en os-prober).
if not VERSION: if not VERSION:
TYPE = "WinLoader" TYPE = "WinLoader"
FILE = ogGetPath(MNTDIR, "boot", "bcd") FILE = FileLib.ogGetPath(MNTDIR, "boot", "bcd")
if not FILE: if not FILE:
FILE = ogGetPath(MNTDIR, "EFI", "Microsoft", "boot", "bcd") FILE = FileLib.ogGetPath(MNTDIR, "EFI", "Microsoft", "boot", "bcd")
if FILE: if FILE:
for DISTRIB in ["Windows Recovery", "Windows Boot"]: for DISTRIB in ["Windows Recovery", "Windows Boot"]:
with open(FILE, "rb") as f: with open(FILE, "rb") as f: