Skip to content

Commit

Permalink
feat: file_type: use fw-magic-db version as system_version
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke committed Dec 12, 2024
1 parent 92080af commit 1ff0d2c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/helperFunctions/fileSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/install/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import subprocess
from contextlib import suppress
Expand Down Expand Up @@ -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():
Expand Down
12 changes: 11 additions & 1 deletion src/plugins/analysis/file_type/code/file_type.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
),
)
Expand Down
6 changes: 6 additions & 0 deletions src/test/unit/helperFunctions/test_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from helperFunctions.fileSystem import (
file_is_empty,
get_bin_dir,
get_config_dir,
get_relative_object_path,
get_src_dir,
Expand Down Expand Up @@ -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'),
[
Expand Down

0 comments on commit 1ff0d2c

Please sign in to comment.