-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Remove dependency on fact_helper_file
- Loading branch information
Showing
10 changed files
with
98 additions
and
13 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.file_system import get_src_dir | ||
|
||
# On ubuntu this is provided by the libmagic-mgc package | ||
_default_magic = os.getenv('MAGIC', '/usr/lib/file/magic.mgc') | ||
_fw_magic = f'{get_src_dir()}/bin/firmware' | ||
_magic_file = f'{_fw_magic}:{_default_magic}' | ||
|
||
_instances = {} | ||
|
||
|
||
def _get_magic_instance(**kwargs): | ||
"""Returns an instance of pymagic.Magic""" | ||
# 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. | ||
""" | ||
instance = _get_magic_instance(magic_file=magic_file, **kwargs) | ||
return instance.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
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
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
Binary file not shown.
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,15 @@ | ||
from pathlib import Path | ||
|
||
from helperFunctions import magic | ||
|
||
from helperFunctions.file_system import get_fact_bin_dir, get_test_data_dir | ||
|
||
|
||
def test_magic(): | ||
firmware_magic_path = Path(get_fact_bin_dir()) / 'firmware' | ||
assert firmware_magic_path.is_file() | ||
|
||
assert ( | ||
magic.from_file(f'{get_test_data_dir()}/ros_header', mime=True) == 'firmware/ros' | ||
), 'firmware-magic-database is not loaded' | ||
assert magic.from_file(f'{get_test_data_dir()}/container/test.zip', mime=True) == 'application/zip' |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# FixMe: deprecated | ||
pluginbase~=1.0.1 | ||
git+https://github.com/fkie-cad/common_helper_unpacking_classifier.git | ||
git+https://github.com/fkie-cad/fact_helper_file.git | ||
python-magic | ||
patool~=2.2.0 | ||
# jffs2: jefferson + deps | ||
git+https://github.com/sviehb/[email protected] | ||
|