Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 25, 2024
1 parent da5fc58 commit e06bcb5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
28 changes: 15 additions & 13 deletions scripts/native_libraries/get_emba_db.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json

import os

import requests

from surfactant.configmanager import ConfigManager


def load_database(url):
response = requests.get(url)
response.raise_for_status()
Expand Down Expand Up @@ -34,12 +35,12 @@ def parse_cfg_file(content):

# Check if it starts with one double quote and ends with two double quotes
if fields[3].startswith('"') and fields[3].endswith('""'):
filecontent = fields[3][1:-1]
filecontent = fields[3][1:-1]
elif fields[3].endswith('""'):
filecontent = fields[3][:-1]
filecontent = fields[3][:-1]
else:
filecontent = fields[3].strip('"')
filecontent = fields[3].strip('"')

# Create a dictionary for this entry and add it to the database
if fields[1] == "" or fields[1] == "strict":
if fields[1] == "strict":
Expand All @@ -59,9 +60,10 @@ def parse_cfg_file(content):

return database


# Use database from specific commit
emba_database_url = "https://raw.githubusercontent.com/e-m-b-a/emba/11d6c281189c3a14fc56f243859b0bccccce8b9a/config/bin_version_strings.cfg"
json_file_path = ConfigManager().get_data_dir_path() / "native_lib_patterns"/ "emba.json"
json_file_path = ConfigManager().get_data_dir_path() / "native_lib_patterns" / "emba.json"

file_content = load_database(emba_database_url)

Expand All @@ -72,13 +74,13 @@ def parse_cfg_file(content):

# Remove leading ^ from each string in the filecontent list
for i, content in enumerate(filecontent_list): # Use enumerate to get index and value
if content.startswith("^"):
filecontent_list[i] = content[1:]
if content.startswith("^"):
filecontent_list[i] = content[1:]

if not content.endswith("\\$"):
if content.endswith("$"):
filecontent_list[i] = content[:-1]
if not content.endswith("\\$"):
if content.endswith("$"):
filecontent_list[i] = content[:-1]

os.makedirs(os.path.dirname(json_file_path), exist_ok=True)
with open(json_file_path, 'w') as json_file:
json.dump(parsed_data, json_file, indent=4)
with open(json_file_path, "w") as json_file:
json.dump(parsed_data, json_file, indent=4)
16 changes: 5 additions & 11 deletions surfactant/infoextractors/native_lib_file.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import json
import pathlib
import re
from typing import Any, Dict, List

from loguru import logger

import surfactant.plugin
from surfactant.sbomtypes import SBOM, Software
from surfactant.configmanager import ConfigManager
from surfactant.filetypeid import id_magic
from surfactant.sbomtypes import SBOM, Software


def supports_file(filetype) -> bool:
Expand Down Expand Up @@ -65,15 +63,11 @@ def extract_native_lib_info(filename):

# Create the single entry for isLibrary
if library_names:
native_lib_info["nativeLibraries"].append({
"isLibrary": library_names
})
native_lib_info["nativeLibraries"].append({"isLibrary": library_names})

# Create the single entry for containsLibrary
if contains_library_names:
native_lib_info["nativeLibraries"].append({
"containsLibrary": contains_library_names
})
native_lib_info["nativeLibraries"].append({"containsLibrary": contains_library_names})

return native_lib_info

Expand All @@ -87,9 +81,9 @@ def match_by_attribute(attribute: str, content: str, database: Dict) -> List[Dic
matches = re.search(pattern, content)
if matches:
libs.append({"isLibrary": name})
#else:
# else:
elif attribute == "filecontent":
matches = re.search(pattern.encode('utf-8'), content)
matches = re.search(pattern.encode("utf-8"), content)
if matches:
libs.append({"containsLibrary": name})
return libs

0 comments on commit e06bcb5

Please sign in to comment.