The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os |
| 16 | import select |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 17 | import subprocess |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import sys |
| 19 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 20 | import platform_utils |
| 21 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | active = False |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 23 | pager_process = None |
| 24 | old_stdout = None |
| 25 | old_stderr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 27 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | def RunPager(globalConfig): |
Shawn O. Pearce | 8f82a4f | 2009-04-01 07:24:22 -0700 | [diff] [blame] | 29 | if not os.isatty(0) or not os.isatty(1): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | return |
| 31 | pager = _SelectPager(globalConfig) |
| 32 | if pager == '' or pager == 'cat': |
| 33 | return |
| 34 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 35 | if platform_utils.isWindows(): |
David Pursehouse | 03ae992 | 2020-02-12 14:14:57 +0900 | [diff] [blame] | 36 | _PipePager(pager) |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 37 | else: |
| 38 | _ForkPager(pager) |
| 39 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 40 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 41 | def TerminatePager(): |
| 42 | global pager_process, old_stdout, old_stderr |
| 43 | if pager_process: |
| 44 | sys.stdout.flush() |
| 45 | sys.stderr.flush() |
| 46 | pager_process.stdin.close() |
David Pursehouse | 03ae992 | 2020-02-12 14:14:57 +0900 | [diff] [blame] | 47 | pager_process.wait() |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 48 | pager_process = None |
| 49 | # Restore initial stdout/err in case there is more output in this process |
| 50 | # after shutting down the pager process |
| 51 | sys.stdout = old_stdout |
| 52 | sys.stderr = old_stderr |
| 53 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 54 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 55 | def _PipePager(pager): |
| 56 | global pager_process, old_stdout, old_stderr |
| 57 | assert pager_process is None, "Only one active pager process at a time" |
| 58 | # Create pager process, piping stdout/err into its stdin |
David Pursehouse | 3cda50a | 2020-02-13 13:17:03 +0900 | [diff] [blame] | 59 | pager_process = subprocess.Popen([pager], stdin=subprocess.PIPE, stdout=sys.stdout, |
| 60 | stderr=sys.stderr) |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 61 | old_stdout = sys.stdout |
| 62 | old_stderr = sys.stderr |
| 63 | sys.stdout = pager_process.stdin |
| 64 | sys.stderr = pager_process.stdin |
| 65 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 66 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 67 | def _ForkPager(pager): |
| 68 | global active |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 69 | # This process turns into the pager; a child it forks will |
| 70 | # do the real processing and output back to the pager. This |
| 71 | # is necessary to keep the pager in control of the tty. |
| 72 | # |
| 73 | try: |
| 74 | r, w = os.pipe() |
| 75 | pid = os.fork() |
| 76 | if not pid: |
| 77 | os.dup2(w, 1) |
| 78 | os.dup2(w, 2) |
| 79 | os.close(r) |
| 80 | os.close(w) |
| 81 | active = True |
| 82 | return |
| 83 | |
| 84 | os.dup2(r, 0) |
| 85 | os.close(r) |
| 86 | os.close(w) |
| 87 | |
| 88 | _BecomePager(pager) |
| 89 | except Exception: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 90 | print("fatal: cannot start pager '%s'" % pager, file=sys.stderr) |
David Pursehouse | 01f443d | 2012-10-03 19:11:28 +0900 | [diff] [blame] | 91 | sys.exit(255) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 92 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 93 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 94 | def _SelectPager(globalConfig): |
| 95 | try: |
| 96 | return os.environ['GIT_PAGER'] |
| 97 | except KeyError: |
| 98 | pass |
| 99 | |
| 100 | pager = globalConfig.GetString('core.pager') |
| 101 | if pager: |
| 102 | return pager |
| 103 | |
| 104 | try: |
| 105 | return os.environ['PAGER'] |
| 106 | except KeyError: |
| 107 | pass |
| 108 | |
| 109 | return 'less' |
| 110 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 111 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | def _BecomePager(pager): |
| 113 | # Delaying execution of the pager until we have output |
| 114 | # ready works around a long-standing bug in popularly |
| 115 | # available versions of 'less', a better 'more'. |
| 116 | # |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 117 | _a, _b, _c = select.select([0], [], [0]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | |
| 119 | os.environ['LESS'] = 'FRSX' |
| 120 | |
| 121 | try: |
| 122 | os.execvp(pager, [pager]) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 123 | except OSError: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | os.execv('/bin/sh', ['sh', '-c', pager]) |