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