pager.py 3.0 KB

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