Merge pull request #2549 from xonsh/free_cwd_on_win

Free cwd on win
This commit is contained in:
Anthony Scopatz 2017-12-03 22:40:55 -05:00 committed by GitHub
commit c4e2ec5815
Failed to generate hash of commit
3 changed files with 41 additions and 26 deletions

View file

@ -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

View file

@ -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",

View file

@ -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
@ -10,26 +10,27 @@
import os
import builtins
import functools
from pathlib import Path
from xonsh.tools import print_exception
from xonsh.platform import ON_WINDOWS, ON_CYGWIN
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):
""" 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.
@ -44,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()
@ -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)