Skip to content

Commit

Permalink
Change: refactor deprecate-vts standalone tool
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-gb committed Oct 9, 2024
1 parent ae1553b commit e62d7ce
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 51 deletions.
50 changes: 28 additions & 22 deletions tests/standalone_plugins/test_deprecate_vts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
_finalize_content,
_get_summary,
deprecate,
filter_files,
get_files_from_path,
parse_args,
parse_files,
update_summary,
)

Expand All @@ -26,15 +26,15 @@ def test_parse_args(self):

args = parse_args(
[
"--files",
"--file",
testfile,
"--output-path",
output_path,
"--deprecation-reason",
reason,
]
)
self.assertEqual(args.files, [Path(testfile)])
self.assertEqual(args.file, [Path(testfile)])
self.assertEqual(args.output_path, Path(output_path))
self.assertEqual(args.deprecation_reason, reason)

Expand All @@ -47,7 +47,7 @@ def test_mandatory_arg_group_both(self):
with self.assertRaises(SystemExit):
parse_args(
[
"--files",
"--file",
testfile,
"--output-path",
output_path,
Expand Down Expand Up @@ -87,6 +87,28 @@ def test_mandatory_arg_group_neither(self):
]
)

def test_parse_from_file(self):
output_path = "attic/"
with TemporaryDirectory() as tempdir:
from_file = tempdir / "from_file.txt"
testfile1 = tempdir / "testfile1.nasl"
testfile2 = tempdir / "testfile2.nasl"

from_file.write_text(f"{testfile1}\n{testfile2}\n", encoding="utf8")

args = parse_args(
[
"--from-file",
str(from_file),
"--output-path",
output_path,
"-r",
"NOTUS",
]
)

self.assertEqual(args.from_file, from_file)


