oepydevshell-internal.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import time
  5. import select
  6. import fcntl
  7. import termios
  8. import readline
  9. import signal
  10. def nonblockingfd(fd):
  11. fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
  12. def echonocbreak(fd):
  13. old = termios.tcgetattr(fd)
  14. old[3] = old[3] | termios.ECHO | termios.ICANON
  15. termios.tcsetattr(fd, termios.TCSADRAIN, old)
  16. def cbreaknoecho(fd):
  17. old = termios.tcgetattr(fd)
  18. old[3] = old[3] &~ termios.ECHO &~ termios.ICANON
  19. termios.tcsetattr(fd, termios.TCSADRAIN, old)
  20. if len(sys.argv) != 3 or sys.argv[1] in ('-h', '--help'):
  21. print('oepydevshell-internal.py: error: the following arguments are required: pty, pid\n'
  22. 'Usage: oepydevshell-internal.py pty pid\n\n'
  23. 'OpenEmbedded oepydevshell-internal.py - internal script called from meta/classes/devshell.bbclass\n\n'
  24. 'arguments:\n'
  25. ' pty pty device name\n'
  26. ' pid parent process id\n\n'
  27. 'options:\n'
  28. ' -h, --help show this help message and exit\n')
  29. sys.exit(2)
  30. pty = open(sys.argv[1], "w+b", 0)
  31. parent = int(sys.argv[2])
  32. nonblockingfd(pty)
  33. nonblockingfd(sys.stdin)
  34. histfile = os.path.expanduser("~/.oedevpyshell-history")
  35. readline.parse_and_bind("tab: complete")
  36. try:
  37. readline.read_history_file(histfile)
  38. except IOError:
  39. pass
  40. try:
  41. i = ""
  42. o = ""
  43. # Need cbreak/noecho whilst in select so we trigger on any keypress
  44. cbreaknoecho(sys.stdin.fileno())
  45. # Send our PID to the other end so they can kill us.
  46. pty.write(str(os.getpid()).encode('utf-8') + b"\n")
  47. while True:
  48. try:
  49. writers = []
  50. if i:
  51. writers.append(sys.stdout)
  52. (ready, _, _) = select.select([pty, sys.stdin], writers , [], 0)
  53. try:
  54. if pty in ready:
  55. readdata = pty.read()
  56. if readdata:
  57. i = i + readdata.decode('utf-8')
  58. if i:
  59. # Write a page at a time to avoid overflowing output
  60. # d.keys() is a good way to do that
  61. sys.stdout.write(i[:4096])
  62. sys.stdout.flush()
  63. i = i[4096:]
  64. if sys.stdin in ready:
  65. echonocbreak(sys.stdin.fileno())
  66. o = input().encode('utf-8')
  67. cbreaknoecho(sys.stdin.fileno())
  68. pty.write(o + b"\n")
  69. except (IOError, OSError) as e:
  70. if e.errno == 11:
  71. continue
  72. if e.errno == 5:
  73. sys.exit(0)
  74. raise
  75. except EOFError:
  76. sys.exit(0)
  77. except KeyboardInterrupt:
  78. os.kill(parent, signal.SIGINT)
  79. except SystemExit:
  80. pass
  81. except Exception as e:
  82. import traceback
  83. print("Exception in oepydehshell-internal: " + str(e))
  84. traceback.print_exc()
  85. time.sleep(5)
  86. finally:
  87. readline.write_history_file(histfile)