88
99from codemodder import __version__ , providers , registry
1010from codemodder .cli import parse_args
11- from codemodder .code_directory import match_files
1211from codemodder .codemods .api import BaseCodemod
1312from codemodder .codemods .semgrep import SemgrepRuleDetector
1413from codemodder .codetf import CodeTF
@@ -72,38 +71,22 @@ def log_report(context, argv, elapsed_ms, files_to_analyze):
7271def apply_codemods (
7372 context : CodemodExecutionContext ,
7473 codemods_to_run : Sequence [BaseCodemod ],
75- semgrep_results : ResultSet ,
76- files_to_analyze : list [Path ],
7774):
7875 log_section ("scanning" )
7976
80- if not files_to_analyze :
77+ if not context . files_to_analyze :
8178 logger .info ("no files to scan" )
8279 return
8380
8481 if not codemods_to_run :
8582 logger .info ("no codemods to run" )
8683 return
8784
88- semgrep_finding_ids = semgrep_results .all_rule_ids ()
89-
9085 # run codemods one at a time making sure to respect the given sequence
9186 for codemod in codemods_to_run :
9287 # NOTE: this may be used as a progress indicator by upstream tools
9388 logger .info ("running codemod %s" , codemod .id )
94-
95- if isinstance (codemod .detector , SemgrepRuleDetector ):
96- if codemod ._internal_name not in semgrep_finding_ids :
97- logger .debug (
98- "no results from semgrep for %s, skipping analysis" ,
99- codemod .id ,
100- )
101- continue
102-
103- files_to_analyze = semgrep_results .files_for_rule (codemod ._internal_name )
104-
105- # Non-semgrep codemods ignore the semgrep results
106- codemod .apply (context , files_to_analyze )
89+ codemod .apply (context )
10790 record_dependency_update (context .process_dependencies (codemod .id ))
10891 context .log_changes (codemod .id )
10992
@@ -197,37 +180,24 @@ def run(original_args) -> int:
197180 sast_only = argv .sonar_issues_json or argv .sarif ,
198181 )
199182
200- included_paths = argv .path_include or codemod_registry .default_include_paths
201-
202183 log_section ("setup" )
203184 log_list (logging .INFO , "running" , codemods_to_run , predicate = lambda c : c .id )
204- log_list (logging .INFO , "including paths" , included_paths )
185+ log_list (logging .INFO , "including paths" , context . included_paths )
205186 log_list (logging .INFO , "excluding paths" , argv .path_exclude )
206187
207- files_to_analyze : list [Path ] = [
208- path
209- for path in match_files (
210- context .directory ,
211- argv .path_exclude ,
212- included_paths ,
213- )
214- if path .is_file () and not path .is_symlink ()
215- ]
216-
217- full_names = [str (path ) for path in files_to_analyze ]
218- log_list (logging .DEBUG , "matched files" , full_names )
188+ log_list (
189+ logging .DEBUG , "matched files" , (str (path ) for path in context .files_to_analyze )
190+ )
219191
220- semgrep_results : ResultSet = find_semgrep_results (
192+ context . semgrep_prefilter_results = find_semgrep_results (
221193 context ,
222194 codemods_to_run ,
223- files_to_analyze ,
195+ context . find_and_fix_paths ,
224196 )
225197
226198 apply_codemods (
227199 context ,
228200 codemods_to_run ,
229- semgrep_results ,
230- files_to_analyze ,
231201 )
232202
233203 elapsed = datetime .datetime .now () - start
@@ -243,7 +213,10 @@ def run(original_args) -> int:
243213 codetf .write_report (argv .output )
244214
245215 log_report (
246- context , argv , elapsed_ms , [] if not codemods_to_run else files_to_analyze
216+ context ,
217+ argv ,
218+ elapsed_ms ,
219+ [] if not codemods_to_run else context .files_to_analyze ,
247220 )
248221 return 0
249222
0 commit comments