def main(argv=None): """Main entry point for xonsh cli.""" if argv is None: argv = sys.argv[1:] args = premain(argv) events.on_post_init.fire() env = builtins.__xonsh_env__ shell = builtins.__xonsh_shell__ try: if args.mode == XonshMode.interactive: # enter the shell env['XONSH_INTERACTIVE'] = True ignore_sigtstp() if (env['XONSH_INTERACTIVE'] and not env['LOADED_CONFIG'] and not any(os.path.isfile(i) for i in env['XONSHRC'])): print( 'Could not find xonsh configuration or run control files.', file=sys.stderr) xonfig_main(['wizard', '--confirm']) events.on_pre_cmdloop.fire() try: shell.shell.cmdloop() finally: events.on_post_cmdloop.fire() elif args.mode == XonshMode.single_command: # run a single command and exit run_code_with_cache(args.command.lstrip(), shell.execer, mode='single') elif args.mode == XonshMode.script_from_file: # run a script contained in a file path = os.path.abspath(os.path.expanduser(args.file)) if os.path.isfile(path): sys.argv = [args.file] + args.args env['ARGS'] = sys.argv[:] # $ARGS is not sys.argv env['XONSH_SOURCE'] = path run_script_with_cache(args.file, shell.execer, glb=shell.ctx, loc=None, mode='exec') else: print('xonsh: {0}: No such file or directory.'.format( args.file)) elif args.mode == XonshMode.script_from_stdin: # run a script given on stdin code = sys.stdin.read() run_code_with_cache(code, shell.execer, glb=shell.ctx, loc=None, mode='exec') finally: events.on_exit.fire() postmain(args)
def test_xonfg_help(capsys, xonsh_builtins): """verify can invoke it, and usage knows about all the options""" with pytest.raises(SystemExit): xonfig_main(["-h"]) capout = capsys.readouterr().out pat = re.compile(r"^usage:\s*xonfig[^\n]*{([\w,-]+)}", re.MULTILINE) m = pat.match(capout) assert m[1] verbs = set(v.strip().lower() for v in m[1].split(",")) exp = set(v.lower() for v in XONFIG_MAIN_ACTIONS) assert verbs == exp
def test_xonfig_info(args, xonsh_builtins): """info works, and reports no jupyter if none in environment""" capout = xonfig_main(args) assert capout.startswith("+---") assert capout.endswith("---+\n") pat = re.compile(r".*on jupyter\s+\|\s+false", re.MULTILINE | re.IGNORECASE) m = pat.search(capout) assert m
def test_xonfg_help(capsys, xession): """verify can invoke it, and usage knows about all the options""" with pytest.raises(SystemExit): xonfig_main(["-h"]) capout = capsys.readouterr().out pat = re.compile(r"^usage:\s*xonfig[^\n]*{([\w,-]+)}", re.MULTILINE) m = pat.match(capout) assert m[1] verbs = {v.strip().lower() for v in m[1].split(",")} assert verbs == { "info", "styles", "wizard", "web", "colors", "tutorial", }
def main(argv=None): """Main entry point for xonsh cli.""" if argv is None: argv = sys.argv[1:] args = premain(argv) events.on_post_init.fire() env = builtins.__xonsh_env__ shell = builtins.__xonsh_shell__ try: if args.mode == XonshMode.interactive: # enter the shell env['XONSH_INTERACTIVE'] = True ignore_sigtstp() if (env['XONSH_INTERACTIVE'] and not env['LOADED_CONFIG'] and not any(os.path.isfile(i) for i in env['XONSHRC'])): print('Could not find xonsh configuration or run control files.', file=sys.stderr) xonfig_main(['wizard', '--confirm']) events.on_pre_cmdloop.fire() try: shell.shell.cmdloop() finally: events.on_post_cmdloop.fire() elif args.mode == XonshMode.single_command: # run a single command and exit run_code_with_cache(args.command.lstrip(), shell.execer, mode='single') elif args.mode == XonshMode.script_from_file: # run a script contained in a file path = os.path.abspath(os.path.expanduser(args.file)) if os.path.isfile(path): sys.argv = [args.file] + args.args env['ARGS'] = sys.argv[:] # $ARGS is not sys.argv env['XONSH_SOURCE'] = path run_script_with_cache(args.file, shell.execer, glb=shell.ctx, loc=None, mode='exec') else: print('xonsh: {0}: No such file or directory.'.format(args.file)) elif args.mode == XonshMode.script_from_stdin: # run a script given on stdin code = sys.stdin.read() run_code_with_cache(code, shell.execer, glb=shell.ctx, loc=None, mode='exec') finally: events.on_exit.fire() postmain(args)
def test_xonfig_info(args, xession): """info works, and reports no jupyter if none in environment""" capout = xonfig_main(args) assert capout.startswith("+---") assert capout.endswith("---+\n") pat = re.compile(r".*history backend\s+\|\s+", re.MULTILINE | re.IGNORECASE) m = pat.search(capout) assert m
def test_xonfig_kernel_with_jupyter(monkeypatch, capsys, fake_lib, xonsh_builtins): cap_args = None cap_spec = None import jupyter_client.kernelspec # from fake_lib, hopefully. def mock_install_kernel_spec(*args, **kwargs): # arg[0] is self nonlocal cap_args nonlocal cap_spec cap_args = dict(args=args, kw=kwargs) spec_file = os.path.join(args[1], "kernel.json") cap_spec = json.load(open(spec_file)) def mock_get_kernel_spec(*args, **kwargs): raise jupyter_client.kernelspec.NoSuchKernel monkeypatch.setattr( jupyter_client.kernelspec.KernelSpecManager, "install_kernel_spec", value=mock_install_kernel_spec, raising=False, ) monkeypatch.setattr( jupyter_client.kernelspec.KernelSpecManager, "get_kernel_spec", value=mock_get_kernel_spec, raising=False, ) rc = xonfig_main(["jupyter-kernel"]) assert rc == 0 capout = capsys.readouterr().out assert "Jupyter" in capout assert "xonsh" == cap_args["args"][2] assert cap_spec assert cap_spec["language"] == "xonsh" assert strip_sep(cap_spec["argv"][0]) == strip_sep(sys.executable) assert cap_spec["argv"][2] == "xonsh.jupyter_kernel"
def xonfig(args, stdin=None): """Runs the xonsh configuration utility.""" from xonsh.xonfig import xonfig_main # lazy import return xonfig_main(args)
def test_xonfig_kernel_no_jupyter(capsys, xonsh_builtins): with pytest.raises(ImportError): rc = xonfig_main(["jupyter-kernel"]) # noqa F841