Skip to content

Commit 18cb1b0

Browse files
authored
Merge pull request #51 from ajinabraham/3.1.2
3.1.2
2 parents ae43889 + 1d75514 commit 18cb1b0

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

libsast/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
__title__ = 'libsast'
1313
__authors__ = 'Ajin Abraham'
1414
__copyright__ = f'Copyright {year} Ajin Abraham, opensecurity.in'
15-
__version__ = '3.1.1'
15+
__version__ = '3.1.2'
1616
__version_info__ = tuple(int(i) for i in __version__.split('.'))
1717
__all__ = [
1818
'Scanner',

libsast/core_matcher/choice_matcher.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, options: dict) -> None:
2424
self.scan_rules = get_rules(options.get('choice_rules'))
2525
self.show_progress = options.get('show_progress')
2626
self.cpu = options.get('cpu_core')
27+
self.queue = options.get('queue')
2728
self.alternative_path = options.get('alternative_path')
2829
exts = options.get('choice_extensions')
2930
self.exts = [ext.lower() for ext in exts] if exts else []
@@ -65,15 +66,20 @@ def read_file_contents(self, paths: list) -> list:
6566

6667
def regex_scan(self, file_contents) -> list:
6768
"""Process regex matches on the file contents."""
68-
# Use ProcessPoolExecutor for regex processing
69-
with ProcessPoolExecutor(max_workers=self.cpu) as cpu_executor:
70-
71-
results = []
72-
for content in file_contents:
73-
# Process Choice Matcher on the file contents
74-
process_future = cpu_executor.submit(
75-
self.choice_matcher, content)
76-
results.append(process_future.result())
69+
if self.queue:
70+
# Use billiard's pool for regex (support queues)
71+
from billiard import Pool
72+
with Pool(processes=self.cpu) as pool:
73+
# Run regex on file data
74+
results = pool.map(
75+
self.choice_matcher,
76+
file_contents)
77+
else:
78+
# Use ProcessPoolExecutor for regex processing
79+
with ProcessPoolExecutor(max_workers=self.cpu) as cpu_executor:
80+
results = list(cpu_executor.map(
81+
self.choice_matcher,
82+
file_contents))
7783

7884
self.add_finding(results)
7985
return self.findings

libsast/core_matcher/pattern_matcher.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self, options: dict) -> None:
2525
self.scan_rules = get_rules(options.get('match_rules'))
2626
self.show_progress = options.get('show_progress')
2727
self.cpu = options.get('cpu_core')
28+
self.queue = options.get('queue')
2829
exts = options.get('match_extensions')
2930
self.exts = [ext.lower() for ext in exts] if exts else []
3031
self.findings = {}
@@ -62,14 +63,23 @@ def read_file_contents(self, paths: list) -> list:
6263

6364
def regex_scan(self, file_contents: list) -> dict:
6465
"""Scan file(s) content."""
65-
# Use a ProcessPool for CPU-bound regex
66-
with ProcessPoolExecutor(max_workers=self.cpu) as cpu_executor:
67-
68-
# Run regex on file data
69-
results = cpu_executor.map(
70-
self.pattern_matcher,
71-
file_contents,
72-
)
66+
if self.queue:
67+
# Use billiard's pool for CPU-bound regex (support queues)
68+
from billiard import Pool
69+
with Pool(processes=self.cpu) as cpu_executor:
70+
# Run regex on file data
71+
results = cpu_executor.map(
72+
self.pattern_matcher,
73+
file_contents,
74+
)
75+
else:
76+
# Use a ProcessPool for CPU-bound regex
77+
with ProcessPoolExecutor(max_workers=self.cpu) as cpu_executor:
78+
# Run regex on file data
79+
results = cpu_executor.map(
80+
self.pattern_matcher,
81+
file_contents,
82+
)
7383

7484
# Compile findings
7585
self.add_finding(results)

libsast/scanner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self, options: dict, paths: list) -> None:
2626
'ignore_paths': [],
2727
'show_progress': False,
2828
'cpu_core': 1,
29+
'queue': False,
2930
# Overwrite with options from invocation
3031
**(options or {}),
3132
}

poetry.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "libsast"
3-
version = "3.1.1"
3+
version = "3.1.2"
44
description = "A generic SAST library built on top of semgrep and regex"
55
keywords = ["libsast", "SAST", "Python SAST", "SAST API", "Regex SAST", "Pattern Matcher"]
66
authors = ["Ajin Abraham <[email protected]>"]
@@ -27,6 +27,7 @@ python = "^3.8"
2727
requests = "*"
2828
pyyaml = ">=6.0"
2929
semgrep = {version = "1.86.0", markers = "sys_platform != 'win32'"}
30+
billiard = "^4.2.1"
3031

3132
[tool.poetry.group.dev.dependencies]
3233
bandit = "*"

0 commit comments

Comments
 (0)