Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | inherit terminal |
| 2 | |
| 3 | DEVSHELL = "${SHELL}" |
| 4 | |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 5 | PATH:prepend:task-devshell = "${COREBASE}/scripts/git-intercept:" |
| 6 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | python do_devshell () { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 8 | if d.getVarFlag("do_devshell", "manualfakeroot"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | d.prependVar("DEVSHELL", "pseudo ") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | fakeenv = d.getVar("FAKEROOTENV").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | for f in fakeenv: |
| 12 | k = f.split("=") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 13 | d.setVar(k[0], k[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | d.appendVar("OE_TERMINAL_EXPORTS", " " + k[0]) |
| 15 | d.delVarFlag("do_devshell", "fakeroot") |
| 16 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 17 | oe_terminal(d.getVar('DEVSHELL'), 'OpenEmbedded Developer Shell', d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | } |
| 19 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 20 | addtask devshell after do_patch do_prepare_recipe_sysroot |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | |
| 22 | # The directory that the terminal starts in |
| 23 | DEVSHELL_STARTDIR ?= "${S}" |
| 24 | do_devshell[dirs] = "${DEVSHELL_STARTDIR}" |
| 25 | do_devshell[nostamp] = "1" |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 26 | do_devshell[network] = "1" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | # devshell and fakeroot/pseudo need careful handling since only the final |
| 29 | # command should run under fakeroot emulation, any X connection should |
| 30 | # be done as the normal user. We therfore carefully construct the envionment |
| 31 | # manually |
| 32 | python () { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 33 | if d.getVarFlag("do_devshell", "fakeroot"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | # We need to signal our code that we want fakeroot however we |
| 35 | # can't manipulate the environment and variables here yet (see YOCTO #4795) |
| 36 | d.setVarFlag("do_devshell", "manualfakeroot", "1") |
| 37 | d.delVarFlag("do_devshell", "fakeroot") |
| 38 | } |
| 39 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 40 | def pydevshell(d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | |
| 42 | import code |
| 43 | import select |
| 44 | import signal |
| 45 | import termios |
| 46 | |
| 47 | m, s = os.openpty() |
| 48 | sname = os.ttyname(s) |
| 49 | |
| 50 | def noechoicanon(fd): |
| 51 | old = termios.tcgetattr(fd) |
| 52 | old[3] = old[3] &~ termios.ECHO &~ termios.ICANON |
| 53 | # &~ termios.ISIG |
| 54 | termios.tcsetattr(fd, termios.TCSADRAIN, old) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 55 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | # No echo or buffering over the pty |
| 57 | noechoicanon(s) |
| 58 | |
| 59 | pid = os.fork() |
| 60 | if pid: |
| 61 | os.close(m) |
| 62 | oe_terminal("oepydevshell-internal.py %s %d" % (sname, pid), 'OpenEmbedded Developer PyShell', d) |
| 63 | os._exit(0) |
| 64 | else: |
| 65 | os.close(s) |
| 66 | |
| 67 | os.dup2(m, sys.stdin.fileno()) |
| 68 | os.dup2(m, sys.stdout.fileno()) |
| 69 | os.dup2(m, sys.stderr.fileno()) |
| 70 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 88 | print("OE PyShell (PN = %s)\n" % d.getVar("PN")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | |
| 90 | def prompt(more): |
| 91 | if more: |
| 92 | prompt = ps2 |
| 93 | else: |
| 94 | prompt = ps1 |
| 95 | sys.stdout.write(prompt) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 96 | sys.stdout.flush() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | |
| 98 | # Restore Ctrl+C since bitbake masks this |
| 99 | def signal_handler(signal, frame): |
| 100 | raise KeyboardInterrupt |
| 101 | signal.signal(signal.SIGINT, signal_handler) |
| 102 | |
| 103 | child = None |
| 104 | |
| 105 | prompt(more) |
| 106 | while True: |
| 107 | try: |
| 108 | try: |
| 109 | (r, _, _) = select.select([sys.stdin], [], [], 1) |
| 110 | if not r: |
| 111 | continue |
| 112 | line = sys.stdin.readline().strip() |
| 113 | if not line: |
| 114 | prompt(more) |
| 115 | continue |
| 116 | except EOFError as e: |
| 117 | sys.stdout.write("\n") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 118 | sys.stdout.flush() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 119 | except (OSError, IOError) as e: |
| 120 | if e.errno == 11: |
| 121 | continue |
| 122 | if e.errno == 5: |
| 123 | return |
| 124 | raise |
| 125 | else: |
| 126 | if not child: |
| 127 | child = int(line) |
| 128 | continue |
| 129 | buf.append(line) |
| 130 | source = "\n".join(buf) |
| 131 | more = i.runsource(source, "<pyshell>") |
| 132 | if not more: |
| 133 | buf = [] |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 134 | sys.stderr.flush() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 135 | prompt(more) |
| 136 | except KeyboardInterrupt: |
| 137 | i.write("\nKeyboardInterrupt\n") |
| 138 | buf = [] |
| 139 | more = False |
| 140 | prompt(more) |
| 141 | except SystemExit: |
| 142 | # Easiest way to ensure everything exits |
| 143 | os.kill(child, signal.SIGTERM) |
| 144 | break |
| 145 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 146 | python do_pydevshell() { |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | import signal |
| 148 | |
| 149 | try: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 150 | pydevshell(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | except SystemExit: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 152 | # Stop the SIGTERM above causing an error exit code |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 153 | return |
| 154 | finally: |
| 155 | return |
| 156 | } |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 157 | addtask pydevshell after do_patch |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 159 | do_pydevshell[nostamp] = "1" |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 160 | do_pydevshell[network] = "1" |