Guest User

Untitled

a guest
Aug 10th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. # works on Mac OS X and Linux. haven't tried BSD. obviously windows is out.
  2.  
  3. import os
  4. import subprocess
  5. import signal
  6.  
  7. def get_tty_fg():
  8. # make a new process group within the same session as the parent. we
  9. # do this so that if the user hits ctrl-c, etc in the curses program
  10. # that's about to be exec'd, the SIGINT and friends won't be sent to
  11. # the parent.
  12. os.setpgrp()
  13.  
  14. # don't stop the process when we get SIGTTOU. since this is now in a
  15. # background process group, SIGTTOU will be sent to this process when
  16. # we call tcsetpgrp(), below. the default action when receiving that
  17. # signal is to stop (process mode T).
  18. hdlr = signal.signal(signal.SIGTTOU, signal.SIG_IGN)
  19.  
  20. # open a file handle to the current tty
  21. tty = os.open('/dev/tty', os.O_RDWR)
  22.  
  23. # ask for our new process group to be the foreground one on the
  24. # controlling tty.
  25. os.tcsetpgrp(tty, os.getpgrp())
  26.  
  27. # replace the old signal handler to minimize the chance of the child
  28. # getting confused by a non-standard starting signal table.
  29. signal.signal(signal.SIGTTOU, hdlr)
  30.  
  31. def spawn_pager(path):
  32. # should always allow users to configure their own pager. "pager" by
  33. # itself is a better default than less on Debian-based systems, but
  34. # that's not everywhere. Best default to "less" for maximum
  35. # compatibility.
  36. pager = os.environ.get('PAGER', 'less')
  37.  
  38. # if your calling process also expects to use the tty, you'll need to
  39. # do some extra stuff here to stop using it and handle SIGTTOU, SIGHUP,
  40. # and possibly SIGTTIN, as appropriate, until the child is done.
  41.  
  42. # run with our tty-foreground code in the child before exec'ing the
  43. # pager. return the pager process object.
  44. return subprocess.Popen([pager, path], preexec_fn=get_tty_fg)
Add Comment
Please, Sign In to add comment