Skip to content

Commit

Permalink
fix: use xpath() instead of findall()
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed May 12, 2024
1 parent 75e816b commit bd9f26d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 44 deletions.
37 changes: 13 additions & 24 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"click",
"python-gvm>=23.12.0",
"click>=8.1",
"python-gvm>=23.12.0",
"typing_extensions",
"lxml",
]

[project.urls]
Expand All @@ -45,12 +47,7 @@ version-file = "src/gvm_sync_targets/_version.py"
requires = ["hatch-containers"]

[tool.hatch.envs.default]
dependencies = [
"coverage[toml]>=6.5",
"pytest>=6.0",
"pre-commit",
"mypy>=1.8",
]
dependencies = ["coverage[toml]>=6.5", "pytest>=6.0", "pre-commit", "mypy>=1.8"]

[tool.hatch.envs.default.scripts]
test = "pytest"
Expand Down Expand Up @@ -177,38 +174,30 @@ enable_error_code = [
"truthy-iterable",
"ignore-without-code",
]
untyped_calls_exclude = [
"gvm.transforms",
]
untyped_calls_exclude = ["gvm.transforms"]

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "--doctest-modules"
testpaths = ["src", "tests"]
filterwarnings = [
"error",
"ignore:defusedxml.lxml .*:DeprecationWarning:",
]
filterwarnings = ["error", "ignore:defusedxml.lxml .*:DeprecationWarning:"]

[tool.coverage.run]
source_pkgs = ["gvm_sync_targets", "tests"]
branch = true
parallel = true
relative_files = true
omit = [
"src/gvm_sync_targets/_version.py",
]
omit = ["src/gvm_sync_targets/_version.py"]

[tool.coverage.paths]
gvm_sync_targets = ["src/gvm_sync_targets", "*/gvm-sync-targets/src/gvm_sync_targets"]
gvm_sync_targets = [
"src/gvm_sync_targets",
"*/gvm-sync-targets/src/gvm_sync_targets",
]
tests = ["tests", "*/gvm-sync-targets/tests"]

[tool.coverage.report]
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]
exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]

[tool.commitizen]
name = "cz_conventional_commits"
Expand Down
1 change: 1 addition & 0 deletions src/gvm_sync_targets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2024-present linuxdaemon <linuxdaemon.irc@gmail.com>
#
# SPDX-License-Identifier: MIT

from gvm_sync_targets._version import (
__version__,
__version_tuple__,
Expand Down
1 change: 1 addition & 0 deletions src/gvm_sync_targets/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2024-present linuxdaemon <linuxdaemon.irc@gmail.com>
#
# SPDX-License-Identifier: MIT

import sys

if __name__ == "__main__":
Expand Down
35 changes: 16 additions & 19 deletions src/gvm_sync_targets/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import logging

# SPDX-FileCopyrightText: 2024-present linuxdaemon <linuxdaemon.irc@gmail.com>
#
# SPDX-License-Identifier: MIT
from email.policy import default
from typing import TYPE_CHECKING, TextIO, cast

import logging
from typing import Optional, TextIO, cast

import click
from gvm.connections import DebugConnection, UnixSocketConnection
from gvm.protocols.gmp import Gmp
from gvm.transforms import EtreeCheckCommandTransform

from gvm_sync_targets import __version__
from gvm_sync_targets.util import to_str

if TYPE_CHECKING:
from lxml.etree import ElementBase
from gvm_sync_targets.util import Element, target_in_use, to_str


@click.group(
Expand Down Expand Up @@ -44,13 +40,13 @@ def gvm_sync_targets(
) as gmp:
hosts = hosts_file.read().splitlines()
gmp.authenticate(username, password)
targets: "ElementBase" = gmp.get_targets()
target = targets.find("target[name='All Hosts']")
targets: Element = gmp.get_targets()
target: Optional[Element] = targets.find("target[name='All Hosts']")

if target is None:
# Create target
new_target = gmp.create_target("All Hosts")
elif target.xpath("sum(in_use/text()) > 0"):
elif target_in_use(target):
new_target = gmp.clone_target(to_str(target.attrib["id"]))
else:
new_target = target
Expand All @@ -62,19 +58,20 @@ def gvm_sync_targets(
# Replace old target in tasks
if target is not None and target is not new_target:
old_target_id = to_str(target.attrib["id"])
tasks = cast("ElementBase", gmp.get_tasks()).findall(
f"task[target/@id='{old_target_id}']"
task_ids = cast(
list[str],
cast(Element, gmp.get_tasks()).xpath(
f"task[target/@id='{old_target_id}']/@id"
),
)

for task in tasks:
gmp.modify_task(
to_str(task.attrib["id"]), target_id=new_target_id
)
for task_id in task_ids:
gmp.modify_task(task_id, target_id=new_target_id)

# Ensure old target is unused
old_target: "ElementBase" = gmp.get_target(old_target_id)[0]
old_target: Element = gmp.get_target(old_target_id)[0]
old_name = old_target.xpath("name/text()[1]")
if old_target.xpath("sum(in_use/text()) > 0"):
if target_in_use(old_target):
msg = "target is still in use"
raise ValueError(msg)

Expand Down
20 changes: 19 additions & 1 deletion src/gvm_sync_targets/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# SPDX-FileCopyrightText: 2024-present linuxdaemon <linuxdaemon.irc@gmail.com>
#
# SPDX-License-Identifier: MIT
from typing import Union

from typing import TYPE_CHECKING, Union

from typing_extensions import TypeAlias

if TYPE_CHECKING:
from lxml.etree import _Element

Element: TypeAlias = "_Element"

__all__ = [
"to_str",
"target_in_use",
"Element",
]


def to_str(text: Union[str, bytes, bytearray, memoryview]) -> str:
Expand Down Expand Up @@ -30,3 +44,7 @@ def to_str(text: Union[str, bytes, bytearray, memoryview]) -> str:
return to_str(text.tobytes())

return text


def target_in_use(target: Element) -> bool:
return bool(target.xpath("boolean(in_use[text()='1'])"))

0 comments on commit bd9f26d

Please sign in to comment.