Try to resolve circular imports
All checks were successful
CI / test (push) Successful in 15m37s
Lint / test (push) Successful in 44s

This commit is contained in:
Miguel Jacq 2026-06-17 09:51:47 +10:00
parent de7531424d
commit ed9ec6893a
Signed by: mig5
GPG key ID: 03906B4110AAD3B8
2 changed files with 61 additions and 33 deletions

View file

@ -2,6 +2,7 @@ from __future__ import annotations
import glob import glob
import os import os
from importlib import import_module
import re import re
import shutil import shutil
import shlex import shlex
@ -20,9 +21,30 @@ from .ignore import IgnorePolicy
from .pathfilter import PathFilter from .pathfilter import PathFilter
from .version import get_enroll_version from .version import get_enroll_version
from .state import write_state from .state import write_state
from .harvest_collectors.context import HarvestContext
UnitQueryError = _systemd.UnitQueryError UnitQueryError = _systemd.UnitQueryError
_COLLECTOR_REEXPORTS = {
"CronLogrotateCollector": ".harvest_collectors.cron_logrotate",
"ExtraPathsCollector": ".harvest_collectors.paths",
"PackageManagerConfigCollector": ".harvest_collectors.package_manager",
"RuntimeStateCollector": ".harvest_collectors.runtime",
"ServicePackageCollector": ".harvest_collectors.services",
"UsersCollector": ".harvest_collectors.users",
"UsrLocalCustomCollector": ".harvest_collectors.paths",
}
def __getattr__(name: str) -> Any:
module_name = _COLLECTOR_REEXPORTS.get(name)
if module_name is None:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
module = import_module(module_name, __package__)
value = getattr(module, name)
globals()[name] = value
return value
def list_enabled_services() -> List[str]: def list_enabled_services() -> List[str]:
return _systemd.list_enabled_services() return _systemd.list_enabled_services()
@ -1457,18 +1479,6 @@ def _collect_firewall_runtime_snapshot(
) )
from .harvest_collectors import (
CronLogrotateCollector,
ExtraPathsCollector,
HarvestContext,
PackageManagerConfigCollector,
RuntimeStateCollector,
ServicePackageCollector,
UsersCollector,
UsrLocalCustomCollector,
)
def harvest( def harvest(
bundle_dir: str, bundle_dir: str,
policy: Optional[IgnorePolicy] = None, policy: Optional[IgnorePolicy] = None,
@ -1491,6 +1501,13 @@ def harvest(
# includes are harvested into an extra role. # includes are harvested into an extra role.
path_filter = PathFilter(include=include_paths or (), exclude=exclude_paths or ()) path_filter = PathFilter(include=include_paths or (), exclude=exclude_paths or ())
from .harvest_collectors.cron_logrotate import CronLogrotateCollector
from .harvest_collectors.package_manager import PackageManagerConfigCollector
from .harvest_collectors.paths import ExtraPathsCollector, UsrLocalCustomCollector
from .harvest_collectors.runtime import RuntimeStateCollector
from .harvest_collectors.services import ServicePackageCollector
from .harvest_collectors.users import UsersCollector
if hasattr(os, "geteuid") and os.geteuid() != 0: if hasattr(os, "geteuid") and os.geteuid() != 0:
print( print(
"Warning: not running as root; harvest may miss files or metadata.", "Warning: not running as root; harvest may miss files or metadata.",

View file

@ -1,27 +1,38 @@
"""Harvest collector package exports"""
from __future__ import annotations
from importlib import import_module
from .context import HarvestCollector, HarvestContext from .context import HarvestCollector, HarvestContext
from .cron_logrotate import CronLogrotateCollection, CronLogrotateCollector
from .package_manager import ( _COLLECTOR_EXPORTS = {
PackageManagerConfigCollection, "CronLogrotateCollection": ".cron_logrotate",
PackageManagerConfigCollector, "CronLogrotateCollector": ".cron_logrotate",
) "ExtraPathsCollector": ".paths",
from .paths import ExtraPathsCollector, UsrLocalCustomCollector "PackageManagerConfigCollection": ".package_manager",
from .runtime import RuntimeStateCollection, RuntimeStateCollector "PackageManagerConfigCollector": ".package_manager",
from .services import ServicePackageCollection, ServicePackageCollector "RuntimeStateCollection": ".runtime",
from .users import UsersCollection, UsersCollector "RuntimeStateCollector": ".runtime",
"ServicePackageCollection": ".services",
"ServicePackageCollector": ".services",
"UsersCollection": ".users",
"UsersCollector": ".users",
"UsrLocalCustomCollector": ".paths",
}
__all__ = [ __all__ = [
"CronLogrotateCollection",
"CronLogrotateCollector",
"ExtraPathsCollector",
"HarvestCollector", "HarvestCollector",
"HarvestContext", "HarvestContext",
"PackageManagerConfigCollection", *_COLLECTOR_EXPORTS,
"PackageManagerConfigCollector",
"RuntimeStateCollection",
"RuntimeStateCollector",
"ServicePackageCollection",
"ServicePackageCollector",
"UsersCollection",
"UsersCollector",
"UsrLocalCustomCollector",
] ]
def __getattr__(name: str):
module_name = _COLLECTOR_EXPORTS.get(name)
if module_name is None:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
module = import_module(module_name, __name__)
value = getattr(module, name)
globals()[name] = value
return value