from pathlib import Path import sys import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from watchdog.events import LoggingEventHandler import asyncio.subprocess as subprocess import asyncio from watchfiles import awatch, watch from termcolor import colored from datetime import datetime async def on_modified(event): sqls = open('./target.sql').read().split('\n\n') p = await subprocess.create_subprocess_shell("xmake", stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, _ = await p.communicate() if b'error' in stdout: print(stdout.decode('utf-8')) print(datetime.now(), '-' * 40) return for sql in sqls: print(f'run sql: \n{colored(sql, "yellow")}\n') p = await subprocess.create_subprocess_exec( 'xmake', 'run', 'sql-parser', sql, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) for t in ['stderr', 'stdout']: content = (await eval(f'p.{t}').readline()).decode('utf-8') if content: print(colored(t + ':', 'green')) print(content) return_code = await p.wait() if return_code != 0: print(f'{return_code=}') break print(datetime.now(), '-' * 40) async def main(): async for changes in awatch('src'): await asyncio.wait_for(on_modified(changes), 20) if __name__ == "__main__": asyncio.run(main())