NASL_CONTENT = (
'...if(description)\n{\n script_oid("1.3.6.1.4.1.25623.1.0.910673");'
Expand Down Expand Up @@ -190,16 +212,12 @@ def test_get_files_from_path(self):

self.assertEqual(result, expected)

def test_filter_files(self):
def test_parse_files(self):
with TemporaryDirectory() as in_dir:
testfile1 = in_dir / "gb_rhsa_2021_8383_8383.nasl"
testfile1.write_text(NASL_CONTENT, encoding="utf8")
testfile2 = in_dir / "gb_rhsa_2020_8383_8383.nasl"
testfile2.write_text(NASL_CONTENT, encoding="utf8")

result = filter_files(
files=[testfile1, testfile2], filename_prefix="gb_rhsa_2021"
)
result = parse_files(files=[testfile1])
expected = [
DeprecatedFile(
name="gb_rhsa_2021_8383_8383.nasl",
Expand All @@ -209,15 +227,3 @@ def test_filter_files(self):
]

self.assertEqual(result, expected)

def test_filter_files_out(self):
with TemporaryDirectory() as in_dir:
testfile1 = in_dir / "gb_rhsa_2021_8383_8383.nasl"
testfile1.write_text(NASL_CONTENT, encoding="utf8")

result = filter_files(
files=[testfile1], filename_prefix="gb_rhsa_2020"
)
expected = []

self.assertEqual(result, expected)
69 changes: 40 additions & 29 deletions troubadix/standalone_plugins/deprecate_vts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
# SPDX-FileCopyrightText: 2024 Greenbone AG

import re
from argparse import ArgumentParser, Namespace
from argparse import ArgumentParser, ArgumentTypeError, Namespace
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Iterable, Optional

from pontos.terminal.terminal import ConsoleTerminal

from troubadix.argparser import directory_type, file_type
from troubadix.helper.patterns import (
ScriptTag,
SpecialScriptTag,
get_script_tag_pattern,
get_special_script_tag_pattern,
)
from troubadix.troubadix import from_file


class Deprecations(Enum):
Expand All @@ -34,6 +37,15 @@ class DeprecatedFile:
KB_ITEMS_PATTERN = re.compile(r"set_kb_item\(.+\);")


def existing_file_type(string: str) -> Path:
file_path = Path(string)
if not file_path.exists():
raise ArgumentTypeError(f'File "{string}" does not exist.')

Check warning on line 43 in troubadix/standalone_plugins/deprecate_vts.py

View check run for this annotation

Codecov / codecov/patch

troubadix/standalone_plugins/deprecate_vts.py#L43

Added line #L43 was not covered by tests
if not file_path.is_file():
raise ArgumentTypeError(f'"{string}" is not a file.')

Check warning on line 45 in troubadix/standalone_plugins/deprecate_vts.py

View check run for this annotation

Codecov / codecov/patch

troubadix/standalone_plugins/deprecate_vts.py#L45

Added line #L45 was not covered by tests
return file_path


def update_summary(file: DeprecatedFile, deprecation_reason: str) -> str:
"""Update the summary of the nasl script by adding the information
that the script has been deprecated, and if possible, the oid of
Expand Down Expand Up @@ -83,26 +95,18 @@ def get_files_from_path(dir_path: Path = None) -> list:
return [file for file in dir_path.glob("**/*")]


def filter_files(
files: list, filename_prefix: str = None
) -> list[DeprecatedFile]:
"""Filter the files based on a provided prefix and convert them into
def parse_files(files: list) -> list[DeprecatedFile]:
"""Convert filepaths into
DeprecatedFile objects
Args:
files: a list of files to deprecate, should be .nasl VTs
filename_prefix: an optional prefix to filter only specific files
Returns:
List of DeprecatedFile objects
"""
to_deprecate = []
if filename_prefix:
filename_filter = re.compile(rf"{filename_prefix}")
files[:] = [
file for file in files if re.match(filename_filter, file.name)
]

for file in files:
with file.open("r", encoding="latin-1") as fh:
Expand Down Expand Up @@ -193,16 +197,6 @@ def parse_args(args: Iterable[str] = None) -> Namespace:
required=True,
help="Path where the deprecated files should be written to.",
)
parser.add_argument(
"-p",
"--filename-prefix",
metavar="<filename_prefix>",
nargs="?",
default=None,
type=str,
help="The prefix of the files you would like to deprecate,"
"for example 'gb_rhsa_2021' to filter on the year",
)
parser.add_argument(
"-r",
"--deprecation-reason",
Expand All @@ -214,37 +208,54 @@ def parse_args(args: Iterable[str] = None) -> Namespace:
"been merged with another still active VT, 'duplicate': The VT has"
"a still active duplicate, 'defunct': The VT is no longer "
"functional.",
required=True,
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-f",
"--files",
metavar="<files>",
"--file",
metavar="<file>",
nargs="+",
default=None,
type=file_type,
help="Files to deprecate",
help="File to deprecate",
)
group.add_argument(
"-i",
"--input-path",
metavar="<input_path>",
default=None,
type=directory_type,
help="Path to the existing nasl script directory",
help="Path to a directory where all files should be deprecated.",
)
group.add_argument(
"--from-file",
metavar="<from_file>",
default=None,
type=existing_file_type,
help=(
"Path to a single file that contains a list of files "
"to be deprecated, separated by new lines."
),
)
return parser.parse_args(args)


def main():
args = parse_args()
terminal = ConsoleTerminal()

Check warning on line 246 in troubadix/standalone_plugins/deprecate_vts.py

View check run for this annotation

Codecov / codecov/patch

troubadix/standalone_plugins/deprecate_vts.py#L246

Added line #L246 was not covered by tests
input_path = args.input_path if args.input_path else None
filename_prefix = args.filename_prefix
files = []

Check warning on line 248 in troubadix/standalone_plugins/deprecate_vts.py

View check run for this annotation

Codecov / codecov/patch

troubadix/standalone_plugins/deprecate_vts.py#L248

Added line #L248 was not covered by tests

files = args.files or get_files_from_path(input_path)
filtered_files = filter_files(files, filename_prefix)
if input_path:
files = get_files_from_path(input_path)

Check warning on line 251 in troubadix/standalone_plugins/deprecate_vts.py

View check run for this annotation

Codecov / codecov/patch

troubadix/standalone_plugins/deprecate_vts.py#L251

Added line #L251 was not covered by tests
elif args.from_file:
files = from_file(include_file=args.from_file, term=terminal)

Check warning on line 253 in troubadix/standalone_plugins/deprecate_vts.py

View check run for this annotation

Codecov / codecov/patch

troubadix/standalone_plugins/deprecate_vts.py#L253

Added line #L253 was not covered by tests
elif args.file:
files = args.file

Check warning on line 255 in troubadix/standalone_plugins/deprecate_vts.py

View check run for this annotation

Codecov / codecov/patch

troubadix/standalone_plugins/deprecate_vts.py#L255

Added line #L255 was not covered by tests

deprecate(args.output_path, filtered_files, args.deprecation_reason)
to_be_deprecated = parse_files(files)
deprecate(args.output_path, to_be_deprecated, args.deprecation_reason)

Check warning on line 258 in troubadix/standalone_plugins/deprecate_vts.py

View check run for this annotation

Codecov / codecov/patch

troubadix/standalone_plugins/deprecate_vts.py#L257-L258

Added lines #L257 - L258 were not covered by tests


if __name__ == "__main__":
Expand Down

0 comments on commit e62d7ce

Please sign in to comment.