Determines if the current terminal session is interactive. sys.stdin must be a terminal input stream. Args: output: If True then sys.stdout must also be a terminal output stream. error: If True then sys.stderr must also be a terminal output stream. heuristic: If True then we also d
(output=False, error=False, heuristic=False)
| 27 | |
| 28 | |
| 29 | def IsInteractive(output=False, error=False, heuristic=False): |
| 30 | """Determines if the current terminal session is interactive. |
| 31 | |
| 32 | sys.stdin must be a terminal input stream. |
| 33 | |
| 34 | Args: |
| 35 | output: If True then sys.stdout must also be a terminal output stream. |
| 36 | error: If True then sys.stderr must also be a terminal output stream. |
| 37 | heuristic: If True then we also do some additional heuristics to check if |
| 38 | we are in an interactive context. Checking home path for example. |
| 39 | |
| 40 | Returns: |
| 41 | True if the current terminal session is interactive. |
| 42 | """ |
| 43 | if not sys.stdin.isatty(): |
| 44 | return False |
| 45 | if output and not sys.stdout.isatty(): |
| 46 | return False |
| 47 | if error and not sys.stderr.isatty(): |
| 48 | return False |
| 49 | |
| 50 | if heuristic: |
| 51 | # Check the home path. Most startup scripts for example are executed by |
| 52 | # users that don't have a home path set. Home is OS dependent though, so |
| 53 | # check everything. |
| 54 | # *NIX OS usually sets the HOME env variable. It is usually '/home/user', |
| 55 | # but can also be '/root'. If it's just '/' we are most likely in an init |
| 56 | # script. |
| 57 | # Windows usually sets HOMEDRIVE and HOMEPATH. If they don't exist we are |
| 58 | # probably being run from a task scheduler context. HOMEPATH can be '\' |
| 59 | # when a user has a network mapped home directory. |
| 60 | # Cygwin has it all! Both Windows and Linux. Checking both is perfect. |
| 61 | home = os.getenv('HOME') |
| 62 | homepath = os.getenv('HOMEPATH') |
| 63 | if not homepath and (not home or home == '/'): |
| 64 | return False |
| 65 | return True |
| 66 | |
| 67 | |
| 68 | def More(contents, out, prompt=None, check_pager=True): |