From f4a53001678197c80614bb7728a8e1fd31dc63ac Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Fri, 21 Jul 2017 22:16:58 +0300 Subject: [PATCH 1/2] Support command line options in EDITOR/GIT_EDITOR On Windows users may like to configure command line options for the editor[1]. Support this configuration. Fixes #162. [1] https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup --- blurb/blurb.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/blurb/blurb.py b/blurb/blurb.py index 98a0666..25ed67f 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -53,6 +53,7 @@ import math import os import re +import shlex import shutil import subprocess import sys @@ -806,11 +807,12 @@ def add(): Add a blurb (a Misc/NEWS entry) to the current CPython repo. """ - editor = find_editor() + editor_command = shlex.split(find_editor(), posix=os.name == 'posix') handle, tmp_path = tempfile.mkstemp(".rst") os.close(handle) atexit.register(lambda : os.unlink(tmp_path)) + editor_command.append(tmp_path) def init_tmp_with_template(): with open(tmp_path, "wt", encoding="utf-8") as f: @@ -832,7 +834,7 @@ def init_tmp_with_template(): init_tmp_with_template() while True: - subprocess.run([editor, tmp_path]) + subprocess.run(editor_command) failure = None blurb = Blurbs() From 9fe661aeadb26c0626f83c651f5a278fb5579e9a Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Sat, 22 Jul 2017 06:36:55 +0300 Subject: [PATCH 2/2] Support both unquoted path and path with options EDITOR environment variable value may be: - "'quoted path' -with -options" - "unquoted path" This previous patch supported only the first; this patch adds support to for both options. If an environment value is set, but the value cannot be resolved, the user has to fix the configuration. There are two possible failures: The environment variable value does not resolve to a file: Error: Invalid 'EDITOR' environment variable "foo bar": Cannot find editor. Splitting the environment variable failed: Error: Invalid 'EDITOR' environment variable "foo bar' -with -options": No closing quotation. --- blurb/blurb.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/blurb/blurb.py b/blurb/blurb.py index 25ed67f..2cd95fa 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -782,11 +782,29 @@ def test(*args): print(tests_run, "tests passed.") +def resolve_command(s): + # "unquoted path to editor"? + if os.path.exists(s): + return [s] + # "'quoted path to editor' -with -options"? + args = shlex.split(s, posix=os.name == 'posix') + if os.path.exists(args[0]): + return args + raise ValueError("Cannot find editor") + + def find_editor(): + # Try relevant environment variables; if set, the value must be valid. for var in 'GIT_EDITOR', 'EDITOR': - editor = os.environ.get(var) - if editor is not None: - return editor + value = os.environ.get(var) + if value is None: + continue + try: + return resolve_command(value) + except ValueError as e: + error('Invalid %r environment variable %r: %s.' % (var, value, e)) + + # If not configured, fallback to platform specific user friendly editor. if sys.platform == 'win32': fallbacks = ['notepad.exe'] else: @@ -797,7 +815,7 @@ def find_editor(): else: found_path = shutil.which(fallback) if found_path and os.path.exists(found_path): - return found_path + return [found_path] error('Could not find an editor! Set the EDITOR environment variable.') @@ -807,7 +825,7 @@ def add(): Add a blurb (a Misc/NEWS entry) to the current CPython repo. """ - editor_command = shlex.split(find_editor(), posix=os.name == 'posix') + editor_command = find_editor() handle, tmp_path = tempfile.mkstemp(".rst") os.close(handle)