Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Allow to clone and adjust a CPE #916

Merged
merged 1 commit into from
Nov 3, 2023
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
34 changes: 34 additions & 0 deletions pontos/cpe/_cpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,40 @@ def as_formatted_string_binding(self) -> str:
f"{edition}:{language}:{sw_edition}:{target_sw}:{target_hw}:{other}"
)

def clone(
self,
**kwargs,
) -> "CPE":
"""
Clone a CPE and allow to override parts

Example:
.. code-block:: python

from pontos.cpe import CPE, ANY

android_13 = CPE.from_string(
"cpe:2.3:o:google:android:13.0:*:*:*:*:*:*:*"
)
all_android_versions = cpe.clone(version=ANY)
"""
args = {
"part": self.part,
"vendor": self.vendor,
"product": self.product,
"version": self.version,
"update": self.update,
"edition": self.edition,
"language": self.language,
"sw_edition": self.sw_edition,
"target_sw": self.target_sw,
"target_hw": self.target_hw,
"other": self.other,
"cpe_string": self.cpe_string,
}
args.update(**kwargs)
return CPE(**args) # type: ignore[arg-type]

def __str__(self) -> str:
"""
Returns the string representation (uri of formatted string) of the CPE
Expand Down
16 changes: 16 additions & 0 deletions tests/cpe/test_cpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,19 @@ def test_has_extended_attribute(self):
target_sw="ipsum",
)
self.assertTrue(cpe.has_extended_attribute())

def test_clone(self):
cpe = CPE.from_string(
"cpe:2.3:a:hp:openview_network_manager:7.51:*:*:*:*:linux:*:*"
)

cpe2 = cpe.clone()
self.assertIsNot(cpe, cpe2)

cpe = CPE.from_string(
"cpe:2.3:a:hp:openview_network_manager:7.51:*:*:*:*:linux:*:*"
)
cpe2 = cpe.clone(version=ANY)
self.assertIsNot(cpe, cpe2)
self.assertEqual(cpe.version, "7\\.51")
self.assertEqual(cpe2.version, ANY)