xonsh/xontrib/free_cwd.py

97 lines
3.3 KiB
Python
Raw Normal View History

2017-03-09 22:23:44 +01:00
""" This will release the lock on the current directory whenever the
2017-12-03 21:34:51 +01:00
prompt is shown. Enabling this will allow other programs or
2017-03-09 22:23:44 +01:00
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()``.
"""
import os
import builtins
import functools
2017-12-03 21:35:32 +01:00
from pathlib import Path
2017-03-09 22:32:58 +01:00
from xonsh.tools import print_exception
2017-12-31 16:32:19 +01:00
from xonsh.platform import ON_WINDOWS, ON_CYGWIN, ON_MSYS
2017-03-09 22:23:44 +01:00
2017-03-10 08:20:16 +01:00
2017-03-09 22:23:44 +01:00
def _chdir_up(path):
""" Change directory to path or if path does not exist
the first valid parent.
"""
2017-12-03 21:35:32 +01:00
path = Path(path)
2017-03-09 22:23:44 +01:00
try:
os.chdir(path)
return str(path.absolute())
2017-03-09 23:02:26 +01:00
except (FileNotFoundError, NotADirectoryError):
2017-12-03 21:35:32 +01:00
path.resolve()
return _chdir_up(path.parent)
2017-03-09 22:23:44 +01:00
2017-03-10 08:20:16 +01:00
2017-03-09 22:23:44 +01:00
def _cwd_release_wrapper(func):
2017-12-03 21:34:51 +01:00
""" Decorator for Windows to wrap the prompt function and release
2017-03-09 22:23:44 +01:00
the process lock on the current directory while the prompt is
displayed. This works by temporarily setting
the workdir to the users home directory.
2017-03-09 22:23:44 +01:00
"""
2018-09-13 17:08:01 -04:00
env = builtins.__xonsh__.env
2017-03-09 22:23:44 +01:00
if env.get('UPDATE_PROMPT_ON_KEYPRESS'):
return func if not hasattr(func, '_orgfunc') else func._orgfunc
if hasattr(func, '_orgfunc'):
# Already wrapped
return func
else:
@functools.wraps(func)
def wrapper(*args, **kwargs):
2017-12-03 21:35:32 +01:00
anchor = Path(os.getcwd()).anchor
os.chdir(anchor)
2017-03-09 22:23:44 +01:00
try:
out = func(*args, **kwargs)
finally:
try:
2017-12-03 21:35:32 +01:00
pwd = env.get('PWD', anchor)
2017-03-09 22:23:44 +01:00
os.chdir(pwd)
2017-03-09 23:02:26 +01:00
except (FileNotFoundError, NotADirectoryError):
2017-03-09 22:23:44 +01:00
print_exception()
2017-03-09 23:02:26 +01:00
newpath = _chdir_up(pwd)
2018-09-13 17:08:01 -04:00
builtins.__xonsh__.env['PWD'] = newpath
2017-03-09 23:02:26 +01:00
raise KeyboardInterrupt
2017-03-09 22:23:44 +01:00
return out
wrapper._orgfunc = func
return wrapper
def _cwd_restore_wrapper(func):
""" Decorator for Windows which will temporary restore the true working
directory. Designed to wrap completer callbacks from the
prompt_toolkit or readline.
"""
2018-09-13 17:08:01 -04:00
env = builtins.__xonsh__.env
2017-03-09 22:23:44 +01:00
if env.get('UPDATE_PROMPT_ON_KEYPRESS'):
return func if not hasattr(func, '_orgfunc') else func._orgfunc
if hasattr(func, '_orgfunc'):
# Already wrapped
return func
else:
@functools.wraps(func)
def wrapper(*args, **kwargs):
workdir = os.getcwd()
_chdir_up(env.get('PWD', workdir))
out = func(*args, **kwargs)
_chdir_up(workdir)
return out
wrapper._orgfunc = func
return wrapper
@events.on_ptk_create
def setup_release_cwd_hook(prompter, history, completer, bindings, **kw):
2017-12-31 16:32:19 +01:00
if ON_WINDOWS and not ON_CYGWIN and not ON_MSYS:
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)