drive_session.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. """Interactive pty driver for the tudor_cli enrollment/verification flow.
  3. Spawns tudor_cli under a pseudo-terminal so its prompts work. Streams all
  4. output and forwards stdin so the session can be driven interactively:
  5. - type commands directly, or
  6. - pass "auto" as first arg to send an enroll command automatically.
  7. """
  8. import os
  9. import pty
  10. import select
  11. import sys
  12. import time
  13. BUILD = "/home/nebulon/projects/fingerprint/work/synaTudor/build"
  14. CLI = os.path.join(BUILD, "cli", "tudor_cli")
  15. DATASTORE = "/home/nebulon/projects/fingerprint/work/debug/datastore.bin"
  16. MODE = sys.argv[1] if len(sys.argv) > 1 else "interactive"
  17. os.chdir(BUILD)
  18. pid, fd = pty.fork()
  19. if pid == 0:
  20. os.execv(CLI, [CLI, DATASTORE])
  21. buf = b""
  22. sent_setup = False
  23. init_deadline = time.time() + 240
  24. while True:
  25. r, _, _ = select.select([fd, sys.stdin], [], [], 1.0)
  26. if sys.stdin in r:
  27. data = os.read(sys.stdin.fileno(), 4096)
  28. if data:
  29. os.write(fd, data)
  30. if fd in r:
  31. try:
  32. data = os.read(fd, 4096)
  33. except OSError:
  34. break
  35. if not data:
  36. break
  37. buf += data
  38. sys.stdout.write(data.decode("utf-8", "replace"))
  39. sys.stdout.flush()
  40. text = buf.decode("utf-8", "replace")
  41. if MODE == "auto" and not sent_setup and "> " in text:
  42. sent_setup = True
  43. time.sleep(0.5)
  44. os.write(fd, b"e\n")
  45. time.sleep(0.5)
  46. os.write(fd, b"1\n")
  47. time.sleep(0.5)
  48. os.write(fd, b"1\n")
  49. print("\n=== auto-enroll input sent; touch the sensor ===")
  50. sys.stdout.flush()
  51. if not sent_setup and MODE == "auto" and time.time() > init_deadline:
  52. print("\n=== init timed out ===")
  53. break
  54. try:
  55. os.close(fd)
  56. except OSError:
  57. pass
  58. os.waitpid(pid, 0)
  59. print("\n=== session ended ===")