run_test.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from pathlib import Path
  2. import sys
  3. import time
  4. from watchdog.observers import Observer
  5. from watchdog.events import FileSystemEventHandler
  6. from watchdog.events import LoggingEventHandler
  7. import asyncio.subprocess as subprocess
  8. import asyncio
  9. from watchfiles import awatch, watch
  10. from termcolor import colored
  11. from datetime import datetime
  12. async def on_modified(event):
  13. sqls = open('./target.sql').read().split('\n\n')
  14. p = await subprocess.create_subprocess_shell("xmake", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  15. stdout, _ = await p.communicate()
  16. if b'error' in stdout:
  17. print(stdout.decode('utf-8'))
  18. print(datetime.now(), '-' * 40)
  19. return
  20. for sql in sqls:
  21. print(f'run sql: \n{colored(sql, "yellow")}\n')
  22. p = await subprocess.create_subprocess_exec(
  23. 'xmake', 'run', 'sql-parser', sql, stdout=subprocess.PIPE, stderr=subprocess.PIPE
  24. )
  25. for t in ['stderr', 'stdout']:
  26. content = (await eval(f'p.{t}').readline()).decode('utf-8')
  27. if content:
  28. print(colored(t + ':', 'green'))
  29. print(content)
  30. return_code = await p.wait()
  31. if return_code != 0:
  32. print(f'{return_code=}')
  33. break
  34. print(datetime.now(), '-' * 40)
  35. async def main():
  36. async for changes in awatch('src'):
  37. await asyncio.wait_for(on_modified(changes), 20)
  38. if __name__ == "__main__":
  39. asyncio.run(main())