editor.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import print_function
  16. import os
  17. import re
  18. import sys
  19. import subprocess
  20. import tempfile
  21. from error import EditorError
  22. class Editor(object):
  23. """Manages the user's preferred text editor."""
  24. _editor = None
  25. globalConfig = None
  26. @classmethod
  27. def _GetEditor(cls):
  28. if cls._editor is None:
  29. cls._editor = cls._SelectEditor()
  30. return cls._editor
  31. @classmethod
  32. def _SelectEditor(cls):
  33. e = os.getenv('GIT_EDITOR')
  34. if e:
  35. return e
  36. if cls.globalConfig:
  37. e = cls.globalConfig.GetString('core.editor')
  38. if e:
  39. return e
  40. e = os.getenv('VISUAL')
  41. if e:
  42. return e
  43. e = os.getenv('EDITOR')
  44. if e:
  45. return e
  46. if os.getenv('TERM') == 'dumb':
  47. print(
  48. """No editor specified in GIT_EDITOR, core.editor, VISUAL or EDITOR.
  49. Tried to fall back to vi but terminal is dumb. Please configure at
  50. least one of these before using this command.""", file=sys.stderr)
  51. sys.exit(1)
  52. return 'vi'
  53. @classmethod
  54. def EditString(cls, data):
  55. """Opens an editor to edit the given content.
  56. Args:
  57. data : the text to edit
  58. Returns:
  59. new value of edited text; None if editing did not succeed
  60. """
  61. editor = cls._GetEditor()
  62. if editor == ':':
  63. return data
  64. fd, path = tempfile.mkstemp()
  65. try:
  66. os.write(fd, data)
  67. os.close(fd)
  68. fd = None
  69. if re.compile("^.*[$ \t'].*$").match(editor):
  70. args = [editor + ' "$@"', 'sh']
  71. shell = True
  72. else:
  73. args = [editor]
  74. shell = False
  75. args.append(path)
  76. try:
  77. rc = subprocess.Popen(args, shell=shell).wait()
  78. except OSError as e:
  79. raise EditorError('editor failed, %s: %s %s'
  80. % (str(e), editor, path))
  81. if rc != 0:
  82. raise EditorError('editor failed with exit status %d: %s %s'
  83. % (rc, editor, path))
  84. fd2 = open(path)
  85. try:
  86. return fd2.read()
  87. finally:
  88. fd2.close()
  89. finally:
  90. if fd:
  91. os.close(fd)
  92. os.remove(path)