diff options
Diffstat (limited to 'pager.py')
| -rw-r--r-- | pager.py | 16 |
1 files changed, 10 insertions, 6 deletions
| @@ -1,5 +1,3 @@ | |||
| 1 | # -*- coding:utf-8 -*- | ||
| 2 | # | ||
| 3 | # Copyright (C) 2008 The Android Open Source Project | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # | 2 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| @@ -14,7 +12,6 @@ | |||
| 14 | # See the License for the specific language governing permissions and | 12 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. | 13 | # limitations under the License. |
| 16 | 14 | ||
| 17 | from __future__ import print_function | ||
| 18 | import os | 15 | import os |
| 19 | import select | 16 | import select |
| 20 | import subprocess | 17 | import subprocess |
| @@ -27,6 +24,7 @@ pager_process = None | |||
| 27 | old_stdout = None | 24 | old_stdout = None |
| 28 | old_stderr = None | 25 | old_stderr = None |
| 29 | 26 | ||
| 27 | |||
| 30 | def RunPager(globalConfig): | 28 | def RunPager(globalConfig): |
| 31 | if not os.isatty(0) or not os.isatty(1): | 29 | if not os.isatty(0) or not os.isatty(1): |
| 32 | return | 30 | return |
| @@ -35,33 +33,37 @@ def RunPager(globalConfig): | |||
| 35 | return | 33 | return |
| 36 | 34 | ||
| 37 | if platform_utils.isWindows(): | 35 | if platform_utils.isWindows(): |
| 38 | _PipePager(pager); | 36 | _PipePager(pager) |
| 39 | else: | 37 | else: |
| 40 | _ForkPager(pager) | 38 | _ForkPager(pager) |
| 41 | 39 | ||
| 40 | |||
| 42 | def TerminatePager(): | 41 | def TerminatePager(): |
| 43 | global pager_process, old_stdout, old_stderr | 42 | global pager_process, old_stdout, old_stderr |
| 44 | if pager_process: | 43 | if pager_process: |
| 45 | sys.stdout.flush() | 44 | sys.stdout.flush() |
| 46 | sys.stderr.flush() | 45 | sys.stderr.flush() |
| 47 | pager_process.stdin.close() | 46 | pager_process.stdin.close() |
| 48 | pager_process.wait(); | 47 | pager_process.wait() |
| 49 | pager_process = None | 48 | pager_process = None |
| 50 | # Restore initial stdout/err in case there is more output in this process | 49 | # Restore initial stdout/err in case there is more output in this process |
| 51 | # after shutting down the pager process | 50 | # after shutting down the pager process |
| 52 | sys.stdout = old_stdout | 51 | sys.stdout = old_stdout |
| 53 | sys.stderr = old_stderr | 52 | sys.stderr = old_stderr |
| 54 | 53 | ||
| 54 | |||
| 55 | def _PipePager(pager): | 55 | def _PipePager(pager): |
| 56 | global pager_process, old_stdout, old_stderr | 56 | global pager_process, old_stdout, old_stderr |
| 57 | assert pager_process is None, "Only one active pager process at a time" | 57 | assert pager_process is None, "Only one active pager process at a time" |
| 58 | # Create pager process, piping stdout/err into its stdin | 58 | # Create pager process, piping stdout/err into its stdin |
| 59 | pager_process = subprocess.Popen([pager], stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr) | 59 | pager_process = subprocess.Popen([pager], stdin=subprocess.PIPE, stdout=sys.stdout, |
| 60 | stderr=sys.stderr) | ||
| 60 | old_stdout = sys.stdout | 61 | old_stdout = sys.stdout |
| 61 | old_stderr = sys.stderr | 62 | old_stderr = sys.stderr |
| 62 | sys.stdout = pager_process.stdin | 63 | sys.stdout = pager_process.stdin |
| 63 | sys.stderr = pager_process.stdin | 64 | sys.stderr = pager_process.stdin |
| 64 | 65 | ||
| 66 | |||
| 65 | def _ForkPager(pager): | 67 | def _ForkPager(pager): |
| 66 | global active | 68 | global active |
| 67 | # This process turns into the pager; a child it forks will | 69 | # This process turns into the pager; a child it forks will |
| @@ -88,6 +90,7 @@ def _ForkPager(pager): | |||
| 88 | print("fatal: cannot start pager '%s'" % pager, file=sys.stderr) | 90 | print("fatal: cannot start pager '%s'" % pager, file=sys.stderr) |
| 89 | sys.exit(255) | 91 | sys.exit(255) |
| 90 | 92 | ||
| 93 | |||
| 91 | def _SelectPager(globalConfig): | 94 | def _SelectPager(globalConfig): |
| 92 | try: | 95 | try: |
| 93 | return os.environ['GIT_PAGER'] | 96 | return os.environ['GIT_PAGER'] |
| @@ -105,6 +108,7 @@ def _SelectPager(globalConfig): | |||
| 105 | 108 | ||
| 106 | return 'less' | 109 | return 'less' |
| 107 | 110 | ||
| 111 | |||
| 108 | def _BecomePager(pager): | 112 | def _BecomePager(pager): |
| 109 | # Delaying execution of the pager until we have output | 113 | # Delaying execution of the pager until we have output |
| 110 | # ready works around a long-standing bug in popularly | 114 | # ready works around a long-standing bug in popularly |
