summaryrefslogtreecommitdiffstats
path: root/pager.py
diff options
context:
space:
mode:
authorRenaud Paquay <rpaquay@google.com>2016-11-01 15:51:59 -0700
committerRenaud Paquay <rpaquay@google.com>2017-08-31 13:49:26 -0700
commite8595e9df7980b0b7d9111de43d294c4439d474c (patch)
tree1a7034b7854eb47965b8112ccaf3b33687cac583 /pager.py
parent227ad2ef42f47798d24814dfc2cef8119c313ab7 (diff)
downloadgit-repo-e8595e9df7980b0b7d9111de43d294c4439d474c.tar.gz
Support pager on Windows
Windows does not support pipe|fork, but we can simulate by creating the pager as a child process, redirecting stdout/in/err appropriately and then waiting for the child process to terminate after we are done executing the repo command. Change-Id: I5dd2bdeb4095e4d93bc678802e53c6d4eda0235b
Diffstat (limited to 'pager.py')
-rwxr-xr-xpager.py38
1 files changed, 36 insertions, 2 deletions
diff --git a/pager.py b/pager.py
index c6211419..0521c0c7 100755
--- a/pager.py
+++ b/pager.py
@@ -16,19 +16,53 @@
16from __future__ import print_function 16from __future__ import print_function
17import os 17import os
18import select 18import select
19import subprocess
19import sys 20import sys
20 21
22import platform_utils
23
21active = False 24active = False
25pager_process = None
26old_stdout = None
27old_stderr = None
22 28
23def RunPager(globalConfig): 29def RunPager(globalConfig):
24 global active
25
26 if not os.isatty(0) or not os.isatty(1): 30 if not os.isatty(0) or not os.isatty(1):
27 return 31 return
28 pager = _SelectPager(globalConfig) 32 pager = _SelectPager(globalConfig)
29 if pager == '' or pager == 'cat': 33 if pager == '' or pager == 'cat':
30 return 34 return
31 35
36 if platform_utils.isWindows():
37 _PipePager(pager);
38 else:
39 _ForkPager(pager)
40
41def 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()
47 pager_process.wait();
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
54def _PipePager(pager):
55 global pager_process, old_stdout, old_stderr
56 assert pager_process is None, "Only one active pager process at a time"
57 # Create pager process, piping stdout/err into its stdin
58 pager_process = subprocess.Popen([pager], stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr)
59 old_stdout = sys.stdout
60 old_stderr = sys.stderr
61 sys.stdout = pager_process.stdin
62 sys.stderr = pager_process.stdin
63
64def _ForkPager(pager):
65 global active
32 # This process turns into the pager; a child it forks will 66 # This process turns into the pager; a child it forks will
33 # do the real processing and output back to the pager. This 67 # do the real processing and output back to the pager. This
34 # is necessary to keep the pager in control of the tty. 68 # is necessary to keep the pager in control of the tty.