mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 00:14:41 +01:00
Make xonsh tolerant to inaccessible paths: history backend, script cache (#5430)
Closes #5319 ### Before ```xsh mkdir -p /tmp/noaccess chmod 000 /tmp/noaccess echo 'print(1)' > /tmp/1.xsh xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess' # Json History error xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess' /tmp/1.xsh # Script cache error ``` ### After ```xsh mkdir -p /tmp/noaccess chmod 000 /tmp/noaccess echo 'print(1)' > /tmp/1.xsh xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess' # Error during load <class 'xonsh.history.json.JsonHistory'>: [Errno 13] Permission denied: '/tmp/noaccess/history_json' # Set $XONSH_HISTORY_BACKEND='dummy' to disable history. # History disabled. # @ xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess /tmp/1.xsh # xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True # update_cache: Cache file is not writable: /tmp/noaccess/xonsh_script_cache/private/tmp/1_.xsh.cpython-311 # Set $XONSH_CACHE_SCRIPTS=0, $XONSH_CACHE_EVERYTHING=0 to disable cache. # 1 ``` And no warnings when everything is switched off: ```xsh xonsh % python -m xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess -DXONSH_HISTORY_BACKEND=dummy # @ xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess -DXONSH_CACHE_SCRIPTS=0 -DXONSH_CACHE_EVERYTHING=0 /tmp/1.xsh # 1 ``` ## For community ⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍 comment** --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
e03bda413a
commit
9073a4c33c
3 changed files with 44 additions and 2 deletions
24
news/fix_noaccess.rst
Normal file
24
news/fix_noaccess.rst
Normal file
|
@ -0,0 +1,24 @@
|
|||
**Added:**
|
||||
|
||||
* Added catching an exceptions during load a history backend to avoid shell exiting e.g. on permission error.
|
||||
* Added catching an exception when cache file is not writable.
|
||||
|
||||
**Changed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Deprecated:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Removed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Fixed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Security:**
|
||||
|
||||
* <news item>
|
|
@ -9,6 +9,7 @@ from xonsh import __version__ as XONSH_VERSION
|
|||
from xonsh.built_ins import XSH
|
||||
from xonsh.lazyasd import lazyobject
|
||||
from xonsh.platform import PYTHON_VERSION_INFO_BYTES
|
||||
from xonsh.tools import is_writable_file, print_warning
|
||||
|
||||
|
||||
def _splitpath(path, sofar=()):
|
||||
|
@ -94,6 +95,13 @@ def update_cache(ccode, cache_file_name):
|
|||
represented by ``ccode``.
|
||||
"""
|
||||
if cache_file_name is not None:
|
||||
if not is_writable_file(cache_file_name):
|
||||
if XSH.env.get("XONSH_DEBUG", "False"):
|
||||
print_warning(
|
||||
f"update_cache: Cache file is not writable: {cache_file_name}\n"
|
||||
f"Set $XONSH_CACHE_SCRIPTS=0, $XONSH_CACHE_EVERYTHING=0 to disable cache."
|
||||
)
|
||||
return
|
||||
os.makedirs(os.path.dirname(cache_file_name), exist_ok=True)
|
||||
with open(cache_file_name, "wb") as cfile:
|
||||
cfile.write(XONSH_VERSION.encode() + b"\n")
|
||||
|
@ -166,7 +174,8 @@ def run_script_with_cache(filename, execer, glb=None, loc=None, mode="exec"):
|
|||
with open(filename, encoding="utf-8") as f:
|
||||
code = f.read()
|
||||
ccode = compile_code(filename, code, execer, glb, loc, mode)
|
||||
update_cache(ccode, cachefname)
|
||||
if use_cache:
|
||||
update_cache(ccode, cachefname)
|
||||
return run_compiled_code(ccode, glb, loc, mode)
|
||||
|
||||
|
||||
|
|
|
@ -36,7 +36,16 @@ def construct_history(backend=None, **kwargs) -> "History":
|
|||
file=sys.stderr,
|
||||
)
|
||||
kls_history = JsonHistory
|
||||
return kls_history(**kwargs)
|
||||
|
||||
try:
|
||||
return kls_history(**kwargs)
|
||||
except Exception as e:
|
||||
xt.print_exception(
|
||||
f"Error during load {kls_history}: {e}\n"
|
||||
f"Set $XONSH_HISTORY_BACKEND='dummy' to disable history.\n"
|
||||
f"History disabled."
|
||||
)
|
||||
return DummyHistory()
|
||||
|
||||
|
||||
def _xh_session_parser(hist=None, newest_first=False, **kwargs):
|
||||
|
|
Loading…
Add table
Reference in a new issue