Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions bin/maillogsentinel_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def interactive_cli_setup(target_config_path, setup_log_fh):
setup_log_fh,
)
_setup_print_and_log(
f"You may need to manually perform these steps or re-run with sudo IF you want systemd integration.",
"You may need to manually perform these steps or re-run with sudo IF you want systemd integration.",
setup_log_fh,
)
_setup_print_and_log(
Expand Down Expand Up @@ -965,10 +965,14 @@ def interactive_cli_setup(target_config_path, setup_log_fh):
f"FATAL ERROR during interactive setup: {e_main_interactive.__class__.__name__}: {e_main_interactive}",
setup_log_fh,
)
import traceback
_setup_print_and_log(
f"FATAL ERROR during interactive setup: {e_main_interactive.__class__.__name__}: {e_main_interactive}",
setup_log_fh,
)
# import traceback # No longer needed

_setup_print_and_log(traceback.format_exc(), setup_log_fh)
return False # Indicate failure
# _setup_print_and_log(traceback.format_exc(), setup_log_fh) # No longer needed
return False
return True


Expand All @@ -983,13 +987,12 @@ def non_interactive_setup(source_config_path: Path, setup_log_fh):
Updates global backed_up_items and created_final_paths lists.
"""
import sys # Ensure sys is imported for stdout
import traceback

print("non_interactive_setup CALLED", flush=True)
# traceback.print_stack(file=sys.stdout, limit=10) # Removed for debugging
print("---", flush=True)

_setup_print_and_log(f"--- MailLogSentinel Non-Interactive Setup ---", setup_log_fh)
_setup_print_and_log("--- MailLogSentinel Non-Interactive Setup ---", setup_log_fh)

if os.geteuid() != 0:
_setup_print_and_log(
Expand Down Expand Up @@ -1156,7 +1159,7 @@ def non_interactive_setup(source_config_path: Path, setup_log_fh):
)
usermod_cmd = shutil.which("usermod")
if not usermod_cmd:
_setup_print_and_log(f"ERROR: 'usermod' not found.", setup_log_fh)
_setup_print_and_log("ERROR: 'usermod' not found.", setup_log_fh)
sys.exit(1) # Restored error
try:
process_result = subprocess.run(
Expand Down
1 change: 0 additions & 1 deletion lib/maillogsentinel/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
Callable,
)
import logging
import sys

# Add bin directory to sys.path to allow importing ipinfo
# sys.path.append(str(Path(__file__).resolve().parent.parent.parent / "bin")) # Removed for cleaner path management
Expand Down
10 changes: 3 additions & 7 deletions lib/maillogsentinel/sql_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import csv
import datetime
import hashlib
import json
import logging
import os
from pathlib import Path
from typing import Optional, List, Dict, Any
import importlib.resources # Added for loading bundled data

import tempfile
import shutil
from lib.maillogsentinel.config import AppConfig # Import AppConfig

# Constants
Expand Down Expand Up @@ -608,7 +608,7 @@ def run_sql_export(config: AppConfig, output_log_level: str = "INFO") -> bool:
if sql_file_path.exists():
sql_file_path.unlink(missing_ok=True) # cleanup
return False
except CSVSchemaError as e: # Already logged by validate_csv_header
except CSVSchemaError: # Already logged by validate_csv_header
# Cleanup already handled in validate_csv_header's calling block
return False
except Exception as e:
Expand Down Expand Up @@ -663,10 +663,6 @@ def run_sql_export(config: AppConfig, output_log_level: str = "INFO") -> bool:
return True


import tempfile
import shutil


# Test configuration class
class DummyTestConfig:
def __init__(self, mapping_file_path_str="", base_dir_name=None):
Expand Down
6 changes: 1 addition & 5 deletions lib/maillogsentinel/sql_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import logging
import sqlite3
import time
import datetime # For handling imported file records
import os # For os.getpid() in FileLock
from pathlib import Path
from typing import List, Tuple, Dict, Any, Optional
from typing import Optional
import fcntl # For file locking on POSIX systems

import importlib.resources # Added for loading bundled data
Expand Down Expand Up @@ -714,6 +713,3 @@ def __init__(self, base_path: Path):
print("\nsql_importer.py direct test finished.")
# Note: For a real test, you'd use pytest and mock AppConfig, Path.is_file(), open(), etc.
# This direct run is just for very basic flow checking.

# Required for FileLock to get PID (if not already imported by another module like logging)
import os
10 changes: 2 additions & 8 deletions tests/bin/test_maillogsentinel_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import tempfile
import os
import io # For mock_log_fh spec
import locale # For mocking in specific test
import re


Expand Down Expand Up @@ -413,7 +412,6 @@ def test_path_management_creation(self):

try:
original_backed_up_items = mls_setup.backed_up_items
original_created_final_paths = mls_setup.created_final_paths
mls_setup.backed_up_items = []
mls_setup.created_final_paths = []

Expand All @@ -433,7 +431,6 @@ def test_path_management_creation(self):
finally:
os.remove(config_path_str)
mls_setup.backed_up_items = original_backed_up_items
mls_setup.created_final_paths = original_created_final_paths

def test_path_management_backup_existing(self):
"""Test backup of workdir and statedir when they already exist."""
Expand Down Expand Up @@ -475,7 +472,6 @@ def test_path_management_backup_existing(self):

try:
original_backed_up_items = mls_setup.backed_up_items
original_created_final_paths = mls_setup.created_final_paths
mls_setup.backed_up_items = []
mls_setup.created_final_paths = []

Expand All @@ -502,7 +498,6 @@ def test_path_management_backup_existing(self):
finally:
os.remove(config_path_str)
mls_setup.backed_up_items = original_backed_up_items
mls_setup.created_final_paths = original_created_final_paths

# User/Group Management Tests
def test_user_verification_non_existent(self):
Expand Down Expand Up @@ -946,7 +941,6 @@ def path_exists_side_effect(*args_passed):
config.read_string(VALID_CONFIG_CONTENT)

try:
original_created_final_paths = mls_setup.created_final_paths
mls_setup.created_final_paths = []
with patch(
"configparser.ConfigParser.read", return_value=[config_path_str_val]
Expand Down Expand Up @@ -1374,7 +1368,7 @@ def test_systemd_control_commands_success(self):
"bin.maillogsentinel_setup.subprocess.run"
) as mock_subprocess_run, patch(
"bin.maillogsentinel_setup._setup_print_and_log"
) as mock_setup_print, patch(
), patch(
"bin.maillogsentinel_setup.sys.exit"
) as mock_sys_exit: # noqa: F841

Expand Down Expand Up @@ -1405,7 +1399,7 @@ def test_systemd_control_commands_success(self):
# Expected calls to systemd-analyze for calendar validation
# These come from the VALID_CONFIG_CONTENT and the defaults in non_interactive_setup
# Order matters here as they are called before systemctl daemon-reload.
expected_calendar_validation_calls = [
[
unittest.mock.call(
[
"/usr/bin/systemd-analyze",
Expand Down
1 change: 0 additions & 1 deletion tests/lib/maillogsentinel/test_log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Import the function to be tested
from lib.maillogsentinel.log_utils import (
_parse_log_line,
MONTHS,
) # MONTHS needed for test_parse_log_line_invalid_month by implication

# --- Mocks and Fixtures ---
Expand Down
3 changes: 1 addition & 2 deletions tests/lib/maillogsentinel/test_progress.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# tests/lib/maillogsentinel/test_progress.py
import unittest
from unittest.mock import patch, MagicMock
from unittest.mock import patch
import io
import sys
from lib.maillogsentinel.progress import (
ProgressTracker, # Import the class
get_terminal_width,
GREEN,
RED,
ORANGE,
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/maillogsentinel/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_send_report_success(
assert len(attachments) == 1
assert attachments[0].get_filename() == mock_app_config.csv_filename
mock_logger.info.assert_any_call(
f"Report sent from testuser@my.server.com to recipient@example.com"
"Report sent from testuser@my.server.com to recipient@example.com"
)


Expand Down Expand Up @@ -376,7 +376,7 @@ def test_send_report_sender_override(
sent_msg: EmailMessage = mock_smtp_instance.send_message.call_args[0][0]
assert sent_msg["From"] == "override@sender.com"
mock_logger.info.assert_any_call(
f"Report sent from override@sender.com to recipient@example.com"
"Report sent from override@sender.com to recipient@example.com"
)


Expand Down
Loading