From 4155ee1e741e90ceb75e415e282a833ddf49d878 Mon Sep 17 00:00:00 2001 From: Marten Ringwelski Date: Tue, 4 Jul 2023 11:59:08 +0000 Subject: [PATCH] feat(helperFunctions): Introduce magic --- src/helperFunctions/magic.py | 42 ++++++++++++++++++++++++++++++ src/install/common.py | 7 +++++ src/install/internal_symlink_magic | 6 +++++ 3 files changed, 55 insertions(+) create mode 100644 src/helperFunctions/magic.py create mode 100644 src/install/internal_symlink_magic diff --git a/src/helperFunctions/magic.py b/src/helperFunctions/magic.py new file mode 100644 index 000000000..e1b8f68ed --- /dev/null +++ b/src/helperFunctions/magic.py @@ -0,0 +1,42 @@ +"""This is a wrapper around pymagic. +It aims to provide the same API but with the ability to load multiple magic +files in the default api. +""" +import magic as pymagic +import os + +from helperFunctions.fileSystem import get_src_dir + +# On ubuntu this is provided by the libmagic-mgc package +_default_magic = os.getenv('MAGIC', '/usr/lib/file/magic.mgc') +_fact_magic = f'{get_src_dir()}/bin/firmware.mgc' +_internal_symlink_magic = f'{get_src_dir()}/bin/internal_symlink.mgc' +_magic_file = f'{_internal_symlink_magic}:{_fact_magic}:{_default_magic}' + +_instances = {} + + +def _get_magic_instance(**kwargs): + """Returns an instance of pymagic.Maigc""" + # Dicts are not hashable but sorting and creating a tuple is a valid hash + key = hash(tuple(sorted(kwargs.items()))) + i = _instances.get(key) + if i is None: + i = _instances[key] = pymagic.Magic(**kwargs) + return i + + +def from_file(filename, magic_file=_magic_file, **kwargs) -> str: + """Like pymagic's ``magic.from_file`` but it accepts all keyword arguments + that ``magic.Magic`` accepts. + """ + m = _get_magic_instance(magic_file=magic_file, **kwargs) + return m.from_file(filename) + + +def from_buffer(filename, magic_file=_magic_file, **kwargs) -> str: + """Like pymagic's ``magic.from_buffer`` but it accepts all keyword arguments + that ``magic.Magic`` accepts. + """ + m = _get_magic_instance(magic_file=magic_file, **kwargs) + return m.from_file(filename) diff --git a/src/install/common.py b/src/install/common.py index 2e0427c6a..c79afc03a 100644 --- a/src/install/common.py +++ b/src/install/common.py @@ -46,6 +46,13 @@ def main(distribution): BIN_DIR.mkdir(exist_ok=True) + with OperateInDirectory('../../src/bin'): + run_cmd_with_logging( + 'wget https://github.com/maringuu/firmware-magic-database/releases/download/v0.1.0/firmware.mgc' + ) + run_cmd_with_logging(f'file -C -m {INSTALL_DIR / "internal_symlink_magic"}') + run_cmd_with_logging(f'mv {INSTALL_DIR / "internal_symlink_magic.mgc"} {BIN_DIR}') + apt_packages_path = INSTALL_DIR / 'apt-pkgs-common.txt' dnf_packages_path = INSTALL_DIR / 'dnf-pkgs-common.txt' diff --git a/src/install/internal_symlink_magic b/src/install/internal_symlink_magic new file mode 100644 index 000000000..5bb311d26 --- /dev/null +++ b/src/install/internal_symlink_magic @@ -0,0 +1,6 @@ +# ====================== faf internal ====================== + +# ---- faf internal link representation ---- +0 string symbolic\ link\ -> symbolic link +>17 string x to '%s' +!:mime inode/symlink