The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 16 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import subprocess |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 18 | import sys |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | import tempfile |
| 20 | |
| 21 | from error import EditorError |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 22 | import platform_utils |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 24 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 25 | class Editor: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 26 | """Manages the user's preferred text editor.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 28 | _editor = None |
| 29 | globalConfig = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 31 | @classmethod |
| 32 | def _GetEditor(cls): |
| 33 | if cls._editor is None: |
| 34 | cls._editor = cls._SelectEditor() |
| 35 | return cls._editor |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 37 | @classmethod |
| 38 | def _SelectEditor(cls): |
| 39 | e = os.getenv("GIT_EDITOR") |
| 40 | if e: |
| 41 | return e |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [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 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 48 | e = os.getenv("VISUAL") |
| 49 | if e: |
| 50 | return e |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 52 | e = os.getenv("EDITOR") |
| 53 | if e: |
| 54 | return e |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 55 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 56 | if os.getenv("TERM") == "dumb": |
| 57 | print( |
| 58 | """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] | 59 | Tried to fall back to vi but terminal is dumb. Please configure at |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 60 | least one of these before using this command.""", # noqa: E501 |
| 61 | file=sys.stderr, |
| 62 | ) |
| 63 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 65 | return "vi" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 67 | @classmethod |
| 68 | def EditString(cls, data): |
| 69 | """Opens an editor to edit the given content. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 71 | Args: |
| 72 | data: The text to edit. |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 73 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 74 | Returns: |
| 75 | New value of edited text. |
Mike Frysinger | 70c54dc | 2019-11-15 01:19:03 -0500 | [diff] [blame] | 76 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 77 | Raises: |
| 78 | EditorError: The editor failed to run. |
| 79 | """ |
| 80 | editor = cls._GetEditor() |
| 81 | if editor == ":": |
| 82 | return data |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 83 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 84 | fd, path = tempfile.mkstemp() |
| 85 | try: |
| 86 | os.write(fd, data.encode("utf-8")) |
| 87 | os.close(fd) |
| 88 | fd = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 90 | if platform_utils.isWindows(): |
| 91 | # Split on spaces, respecting quoted strings |
| 92 | import shlex |
Shawn O. Pearce | 1dcb58a | 2009-07-02 12:45:47 -0700 | [diff] [blame] | 93 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 94 | args = shlex.split(editor) |
| 95 | shell = False |
| 96 | elif re.compile("^.*[$ \t'].*$").match(editor): |
| 97 | args = [editor + ' "$@"', "sh"] |
| 98 | shell = True |
| 99 | else: |
| 100 | args = [editor] |
| 101 | shell = False |
| 102 | args.append(path) |
Shawn O. Pearce | 54fccd7 | 2009-06-24 07:09:51 -0700 | [diff] [blame] | 103 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 104 | try: |
| 105 | rc = subprocess.Popen(args, shell=shell).wait() |
| 106 | except OSError as e: |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 107 | raise EditorError(f"editor failed, {str(e)}: {editor} {path}") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 108 | if rc != 0: |
| 109 | raise EditorError( |
| 110 | "editor failed with exit status %d: %s %s" |
| 111 | % (rc, editor, path) |
| 112 | ) |
| 113 | |
| 114 | with open(path, mode="rb") as fd2: |
| 115 | return fd2.read().decode("utf-8") |
| 116 | finally: |
| 117 | if fd: |
| 118 | os.close(fd) |
| 119 | platform_utils.remove(path) |