""" Transfer the software list of inventory reports in device comments into netbox-slm. Two writers put a software list into the comments of a client device: * the NetBox Windows Agent (mode ``netbox``):: ## Installed software (2) - 7-Zip 23.01 (x64) 23.01 — Igor Pavlov - Mozilla Firefox (x64 de) 120.0 — Mozilla * the NetBox Synchronizer desktop app (agent scan in mode ``listen``):: ### Installierte Software (2) | Programm | Version | Hersteller | |---|---|---| | 7-Zip 23.01 (x64) | 23.01 | Igor Pavlov | On every device save the installations recorded this way are brought in line with the report. Installations entered by hand are never changed or removed. """ import logging import re from dataclasses import dataclass logger = logging.getLogger(__name__) # Marks installations created from a report; only these are removed again when a report drops them. SYNC_MARKER = "Automatisch aus dem Inventarbericht übernommen (netbox-utilities)." UNKNOWN_VERSION = "unbekannt" _HEADING = re.compile(r"^\s*#{1,6}\s*(installed software|installierte software)\b", re.IGNORECASE) _ANY_HEADING = re.compile(r"^\s*#{1,6}\s") # At least one dot, so a year in a product name ("Office 2024") is not taken for a version. _VERSION = re.compile(r"^v?\d+(?:\.\w+)+(?:[\-_+]\w+)*$", re.IGNORECASE) _TABLE_SEPARATOR = re.compile(r"^\|?\s*:?-{2,}") @dataclass(frozen=True) class SoftwareEntry: name: str version: str publisher: str = "" @property def key(self): return self.name.casefold(), self.version.casefold() def has_software_report(comments): return any(_HEADING.match(line) for line in str(comments or "").splitlines()) def _software_lines(comments): """Lines of the first software section, up to the next heading.""" inside = False for line in str(comments or "").splitlines(): if _HEADING.match(line): if inside: return inside = True continue if inside: if _ANY_HEADING.match(line) or line.strip().startswith("