Skip to content

Commit

Permalink
chore: fix or properly ignore ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke authored and maringuu committed Nov 27, 2024
1 parent 7d418b8 commit 92b7508
Show file tree
Hide file tree
Showing 32 changed files with 190 additions and 202 deletions.
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ fixable = ["ALL"]

[tool.ruff.lint.per-file-ignores]
"test*.py" = ["ARG002", "PLR2004"]
"conftest.py" = ["ARG002"]
"conftest.py" = ["ARG001", "ARG002"]
"common_helper.py" = ["ARG002"]
# ignore prints in CLI scripts
"migrate_db_to_postgresql.py" = ["T201"]
"manage_users.py" = ["T201"]
"migrate_database.py" = ["T201"]

[tool.ruff.lint.isort]
known-first-party = ["analysis", "compare", "helperFunctions", "install", "intercom", "objects", "plugins", "scheduler",
Expand Down
4 changes: 3 additions & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ def load(path: str | Path | None = None):
Frontend.model_rebuild()
if path is None:
path = Path(__file__).parent / 'config/fact-core-config.toml'
elif isinstance(path, str):
path = Path(path)

with open(path, encoding='utf8') as f: # noqa: PTH123
with path.open(encoding='utf8') as f:
cfg = toml.load(f)

_replace_hyphens_with_underscores(cfg)
Expand Down
10 changes: 5 additions & 5 deletions src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def docker_mount_base_dir() -> str:


@pytest.fixture
def _firmware_file_storage_directory() -> str: # noqa: PT005
def firmware_file_storage_directory() -> str:
with TemporaryDirectory(prefix='fact-firmware-file-storage-directory') as tmp_dir:
yield tmp_dir

Expand Down Expand Up @@ -98,11 +98,11 @@ def common_config(request, docker_mount_base_dir) -> config.Common:


@pytest.fixture
def backend_config(request, common_config, _firmware_file_storage_directory) -> config.Backend:
def backend_config(request, common_config, firmware_file_storage_directory) -> config.Backend:
overwrite_config = merge_markers(request, 'backend_config_overwrite', dict)

test_config = {
'firmware_file_storage_directory': _firmware_file_storage_directory,
'firmware_file_storage_directory': firmware_file_storage_directory,
'block_delay': 0.1,
'ssdeep_ignore': 1,
'intercom_poll_delay': 1.0,
Expand Down Expand Up @@ -156,7 +156,7 @@ def frontend_config(request, common_config) -> config.Frontend:


@pytest.fixture(autouse=True)
def patch_config(monkeypatch, common_config, backend_config, frontend_config): # noqa: PT004
def _patch_config(monkeypatch, common_config, backend_config, frontend_config):
"""This fixture will replace :py:data`config.common`, :py:data:`config.backend` and :py:data:`config.frontend`
with the default test config.
Expand Down Expand Up @@ -194,7 +194,7 @@ class AnalysisPluginTestConfig(BaseModel):


@pytest.fixture
def analysis_plugin(request, patch_config): # noqa: ARG001
def analysis_plugin(request, _patch_config):
"""Returns an instance of an AnalysisPlugin.
This fixture can be configured by the supplying an instance of ``AnalysisPluginTestConfig`` as marker of the same
name.
Expand Down
2 changes: 1 addition & 1 deletion src/helperFunctions/yara_binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self):
self.db = DbInterfaceCommon()
self.fs_organizer = FSOrganizer()

def _execute_yara_search(self, rule_file_path: str, target_path: str | None = None) -> str:
def _execute_yara_search(self, rule_file_path: str | Path, target_path: str | Path | None = None) -> str:
"""
Scans the (whole) db directory with the provided rule file and returns the (raw) results.
Yara-python cannot be used, because it (currently) supports single-file scanning only.
Expand Down
2 changes: 1 addition & 1 deletion src/install/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def _install_yara():
raise InstallationError(f'Error on yara extraction.\n{unzip_process.stdout}')
yara_folder = [p for p in Path().iterdir() if p.name.startswith('yara-')][0]
with OperateInDirectory(yara_folder.name, remove=True):
os.chmod('bootstrap.sh', 0o775) # noqa: PTH101
Path('bootstrap.sh').chmod(0o775)
for command in ['./bootstrap.sh', './configure --enable-magic', 'make -j$(nproc)', 'sudo make install']:
cmd_process = subprocess.run(command, shell=True, stdout=PIPE, stderr=STDOUT, text=True, check=False)
if cmd_process.returncode != 0:
Expand Down
23 changes: 11 additions & 12 deletions src/manage_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import argparse
import getpass
import sys
from pathlib import Path

from flask_security import hash_password
from prompt_toolkit import PromptSession
Expand Down Expand Up @@ -58,7 +57,7 @@ def __init__(self, session, app, store, db):

@staticmethod
def help():
print( # noqa: T201
print(
'\nOne of the following actions can be chosen:\n'
'\n\t[add_role_to_user]\tadd existing role to an existing user'
'\n\t[create_role]\t\tcreate new role'
Expand Down Expand Up @@ -94,7 +93,7 @@ def create_user(self):
while True:
password = getpass.getpass('password: ')
if not password_is_legal(password):
print('Password is not legal. Please choose another password.') # noqa: T201
print('Password is not legal. Please choose another password.')
continue
break
with self.app.app_context():
Expand Down Expand Up @@ -172,14 +171,14 @@ def get_apikey_for_user(self):
user = self.store.find_user(email=user)

apikey = user.api_key
print(f'key: {apikey}') # noqa: T201
print(f'key: {apikey}')

def list_all_users(self):
user_list = self.store.list_users()
for user in user_list:
user_roles = ', '.join([role.name for role in user.roles])
print(f'\n\t{user.email} ({user_roles})') # noqa: T201
print() # noqa: T201
print(f'\n\t{user.email} ({user_roles})')
print()

@staticmethod
def exit():
Expand All @@ -198,8 +197,8 @@ def initialise_roles(app, interface, db):


def prompt_loop(app, store, db, session):
print(FACT_ASCII_ART) # noqa: T201
print('\nWelcome to the FACT User Management (FACTUM)\n') # noqa: T201
print(FACT_ASCII_ART)
print('\nWelcome to the FACT User Management (FACTUM)\n')
initialise_roles(app, store, db)
actions = Actions(session, app, store, db)

Expand All @@ -218,13 +217,13 @@ def prompt_loop(app, store, db, session):
acting_function()

except KeyboardInterrupt:
print('returning to action selection') # noqa: T201
print('returning to action selection')
except AssertionError as assertion_error:
print(f'error: {assertion_error}') # noqa: T201
print(f'error: {assertion_error}')
except EOFError:
break

print('\nQuitting ..') # noqa: T201
print('\nQuitting ..')


def start_user_management(app, store, db, session):
Expand All @@ -235,7 +234,7 @@ def start_user_management(app, store, db, session):

def main():
args = setup_argparse()
config.load(Path(args.config_file))
config.load(args.config_file)
app = create_app()
user_db, user_datastore = add_flask_security_to_app(app)

Expand Down
4 changes: 2 additions & 2 deletions src/migrate_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def upgrade(cur):
cur.execute('DROP TABLE "user"')
cur.execute('ALTER TABLE "user_tmp" RENAME TO "user"')

print('Successfully upgraded the database') # noqa: T201
print('Successfully upgraded the database')


def downgrade(cur):
Expand All @@ -60,7 +60,7 @@ def downgrade(cur):
cur.execute('DROP TABLE "user"')
cur.execute('ALTER TABLE "user_tmp" RENAME TO "user"')

print('Successfully downgraded the database') # noqa: T201
print('Successfully downgraded the database')


def main():
Expand Down
10 changes: 5 additions & 5 deletions src/migrate_db_to_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
try:
from rich.progress import BarColumn, Progress, TimeElapsedColumn
except ImportError:
print('Error: rich not found. Please install it:\npython3 -m pip install rich') # noqa: T201
print('Error: rich not found. Please install it:\npython3 -m pip install rich')
sys.exit(1)

PERCENTAGE = '[progress.percentage]{task.percentage:>3.0f}%'
Expand Down Expand Up @@ -299,9 +299,9 @@ def main():
migrator = DbMigrator(postgres=postgres, mongo=db, progress=progress)
migrated_fw_count = migrator.migrate_fw(query={}, root=True, label='firmwares')
if not migrated_fw_count:
print('No firmware to migrate') # noqa: T201
print('No firmware to migrate')
else:
print(f'Successfully migrated {migrated_fw_count} firmware DB entries') # noqa: T201
print(f'Successfully migrated {migrated_fw_count} firmware DB entries')
migrate_comparisons(db)
except errors.ServerSelectionTimeoutError:
logging.error(
Expand Down Expand Up @@ -387,9 +387,9 @@ def migrate_comparisons(mongo: MigrationMongoInterface):
compare_db.insert_comparison(comparison_id, results)
count += 1
if not count:
print('No firmware comparison entries to migrate') # noqa: T201
print('No firmware comparison entries to migrate')
else:
print(f'Migrated {count} comparison DB entries') # noqa: T201
print(f'Migrated {count} comparison DB entries')


if __name__ == '__main__':
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/analysis/binwalk/test/test_plugin_binwalk.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from pathlib import Path

import pytest

from test.common_helper import get_test_data_dir

from ..code.binwalk import AnalysisPlugin

TEST_FILE = Path(get_test_data_dir()) / 'container' / 'test.zip'
TEST_FILE = get_test_data_dir() / 'container' / 'test.zip'


@pytest.mark.AnalysisPluginTestConfig(plugin_class=AnalysisPlugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from ..code.elf_analysis import AnalysisPlugin

TEST_DATA = Path(get_test_data_dir(), 'test_data_file.bin')
TEST_DATA = get_test_data_dir() / 'test_data_file.bin'

TEST_DATA_DIR = Path(__file__).parent / 'data'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from pathlib import Path

import pytest

from objects.file import FileObject
from test.common_helper import get_test_data_dir

from ..code.hardware_analysis import AnalysisPlugin

TEST_DATA = Path(get_test_data_dir())


@pytest.mark.AnalysisPluginTestConfig(plugin_class=AnalysisPlugin)
class TestHardwareAnalysis:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,5 +474,5 @@ class MockFSOrganizer:
@staticmethod
def generate_path(fo):
if fo.uid != 'foo':
return os.path.join(get_test_data_dir(), 'container/test.zip') # noqa: PTH118
return str(get_test_data_dir() / 'container/test.zip')
return None
8 changes: 4 additions & 4 deletions src/test/acceptance/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@pytest.fixture(autouse=True)
def _autouse_database_interfaces(database_interfaces): # noqa: ARG001
def _autouse_database_interfaces(database_interfaces):
pass


Expand All @@ -27,12 +27,12 @@ def test_client(web_frontend):


@pytest.fixture
def intercom_backend_binding(_unpacking_lock_manager, analysis_scheduler, comparison_scheduler, unpacking_scheduler):
def intercom_backend_binding(unpacking_lock_manager, analysis_scheduler, comparison_scheduler, unpacking_scheduler):
_intercom_backend_binding = InterComBackEndBinding(
analysis_service=analysis_scheduler,
compare_service=comparison_scheduler,
unpacking_service=unpacking_scheduler,
unpacking_locks=_unpacking_lock_manager,
unpacking_locks=unpacking_lock_manager,
)
_intercom_backend_binding.start()

Expand Down Expand Up @@ -76,7 +76,7 @@ def __init__(self, uid, path, name):


def upload_test_firmware(test_client, test_fw):
testfile_path = Path(get_test_data_dir()) / test_fw.path
testfile_path = get_test_data_dir() / test_fw.path
with testfile_path.open('rb') as fp:
data = {
'file': (fp, test_fw.file_name),
Expand Down
3 changes: 1 addition & 2 deletions src/test/acceptance/rest/test_rest_compare.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import urllib.parse
from base64 import standard_b64encode
from pathlib import Path

import pytest

Expand All @@ -10,7 +9,7 @@

class TestRestCompareFirmware:
def _rest_upload_firmware(self, test_client, fw):
testfile_path = Path(get_test_data_dir()) / fw.path
testfile_path = get_test_data_dir() / fw.path
file_content = testfile_path.read_bytes()
data = {
'binary': standard_b64encode(file_content).decode(),
Expand Down
5 changes: 2 additions & 3 deletions src/test/acceptance/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import os
import time
from urllib.parse import quote

Expand Down Expand Up @@ -32,8 +31,8 @@ def _upload_firmware_get(self, test_client):
assert b'<h3 class="mb-3">Upload Firmware</h3>' in rv.data, 'upload page not displayed correctly'

def _upload_firmware_put(self, test_client, path, device_name, uid):
testfile_path = os.path.join(get_test_data_dir(), path) # noqa: PTH118
with open(testfile_path, 'rb') as fp: # noqa: PTH123
testfile_path = get_test_data_dir() / path
with testfile_path.open('rb') as fp:
data = {
'file': fp,
'device_name': device_name,
Expand Down
Loading

0 comments on commit 92b7508

Please sign in to comment.