xonsh/xontrib/xog.py
Noorhteen Raja NJ 38295a1dd9
Remove globals (#4280)
* refactor: remove usage of global variables in abbrevs.py

* chore: add flake8-mutable to prevent mutable defaults

* fix: abbrevs expand test

* refactor: add xonsh session singleton

* refactor: fix circular errors when using xonshSession as singleton

* refactor: remove black magicked builtin attributes

* style: black format tests as well

* refactor: update tests to use xonsh-session singleton

* refactor: update abbrevs to not use builtins

* test: remove DummyCommandsCache and patch orig class

* fix: failing test_command_completers

* test: use monkeypatch to update xession fixture

* fix: failing test_pipelines

* fix: failing test_main

* chore: run test suit as single invocation

* test: fix tests/test_xonsh.xsh

* refactor: remove builtins from docs/conf.py

* fix: mypy error in jobs

* fix: test error from test_main

* test: close xession error in test_command_completers

* chore: use pytest-cov for reporting coverage

this will include subprocess calls, and will increase coverage

* style:
2021-05-20 13:14:26 +03:00

69 lines
1.5 KiB
Python

"""
This adds `xog` - a simple command to establish and print temporary traceback log file.
"""
import os
import pathlib
import tempfile
from xonsh.built_ins import XSH
__all__ = ()
def _get_log_file_name():
return pathlib.Path(f"{tempfile.gettempdir()}/xonsh-{os.getpid()}.log")
def _clear_log(logfile, stderr):
try:
os.remove(logfile)
except OSError as e:
print(f"xog: {e}", file=stderr)
return False
return True
def _print_log(logfile, stdout, stderr):
try:
with open(logfile) as log:
for line in log:
print(line, end="", file=stdout)
except OSError as e:
print(f"xog: {e}", file=stderr)
return False
return True
def _print_help(stdout):
print(
"""Usage: xog [OPTIONS]
Prints contents of the shell's traceback log file.
Options:
-c, --clear\t\tclear the log file contents
-?, --help\t\tprint this help text
"""
)
def _xog(args, stdout=None, stderr=None):
if "-?" in args or "--help" in args:
_print_help(stdout)
return 0
logfile = XSH.env.get("XONSH_TRACEBACK_LOGFILE", "")
if not (logfile and os.path.isfile(logfile)):
print("Traceback log file doesn't exist.", file=stderr)
return -1
if "-c" in args or "--clear" in args:
rc = _clear_log(logfile, stderr=stderr)
return 0 if rc else -1
rc = _print_log(logfile, stdout, stderr)
return 0 if rc else -1
XSH.env["XONSH_TRACEBACK_LOGFILE"] = _get_log_file_name()
XSH.aliases["xog"] = _xog