#!/usr/bin/env python3
"""Fix the inline sqlite3 in echo."""
path = '/opt/struktur/obsidian-graphiti-import/aa043-writer-loop.sh'
with open(path) as f:
content = f.read()
# Find the problematic line and replace
old = ''' echo "$(date -u +%H:%M:%S) usage=$CUR_USAGE spent=$SPENT done=$(sqlite3 $DB \\"select count(*) from graphiti_import_queue where reconciliation_record_id like 'aa043:%' and graphiti_status='done'\\") no_prog_secs=$(( $(date -u +%s) - LAST_DONE_TIME ))" | tee -a $LOG'''
new = ''' DONE_COUNT=$(sqlite3 $DB "select count(*) from graphiti_import_queue where reconciliation_record_id like 'aa043:%' and graphiti_status='done'")
NO_PROG_SECS=$(( $(date -u +%s) - LAST_DONE_TIME ))
echo "$(date -u +%H:%M:%S) usage=$CUR_USAGE spent=$SPENT done=$DONE_COUNT no_prog_secs=$NO_PROG_SECS" | tee -a $LOG'''
if old in content:
content = content.replace(old, new)
print('Fixed')
else:
print('Pattern not found, searching...')
import re
# Try regex
content2 = re.sub(r' echo \".*done=\\$\(sqlite3.*no_prog_secs.*$', new, content, flags=re.M)
if content2 != content:
content = content2
print('Fixed via regex')
else:
print('NOT FOUND')
with open(path, 'w') as f:
f.write(content)