-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helperFunctions): Introduce magic
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""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. | ||
""" | ||
from __future__ import annotations | ||
|
||
import os | ||
from os import PathLike | ||
|
||
import magic as pymagic | ||
|
||
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_magic.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()))) | ||
instance = _instances.get(key) | ||
if instance is None: | ||
instance = _instances[key] = pymagic.Magic(**kwargs) | ||
return instance | ||
|
||
|
||
def from_file(filename: bytes | str | PathLike, magic_file: str | None = _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(buf: bytes | str, magic_file: str | None = _magic_file, **kwargs) -> str: | ||
"""Like pymagic's ``magic.from_buffer`` but it accepts all keyword arguments | ||
that ``magic.Magic`` accepts. | ||
""" | ||
instance = _get_magic_instance(magic_file=magic_file, **kwargs) | ||
return instance.from_buffer(buf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# ====================== fact internal ====================== | ||
|
||
# ---- fact internal link representation ---- | ||
0 string symbolic\ link\ -> symbolic link | ||
>17 string x to '%s' | ||
!:mime inode/symlink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from helperFunctions import magic | ||
|
||
|
||
def test_internal_magic(): | ||
assert magic.from_buffer('symbolic link -> /foo/bar', mime=True) == 'inode/symlink' | ||
|
||
|
||
def test_firmware_magic(): | ||
assert magic.from_buffer('BOOTLOADER!', mime=False) == 'Mediatek bootloader' | ||
|
||
|
||
def test_magic_from_file(): | ||
assert magic.from_file('/dev/null', mime=True) == 'inode/chardevice' |