Files
utun2/replace_remaining.py
T

119 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Replace remaining printf calls in test files.
"""
import os
import re
import sys
def process_file(filepath):
with open(filepath, 'r') as f:
content = f.read()
original = content
# Determine category based on filename
filename = os.path.basename(filepath).lower()
if 'crypto' in filename or 'ecc' in filename:
category = 'DEBUG_CATEGORY_CRYPTO'
elif 'etcp' in filename:
category = 'DEBUG_CATEGORY_ETCP'
elif 'memory' in filename:
category = 'DEBUG_CATEGORY_MEMORY'
elif 'u_async' in filename:
category = 'DEBUG_CATEGORY_UASYNC'
elif 'll_queue' in filename:
category = 'DEBUG_CATEGORY_LL_QUEUE'
elif 'config' in filename:
category = 'DEBUG_CATEGORY_CONFIG'
else:
category = 'DEBUG_CATEGORY_ALL'
# Replace printf calls - simple pattern for single line printf
lines = content.split('\n')
new_lines = []
i = 0
while i < len(lines):
line = lines[i]
stripped = line.strip()
# Look for printf
if stripped.startswith('printf('):
# Try to capture the whole printf call (may span multiple lines)
# Collect lines until we find semicolon
printf_lines = [line]
j = i + 1
while j < len(lines) and ';' not in lines[j-1]:
printf_lines.append(lines[j])
j += 1
# Now we have block of lines
block = '\n'.join(printf_lines)
# Simple regex for single line printf with arguments on same line
# Match printf("format", args...);
match = re.search(r'printf\s*\(\s*"([^"]*)"\s*,?(.*)\)\s*;', block, re.DOTALL)
if match:
fmt = match.group(1).replace('\n', '').replace('\\n', '')
args = match.group(2).strip()
# Determine level
fmt_lower = fmt.lower()
if '❌' in fmt or 'error' in fmt_lower or 'failed' in fmt_lower:
level = 'DEBUG_ERROR'
elif 'warn' in fmt_lower:
level = 'DEBUG_WARN'
elif '✅' in fmt_lower or 'success' in fmt_lower or 'passed' in fmt_lower:
level = 'DEBUG_INFO'
elif '[debug]' in fmt_lower:
level = 'DEBUG_DEBUG'
else:
level = 'DEBUG_INFO'
# Remove emojis
fmt = fmt.replace('❌', '').replace('✅', '').replace('ℹ️', '').strip()
# Build new line
if args:
new_line = line.replace(stripped, f'{level}({category}, "{fmt}", {args});')
else:
new_line = line.replace(stripped, f'{level}({category}, "{fmt}");')
new_lines.append(new_line)
i = j # Skip the lines we consumed
continue
else:
# Fallback: just remove printf line
new_lines.append(line)
else:
new_lines.append(line)
i += 1
content = '\n'.join(new_lines)
# Remove fflush(stdout) lines
content = re.sub(r'^\s*fflush\s*\(\s*stdout\s*\)\s*;\s*$', '', content, flags=re.MULTILINE)
if content != original:
with open(filepath, 'w') as f:
f.write(content)
print(f"Updated {filepath}")
else:
print(f"No changes in {filepath}")
def main():
# Files that still have printf according to grep
files = [
'tests/test_ll_queue.c',
'tests/test_ecc_encrypt.c',
'tests/test_debug_categories.c',
'tests/test_etcp_two_instances.c',
'tests/test_packet_dump.c',
'tests/test_memory_pool_and_config.c',
'tests/test_u_async_performance.c',
'tests/test_etcp_minimal.c'
]
for filepath in files:
if os.path.exists(filepath):
process_file(filepath)
else:
print(f"File not found: {filepath}")
if __name__ == '__main__':
main()