Skip to content

Commit

Permalink
Fixed the logic for case insensitive to not do multiple globs but sti…
Browse files Browse the repository at this point in the history
…ll keep the correct path names (#38)
  • Loading branch information
dacoburn authored Jan 21, 2025
1 parent 6d4fc56 commit f394b66
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion socketsecurity/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__author__ = 'socket.dev'
__version__ = '1.0.38'
__version__ = '1.0.40'
40 changes: 21 additions & 19 deletions socketsecurity/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,25 +437,17 @@ def find_files(path: str) -> list:
for ecosystem in socket_globs:
patterns = socket_globs[ecosystem]
for file_name in patterns:
pattern = patterns[file_name]["pattern"]
# Keep path as-is but try filename variations
file_paths = [
f"{path}/**/{pattern}",
f"{path}/**/{pattern.lower()}",
f"{path}/**/{pattern.upper()}",
f"{path}/**/{pattern.capitalize()}"
]

for file_path in file_paths:
log.debug(f"Globbing {file_path}")
glob_start = time.time()
glob_files = glob(file_path, recursive=True)
for glob_file in glob_files:
if glob_file not in files:
files.add(glob_file)
glob_end = time.time()
glob_total_time = glob_end - glob_start
log.debug(f"Glob for pattern {file_path} took {glob_total_time:.2f} seconds")
pattern = Core.to_case_insensitive_regex(patterns[file_name]["pattern"])
file_path = f"{path}/**/{pattern}"
log.debug(f"Globbing {file_path}")
glob_start = time.time()
glob_files = glob(file_path, recursive=True)
for glob_file in glob_files:
if glob_file not in files:
files.add(glob_file)
glob_end = time.time()
glob_total_time = glob_end - glob_start
log.debug(f"Glob for pattern {file_path} took {glob_total_time:.2f} seconds")

log.debug("Finished Find Files")
end_time = time.time()
Expand Down Expand Up @@ -872,6 +864,16 @@ def save_file(file_name: str, content: str) -> None:
file.write(content)
file.close()

@staticmethod
def to_case_insensitive_regex(input_string: str) -> str:
"""
Converts a string into a case-insensitive regex format.
Example: "pipfile" -> "[Pp][Ii][Pp][Ff][Ii][Ll][Ee]"
:param input_string: The input string to convert.
:return: A case-insensitive regex string.
"""
return ''.join(f'[{char.lower()}{char.upper()}]' if char.isalpha() else char for char in input_string)

# @staticmethod
# def create_license_file(diff: Diff) -> None:
# output = []
Expand Down

0 comments on commit f394b66

Please sign in to comment.