From f066f6bf2027a3e83d1e2eef0d88af74b4dc2ada Mon Sep 17 00:00:00 2001 From: Morten Enemark Lund Date: Sun, 3 Dec 2017 21:34:04 +0100 Subject: [PATCH 1/5] Ensure free_cwd is only active on pure windows --- xontrib/free_cwd.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/xontrib/free_cwd.py b/xontrib/free_cwd.py index 86fec0429..77a0e2e8f 100644 --- a/xontrib/free_cwd.py +++ b/xontrib/free_cwd.py @@ -11,6 +11,7 @@ import os import builtins import functools from xonsh.tools import print_exception +from xonsh.platform import ON_WINDOWS, ON_CYGWIN def _chdir_up(path): @@ -88,7 +89,8 @@ def _cwd_restore_wrapper(func): @events.on_ptk_create def setup_release_cwd_hook(prompter, history, completer, bindings, **kw): - prompter.prompt = _cwd_release_wrapper(prompter.prompt) - if completer.completer: - # Temporarily restore cwd for callbacks to the completer - completer.completer.complete = _cwd_restore_wrapper(completer.completer.complete) + if ON_WINDOWS and not ON_CYGWIN: + prompter.prompt = _cwd_release_wrapper(prompter.prompt) + if completer.completer: + # Temporarily restore cwd for callbacks to the completer + completer.completer.complete = _cwd_restore_wrapper(completer.completer.complete) From 1e10c8ca53059ce8a3b11678f26503b25fbbeec0 Mon Sep 17 00:00:00 2001 From: Morten Enemark Lund Date: Sun, 3 Dec 2017 21:34:51 +0100 Subject: [PATCH 2/5] Fix doc typos --- xontrib/free_cwd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xontrib/free_cwd.py b/xontrib/free_cwd.py index 77a0e2e8f..374d0a006 100644 --- a/xontrib/free_cwd.py +++ b/xontrib/free_cwd.py @@ -1,5 +1,5 @@ """ This will release the lock on the current directory whenever the - prompt is shown. Enabling this will allow the other programs or + prompt is shown. Enabling this will allow other programs or Windows Explorer to delete or rename the current or parent directories. Internally, it is accomplished by temporarily resetting CWD to the root drive folder while waiting at the prompt. This only @@ -30,7 +30,7 @@ def _chdir_up(path): def _cwd_release_wrapper(func): - """ Decorator for Windows to the wrap the prompt function and release + """ Decorator for Windows to wrap the prompt function and release the process lock on the current directory while the prompt is displayed. This works by temporarily setting the workdir to the users home directory. From 93a72c0b859d367ef91bd3f13bdd5679c8739138 Mon Sep 17 00:00:00 2001 From: Morten Enemark Lund Date: Sun, 3 Dec 2017 21:35:32 +0100 Subject: [PATCH 3/5] Use pathlib for path manipulations --- xontrib/free_cwd.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/xontrib/free_cwd.py b/xontrib/free_cwd.py index 374d0a006..4ced51cca 100644 --- a/xontrib/free_cwd.py +++ b/xontrib/free_cwd.py @@ -10,6 +10,8 @@ import os import builtins import functools +from pathlib import Path + from xonsh.tools import print_exception from xonsh.platform import ON_WINDOWS, ON_CYGWIN @@ -18,15 +20,13 @@ def _chdir_up(path): """ Change directory to path or if path does not exist the first valid parent. """ + path = Path(path) try: os.chdir(path) return path except (FileNotFoundError, NotADirectoryError): - parent = os.path.dirname(path) - if parent != path: - return _chdir_up(parent) - else: - raise + path.resolve() + return _chdir_up(path.parent) def _cwd_release_wrapper(func): @@ -45,13 +45,13 @@ def _cwd_release_wrapper(func): else: @functools.wraps(func) def wrapper(*args, **kwargs): - rootdir = os.path.splitdrive(os.getcwd())[0] + '\\' - os.chdir(rootdir) + anchor = Path(os.getcwd()).anchor + os.chdir(anchor) try: out = func(*args, **kwargs) finally: try: - pwd = env.get('PWD', rootdir) + pwd = env.get('PWD', anchor) os.chdir(pwd) except (FileNotFoundError, NotADirectoryError): print_exception() From c93082a53abaf83226578c8cb2fd6204a538d878 Mon Sep 17 00:00:00 2001 From: Morten Enemark Lund Date: Sun, 3 Dec 2017 21:39:50 +0100 Subject: [PATCH 4/5] Add changelog --- news/free_cwd_only_on_win.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 news/free_cwd_only_on_win.rst diff --git a/news/free_cwd_only_on_win.rst b/news/free_cwd_only_on_win.rst new file mode 100644 index 000000000..e125b3b83 --- /dev/null +++ b/news/free_cwd_only_on_win.rst @@ -0,0 +1,13 @@ +**Added:** None + +**Changed:** None + +**Deprecated:** None + +**Removed:** None + +**Fixed:** + +* Ensure that the ``free_cwd`` contrib can only be active on pure Windows. + +**Security:** None From dccb41ae52e9993b7f37ff15c8196b2cdc6c5396 Mon Sep 17 00:00:00 2001 From: Morten Enemark Lund Date: Sun, 3 Dec 2017 21:46:04 +0100 Subject: [PATCH 5/5] Make it clear that ``free_cwd`` is a Win only xontrib --- xonsh/xontribs.json | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/xonsh/xontribs.json b/xonsh/xontribs.json index 7c1aa7551..5f0af2fb1 100644 --- a/xonsh/xontribs.json +++ b/xonsh/xontribs.json @@ -81,18 +81,18 @@ "description": ["Adds return code info to the prompt"] }, {"name": "free_cwd", - "package": "xonsh", - "url": "http://xon.sh", - "description": [ - "Release the lock on the current directory whenever the", - "prompt is shown. Enabling this will allow the other programs or", - "Windows Explorer to delete or rename the current or parent", - "directories. Internally, it is accomplished by temporarily resetting", - "CWD to the root drive folder while waiting at the prompt. This only", - "works with the prompt_toolkit backend and can cause cause issues", - "if any extensions are enabled that hook the prompt and relies on", - "``os.getcwd()``"] - }, + "package": "xonsh", + "url": "http://xon.sh", + "description": [ + "Windows only xontrib, to release the lock on the current directory", + "whenever the prompt is shown. Enabling this will allow the other", + "programs or Windows Explorer to delete or rename the current or parent", + "directories. Internally, it is accomplished by temporarily resetting", + "CWD to the root drive folder while waiting at the prompt. This only", + "works with the prompt_toolkit backend and can cause cause issues", + "if any extensions are enabled that hook the prompt and relies on", + "``os.getcwd()``"] + }, {"name": "whole_word_jumping", "package": "xonsh", "url": "http://xon.sh",