@@ -210,6 +210,18 @@ def search_version_string(self, matched_list: list[str]) -> list[str]:
210
210
) # TODO: regex highlight in these matched strings?
211
211
return version_strings
212
212
213
+ def remove_digits (self , product_name : str ) -> str :
214
+ """
215
+ tries to remove digits from product name
216
+ Example: testpkg1.2-3.4 becomes testpkg
217
+ """
218
+ # try removing numeric characters from the product_name
219
+ LOGGER .debug (f"removing digits from product_name={ product_name } " )
220
+ result = "" .join (filter (lambda x : not x .isdigit (), product_name ))
221
+
222
+ # skip trailing characters that can often happen after digits removal
223
+ return result .rstrip ("-_. " )
224
+
213
225
def parse_filename (self , filename : str ) -> tuple [str , str ]:
214
226
"""
215
227
returns package_name/product_name from package_filename of types .rpm, .deb, etc.
@@ -226,7 +238,7 @@ def parse_filename(self, filename: str) -> tuple[str, str]:
226
238
product_name = filename .rsplit ("-" , 3 )[0 ]
227
239
version_number = filename .rsplit ("-" , 3 )[1 ]
228
240
# example: libarchive-3.5.1-1-aarch64.pkg.tar.xz
229
- elif filename . endswith ( ".deb" ) or filename . endswith ( ".ipk" ) :
241
+ elif "_" in filename :
230
242
product_name = filename .rsplit ("_" )[0 ]
231
243
version_number = filename .rsplit ("_" )[1 ].rsplit ("-" )[0 ].rsplit ("+" )[0 ]
232
244
# example: varnish_6.4.0-3_amd64.deb
@@ -240,22 +252,57 @@ def parse_filename(self, filename: str) -> tuple[str, str]:
240
252
if not self .version_number :
241
253
self .version_number = version_number
242
254
243
- self .vendor_product = self .find_vendor_product ()
244
-
245
255
LOGGER .debug (
246
256
f"Parsing file '{ filename } ': Results: product_name='{ self .product_name } ', version_number='{ self .version_number } '"
247
257
)
248
- return product_name , version_number
258
+
259
+ # first try
260
+ self .vendor_product = self .find_vendor_product (self .product_name )
261
+ if self .vendor_product :
262
+ return product_name , version_number
263
+ # failed, check lib prefix
264
+ if self .product_name .startswith ("lib" ):
265
+ # try without lib prefix
266
+ LOGGER .debug (f"trying without lib in product_name={ self .product_name } " )
267
+ name_no_lib = self .product_name [3 :]
268
+ self .vendor_product = self .find_vendor_product (name_no_lib )
269
+ if self .vendor_product :
270
+ return product_name , version_number
271
+ # try without lib prefix and digits
272
+ if any (char .isdigit () for char in name_no_lib ):
273
+ self .vendor_product = self .find_vendor_product (
274
+ self .remove_digits (name_no_lib )
275
+ )
276
+ if self .vendor_product :
277
+ return product_name , version_number
278
+ # try without numeric characters
279
+ if any (char .isdigit () for char in self .product_name ):
280
+ self .vendor_product = self .find_vendor_product (
281
+ self .remove_digits (self .product_name )
282
+ )
283
+ if self .vendor_product :
284
+ return product_name , version_number
285
+ # all attempts failed, raise error and ask for product_name
286
+ LOGGER .warning (
287
+ textwrap .dedent (
288
+ f"""
289
+ =================================================================
290
+ No match was found for "{ self .product_name } " in database.
291
+ Please check your file or try specifying the "product_name" also.
292
+ =================================================================
293
+ """
294
+ )
295
+ )
249
296
else :
250
297
# raise error for unknown archive types
251
298
with ErrorHandler (mode = ErrorMode .NoTrace , logger = LOGGER ):
252
299
raise UnknownArchiveType (filename )
253
300
254
- def find_vendor_product (self ) -> list [tuple [str , str ]]:
301
+ def find_vendor_product (self , product_name ) -> list [tuple [str , str ]]:
255
302
"""find vendor-product pairs from database"""
256
303
257
304
LOGGER .debug (
258
- f"checking for product_name='{ self . product_name } ' and version_name='{ self .version_number } ' in the database"
305
+ f"checking for product_name='{ product_name } ' and version_name='{ self .version_number } ' in the database"
259
306
)
260
307
261
308
cursor = CVEDB .db_open_and_get_cursor (self )
@@ -268,7 +315,7 @@ def find_vendor_product(self) -> list[tuple[str, str]]:
268
315
if cursor is None :
269
316
return []
270
317
271
- cursor .execute (query , {"product" : self . product_name })
318
+ cursor .execute (query , {"product" : product_name })
272
319
data = cursor .fetchall ()
273
320
274
321
# checking if (vendor, product) was found in the database
@@ -280,41 +327,22 @@ def find_vendor_product(self) -> list[tuple[str, str]]:
280
327
textwrap .dedent (
281
328
f"""
282
329
===============================================================
283
- Multiple ("vendor", "product") pairs found for "{ self . product_name } "
330
+ Multiple ("vendor", "product") pairs found for "{ product_name } "
284
331
Please manually select the appropriate pair.
285
332
===============================================================
286
333
"""
287
334
)
288
335
)
289
336
WARNED = True # prevent same warning multiple times
337
+
338
+ # we found correct product_name, set it
339
+ self .product_name = product_name
290
340
return data # [('vendor', 'product')]
291
- else :
292
- if self .product_name :
293
- # removing numeric characters from the product_name
294
- if any (char .isdigit () for char in self .product_name ):
295
- LOGGER .debug (
296
- f"removing digits from product_name={ self .product_name } "
297
- )
298
- self .product_name = "" .join (
299
- filter (lambda x : not x .isdigit (), self .product_name )
300
- )
301
- return self .find_vendor_product ()
302
- else :
303
- # raise error and ask for product_name
304
- LOGGER .warning (
305
- textwrap .dedent (
306
- f"""
307
- =================================================================
308
- No match was found for "{ self .product_name } " in database.
309
- Please check your file or try specifying the "product_name" also.
310
- =================================================================
311
- """
312
- )
313
- )
314
- return []
315
341
316
342
CVEDB .db_close (self ) # type: ignore
317
343
344
+ return []
345
+
318
346
def output_single (self ) -> None :
319
347
"""display beautiful output for Helper-Script"""
320
348
0 commit comments