139 lines
5.6 KiB
Python
139 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Clean violation patterns from dacli source comments.
|
|
|
|
Targets 6 patterns from check_clean_comments.sh:
|
|
1. BS-[a-zA-Z]*[0-9] 2. R[0-9]+[a-z]* 3. Round[0-9]+
|
|
4. jxcj|workSystem|developSystem|tds 5. v0.XX.YY+ 6. ssw-XXX-biz/common
|
|
|
|
Only modifies comment lines (//, *, /*).
|
|
"""
|
|
import re, sys, os
|
|
|
|
def clean_line(line: str) -> str:
|
|
"""Clean one line. Returns '' to delete the line entirely."""
|
|
s = line.strip()
|
|
if not (s.startswith('//') or s.startswith('*') or s.startswith('/*')):
|
|
return line # code line, don't touch
|
|
|
|
# --- Pattern 1: BS-* tags ---
|
|
# Delete lines that are ONLY a BS tag comment (no other useful content)
|
|
if re.match(r'^\s*//\s*BS-\S+\s*(?:fix|修复|修正|强化|兜底)?\s*$', line): return ''
|
|
if re.match(r'^\s*\*\s*BS-\S+\s*(?:fix|修复|修正|强化|兜底)?\s*$', line): return ''
|
|
|
|
# Strip BS tag from mixed-content lines: "// BS-XX fix: real text" -> "// real text"
|
|
line = re.sub(r'BS-\S+\s*(?:fix|修复|修正|强化|兜底)?\s*:\s*', '', line)
|
|
line = re.sub(r'BS-\S+\s+', '', line) # "// BS-XX explanation" -> "// explanation"
|
|
|
|
# --- Pattern 2: R tags (R17, R3.3, R13a, etc.) ---
|
|
if re.match(r'^\s*//\s*\(?R\d+(?:\.\d+)?[a-z]*\)?\s*$', line): return ''
|
|
if re.match(r'^\s*\*\s*\(?R\d+(?:\.\d+)?[a-z]*\)?\s*$', line): return ''
|
|
# R17 P1-3: text
|
|
line = re.sub(r'R\d+(?:\.\d+)?[a-z]*\s+P\d+-\d+\s*:\s*', '', line)
|
|
# (R3.3): text or R3.3: text or R3.3 fix: text
|
|
line = re.sub(r'\(?R\d+(?:\.\d+)?[a-z]*\)?\s*(?:fix|修复|修正)?\s*:\s*', '', line)
|
|
# (R3.3) text or R3.3 text
|
|
line = re.sub(r'\(?R\d+(?:\.\d+)?[a-z]*\)?\s+', '', line)
|
|
|
|
# --- Pattern 3: Round tags ---
|
|
if re.match(r'^\s*//\s*Round\d+\s*$', line): return ''
|
|
if re.match(r'^\s*\*\s*Round\d+\s*$', line): return ''
|
|
line = re.sub(r'Round\d+\s+BS-\S+\s*:\s*', '', line)
|
|
line = re.sub(r'Round\d+\s+', '', line)
|
|
|
|
# --- Pattern 4: jxcj/workSystem/developSystem/tds ---
|
|
# Only in comments. Replace with generic term.
|
|
line = re.sub(r'\bjxcj\b', 'app', line)
|
|
line = re.sub(r'\bworkSystem\b', 'reference app', line)
|
|
line = re.sub(r'\bdevelopSystem\b', 'reference app', line)
|
|
line = re.sub(r'\btds\b', 'app', line)
|
|
|
|
# --- Pattern 5: v0.x+ version markers (handle v0.10, v0.10.1, v0.11h+, v0.21+, etc.) ---
|
|
if re.match(r'^\s*//\s*v0\.\d+(?:\.\d+)?[a-z]*\+?\s*$', line): return ''
|
|
if re.match(r'^\s*\*\s*v0\.\d+(?:\.\d+)?[a-z]*\+?\s*$', line): return ''
|
|
# v0.XX+: text or v0.XX.YY: text or v0.XX fix: text
|
|
line = re.sub(r'v0\.\d+(?:\.\d+)?[a-z]*\+?\s*(?:fix|修复|修正)?\s*:', '', line)
|
|
# v0.XX+, text or v0.XX+ text or v0.XX+ — text
|
|
line = re.sub(r'v0\.\d+(?:\.\d+)?[a-z]*\+?[,]?\s+(?:fix|修复|修正)?\s*', '', line)
|
|
# v0.XX+ remaining (at end of line, no text after)
|
|
line = re.sub(r'\(?v0\.\d+(?:\.\d+)?[a-z]*\+?\)?', '', line)
|
|
|
|
# --- Pattern 6: ssw-*-biz/ssw-common ---
|
|
# Only in comments. Remove or simplify.
|
|
line = re.sub(r'ssw-[a-z]+-biz', 'ssw module', line)
|
|
line = re.sub(r'ssw-common', 'shared module', line)
|
|
|
|
# --- Additional cleanup: dates, B-*, P* tags in comments ---
|
|
line = re.sub(r'\(\d{4}-\d{2}-\d{2}\)\s*', '', line)
|
|
line = re.sub(r'\(\d{4}-\d{2}-\d{2}\s+[^)]+\)\s*:?\s*', '', line)
|
|
line = re.sub(r'\d{4}-\d{2}-\d{2}\s+', '', line)
|
|
line = re.sub(r'B-\d+\s*(?:fix|修复)?\s*:\s*', '', line)
|
|
line = re.sub(r'P[01]-\d+\s*(?:fix|修复|兜底)?\s*:\s*', '', line)
|
|
|
|
# --- Cleanup whitespace ---
|
|
line = re.sub(r'//\s{2,}', '// ', line)
|
|
line = re.sub(r'\*\s{2,}', '* ', line)
|
|
|
|
# Delete line if it became just empty comment noise
|
|
stripped = line.strip()
|
|
if stripped in ('//', '// fix:', '// 修复:', '// 修正:', '*', '* fix:', '* 修复:',
|
|
'// app', '* app', '// app M1', '* app M1',
|
|
'// ssw module', '* ssw module', '// shared module', '* shared module'):
|
|
return ''
|
|
|
|
# Remove leading "修复: " or "fix: " at comment start (when marker was prefix)
|
|
line = re.sub(r'^(\s*//\s*)(?:fix:|修复:|修正:|强化:|兜底:)\s*', r'\1', line)
|
|
line = re.sub(r'^(\s*\*\s*)(?:fix:|修复:|修正:|强化:|兜底:)\s*', r'\1', line)
|
|
|
|
# Remove leading ": " after stripping version/date markers at comment start
|
|
line = re.sub(r'^(\s*//\s*):\s+', r'\1', line)
|
|
line = re.sub(r'^(\s*\*\s*):\s+', r'\1', line)
|
|
|
|
# After all stripping, if main content is gone, delete
|
|
if re.match(r'^\s*//\s*$', line): return ''
|
|
if re.match(r'^\s*\*\s*$', line): return ''
|
|
|
|
# Trim trailing whitespace
|
|
line = line.rstrip() + '\n' if line.endswith('\n') else line.rstrip()
|
|
|
|
return line
|
|
|
|
def clean_file(filepath: str) -> int:
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
lines = f.readlines()
|
|
|
|
new_lines = []
|
|
modified = 0
|
|
for line in lines:
|
|
cleaned = clean_line(line)
|
|
if cleaned != line:
|
|
modified += 1
|
|
if cleaned:
|
|
new_lines.append(cleaned)
|
|
elif cleaned == '':
|
|
continue
|
|
else:
|
|
new_lines.append(line)
|
|
|
|
if modified > 0:
|
|
with open(filepath, 'w', encoding='utf-8') as f:
|
|
f.writelines(new_lines)
|
|
return modified
|
|
|
|
def main():
|
|
files = sys.argv[1:]
|
|
if not files:
|
|
print("Usage: clean_comments.py <file1> [file2 ...]")
|
|
sys.exit(1)
|
|
total = 0
|
|
for f in files:
|
|
if not os.path.exists(f):
|
|
print(f"SKIP: {f}")
|
|
continue
|
|
n = clean_file(f)
|
|
total += n
|
|
print(f" {n:4d} lines: {f}")
|
|
print(f"Total: {total} lines modified across {len(files)} files")
|
|
|
|
if __name__ == '__main__':
|
|
main()
|