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("=") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 11 | d.setVar(k[0], k[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 18 | addtask devshell after do_patch do_prepare_recipe_sysroot |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 37 | def pydevshell(d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 52 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 = [] |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 131 | sys.stderr.flush() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 143 | python do_pydevshell() { |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 144 | import signal |
| 145 | |
| 146 | try: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 147 | pydevshell(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | except SystemExit: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 149 | # Stop the SIGTERM above causing an error exit code |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 150 | return |
| 151 | finally: |
| 152 | return |
| 153 | } |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 154 | addtask pydevshell after do_patch |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 156 | do_pydevshell[nostamp] = "1" |