Last active
January 21, 2022 13:32
-
-
Save Glutexo/baa309c4bc79321d30d76987c40e774d to your computer and use it in GitHub Desktop.
My .xonshrc, to make my life better
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Prevent warning caused by upgrade to Python 3.10. | |
| # See https://github.com/xonsh/xonsh//issues/4409. | |
| import warnings | |
| warnings.filterwarnings( | |
| 'ignore', | |
| message='There is no current event loop', | |
| category=DeprecationWarning, | |
| module='prompt_toolkit', | |
| ) | |
| del warnings | |
| xontrib load docker_tabcomplete autovox vox | |
| locale = "cs_CZ.UTF-8" | |
| $LC_ALL = locale | |
| $LC_NUMERIC = locale | |
| $LANG = locale | |
| del locale | |
| # Configure xonsh a bit | |
| $XONSH_SHOW_TRACEBACK = True # Disable a nasty warning | |
| $PROMPT = \ | |
| "{env_name:{} }"\ | |
| "{BOLD_GREEN}{user}@{hostname}"\ | |
| "{BOLD_BLUE} {cwd}{branch_color}{curr_branch: {}}"\ | |
| "{RESET}\n"\ | |
| "{BOLD_BLUE}{prompt_end}"\ | |
| "{RESET} " | |
| POSTGRESQL_LOG_PATH = "/usr/local/var/log/[email protected]" | |
| def heal_postgresql(): | |
| def _is_postgresql(line): | |
| return line[0] == "[email protected]" | |
| output = $(brew services list).rstrip().split("\n")[1:] | |
| for line in filter(_is_postgresql, output): | |
| print(line[3]) | |
| def add_to_path(path): | |
| $PATH.add(path, front=True, replace=True) | |
| def command_present(command): | |
| result = !(which @(command) 2>/dev/null) | |
| return bool(result) | |
| # bash_completions_path = '/usr/local/etc/bash_completion.d' | |
| # if bash_completions_path not in $BASH_COMPLETIONS: | |
| # $BASH_COMPLETIONS.insert(0, bash_completions_path) | |
| # del bash_completions_path | |
| # Add paths to $PATH | |
| $PATH = [] | |
| paths = [ | |
| "/bin", | |
| "/sbin", | |
| "/usr/bin", | |
| "/usr/sbin", | |
| "/usr/local/bin", | |
| "/usr/local/sbin", | |
| "/usr/local/opt/gnu-tar/libexec/gnubin", | |
| "/usr/local/opt/icu4c/bin", | |
| "/usr/local/opt/[email protected]/bin", | |
| "/usr/local/opt/ruby/bin", | |
| "/usr/local/opt/tcl-tk/bin", | |
| "/usr/local/JetBrains/bin", | |
| "/Library/TeX/texbin", | |
| f"{$HOME}/go/bin" | |
| ] | |
| for path in paths: | |
| add_to_path(path) | |
| del paths, path | |
| # The yarn comand can be used only after extending the PATH in the earlier step. | |
| if command_present("yarn"): | |
| yarn_path = $(yarn global bin).rstrip() | |
| add_to_path(yarn_path) | |
| del yarn_path | |
| # Add a virtualenv bin to $PATH | |
| if 'VIRTUAL_ENV' in ${...}: | |
| from os.path import join | |
| add_to_path(join($VIRTUAL_ENV, "bin")) | |
| del join | |
| paths = [ | |
| f"~/bin", | |
| "~/.rbenv/shims", | |
| "~/.pyenv/shims", | |
| ] | |
| for path in paths: | |
| add_to_path(path) | |
| del paths, path | |
| del add_to_path | |
| if command_present("yarn"): | |
| yarn_path = $(yarn bin).rstrip() | |
| add_to_path(yarn_path) | |
| del yarn_path | |
| $RBENV_SHELL = 'xonsh' | |
| # Save some variables for Insights development | |
| $ZOOKEEPER = "localhost:2181" | |
| $KAFKAMQ = "localhost:9092" | |
| $KAFKA_BOOTSTRAP_SERVERS = "localhost:9092" | |
| $UPLOAD_SERVICE = "localhost:8888" | |
| aliases["kafka-topics-local"] = ["kafka-topics", f"--zookeeper={$ZOOKEEPER}"] | |
| aliases["kafka-console-producer-local"] = ["kafka-console-producer", f"--broker-list={$KAFKAMQ}"] | |
| aliases["kafka-console-consumer-local"] = ["kafka-console-consumer", f"--bootstrap-server={$KAFKAMQ}"] | |
| aliases["kafka-configs-local"] = ["kafka-configs", "--zookeeper", $ZOOKEEPER] | |
| aliases["oc-get-builds-insights-host-inventory"] = ["oc", "get", "builds", "--namespace=insights-host-inventory"] | |
| aliases["get-platform-log-events"] = ["awslocal", "logs", "get-log-events", "--log-group-name", "platform", "--log-stream-name", "PlatformStream"] | |
| aliases["reload-xonshrc"] = ["source", "~/.xonshrc"] | |
| aliases["upgrade-pip"] = ["pip", "install", "--upgrade", "pip"] | |
| from contextlib import contextmanager | |
| @contextmanager | |
| def swap(**kwargs): | |
| with ${...}.swap(**kwargs): | |
| yield | |
| @contextmanager | |
| def install_pyicu(): | |
| from os.path import join | |
| icu_path = join("/usr", "local", "opt", "icu4c") | |
| lib_path = join(icu_path, "lib") | |
| include_path = join(icu_path, "include") | |
| pkgconfig_path = join(lib_path, "pkgconfig") | |
| del join | |
| with swap( | |
| PYICU_CFLAGS=f"-std=c++1", | |
| PYICU_INCLUDES=include_path, | |
| PYICU_LFLAGS=f"-L{lib_path}", | |
| PYICU_LIBRARIES=f"-L{icu_path}/lib", | |
| PKG_CONFIG_PATH=pkgconfig_path | |
| ): | |
| yield | |
| from os.path import abspath | |
| @contextmanager | |
| def gopath(path="."): | |
| with swap(GOPATH=abspath(path)): | |
| yield | |
| del abspath | |
| @contextmanager | |
| def pythonpath(path="."): | |
| with swap(PYTHONPATH=path): | |
| yield | |
| del contextmanager | |
| custom_commands = [] | |
| def custom_command(lst): | |
| def decorator(func): | |
| lst.append(func) | |
| return func | |
| return decorator | |
| custom_command = custom_command(custom_commands) | |
| @custom_command | |
| def docker_machine_env(): | |
| from tempfile import NamedTemporaryFile | |
| with NamedTemporaryFile() as env_file: | |
| docker_machine_env = $(docker-machine env) | |
| env_file.write(docker_machine_env.encode('UTF-8')) | |
| env_file.flush() | |
| source-bash @(env_file.name) | |
| del docker_machine_env | |
| @custom_command | |
| def pyenv_install(args): | |
| prefix = $(brew --prefix tcl-tk).rstrip() | |
| include_path = f"{prefix}/include" | |
| lib_path = f"{prefix}/lib" | |
| environment = { | |
| "PYTHON_CONFIGURE_OPTS": f"--with-tcltk-includes='-I{include_path}' --with-tcltk-libs='-L{lib_path} -ltcl8.6 -ltk8.6'", | |
| "LDFLAGS": f"-L{lib_path}", | |
| "CPPFLAGS": f"-I{include_path}", | |
| "CFLAGS": f"-I{include_path}", | |
| "PKG_CONFIG_PATH": f"{lib_path}/pkgconfig" | |
| } | |
| with ${...}.swap(**environment): | |
| pyenv install @(args) | |
| del pyenv_install | |
| @custom_command | |
| def random_uuid(): | |
| from uuid import uuid4 | |
| return str(uuid4()) | |
| del random_uuid | |
| @custom_command | |
| def utcnow(): | |
| from datetime import datetime | |
| return datetime.utcnow() | |
| del utcnow | |
| def compile_command( | |
| function_name, compiler_command, compiler_args, file_extension | |
| ): | |
| def _compile(command_args): | |
| file_name = command_args[0] | |
| execute = command_args[1] if len(command_args) > 1 else False | |
| from re import sub | |
| file_base = sub(rf"\.{file_extension}$", "", file_name) | |
| src_file = f"{file_base}.{file_extension}" | |
| @(compiler_command) @(src_file) -o @(file_base) @(compiler_args) | |
| if execute: | |
| ./@(file_base) | |
| _compile.__name__ = function_name | |
| return custom_command(_compile) | |
| compile_command("gcc_compile_c", "gcc", (), "c") | |
| compile_command("gcc_compile_cpp", "gcc", ("-lstdc++"), "cpp") | |
| compile_command("gcc_compile_gpp", "g++", (), "cpp") | |
| custom_commands_names = [] | |
| for command_func in custom_commands: | |
| command_name = command_func.__name__.replace("_", "-") | |
| custom_commands_names.append(command_name) | |
| aliases[command_name] = command_func | |
| del command_func, command_name | |
| CUSTOM_COMMANDS = tuple(custom_commands_names) | |
| del custom_commands_names | |
| from os import getcwd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment