oepydevshell-internal.py 3.1 KB

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