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