blob: 445143647398b210cf0ff5e4437ef5b481ab629f [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001inherit terminal
2
3DEVSHELL = "${SHELL}"
4
5python do_devshell () {
6 if d.getVarFlag("do_devshell", "manualfakeroot"):
7 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 () {
30 if d.getVarFlag("do_devshell", "fakeroot"):
31 # 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
68 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
69 sys.stdin = os.fdopen(sys.stdin.fileno(), 'r', 0)
70
71 bb.utils.nonblockingfd(sys.stdout)
72 bb.utils.nonblockingfd(sys.stderr)
73 bb.utils.nonblockingfd(sys.stdin)
74
75 _context = {
76 "os": os,
77 "bb": bb,
78 "time": time,
79 "d": d,
80 }
81
82 ps1 = "pydevshell> "
83 ps2 = "... "
84 buf = []
85 more = False
86
87 i = code.InteractiveInterpreter(locals=_context)
88 print("OE PyShell (PN = %s)\n" % d.getVar("PN", True))
89
90 def prompt(more):
91 if more:
92 prompt = ps2
93 else:
94 prompt = ps1
95 sys.stdout.write(prompt)
96
97 # Restore Ctrl+C since bitbake masks this
98 def signal_handler(signal, frame):
99 raise KeyboardInterrupt
100 signal.signal(signal.SIGINT, signal_handler)
101
102 child = None
103
104 prompt(more)
105 while True:
106 try:
107 try:
108 (r, _, _) = select.select([sys.stdin], [], [], 1)
109 if not r:
110 continue
111 line = sys.stdin.readline().strip()
112 if not line:
113 prompt(more)
114 continue
115 except EOFError as e:
116 sys.stdout.write("\n")
117 except (OSError, IOError) as e:
118 if e.errno == 11:
119 continue
120 if e.errno == 5:
121 return
122 raise
123 else:
124 if not child:
125 child = int(line)
126 continue
127 buf.append(line)
128 source = "\n".join(buf)
129 more = i.runsource(source, "<pyshell>")
130 if not more:
131 buf = []
132 prompt(more)
133 except KeyboardInterrupt:
134 i.write("\nKeyboardInterrupt\n")
135 buf = []
136 more = False
137 prompt(more)
138 except SystemExit:
139 # Easiest way to ensure everything exits
140 os.kill(child, signal.SIGTERM)
141 break
142
143python do_devpyshell() {
144 import signal
145
146 try:
147 devpyshell(d)
148 except SystemExit:
149 # Stop the SIGTERM above causing an error exit code
150 return
151 finally:
152 return
153}
154addtask devpyshell after do_patch
155
156do_devpyshell[nostamp] = "1"