diff --git a/src/helperFunctions/fileSystem.py b/src/helperFunctions/fileSystem.py index 35572b0e7..26fd5c40f 100644 --- a/src/helperFunctions/fileSystem.py +++ b/src/helperFunctions/fileSystem.py @@ -21,6 +21,15 @@ def get_template_dir() -> Path: return Path(get_src_dir()) / 'web_interface' / 'templates' +def get_bin_dir() -> Path: + """ + Retrieves the absolute path of the bin directory. + + :return: The (absolute) path of the bin directory. + """ + return Path(get_src_dir()) / 'bin' + + def get_relative_object_path(path: Path, offset_path: Path) -> str: """ FACT extraction unpacks files into a temporary directory. These files have to be offset to get the path relative diff --git a/src/install/common.py b/src/install/common.py index c1c5ec5c3..06eb20cec 100644 --- a/src/install/common.py +++ b/src/install/common.py @@ -1,3 +1,4 @@ +import json import logging import subprocess from contextlib import suppress @@ -85,6 +86,7 @@ def _install_fw_magic(version: str = 'v0.2.2'): # compile the magic files (results in .mgc suffix) so that we don't get warnings when using them run_cmd_with_logging('file -C -m firmware') run_cmd_with_logging('file -C -m internal_symlink_magic') + Path('fw_magic_version.json').write_text(json.dumps({'version': version.lstrip('v')})) def _update_submodules(): diff --git a/src/plugins/analysis/file_type/code/file_type.py b/src/plugins/analysis/file_type/code/file_type.py index 09fa0cd97..439988017 100644 --- a/src/plugins/analysis/file_type/code/file_type.py +++ b/src/plugins/analysis/file_type/code/file_type.py @@ -1,14 +1,18 @@ from __future__ import annotations +import json import typing +from pathlib import Path from typing import List import pydantic from pydantic import Field +from semver import Version from analysis.plugin import AnalysisPluginV0 from analysis.plugin.compat import AnalysisBasePluginAdapterMixin from helperFunctions import magic +from helperFunctions.fileSystem import get_bin_dir if typing.TYPE_CHECKING: import io @@ -24,11 +28,17 @@ class Schema(pydantic.BaseModel): ) def __init__(self): + try: + version_file = Path(get_bin_dir()) / 'version.json' + fw_magic_db_version = json.loads(version_file.read_text()).get('version') + except (json.JSONDecodeError, FileNotFoundError): + fw_magic_db_version = None super().__init__( metadata=AnalysisPluginV0.MetaData( name='file_type', description='identify the file type', - version='1.0.0', + version=Version(1, 0, 1), + system_version=fw_magic_db_version, Schema=AnalysisPlugin.Schema, ), ) diff --git a/src/test/unit/helperFunctions/test_file_system.py b/src/test/unit/helperFunctions/test_file_system.py index c83b08ce9..ac6b78288 100644 --- a/src/test/unit/helperFunctions/test_file_system.py +++ b/src/test/unit/helperFunctions/test_file_system.py @@ -5,6 +5,7 @@ from helperFunctions.fileSystem import ( file_is_empty, + get_bin_dir, get_config_dir, get_relative_object_path, get_src_dir, @@ -39,6 +40,11 @@ def test_get_template_dir(): assert '.html' in file_suffixes_in_template_dir +def test_get_bin_dir(): + bin_dir = get_bin_dir() + assert bin_dir.is_dir(), 'bin dir not found' + + @pytest.mark.parametrize( ('base', 'offset', 'result', 'message'), [