import contextlib import datetime as dt import grp import os import re import shutil import stat import subprocess import sys import tempfile TERMUX=shutil.which('termux-info') is not None $VI_MODE = True $AUTO_PUSHD = True $XONSH_AUTOPAIR = True # tab selection: do not execute cmd when pressing enter $COMPLETIONS_CONFIRM = True $_Z_EXCLUDE_DIRS = ['/tmp'] $XONSH_HISTORY_SIZE = '32 MB' # alias 'xontrib' wraps xontribs_load() # xontrib load vox z from xonsh.xontribs import xontribs_load xontribs_load(['vox', 'z']) def _last_exit_status(): try: exit_status = __xonsh__.history.rtns[-1] return exit_status if exit_status != 0 else None except IndexError: return None $PROMPT_FIELDS['last_exit_status'] = _last_exit_status $SHLVL = int($SHLVL) + 1 if 'SHLVL' in ${...} else 1 $XONSH_STDERR_PREFIX = '{RED}' $XONSH_STDERR_POSTFIX = '{NO_COLOR}' $DYNAMIC_CWD_WIDTH = '30%' $DYNAMIC_CWD_ELISION_CHAR = '…' $PROMPT = ''.join([ '{RED}{last_exit_status:[{}] }', '{BOLD_GREEN}{user}@{hostname} ' if not TERMUX or 'SSH_CLIENT' in ${...} else '', '{YELLOW}{cwd} ', '{{BLUE}}{} '.format('{prompt_end}' * $SHLVL), '{NO_COLOR}', ]) $RIGHT_PROMPT = '{gitstatus}{env_name: {}}' $XONSH_APPEND_NEWLINE = True os.umask(stat.S_IWGRP | stat.S_IRWXO) # 027 # default locale # will be used for all non-explicitly set LC_* variables $LANG = 'en_US.UTF-8' # fallback locales # GNU gettext gives preference to LANGUAGE over LC_ALL and LANG # for the purpose of message handling # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html # cave: if this list contains 'de(_.*)?' at any (sic!) position # vim 7.4.1689 will switch to german $LANGUAGE = ':'.join(['en_US', 'en']) $LC_COLLATE = 'C.UTF-8' # char classification, case conversion & other char attrs if not TERMUX: $LC_CTYPE = 'de_AT.UTF-8' # $ locale currency_symbol if not TERMUX: $LC_MONETARY = 'de_AT.UTF-8' # $ locale -k LC_NUMERIC | head -n 3 # decimal_point="." # thousands_sep="" # grouping=-1 $LC_NUMERIC = 'C.UTF-8' # A4 $LC_PAPER = 'de_AT.UTF-8' USER_BIN_PATH = os.path.join($HOME, '.local', 'bin') if os.path.isdir(USER_BIN_PATH): $PATH.insert(0, USER_BIN_PATH) $PAGER = 'less' $EDITOR = 'vim' # i3-sensible-terminal $TERMINAL = 'termite' if shutil.which('gpgconf'): # required by pinentry-tty when using gpg command # takes approx 100ms in termux on galaxy S7 $GPG_TTY = $(tty) # required by scute $GPG_AGENT_INFO = $(gpgconf --list-dir agent-socket).rstrip() + ':0:1' if not 'SSH_CLIENT' in ${...}: # in gnupg 2.1.13 the location of agents socket changed $SSH_AUTH_SOCK = $(gpgconf --list-dir agent-ssh-socket).rstrip() # wrapper for termite required when launching termite from ranger: $TERMCMD = os.path.join(os.path.dirname(__file__), 'ranger-termite-termcmd') # https://docs.docker.com/engine/security/trust/content_trust/ # $DOCKER_CONTENT_TRUST = 1 $PASSWORD_STORE_UMASK = '027' $PASSWORD_STORE_CHARACTER_SET_NO_SYMBOLS = '1-9a-km-zA-HJKLMNPR-Z' $PASSWORD_STORE_CHARACTER_SET = $PASSWORD_STORE_CHARACTER_SET_NO_SYMBOLS + r'*+!&#@%.\-_' @contextlib.contextmanager def chdir(path): import shlex previous = os.getcwd() try: sys.stderr.write('cd {}\n'.format(shlex.quote(path))) os.chdir(path) yield path finally: os.chdir(previous) sys.stderr.write('cd {}\n'.format(shlex.quote(previous))) def dpkg_welse(cmd): pkg_name, _ = dpkg_which(cmd) return $(dpkg --listfiles @(pkg_name)).split('\n')[:-1] def dpkg_which(cmd): cmd_path = shutil.which(cmd) assert cmd_path, 'cmd {!r} not found'.format(cmd) search_match = re.search(r'^(\S+): ', $(dpkg --search @(cmd_path))) assert search_match, f'{cmd_path!r} not installed via dpkg' return search_match.group(1), cmd_path @contextlib.contextmanager def encfs_mount(root_dir_path, mount_point_path, extpass=None): mount_arg_patterns = ['encfs', root_dir_path, mount_point_path] if extpass: mount_arg_patterns.extend(['--extpass', shlex_join(extpass)]) with fuse_mount(mount_arg_patterns=mount_arg_patterns, mount_point_path=mount_point_path): yield mount_point_path @contextlib.contextmanager def fuse_mount(mount_arg_patterns, mount_point_path): import shlex mount_args = [a.format(mp=shlex.quote(mount_point_path)) for a in mount_arg_patterns] sys.stderr.write('{}\n'.format(shlex_join(mount_args))) subprocess.check_call(mount_args) try: yield mount_point_path finally: umount_args = ['fusermount', '-u', '-z', mount_point_path] sys.stderr.write('{}\n'.format(shlex_join(umount_args))) subprocess.check_call(umount_args) def shlex_join(params): import shlex assert isinstance(params, list) or isinstance(params, tuple), params return ' '.join(shlex.quote(p) for p in params) def timestamp_now_utc(): return dt.datetime.utcnow().replace(tzinfo=dt.timezone.utc) def timestamp_now_local(): # if called without tz argument astimezone() assumes # the system local timezone for the target timezone return timestamp_now_utc().astimezone() def timestamp_iso_local(): # if called without tz argument astimezone() assumes # the system local timezone for the target timezone return timestamp_now_local().strftime('%Y%m%dT%H%M%S%z') def yaml_load(path): import yaml with open(path, 'r') as f: return yaml.load(f.read()) def yaml_write(path, data): import yaml with open(path, 'w') as f: f.write(yaml.dump(data, default_flow_style=False)) aliases['cdtmp'] = lambda: os.chdir(tempfile.mkdtemp()) try: docker_gid = grp.getgrnam('docker').gr_gid except KeyError: docker_gid = None docker_command_prefix = [] if docker_gid and docker_gid in os.getgroups() else ['sudo'] aliases['c'] = docker_command_prefix + ['docker-compose'] aliases['d'] = docker_command_prefix + ['docker'] aliases['d-e'] = docker_command_prefix + ['docker', 'exec', '--interactive', '--tty'] aliases['d-v'] = docker_command_prefix + ['docker', 'volume', 'list'] aliases['dpkg-welse'] = lambda args: '\n'.join(dpkg_welse(args[0])) aliases['dpkg-which'] = lambda args: '\t'.join(dpkg_which(args[0])) aliases['g'] = ['git'] aliases['ip'] = ['ip', '-human-readable', '-color'] aliases['j'] = ['journalctl', '--output=short-iso'] aliases['less'] = ['less', '--jump-target=.2'] aliases['ll'] = ['ls', '-l', '--all', '--indicator-style=slash', '--human-readable', '--time-style=long-iso', '--color=auto'] aliases['mkdircd'] = lambda args: (os.makedirs(args[0]), os.chdir(args[0])) aliases['p'] = ['pass'] aliases['t'] = ['timew'] # timewarrior if shutil.which('startx') and $(tty).rstrip() == '/dev/tty1': exec startx # vim: filetype=python