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