Skip to content

Commit 29a605e

Browse files
committed
fix delayed caching; further improved typing annotations
1 parent 862279a commit 29a605e

7 files changed

Lines changed: 36 additions & 43 deletions

File tree

src/ts_deepscan/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def onProgress(finished: int, total: int):
188188
'finished': _scanner.finishedTasks
189189
}
190190

191+
result = {relpath:{res.category: res.data for res in res_list} for relpath, res_list in result.items()}
192+
191193
_scan = Scan(result=result,
192194
no_result=no_result,
193195
stats=stats, # prepare_stats(result, no_result, stats)

src/ts_deepscan/scanner/ParallelScanner.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ def _do_scan(self, files: t.List[FileScanInput]) -> ScanResults:
100100
self._notifyCompletion(relpath, result, errors)
101101

102102
if result:
103-
results.update({
104-
relpath: result
105-
})
103+
results[relpath] = result
106104

107105
tasks_done += 1
108106

src/ts_deepscan/scanner/PoolExecutorScanner.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def _do_scan(self, files: t.List[FileScanInput]) -> ScanResults:
2222
results = {}
2323
tasks = []
2424

25-
def task_completed(_task: futures.Future[t.Tuple[str, FileScanResult, list]]):
25+
def task_completed(_task: futures.Future[FileScanResult]):
2626
if _task.cancelled():
2727
self.finishedTasks += 1
2828
self._progress()
@@ -32,9 +32,7 @@ def task_completed(_task: futures.Future[t.Tuple[str, FileScanResult, list]]):
3232
self._notifyCompletion(relpath, result, errors)
3333

3434
if result:
35-
results.update({
36-
relpath: result
37-
})
35+
results[relpath] = result
3836

3937
executor = futures.ProcessPoolExecutor(max_workers=self._num_jobs if self._num_jobs > 0 else None)
4038

src/ts_deepscan/scanner/PoolScanner.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from queue import Queue
1010

1111
from . import FileScanInput, ScanResults
12+
from ..analyser import AnalysisResult
1213

1314
from .pool import Pool
1415
from .Scanner import Scanner
@@ -56,13 +57,12 @@ def process_results():
5657
error_msgs.append(f'An error occured while scanning {relpath} using \'{cat}\' analyser')
5758

5859
self._notifyCompletion(relpath, result, error_msgs)
59-
60+
6061
if result:
61-
results.update({
62-
relpath: result
63-
})
62+
results[relpath] = result
63+
6464

65-
def report_results(relpath: str, result: dict, errors: t.Dict[str, str]):
65+
def report_results(relpath: str, result: t.List[AnalysisResult], errors: t.Dict[str, str]):
6666
results_queue.put((relpath, result, errors))
6767

6868
pool = futures.ThreadPoolExecutor()
@@ -87,11 +87,11 @@ def report_results(relpath: str, result: dict, errors: t.Dict[str, str]):
8787

8888
def _get_scan_file_fn(self) -> t.Callable[[Path,
8989
t.Optional[Path],
90-
t.Callable[[str, dict, t.Dict[str, str]], None]], None]:
90+
t.Callable[[str, t.List[AnalysisResult], t.Dict[str, str]], None]], None]:
9191

9292
def _scan_file(path: Path,
9393
root: t.Optional[Path],
94-
report_results: t.Callable[[str, dict, t.Dict[str, str]], None]):
94+
report_results: t.Callable[[str, t.List[AnalysisResult], t.Dict[str, str]], None]):
9595

