From 1085351db15b8445eba18145d0971f94ec27900d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Stucke?= Date: Wed, 6 Mar 2024 09:58:30 +0100 Subject: [PATCH] chore: Manually fix ruff issues --- .../analysis/architecture_detection/internal/dt.py | 2 +- src/plugins/analysis/cve_lookup/code/cve_lookup.py | 2 +- src/plugins/analysis/file_type/code/file_type.py | 7 ++++++- .../internal/kernel_config_hardening_check.py | 2 +- .../analysis/kernel_config/test/test_kernel_config.py | 7 +++---- src/scheduler/analysis/plugin.py | 8 ++++++-- src/scheduler/analysis/scheduler.py | 2 +- src/start_fact_frontend.py | 4 +++- src/test/conftest.py | 6 ++++-- src/test/unit/conftest.py | 2 ++ src/test/unit/web_interface/rest/test_rest_firmware.py | 4 ++-- 11 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/plugins/analysis/architecture_detection/internal/dt.py b/src/plugins/analysis/architecture_detection/internal/dt.py index 81de3e698..096d2c888 100644 --- a/src/plugins/analysis/architecture_detection/internal/dt.py +++ b/src/plugins/analysis/architecture_detection/internal/dt.py @@ -58,7 +58,7 @@ def _get_compatible_entry(dts: str) -> str | None: break cpu = cpus[cpu_name] - if 'compatible' not in cpu.keys(): + if 'compatible' not in cpu: continue compatible = cpu['compatible'] diff --git a/src/plugins/analysis/cve_lookup/code/cve_lookup.py b/src/plugins/analysis/cve_lookup/code/cve_lookup.py index 2d4df843d..489422832 100644 --- a/src/plugins/analysis/cve_lookup/code/cve_lookup.py +++ b/src/plugins/analysis/cve_lookup/code/cve_lookup.py @@ -41,7 +41,7 @@ def process_object(self, file_object: FileObject) -> FileObject: cves = {'cve_results': {}} connection = DbConnection(f'sqlite:///{DB_PATH}') lookup = Lookup(file_object, connection) - for _key, value in file_object.processed_analysis['software_components']['result'].items(): + for value in file_object.processed_analysis['software_components']['result'].values(): product = value['meta']['software_name'] version = value['meta']['version'][0] if product and version: diff --git a/src/plugins/analysis/file_type/code/file_type.py b/src/plugins/analysis/file_type/code/file_type.py index f0626addd..32ee77f8e 100644 --- a/src/plugins/analysis/file_type/code/file_type.py +++ b/src/plugins/analysis/file_type/code/file_type.py @@ -1,4 +1,6 @@ -import io +from __future__ import annotations + +import typing from typing import List import pydantic @@ -8,6 +10,9 @@ from analysis.plugin import AnalysisPluginV0 from analysis.plugin.compat import AnalysisBasePluginAdapterMixin +if typing.TYPE_CHECKING: + import io + class AnalysisPlugin(AnalysisPluginV0, AnalysisBasePluginAdapterMixin): class Schema(pydantic.BaseModel): diff --git a/src/plugins/analysis/kernel_config/internal/kernel_config_hardening_check.py b/src/plugins/analysis/kernel_config/internal/kernel_config_hardening_check.py index 2901ef5a1..fe9efd9ba 100644 --- a/src/plugins/analysis/kernel_config/internal/kernel_config_hardening_check.py +++ b/src/plugins/analysis/kernel_config/internal/kernel_config_hardening_check.py @@ -150,7 +150,7 @@ def _add_protection_info(hardening_result: list[list[str]]) -> list[HardeningChe for single_result in hardening_result: config_key = single_result[0] actual_value = _detach_actual_value_from_result(single_result) - protection_info = PROTECTS_AGAINST[config_key] if config_key in PROTECTS_AGAINST else [] + protection_info = PROTECTS_AGAINST.get(config_key, []) full_result.append(HardeningCheckResult(*single_result, actual_value, protection_info)) return full_result diff --git a/src/plugins/analysis/kernel_config/test/test_kernel_config.py b/src/plugins/analysis/kernel_config/test/test_kernel_config.py index 0ff5bfcec..6c6033679 100644 --- a/src/plugins/analysis/kernel_config/test/test_kernel_config.py +++ b/src/plugins/analysis/kernel_config/test/test_kernel_config.py @@ -1,4 +1,3 @@ -import glob from pathlib import Path from subprocess import CompletedProcess @@ -81,7 +80,7 @@ def test_extract_ko_success(self, analysis_plugin): assert analysis_plugin.probably_kernel_config(result) def test_process_objects_kernel_image(self, analysis_plugin): - for valid_image in glob.glob(str(TEST_DATA_DIR / 'synthetic/*.image')): + for valid_image in (TEST_DATA_DIR / 'synthetic').glob('*.image'): test_file = FileObject(file_path=str(valid_image)) test_file.processed_analysis['file_type'] = {'result': {'mime': 'application/octet-stream'}} test_file.processed_analysis['software_components'] = {'summary': ['Linux Kernel']} @@ -91,7 +90,7 @@ def test_process_objects_kernel_image(self, analysis_plugin): assert test_file.processed_analysis[analysis_plugin.NAME]['is_kernel_config'] assert len(test_file.processed_analysis[analysis_plugin.NAME]['kernel_config']) > 0 - for bad_image in glob.glob(str(TEST_DATA_DIR / 'random_invalid/*.image')): + for bad_image in (TEST_DATA_DIR / 'random_invalid').glob('*.image'): test_file = FileObject(file_path=str(bad_image)) test_file.processed_analysis['file_type'] = {'result': {'mime': 'application/octet-stream'}} test_file.processed_analysis['software_components'] = {'summary': ['Linux Kernel']} @@ -149,7 +148,7 @@ def test_try_extract_fail(): def test_try_extract_random_fail(): - for fp in glob.glob(str(TEST_DATA_DIR / 'random_invalid/*.image')): + for fp in (TEST_DATA_DIR / 'random_invalid').glob('*.image'): test_file = FileObject(file_path=fp) test_file.processed_analysis['file_type'] = {'result': {'mime': 'application/octet-stream'}} test_file.processed_analysis['software_components'] = {'summary': ['Linux Kernel']} diff --git a/src/scheduler/analysis/plugin.py b/src/scheduler/analysis/plugin.py index 52c3e9c08..1a8a23945 100644 --- a/src/scheduler/analysis/plugin.py +++ b/src/scheduler/analysis/plugin.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import ctypes import io import logging @@ -14,11 +16,13 @@ from pydantic import BaseModel, ConfigDict import config -from analysis.plugin import AnalysisPluginV0 -from objects.file import FileObject +from objects.file import FileObject # noqa: TCH001 # needed by pydantic from statistic.analysis_stats import ANALYSIS_STATS_LIMIT from storage.fsorganizer import FSOrganizer +if typing.TYPE_CHECKING: + from analysis.plugin import AnalysisPluginV0 + class PluginRunner: class Config(BaseModel): diff --git a/src/scheduler/analysis/scheduler.py b/src/scheduler/analysis/scheduler.py index 67fc9fe43..337328203 100644 --- a/src/scheduler/analysis/scheduler.py +++ b/src/scheduler/analysis/scheduler.py @@ -611,7 +611,7 @@ def check_exceptions(self) -> bool: :return: Boolean value stating if any attached process ran into an exception """ - for _, plugin in self.analysis_plugins.items(): + for plugin in self.analysis_plugins.values(): if isinstance(plugin, AnalysisPluginV0): continue if plugin.check_exceptions(): diff --git a/src/start_fact_frontend.py b/src/start_fact_frontend.py index 1ded8a2d5..092b91fb5 100755 --- a/src/start_fact_frontend.py +++ b/src/start_fact_frontend.py @@ -1,7 +1,7 @@ #! /usr/bin/env python3 """ Firmware Analysis and Comparison Tool (FACT) - Copyright (C) 2015-2024 Fraunhofer FKIE + Copyright (C) 2015-2023 Fraunhofer FKIE This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,6 +17,8 @@ along with this program. If not, see . """ +from __future__ import annotations + import logging import pickle import signal diff --git a/src/test/conftest.py b/src/test/conftest.py index 520093dc2..cdb356a3b 100644 --- a/src/test/conftest.py +++ b/src/test/conftest.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from multiprocessing import Event, Queue, Value from pathlib import Path from typing import List, NamedTuple, Type, TypeVar @@ -290,7 +292,7 @@ def _unpacking_lock_manager() -> UnpackingLockManager: # noqa: PT005 @pytest.fixture(name='test_config') -def _scheduler_test_config(request) -> 'SchedulerTestConfig': # noqa: PT005 +def _scheduler_test_config(request) -> SchedulerTestConfig: # noqa: PT005 return SchedulerTestConfig.get_instance_from_request(request) @@ -566,7 +568,7 @@ def Acceptance(**kwargs): # noqa: N802 ) @staticmethod - def get_instance_from_request(request: pytest.FixtureRequest) -> 'SchedulerTestConfig': + def get_instance_from_request(request: pytest.FixtureRequest) -> SchedulerTestConfig: err = ValueError(f'{request.module} is neither a unit, acceptance nor integration test') test_config_dict = merge_markers(request, 'SchedulerTestConfig', dict) diff --git a/src/test/unit/conftest.py b/src/test/unit/conftest.py index cac1111ee..c0a1ad470 100644 --- a/src/test/unit/conftest.py +++ b/src/test/unit/conftest.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import Type import pytest diff --git a/src/test/unit/web_interface/rest/test_rest_firmware.py b/src/test/unit/web_interface/rest/test_rest_firmware.py index 57653b0ec..abf87ff3c 100644 --- a/src/test/unit/web_interface/rest/test_rest_firmware.py +++ b/src/test/unit/web_interface/rest/test_rest_firmware.py @@ -27,8 +27,8 @@ class DbMock(CommonDatabaseMock): def rest_get_firmware_uids( limit: int = 10, offset: int = 0, - query=None, - recursive=False, + query=None, # noqa: ARG004 + recursive=False, # noqa: ARG004 inverted=False, # noqa: ARG004 ): return [f'uid{i}' for i in range(offset, limit or 10)]