Skip to content

Commit

Permalink
Merge pull request #54 from ajinabraham/3.1.5
Browse files Browse the repository at this point in the history
Support multiple multiprocessing options
  • Loading branch information
ajinabraham authored Nov 14, 2024
2 parents 3171b66 + 330d5bc commit b8b4048
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libsast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__title__ = 'libsast'
__authors__ = 'Ajin Abraham'
__copyright__ = f'Copyright {year} Ajin Abraham, opensecurity.in'
__version__ = '3.1.4'
__version__ = '3.1.5'
__version_info__ = tuple(int(i) for i in __version__.split('.'))
__all__ = [
'Scanner',
Expand Down
10 changes: 8 additions & 2 deletions libsast/core_matcher/choice_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, options: dict) -> None:
self.scan_rules = get_rules(options.get('choice_rules'))
self.show_progress = options.get('show_progress')
self.cpu = options.get('cpu_core')
self.queue = options.get('queue')
self.multiprocessing = options.get('multiprocessing')
self.alternative_path = options.get('alternative_path')
exts = options.get('choice_extensions')
self.exts = [ext.lower() for ext in exts] if exts else []
Expand Down Expand Up @@ -72,14 +72,20 @@ def regex_scan(self, file_contents: list, rules=None) -> dict:
return {}
self.validate_rules()

if self.queue:
if self.multiprocessing == 'billiard':
# Use billiard's pool for regex (support queues)
from billiard import Pool
with Pool(processes=self.cpu) as pool:
# Run regex on file data
results = pool.map(
self.choice_matcher,
file_contents)
elif self.multiprocessing == 'thread':
# Use a ThreadPool for regex check
with ThreadPoolExecutor() as io_executor:
results = list(io_executor.map(
self.choice_matcher,
file_contents))
else:
# Use ProcessPoolExecutor for regex processing
with ProcessPoolExecutor(max_workers=self.cpu) as cpu_executor:
Expand Down
12 changes: 10 additions & 2 deletions libsast/core_matcher/pattern_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, options: dict) -> None:
self.scan_rules = get_rules(options.get('match_rules'))
self.show_progress = options.get('show_progress')
self.cpu = options.get('cpu_core')
self.queue = options.get('queue')
self.multiprocessing = options.get('multiprocessing')
exts = options.get('match_extensions')
self.exts = [ext.lower() for ext in exts] if exts else []
self.findings = {}
Expand Down Expand Up @@ -68,7 +68,7 @@ def regex_scan(self, file_contents: list, rules=None) -> dict:
return {}
self.validate_rules()

if self.queue:
if self.multiprocessing == 'billiard':
# Use billiard's pool for CPU-bound regex (support queues)
from billiard import Pool
with Pool(processes=self.cpu) as cpu_executor:
Expand All @@ -77,6 +77,14 @@ def regex_scan(self, file_contents: list, rules=None) -> dict:
self.pattern_matcher,
file_contents,
)
elif self.multiprocessing == 'thread':
# Use a ThreadPool for regex check
with ThreadPoolExecutor() as io_executor:
# Run regex on file data
results = io_executor.map(
self.pattern_matcher,
file_contents,
)
else:
# Use a ProcessPool for CPU-bound regex
with ProcessPoolExecutor(max_workers=self.cpu) as cpu_executor:
Expand Down
2 changes: 1 addition & 1 deletion libsast/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, options: dict, paths: list) -> None:
'ignore_paths': [],
'show_progress': False,
'cpu_core': 1,
'queue': False,
'multiprocessing': 'default',
# Overwrite with options from invocation
**(options or {}),
}
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "libsast"
version = "3.1.4"
version = "3.1.5"
description = "A generic SAST library built on top of semgrep and regex"
keywords = ["libsast", "SAST", "Python SAST", "SAST API", "Regex SAST", "Pattern Matcher"]
authors = ["Ajin Abraham <ajin@opensecurity.in>"]
Expand Down

0 comments on commit b8b4048

Please sign in to comment.