2019-06-13 06:30:51 +00:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-10-21 14:00:00 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2012-11-02 05:59:27 +00:00
|
|
|
from __future__ import print_function
|
2008-10-21 14:00:00 +00:00
|
|
|
import os
|
2009-07-02 19:45:47 +00:00
|
|
|
import re
|
2008-10-21 14:00:00 +00:00
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from error import EditorError
|
2016-11-11 22:25:29 +00:00
|
|
|
import platform_utils
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
class Editor(object):
|
|
|
|
"""Manages the user's preferred text editor."""
|
|
|
|
|
|
|
|
_editor = None
|
|
|
|
globalConfig = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _GetEditor(cls):
|
|
|
|
if cls._editor is None:
|
|
|
|
cls._editor = cls._SelectEditor()
|
|
|
|
return cls._editor
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _SelectEditor(cls):
|
|
|
|
e = os.getenv('GIT_EDITOR')
|
|
|
|
if e:
|
|
|
|
return e
|
|
|
|
|
2009-07-02 19:45:47 +00:00
|
|
|
if cls.globalConfig:
|
|
|
|
e = cls.globalConfig.GetString('core.editor')
|
|
|
|
if e:
|
|
|
|
return e
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
e = os.getenv('VISUAL')
|
|
|
|
if e:
|
|
|
|
return e
|
|
|
|
|
|
|
|
e = os.getenv('EDITOR')
|
|
|
|
if e:
|
|
|
|
return e
|
|
|
|
|
|
|
|
if os.getenv('TERM') == 'dumb':
|
2012-11-02 05:59:27 +00:00
|
|
|
print(
|
2020-02-12 05:58:39 +00:00
|
|
|
"""No editor specified in GIT_EDITOR, core.editor, VISUAL or EDITOR.
|
2008-10-21 14:00:00 +00:00
|
|
|
Tried to fall back to vi but terminal is dumb. Please configure at
|
2012-11-02 05:59:27 +00:00
|
|
|
least one of these before using this command.""", file=sys.stderr)
|
2008-10-21 14:00:00 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
return 'vi'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def EditString(cls, data):
|
|
|
|
"""Opens an editor to edit the given content.
|
|
|
|
|
2019-11-15 06:19:03 +00:00
|
|
|
Args:
|
|
|
|
data: The text to edit.
|
2012-11-02 05:59:27 +00:00
|
|
|
|
2019-11-15 06:19:03 +00:00
|
|
|
Returns:
|
|
|
|
New value of edited text.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
EditorError: The editor failed to run.
|
2008-10-21 14:00:00 +00:00
|
|
|
"""
|
2009-07-02 19:45:47 +00:00
|
|
|
editor = cls._GetEditor()
|
|
|
|
if editor == ':':
|
|
|
|
return data
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
fd, path = tempfile.mkstemp()
|
|
|
|
try:
|
2019-11-15 06:19:03 +00:00
|
|
|
os.write(fd, data.encode('utf-8'))
|
2008-10-21 14:00:00 +00:00
|
|
|
os.close(fd)
|
|
|
|
fd = None
|
|
|
|
|
2016-11-03 22:59:05 +00:00
|
|
|
if platform_utils.isWindows():
|
|
|
|
# Split on spaces, respecting quoted strings
|
|
|
|
import shlex
|
|
|
|
args = shlex.split(editor)
|
|
|
|
shell = False
|
|
|
|
elif re.compile("^.*[$ \t'].*$").match(editor):
|
2010-07-30 00:10:47 +00:00
|
|
|
args = [editor + ' "$@"', 'sh']
|
2009-07-02 19:45:47 +00:00
|
|
|
shell = True
|
|
|
|
else:
|
|
|
|
args = [editor]
|
|
|
|
shell = False
|
|
|
|
args.append(path)
|
|
|
|
|
2009-06-24 14:09:51 +00:00
|
|
|
try:
|
2009-07-02 19:45:47 +00:00
|
|
|
rc = subprocess.Popen(args, shell=shell).wait()
|
2012-09-09 22:37:57 +00:00
|
|
|
except OSError as e:
|
2009-06-24 14:09:51 +00:00
|
|
|
raise EditorError('editor failed, %s: %s %s'
|
2020-02-12 05:58:39 +00:00
|
|
|
% (str(e), editor, path))
|
2009-06-24 14:09:51 +00:00
|
|
|
if rc != 0:
|
|
|
|
raise EditorError('editor failed with exit status %d: %s %s'
|
2020-02-12 05:58:39 +00:00
|
|
|
% (rc, editor, path))
|
2009-06-24 14:09:51 +00:00
|
|
|
|
2019-11-15 06:19:03 +00:00
|
|
|
with open(path, mode='rb') as fd2:
|
|
|
|
return fd2.read().decode('utf-8')
|
2008-10-21 14:00:00 +00:00
|
|
|
finally:
|
|
|
|
if fd:
|
|
|
|
os.close(fd)
|
2016-11-11 22:25:29 +00:00
|
|
|
platform_utils.remove(path)
|