Skip to content

Commit

Permalink
Add: Allow to clone and adjust a CPE
Browse files Browse the repository at this point in the history
Add a new CPE method to clone a CPE object and allow to adjust the parts
of the CPE while cloning. This will become necessary when transforming a
CPE for example having a CPE with a specific version put we want to
address all versions of this product.
  • Loading branch information
bjoernricks committed Nov 3, 2023
1 parent 872f7cd commit c2abe51
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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)

0 comments on commit c2abe51

Please sign in to comment.