The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import os |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 18 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | import sys |
| 20 | import subprocess |
| 21 | import tempfile |
| 22 | |
| 23 | from error import EditorError |
| 24 | |
| 25 | class Editor(object): |
| 26 | """Manages the user's preferred text editor.""" |
| 27 | |
| 28 | _editor = None |
| 29 | globalConfig = None |
| 30 | |
| 31 | @classmethod |
| 32 | def _GetEditor(cls): |
| 33 | if cls._editor is None: |
| 34 | cls._editor = cls._SelectEditor() |
| 35 | return cls._editor |
| 36 | |
| 37 | @classmethod |
| 38 | def _SelectEditor(cls): |
| 39 | e = os.getenv('GIT_EDITOR') |
| 40 | if e: |
| 41 | return e |
| 42 | |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 43 | if cls.globalConfig: |
| 44 | e = cls.globalConfig.GetString('core.editor') |
| 45 | if e: |
| 46 | return e |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | |
| 48 | e = os.getenv('VISUAL') |
| 49 | if e: |
| 50 | return e |
| 51 | |
| 52 | e = os.getenv('EDITOR') |
| 53 | if e: |
| 54 | return e |
| 55 | |
| 56 | if os.getenv('TERM') == 'dumb': |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 57 | print( |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 58 | """No editor specified in GIT_EDITOR, core.editor, VISUAL or EDITOR. |
| 59 | Tried to fall back to vi but terminal is dumb. Please configure at |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 60 | least one of these before using this command.""", file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | sys.exit(1) |
| 62 | |
| 63 | return 'vi' |
| 64 | |
| 65 | @classmethod |
| 66 | def EditString(cls, data): |
| 67 | """Opens an editor to edit the given content. |
| 68 | |
| 69 | Args: |
| 70 | data : the text to edit |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 71 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | Returns: |
| 73 | new value of edited text; None if editing did not succeed |
| 74 | """ |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 75 | editor = cls._GetEditor() |
| 76 | if editor == ':': |
| 77 | return data |
| 78 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | fd, path = tempfile.mkstemp() |
| 80 | try: |
| 81 | os.write(fd, data) |
| 82 | os.close(fd) |
| 83 | fd = None |
| 84 | |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 85 | if re.compile("^.*[$ \t'].*$").match(editor): |
Patrick Dubroy | b715b14 | 2010-07-29 17:10:47 -0700 | [diff] [blame] | 86 | args = [editor + ' "$@"', 'sh'] |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 87 | shell = True |
| 88 | else: |
| 89 | args = [editor] |
| 90 | shell = False |
| 91 | args.append(path) |
| 92 | |
Shawn O. Pearce | 54fccd7 | 2009-06-24 07:09:51 -0700 | [diff] [blame] | 93 | try: |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 94 | rc = subprocess.Popen(args, shell=shell).wait() |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 95 | except OSError as e: |
Shawn O. Pearce | 54fccd7 | 2009-06-24 07:09:51 -0700 | [diff] [blame] | 96 | raise EditorError('editor failed, %s: %s %s' |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 97 | % (str(e), editor, path)) |
Shawn O. Pearce | 54fccd7 | 2009-06-24 07:09:51 -0700 | [diff] [blame] | 98 | if rc != 0: |
| 99 | raise EditorError('editor failed with exit status %d: %s %s' |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 100 | % (rc, editor, path)) |
Shawn O. Pearce | 54fccd7 | 2009-06-24 07:09:51 -0700 | [diff] [blame] | 101 | |
Shawn O. Pearce | a8421a1 | 2009-04-18 16:57:46 -0700 | [diff] [blame] | 102 | fd2 = open(path) |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 103 | try: |
Shawn O. Pearce | a8421a1 | 2009-04-18 16:57:46 -0700 | [diff] [blame] | 104 | return fd2.read() |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 105 | finally: |
Shawn O. Pearce | a8421a1 | 2009-04-18 16:57:46 -0700 | [diff] [blame] | 106 | fd2.close() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | finally: |
| 108 | if fd: |
| 109 | os.close(fd) |
| 110 | os.remove(path) |