9696
PoolScanner._scan_file_parallel(path,
9797
root,
@@ -107,18 +107,16 @@ def _scan_file_parallel(path: Path,
107107
root: t.Optional[Path],
108108
analysers: t.List[FileAnalyser],
109109
timeout: int,
110-
report_results: t.Callable[[str, dict, t.Dict[str, str]], None],
110+
report_results: t.Callable[[str, t.List[AnalysisResult], t.Dict[str, str]], None],
111111
pool: Pool):
112-
result = {}
112+
result = []
113113
errors = {}
114114

115115
relpath = str(path.relative_to(root) if root else path)
116116

117-
def task_completed(_cat):
118-
def _callback(_res):
119-
if _res:
120-
result[_res.category] = _res.data
121-
return _callback
117+
def task_completed(_res):
118+
if _res:
119+
result.append(_res)
122120

123121
def task_failed(_cat):
124122
def _callback(_err):
@@ -127,7 +125,7 @@ def _callback(_err):
127125

128126
tasks = [pool.apply_async(
129127
_apply_analysis, (analyser, path, root),
130-
callback=task_completed(analyser.category),
128+
callback=task_completed,
131129
error_callback=task_failed(analyser.category))
132130
for analyser in analysers if analyser.accepts(path)]
133131

src/ts_deepscan/scanner/Scanner.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from gitignore_parser import parse_gitignore
1717

1818
from . import FileScanInput, FileScanResult, ScanResults
19-
from ..analyser import FileAnalyser
19+
20+
from ..analyser import FileAnalyser, AnalysisResult
2021
from .postprocessing import PostProcessor
2122

23+
2224
def _register_unpack_formats() -> t.Set[str]:
2325
"""
2426
Registers unpack formats and returns all supported archive extensions.
@@ -79,7 +81,7 @@ def __init__(self,
7981
# Callback accepting a relative path
8082
self.onPathIgnored: t.Optional[t.Callable[[str], None]] = None
8183
# Callback accepting relative path and results
82-
self.onFileScanCompleted: t.Optional[t.Callable[[str, dict, list], None]] = None
84+
self.onFileScanCompleted: t.Optional[t.Callable[[str, t.List[AnalysisResult], t.List[str]], None]] = None
8385
# Callback accepting number of finished and total tasks
8486
self.onProgress: t.Optional[t.Callable[[int, int], None]] = None
8587

@@ -90,22 +92,22 @@ def __init__(self,
9092
self._cleanup: t.List[Path] = []
9193

9294
@staticmethod
93-
def _scan_file(path: Path, analysers: t.List[FileAnalyser], root: t.Optional[Path]) -> t.Tuple[str, FileScanResult, t.List[str]]:
94-
result = {}
95+
def _scan_file(path: Path, analysers: t.List[FileAnalyser], root: t.Optional[Path]) -> FileScanResult:
96+
results = []
9597
errors = []
9698

9799
relpath = str(path.relative_to(root) if root else path)
98100

99101
for analyser in analysers:
100102
try:
101103
if analyser.accepts(path) and (res := analyser(path, root=root)):
102-
result[res.category] = res.data
104+
results.append(res)
103105
except: # noqa
104106
msg = f'An error occured while scanning {relpath} using \'{analyser.category}\' analyser'
105107
util.error(msg)
106108
errors.append(msg)
107109

108-
return relpath, result, errors
110+
return relpath, results, errors
109111

110112
@property
111113
def options(self) -> dict:
@@ -201,11 +203,9 @@ def _do_scan(self, files: t.List[FileScanInput]) -> ScanResults:
201203
relpath, result, errors = self.__class__._scan_file(path, self.analysers, root)
202204

203205
self._notifyCompletion(relpath, result, errors)
204-
206+
205207
if result:
206-
results.update({
207-
relpath: result
208-
})
208+
results[relpath] = result
209209

210210
return results
211211

@@ -222,7 +222,7 @@ def _progress(self):
222222
if self.onProgress:
223223
self.onProgress(self.finishedTasks, self.totalTasks)
224224

225-
def _notifyCompletion(self, relpath: str, result: dict, errors: list):
225+
def _notifyCompletion(self, relpath: str, result: t.List[AnalysisResult], errors: t.List[str]):
226226
if self.onFileScanCompleted:
227227
self.onFileScanCompleted(relpath, result, errors)
228228

src/ts_deepscan/scanner/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import typing as t
6-
import dataclasses_json
76
import osadl_matrix
87

98
from pathlib import Path
@@ -13,18 +12,19 @@
1312
from dataclasses import dataclass, field
1413
from dataclasses_json import dataclass_json
1514

15+
from ..analyser import AnalysisResult
1616

1717
# Alias for a file scan input represented as:
1818
# ( absolute_file_path, optional_root_path )
1919
FileScanInput = t.Tuple[Path, t.Optional[Path]]
2020

2121
# Alias for a file scan result represented as:
22-
# { analyser_category: analyser_result_data }
23-
FileScanResult = t.Dict[str, t.Any]
22+
# ( relative_file_path, [ analyser_results ], [ error_messages ] )
23+
FileScanResult = t.Tuple[str, t.List[AnalysisResult], t.List[str]]
2424

2525
# Alisas for scanner results represented as:
26-
# { relative_file_path: { analyser_category: analyser_result_data } }
27-
ScanResults = t.Dict[str, FileScanResult]
26+
# { relative_file_path: [ analyser_results ] }
27+
ScanResults = t.Dict[str, t.List[AnalysisResult]]
2828

2929

3030
@dataclass_json

src/ts_deepscan/scanner/postprocessing/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ def apply(self, results: ScanResults) -> ScanResults:
2828

2929

3030
class CachingPostProcessor(PostProcessor):
31-
from ...caching import ResultsCache, CacheableResult
32-
3331
def __init__(self, cache: ResultsCache, processor: t.Optional[PostProcessor] = None):
3432
self._cache = cache
3533
self._processor = processor
@@ -38,10 +36,9 @@ def apply(self, results: ScanResults) -> ScanResults:
3836
if self._processor:
3937
results = self._processor.apply(results)
4038

41-
for file_results in results.values():
42-
for cat, res in file_results.items():
39+
for file_results in results.values():
40+
for res in file_results:
4341
if isinstance(res, CacheableResult):
4442
self._cache.store(res)
45-
file_results[cat] = res.data
4643

4744
return results

0 commit comments

Comments
 (0)