refs #1093 rebuild ogGetOsVersion

code-review
Antonio Guerrero 2024-11-12 23:34:52 -06:00
parent 9a1c90234e
commit 77a9eb2439
1 changed files with 34 additions and 174 deletions

View File

@ -196,182 +196,42 @@ def ogListSoftware(disk, partition):
return sorted(set(apps))
def ogGetOsVersion(ndisk, nfilesys):
# Variables locales.
MNTDIR = FileSystemLib.ogMount(ndisk, nfilesys)
TYPE = ""
VERSION = ""
IS64BIT = ""
# Si se solicita, mostrar ayuda.
if len(sys.argv) > 1 and sys.argv[1] == "help":
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
# Error si no se reciben 2 parametros.
if len(sys.argv) != 3:
SystemLib.ogRaiseError(OG_ERR_FORMAT)
return
# Montar la particion, si no lo estaba previamente.
MNTDIR = FileSystemLib.ogMount(sys.argv[1], sys.argv[2])
if not MNTDIR:
return
# Buscar tipo de sistema operativo.
# Para GNU/Linux: leer descripción.
TYPE = "Linux"
FILE = os.path.join(MNTDIR, "etc", "os-release")
if os.path.exists(FILE):
with open(FILE, "r") as f:
for line in f:
if line.startswith("PRETTY_NAME"):
VERSION = line.split("=")[1].strip().strip('"')
break
# Si no se puede obtener, buscar en ficheros del sistema.
if not VERSION:
FILE = os.path.join(MNTDIR, "etc", "lsb-release")
if os.path.exists(FILE):
with open(FILE, "r") as f:
def ogGetOsVersion(disk, part):
try:
mnt_dir = FileSystemLib.ogMount(disk, part)
version = None
os_release = os.path.join(mnt_dir, "etc/os-release")
if os.path.isfile(os_release):
with open(os_release, "r") as f:
for line in f:
if line.startswith("DESCRIPTION"):
VERSION = line.split("=")[1].strip().strip('"')
if line.startswith("PRETTY_NAME"):
version = line.split("=", 1)[1].strip().strip('"')
break
for DISTRIB in ["redhat", "SuSE", "mandrake", "gentoo"]:
FILE = os.path.join(MNTDIR, "etc", f"{DISTRIB}-release")
if os.path.exists(FILE):
with open(FILE, "r") as f:
VERSION = f.readline().strip()
break
FILE = os.path.join(MNTDIR, "etc", "arch-release")
if os.path.exists(FILE):
VERSION = "Arch Linux"
FILE = os.path.join(MNTDIR, "etc", "slackware-version")
if os.path.exists(FILE):
with open(FILE, "r") as f:
VERSION = f.read().strip()
# Si no se encuentra, intentar ejecutar "lsb_release".
if not VERSION:
try:
output = subprocess.check_output(["chroot", MNTDIR, "lsb_release", "-d"], stderr=subprocess.DEVNULL).decode().strip()
VERSION = output.split(":")[1].strip()
except subprocess.CalledProcessError:
pass
# Comprobar Linux de 64 bits.
if VERSION and os.path.exists(os.path.join(MNTDIR, "lib64")):
IS64BIT = "64 bits"
# Para Android, leer fichero de propiedades.
if not VERSION:
TYPE = "Android"
FILE = os.path.join(MNTDIR, "android*", "system", "build.prop")
if glob.glob(FILE):
with open(glob.glob(FILE)[0], "r") as f:
lines = f.readlines()
brand = ""
release = ""
for line in lines:
if line.startswith("product.brand"):
brand = line.split("=")[1].strip()
elif line.startswith("build.version.release"):
release = line.split("=")[1].strip()
VERSION = f"Android {brand} {release}"
if os.path.exists(os.path.join(MNTDIR, "lib64")):
IS64BIT = "64 bits"
# Para GNU/Hurd, comprobar fichero de inicio (basado en os-prober).
if not VERSION:
TYPE = "Hurd"
FILE = os.path.join(MNTDIR, "hurd", "init")
if os.path.exists(FILE):
VERSION = "GNU/Hurd"
# Para Windows: leer la version del registro.
if not VERSION:
TYPE = "Windows"
FILE = ogGetHivePath(MNTDIR, "SOFTWARE")
if FILE:
try:
output = subprocess.check_output(["hivexsh", "load", FILE, "cd", "\\Microsoft\\Windows NT\\CurrentVersion", "lsval", "ProductName", "lsval", "DisplayVersion"], stderr=subprocess.DEVNULL).decode().strip()
lines = output.split("\n")
if len(lines) == 2:
VERSION = lines[1].strip()
if Registry.ogGetRegistryValue(MNTDIR, "SOFTWARE", "\\Microsoft\\Windows\\CurrentVersion\\ProgramW6432Dir"):
IS64BIT = "64 bits"
except subprocess.CalledProcessError:
pass
# Para cargador Windows: buscar versión en fichero BCD (basado en os-prober).
if not VERSION:
TYPE = "WinLoader"
FILE = FileLib.ogGetPath(MNTDIR, "boot", "bcd")
if not FILE:
FILE = FileLib.ogGetPath(MNTDIR, "EFI", "Microsoft", "boot", "bcd")
if FILE:
for DISTRIB in ["Windows Recovery", "Windows Boot"]:
with open(FILE, "rb") as f:
if re.search(DISTRIB.encode(), f.read()):
VERSION = f"{DISTRIB} loader"
if not version:
lsb_release = os.path.join(mnt_dir, "etc/lsb-release")
if os.path.isfile(lsb_release):
with open(lsb_release, "r") as f:
for line in f:
if line.startswith("DISTRIB_DESCRIPTION"):
version = line.split("=", 1)[1].strip().strip('"')
break
if not version:
for distrib in ["redhat", "SuSE", "mandrake", "gentoo"]:
distrib_file = os.path.join(mnt_dir, f"etc/{distrib}-release")
if os.path.isfile(distrib_file):
with open(distrib_file, "r") as f:
version = f.readline().strip()
break
is_64bit = os.path.exists(os.path.join(mnt_dir, "lib64"))
if is_64bit:
version = f"{version} 64 bits"
# Para macOS: detectar kernel y completar con fichero plist de información del sistema.
if not VERSION:
TYPE = "MacOS"
FILE = os.path.join(MNTDIR, "mach_kernel")
if os.path.exists(FILE) and not subprocess.check_output(["file", "-b", FILE]).decode().strip().startswith("text"):
if subprocess.check_output(["file", "-b", FILE]).decode().strip().startswith("Mach-O"):
VERSION = "macOS"
if subprocess.check_output(["file", "-b", FILE]).decode().strip().startswith("Mach-O 64-bit"):
IS64BIT = "64 bits"
FILE = os.path.join(MNTDIR, "System", "Library", "CoreServices", "SystemVersion.plist")
if os.path.exists(FILE):
with open(FILE, "r") as f:
plist_data = f.read()
product_name = re.search(r"<key>ProductName</key>\s*<string>(.*?)</string>", plist_data)
product_version = re.search(r"<key>ProductVersion</key>\s*<string>(.*?)</string>", plist_data)
if product_name and product_version:
VERSION = f"{product_name.group(1)} {product_version.group(1)}"
FILE = os.path.join(MNTDIR, "com.apple.recovery.boot")
if os.path.exists(FILE) and VERSION:
VERSION = f"{VERSION} recovery"
# Para FreeBSD: obtener datos del Kernel.
if not VERSION:
TYPE = "BSD"
FILE = os.path.join(MNTDIR, "boot", "kernel", "kernel")
if os.path.exists(FILE):
output = subprocess.check_output(["strings", FILE]).decode().strip()
match = re.search(r"@.*RELEASE", output)
if match:
VERSION = match.group().split("@")[0].strip()
if subprocess.check_output(["file", "-b", FILE]).decode().strip().endswith("x86-64"):
IS64BIT = "64 bits"
# Para Solaris: leer el fichero de versión.
if not VERSION:
TYPE = "Solaris"
FILE = os.path.join(MNTDIR, "etc", "release")
if os.path.exists(FILE):
with open(FILE, "r") as f:
VERSION = f.readline().strip()
# Para cargador GRUB, comprobar fichero de configuración.
if not VERSION:
TYPE = "GrubLoader"
for FILE in [os.path.join(MNTDIR, "grub", "menu.lst"), os.path.join(MNTDIR, "boot", "grub", "menu.lst")]:
if os.path.exists(FILE):
VERSION = "GRUB Loader"
break
for FILE in glob.glob(os.path.join(MNTDIR, "{,boot/}{grub{,2},EFI/*}/grub.cfg")):
if os.path.exists(FILE):
VERSION = "GRUB2 Loader"
break
# Mostrar resultado y salir sin errores.
if VERSION:
print(f"{TYPE}:{VERSION} {IS64BIT}")
return 0
return f"Linux: {version}" if version else "Linux: Unknown"
except Exception as e:
print(f"Fail to get OS: {e}")
return None