Skip to content

Commit

Permalink
helperFunctions: Introduce magic
Browse files Browse the repository at this point in the history
  • Loading branch information
maringuu committed Oct 18, 2023
1 parent 6cb9354 commit 867ae5e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/helperFunctions/magic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""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

# Default magic database according to libmagic(3)
_default_magic = os.getenv('MAGIC', '/usr/share/file/misc/magic.mgc')
_fact_magic = f'{get_src_dir()}/bin/fact.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)

return pymagic.from_file(filename, magic_file=magic_file, **kwargs)


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)
7 changes: 7 additions & 0 deletions src/install/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
3 changes: 3 additions & 0 deletions src/install/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def main(skip_docker, radare, nginx, distribution):
pkgs = read_package_list_from_file(INSTALL_DIR / 'dnf-pkgs-frontend.txt')
dnf_install_packages(*pkgs)

with OperateInDirectory('../../src/bin'):
run_cmd_with_logging('wget https://github.com/maringuu/fact-mime-database/releases/download/v0.1.0/fact.mgc')

# flask-security is not maintained anymore and replaced by flask-security-too.
# Since python package naming conflicts are not resolved automatically, we remove flask-security manually.
pip = 'pip' if is_virtualenv() else 'sudo -EH pip3'
Expand Down
6 changes: 6 additions & 0 deletions src/install/internal_symlink_magic
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ====================== faf internal ======================

# ---- faf internal link representation ----
0 string symbolic\ link\ -> symbolic link
>17 string x to '%s'
!:mime inode/symlink

0 comments on commit 867ae5e

Please sign in to comment.