From dd4ccc53175a51f3543a025c683053337a80c8bc Mon Sep 17 00:00:00 2001 From: timoxa0 Date: Thu, 16 May 2024 23:29:49 +0500 Subject: [PATCH] Add check_roofs function --- lon_deployer/utils.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lon_deployer/utils.py b/lon_deployer/utils.py index da3e415..7ba061b 100644 --- a/lon_deployer/utils.py +++ b/lon_deployer/utils.py @@ -1,14 +1,16 @@ import logging +import pathlib +import platform import re import socket +import subprocess from random import randint from time import sleep -from . import exceptions - import adbutils -from rich.logging import RichHandler +from magic import Magic from rich.console import Console +from rich.logging import RichHandler from rich.progress import ( BarColumn, DownloadColumn, @@ -18,6 +20,8 @@ from rich.progress import ( TransferSpeedColumn, ) +from . import exceptions + console = Console(log_path=False) @@ -93,3 +97,19 @@ def repartition(serial: str, size: int, percents=False) -> None: for cmd in cmds: device.shell(cmd) sleep(1) + + +def check_rootfs(filepath: pathlib.Path) -> bool: + osname = platform.system() + if osname == "Linux": + magic_file = subprocess.check_output(["file", "--version"]) \ + .decode().splitlines()[1].split()[-1].split(":")[-1] + ".mgc" + logger.debug(f"Magic file: {magic_file}") + magic = Magic(mime=True, magic_file=magic_file) + elif osname == "Windows": + magic = Magic(mime=True) + else: + raise exceptions.UnsupportedPlatform(osname) + filetype = magic.from_file(filepath.absolute()) + logger.debug(f"RootFS MIME type: {filetype}") + return filetype in ["application/octet-stream", "inode/blockdevice"]