import os
import re
import shutil
import subprocess
import sys
def check_ripgrep_available() -> bool:
return shutil.which("rg") is not None
def search_with_ripgrep(file_path: str) -> list[tuple[int, str]]:
results = []
try:
cmd = [
"rg",
"-a",
"-b",
"-o",
r"[a-f0-9]+(-dirty)? [A-Z][a-z]{2} {1,2}\d{1,2} \d{4} \d{2}:\d{2}:\d{2}",
file_path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0 and result.stdout.strip():
for line in result.stdout.strip().splitlines():
if ":" in line:
offset_str, matched_text = line.split(":", 1)
results.append((int(offset_str), matched_text))
return results
except Exception as e:
print(f"Error running ripgrep: {e}", file=sys.stderr)
return results
def search_with_python(file_path: str) -> list[tuple[int, str]]:
results = []
try:
pattern = re.compile(
rb"[a-f0-9]+(-dirty)? [A-Z][a-z]{2} {1,2}\d{1,2} \d{4} \d{2}:\d{2}:\d{2}"
)
with open(file_path, "rb") as f:
content = f.read()
for match in pattern.finditer(content):
offset = match.start()
matched_text = match.group().decode("utf-8", errors="replace")
results.append((offset, matched_text))
return results
except Exception as e:
print(f"Error searching file: {e}", file=sys.stderr)
return results
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <file_path>", file=sys.stderr)
sys.exit(1)
file_path = sys.argv[1]
if not os.path.exists(file_path):
print(f"Error: File '{file_path}' not found", file=sys.stderr)
sys.exit(1)
if check_ripgrep_available():
print("Using ripgrep for search...", file=sys.stderr)
results = search_with_ripgrep(file_path)
else:
print("ripgrep not found, using Python implementation...", file=sys.stderr)
results = search_with_python(file_path)
if results:
for offset, matched_text in results:
print(f"0x{offset:08x}:{matched_text}")
else:
print("No match found", file=sys.stderr)
sys.exit(1)