run_test.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. async def on_modified(event):
  12. sqls = open('./target.sql').read().split('\n\n')
  13. p = await subprocess.create_subprocess_shell("xmake", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  14. stdout, _ = await p.communicate()
  15. if b'error' in stdout:
  16. print(stdout.decode('utf-8'))
  17. return
  18. for sql in sqls:
  19. print(f'run sql: \n{colored(sql, "yellow")}\n')
  20. p = await subprocess.create_subprocess_exec(
  21. 'xmake', 'run', 'sql-parser', sql, stdout=subprocess.PIPE, stderr=subprocess.PIPE
  22. )
  23. for t in ['stderr', 'stdout']:
  24. content = (await eval(f'p.{t}').readline()).decode('utf-8')
  25. if content:
  26. print(colored(t + ':', 'green'))
  27. print(content)
  28. return_code = await p.wait()
  29. if return_code != 0:
  30. print(f'{return_code=}')
  31. break
  32. print('-'*40)
  33. async def main():
  34. async for changes in awatch('src'):
  35. await asyncio.wait_for(on_modified(changes), 20)
  36. if __name__ == "__main__":
  37. asyncio.run(main())