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