Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | inherit terminal |
| 2 | |
| 3 | DEVSHELL = "${SHELL}" |
| 4 | |
| 5 | python do_devshell () { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 6 | if d.getVarFlag("do_devshell", "manualfakeroot"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | d.prependVar("DEVSHELL", "pseudo ") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 8 | fakeenv = d.getVar("FAKEROOTENV").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 15 | oe_terminal(d.getVar('DEVSHELL'), 'OpenEmbedded Developer Shell', d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | addtask devshell after do_patch |
| 19 | |
| 20 | # The directory that the terminal starts in |
| 21 | DEVSHELL_STARTDIR ?= "${S}" |
| 22 | do_devshell[dirs] = "${DEVSHELL_STARTDIR}" |
| 23 | do_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 |
| 29 | python () { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 30 | if d.getVarFlag("do_devshell", "fakeroot"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 37 | def 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | 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) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 85 | print("OE PyShell (PN = %s)\n" % d.getVar("PN")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | |
| 87 | def prompt(more): |
| 88 | if more: |
| 89 | prompt = ps2 |
| 90 | else: |
| 91 | prompt = ps1 |
| 92 | sys.stdout.write(prompt) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 93 | sys.stdout.flush() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | |
| 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 115 | sys.stdout.flush() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | 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 | |
| 142 | python 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 | } |
| 153 | addtask devpyshell after do_patch |
| 154 | |
| 155 | do_devpyshell[nostamp] = "1" |