blob: be71aff35fd8fa52d9fa56a770e95a09693c14ef [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001inherit terminal
2
3DEVSHELL = "${SHELL}"
4
5python do_devshell () {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05006 if d.getVarFlag("do_devshell", "manualfakeroot", True):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007 d.prependVar("DEVSHELL", "pseudo ")
8 fakeenv = d.getVar("FAKEROOTENV", True).split()
9 for f in fakeenv:
10 k = f.split("=")
11 d.setVar(k[0], k[1])
12 d.appendVar("OE_TERMINAL_EXPORTS", " " + k[0])
13 d.delVarFlag("do_devshell", "fakeroot")
14
15 oe_terminal(d.getVar('DEVSHELL', True), 'OpenEmbedded Developer Shell', d)
16}
17
18addtask devshell after do_patch
19
20# The directory that the terminal starts in
21DEVSHELL_STARTDIR ?= "${S}"
22do_devshell[dirs] = "${DEVSHELL_STARTDIR}"
23do_devshell[nostamp] = "1"
24
25# devshell and fakeroot/pseudo need careful handling since only the final
26# command should run under fakeroot emulation, any X connection should
27# be done as the normal user. We therfore carefully construct the envionment
28# manually
29python () {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030 if d.getVarFlag("do_devshell", "fakeroot", True):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031 # We need to signal our code that we want fakeroot however we
32 # can't manipulate the environment and variables here yet (see YOCTO #4795)
33 d.setVarFlag("do_devshell", "manualfakeroot", "1")
34 d.delVarFlag("do_devshell", "fakeroot")
35}
36
37def devpyshell(d):
38
39 import code
40 import select
41 import signal
42 import termios
43
44 m, s = os.openpty()
45 sname = os.ttyname(s)
46
47 def noechoicanon(fd):
48 old = termios.tcgetattr(fd)
49 old[3] = old[3] &~ termios.ECHO &~ termios.ICANON
50 # &~ termios.ISIG
51 termios.tcsetattr(fd, termios.TCSADRAIN, old)
52
53 # No echo or buffering over the pty
54 noechoicanon(s)
55
56 pid = os.fork()
57 if pid:
58 os.close(m)
59 oe_terminal("oepydevshell-internal.py %s %d" % (sname, pid), 'OpenEmbedded Developer PyShell', d)
60 os._exit(0)
61 else:
62 os.close(s)
63
64 os.dup2(m, sys.stdin.fileno())
65 os.dup2(m, sys.stdout.fileno())
66 os.dup2(m, sys.stderr.fileno())
67
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 bb.utils.nonblockingfd(sys.stdout)
69 bb.utils.nonblockingfd(sys.stderr)
70 bb.utils.nonblockingfd(sys.stdin)
71
72 _context = {
73 "os": os,
74 "bb": bb,
75 "time": time,
76 "d": d,
77 }
78
79 ps1 = "pydevshell> "
80 ps2 = "... "
81 buf = []
82 more = False
83
84 i = code.InteractiveInterpreter(locals=_context)
85 print("OE PyShell (PN = %s)\n" % d.getVar("PN", True))
86
87 def prompt(more):
88 if more:
89 prompt = ps2
90 else:
91 prompt = ps1
92 sys.stdout.write(prompt)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060093 sys.stdout.flush()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094
95 # Restore Ctrl+C since bitbake masks this
96 def signal_handler(signal, frame):
97 raise KeyboardInterrupt
98 signal.signal(signal.SIGINT, signal_handler)
99
100 child = None
101
102 prompt(more)
103 while True:
104 try:
105 try:
106 (r, _, _) = select.select([sys.stdin], [], [], 1)
107 if not r:
108 continue
109 line = sys.stdin.readline().strip()
110 if not line:
111 prompt(more)
112 continue
113 except EOFError as e:
114 sys.stdout.write("\n")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600115 sys.stdout.flush()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 except (OSError, IOError) as e:
117 if e.errno == 11:
118 continue
119 if e.errno == 5:
120 return
121 raise
122 else:
123 if not child:
124 child = int(line)
125 continue
126 buf.append(line)
127 source = "\n".join(buf)
128 more = i.runsource(source, "<pyshell>")
129 if not more:
130 buf = []
131 prompt(more)
132 except KeyboardInterrupt:
133 i.write("\nKeyboardInterrupt\n")
134 buf = []
135 more = False
136 prompt(more)
137 except SystemExit:
138 # Easiest way to ensure everything exits
139 os.kill(child, signal.SIGTERM)
140 break
141
142python do_devpyshell() {
143 import signal
144
145 try:
146 devpyshell(d)
147 except SystemExit:
148 # Stop the SIGTERM above causing an error exit code
149 return
150 finally:
151 return
152}
153addtask devpyshell after do_patch
154
155do_devpyshell[nostamp] = "1"