-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathdeep_validator.py
More file actions
251 lines (200 loc) · 8.51 KB
/
deep_validator.py
File metadata and controls
251 lines (200 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Deep validator module for WRITEME to check for issues in the codebase.
This version performs a more thorough check for duplicate snippet tags by
directly scanning the files in the repository.
"""
import logging
import os
import re
import concurrent.futures
from collections import defaultdict
from pathlib import Path
from typing import Dict, List, Set, Tuple, Optional, Any
from aws_doc_sdk_examples_tools.doc_gen import DocGen
logger = logging.getLogger(__name__)
class ValidationError(Exception):
"""Exception raised for validation errors."""
pass
def find_snippet_tags_in_file(file_path: Path) -> List[Tuple[str, int]]:
"""
Find all snippet tags in a file by directly parsing the file content.
Args:
file_path: Path to the file to check
Returns:
List of tuples containing (tag, line_number)
"""
if not file_path.exists():
return []
try:
with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
lines = f.readlines()
except Exception as e:
logger.warning(f"Error reading file {file_path}: {e}")
return []
# Common snippet tag patterns
patterns = [
# Standard snippet tag format
r'snippet-start:\s*\[([^\]]+)\]',
r'snippet-end:\s*\[([^\]]+)\]',
# Alternative formats
r'SNIPPET\s+START\s+\[([^\]]+)\]',
r'SNIPPET\s+END\s+\[([^\]]+)\]',
r'//\s*SNIPPET:\s*([^\s]+)',
r'#\s*SNIPPET:\s*([^\s]+)',
r'<!--\s*SNIPPET:\s*([^\s]+)\s*-->',
# Look for any other potential tag formats
r'snippet[:\-_]([a-zA-Z0-9_\-]+)',
# Common AWS SDK snippet formats
r'//\s*snippet-start:\s*([^\s]+)',
r'#\s*snippet-start:\s*([^\s]+)',
r'<!--\s*snippet-start:\s*([^\s]+)\s*-->',
r'//\s*snippet-end:\s*([^\s]+)',
r'#\s*snippet-end:\s*([^\s]+)',
r'<!--\s*snippet-end:\s*([^\s]+)\s*-->',
]
results = []
for i, line in enumerate(lines, 1):
for pattern in patterns:
matches = re.findall(pattern, line, re.IGNORECASE)
for match in matches:
results.append((match, i))
return results
def scan_directory_for_snippet_tags(
root_dir: Path,
extensions: Optional[List[str]] = None,
max_workers: int = 10
) -> Dict[str, List[Tuple[str, int, str]]]:
"""
Scan a directory recursively for files containing snippet tags.
Uses parallel processing for faster scanning.
Args:
root_dir: Root directory to scan
extensions: Optional list of file extensions to check
max_workers: Maximum number of parallel workers
Returns:
Dictionary mapping snippet tags to lists of (file_path, line_number, context)
"""
if extensions is None:
# Default extensions to check
extensions = [
'.py', '.java', '.js', '.ts', '.cs', '.cpp', '.c', '.go', '.rb',
'.php', '.swift', '.kt', '.rs', '.abap', '.md', '.html', '.xml'
]
# Find all files with the specified extensions
files_to_scan = []
for root, _, files in os.walk(root_dir):
for file in files:
if any(file.endswith(ext) for ext in extensions):
files_to_scan.append(Path(root) / file)
# Process files in parallel
tag_to_locations = defaultdict(list)
def process_file(file_path):
try:
relative_path = file_path.relative_to(root_dir)
tags = find_snippet_tags_in_file(file_path)
results = []
for tag, line_number in tags:
# Get some context from the file
try:
with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
lines = f.readlines()
start_line = max(0, line_number - 2)
end_line = min(len(lines), line_number + 1)
context = ''.join(lines[start_line:end_line]).strip()
except Exception:
context = "<context unavailable>"
results.append((str(relative_path), line_number, context))
return {tag: [loc] for tag, line_number in tags for loc in [(str(relative_path), line_number, "")]}
except Exception as e:
logger.warning(f"Error processing file {file_path}: {e}")
return {}
# Use ThreadPoolExecutor for parallel processing
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_file = {executor.submit(process_file, file): file for file in files_to_scan}
for future in concurrent.futures.as_completed(future_to_file):
file_results = future.result()
for tag, locations in file_results.items():
tag_to_locations[tag].extend(locations)
return tag_to_locations
def check_duplicate_snippet_tags_deep(doc_gen: DocGen) -> List[Tuple[str, List[Dict[str, Any]]]]:
"""
Deep check for duplicate snippet tags in the codebase.
This function scans all files directly to find snippet tags.
Args:
doc_gen: The DocGen instance containing snippets
Returns:
List of tuples containing (tag, [location_details]) for duplicate tags
"""
logger.info("Starting deep scan for duplicate snippet tags...")
# Scan the repository directly for snippet tags
root_dir = doc_gen.root
tag_locations = scan_directory_for_snippet_tags(root_dir)
# Find tags that appear in multiple files
duplicates = []
for tag, locations in tag_locations.items():
# Group locations by file path
files = {}
for file_path, line_number, context in locations:
if file_path not in files:
files[file_path] = []
files[file_path].append({"line": line_number, "context": context})
# If the tag appears in multiple files, it's a duplicate
if len(files) > 1:
duplicate_info = []
for file_path, occurrences in files.items():
duplicate_info.append({
"file": file_path,
"occurrences": occurrences
})
duplicates.append((tag, duplicate_info))
logger.info(f"Deep scan complete. Found {len(duplicates)} duplicate tags.")
return duplicates
def format_duplicate_report(duplicates: List[Tuple[str, List[Dict[str, Any]]]]) -> str:
"""
Format a detailed report of duplicate snippet tags.
Args:
duplicates: List of duplicate tag information
Returns:
Formatted report as a string
"""
if not duplicates:
return "No duplicate snippet tags found."
report = [f"Found {len(duplicates)} duplicate snippet tags:"]
for tag, locations in duplicates:
report.append(f"\nTag: '{tag}' found in {len(locations)} files:")
for location in locations:
file_path = location["file"]
occurrences = location["occurrences"]
report.append(f" File: {file_path}")
for occurrence in occurrences:
line = occurrence.get("line", "unknown")
context = occurrence.get("context", "").replace("\n", " ").strip()
if context:
context = f" - Context: {context[:60]}..."
report.append(f" Line {line}{context}")
return "\n".join(report)
def validate_snippets_deep(doc_gen: DocGen, strict: bool = False) -> bool:
"""
Deep validation of snippets in the codebase.
Args:
doc_gen: The DocGen instance containing snippets
strict: If True, raise an exception for validation errors
Returns:
True if validation passed, False otherwise
"""
validation_passed = True
# Check for duplicate snippet tags using the deep method
duplicates = check_duplicate_snippet_tags_deep(doc_gen)
if duplicates:
validation_passed = False
report = format_duplicate_report(duplicates)
print("\n=== DUPLICATE SNIPPET TAGS (DEEP SCAN) ===")
print(report)
# Exit with error if strict validation is enabled
if strict:
raise ValidationError("Validation failed: duplicate snippet tags found")
else:
print("No duplicate snippet tags found in deep scan.")
return validation_passed