rc.xsh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. $VI_MODE = True
  2. $AUTO_PUSHD = True
  3. $XONSH_AUTOPAIR = True
  4. # tab selection: do not execute cmd when pressing enter
  5. $COMPLETIONS_CONFIRM = True
  6. $_Z_EXCLUDE_DIRS = ['/tmp']
  7. xontrib load vox z
  8. def _last_exit_status():
  9. try:
  10. exit_status = __xonsh_history__.rtns[-1]
  11. return exit_status if exit_status != 0 else None
  12. except IndexError:
  13. return None
  14. $PROMPT_FIELDS['last_exit_status'] = _last_exit_status
  15. $SHLVL = int($SHLVL) + 1 if 'SHLVL' in ${...} else 1
  16. $XONSH_STDERR_PREFIX = '{RED}'
  17. $XONSH_STDERR_POSTFIX = '{NO_COLOR}'
  18. $DYNAMIC_CWD_WIDTH = '30%'
  19. $DYNAMIC_CWD_ELISION_CHAR = '…'
  20. $PROMPT = ''.join([
  21. '{RED}{last_exit_status:[{}] }',
  22. '{BOLD_GREEN}{user}@{hostname} ',
  23. '{YELLOW}{cwd} ',
  24. '{{BLUE}}{} '.format('{prompt_end}' * $SHLVL),
  25. '{NO_COLOR}',
  26. ])
  27. $RIGHT_PROMPT = '{gitstatus}{env_name: {}}'
  28. $XONSH_APPEND_NEWLINE = True
  29. import contextlib
  30. import datetime as dt
  31. import io
  32. import os
  33. import re
  34. import shutil
  35. import stat
  36. import subprocess
  37. import sys
  38. os.umask(stat.S_IWGRP | stat.S_IRWXO) # 027
  39. # default locale
  40. # will be used for all non-explicitly set LC_* variables
  41. $LANG = 'en_US.UTF-8'
  42. # fallback locales
  43. # GNU gettext gives preference to LANGUAGE over LC_ALL and LANG
  44. # for the purpose of message handling
  45. # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
  46. # cave: if this list contains 'de(_.*)?' at any (sic!) position
  47. # vim 7.4.1689 will switch to german
  48. $LANGUAGE = ':'.join(['en_US', 'en'])
  49. $LC_COLLATE = 'C.UTF-8'
  50. # char classification, case conversion & other char attrs
  51. $LC_CTYPE = 'de_AT.UTF-8'
  52. # $ locale currency_symbol
  53. $LC_MONETARY = 'de_AT.UTF-8'
  54. # $ locale -k LC_NUMERIC | head -n 3
  55. # decimal_point="."
  56. # thousands_sep=""
  57. # grouping=-1
  58. $LC_NUMERIC = 'C.UTF-8'
  59. # A4
  60. $LC_PAPER = 'de_AT.UTF-8'
  61. USER_BIN_PATH = os.path.join($HOME, '.local', 'bin')
  62. if os.path.isdir(USER_BIN_PATH):
  63. $PATH.insert(0, USER_BIN_PATH)
  64. $PAGER = 'less'
  65. $EDITOR = 'vim'
  66. # i3-sensible-terminal
  67. $TERMINAL = 'termite'
  68. # required by pinentry-tty when using gpg command:
  69. $GPG_TTY = $(tty)
  70. if shutil.which('gpgconf'):
  71. # required by scute
  72. $GPG_AGENT_INFO = $(gpgconf --list-dir agent-socket).rstrip() + ':0:1'
  73. if not 'SSH_CLIENT' in ${...}:
  74. # in gnupg 2.1.13 the location of agents socket changed
  75. $SSH_AUTH_SOCK = $(gpgconf --list-dir agent-ssh-socket).rstrip()
  76. # wrapper for termite required when launching termite from ranger:
  77. $TERMCMD = os.path.join(os.path.dirname(__file__), 'ranger-termite-termcmd')
  78. # https://docs.docker.com/engine/security/trust/content_trust/
  79. $DOCKER_CONTENT_TRUST = 1
  80. class DockerImage:
  81. def __init__(self, image):
  82. import json
  83. attrs, = json.loads(subprocess.check_output(['sudo', 'docker', 'image', 'inspect', image])
  84. .decode(sys.stdout.encoding))
  85. self._id = attrs['Id']
  86. self._tags = attrs['RepoTags']
  87. def __repr__(self):
  88. return '{}(id={!r}, tags={!r})'.format(type(self).__name__, self._id, self._tags)
  89. @classmethod
  90. def build(cls, dockerfile_or_path, tag=None):
  91. out = io.BytesIO()
  92. args = ['sudo', 'docker', 'build']
  93. if tag:
  94. args.extend(['--tag', tag])
  95. with StdoutTee(out) as tee:
  96. if os.path.exists(dockerfile_or_path):
  97. p = subprocess.Popen(args + [dockerfile_or_path],
  98. stdin=None, stdout=tee)
  99. else:
  100. p = subprocess.Popen(args + ['-'],
  101. stdin=subprocess.PIPE, stdout=tee)
  102. p.stdin.write(dockerfile_or_path.encode())
  103. p.stdin.close()
  104. assert p.wait() == 0, 'docker build failed'
  105. image_id, = re.search(rb'^Successfully built (\S+)$', out.getvalue(), re.MULTILINE).groups()
  106. return cls(image_id.decode(sys.stdout.encoding))
  107. @classmethod
  108. def pull(cls, image):
  109. out = io.BytesIO()
  110. with StdoutTee(out) as tee:
  111. subprocess.run(['sudo', 'docker', 'image', 'pull', image], stdout=tee)
  112. repo_digest, = re.search(rb'^Digest: (sha\S+:\S+)$', out.getvalue(), re.MULTILINE).groups()
  113. return cls('{}@{}'.format(image, repo_digest.decode()))
  114. def run(self, args=[], name=None, detach=False, env={},
  115. network=None, publish_ports=[], volumes=[], caps=[]):
  116. params = ['sudo', 'docker', 'run', '--rm']
  117. if name:
  118. params.extend(['--name', name])
  119. if detach:
  120. params.append('--detach')
  121. else:
  122. params.extend(['--interactive', '--tty'])
  123. params.extend([a for k, v in env.items() for a in ['--env', '{}={}'.format(k, v)]])
  124. if network:
  125. params.extend(['--network', network])
  126. params.extend([a for v in volumes for a in ['--volume', ':'.join(v)]])
  127. params.extend('--publish=' + ':'.join([str(a) for a in p]) for p in publish_ports)
  128. params.extend(['--security-opt=no-new-privileges', '--cap-drop=all'])
  129. params.extend(['--cap-add={}'.format(c) for c in caps])
  130. params.append(self._id)
  131. params.extend(args)
  132. sys.stderr.write('{}\n'.format(shlex_join(params)))
  133. subprocess.run(params, check=True)
  134. class StdoutTee:
  135. def __init__(self, sink):
  136. self._sink = sink
  137. def __enter__(self):
  138. self._read_fd, self._write_fd = os.pipe()
  139. import threading
  140. self._thread = threading.Thread(target=self._loop)
  141. self._thread.start()
  142. return self
  143. def _loop(self):
  144. while True:
  145. try:
  146. data = os_read_non_blocking(self._read_fd)
  147. except OSError: # fd closed
  148. return
  149. if data:
  150. self._sink.write(data)
  151. sys.stdout.buffer.write(data)
  152. sys.stdout.flush()
  153. def fileno(self):
  154. return self._write_fd
  155. def __exit__(self, exc_type, exc_value, traceback):
  156. os.close(self._read_fd)
  157. os.close(self._write_fd)
  158. self._thread.join()
  159. @contextlib.contextmanager
  160. def chdir(path):
  161. previous = os.getcwd()
  162. try:
  163. os.chdir(path)
  164. yield path
  165. finally:
  166. os.chdir(previous)
  167. def dpkg_listfiles(pkg_name):
  168. assert isinstance(pkg_name, str)
  169. paths = $(dpkg --listfiles @(pkg_name)).split('\n')[:-1]
  170. assert len(paths) > 0, 'pkg {!r} not installed'.format(pkg_name)
  171. return paths
  172. def dpkg_search(path_search_pattern):
  173. assert isinstance(path_search_pattern, str)
  174. return re.findall(
  175. '^(\S+): (.*)$\n',
  176. $(dpkg --search @(path_search_pattern)),
  177. flags=re.MULTILINE,
  178. )
  179. def dpkg_welse(cmd):
  180. pkg_name, cmd_path = dpkg_which(cmd)
  181. return dpkg_listfiles(pkg_name)
  182. def dpkg_which(cmd):
  183. cmd_path = shutil.which(cmd)
  184. assert cmd_path, 'cmd {!r} not found'.format(cmd)
  185. matches = dpkg_search(cmd_path)
  186. assert len(matches) != 0, '{!r} not installed via dpkg'.format(cmd_path)
  187. assert len(matches) == 1
  188. return matches[0]
  189. @contextlib.contextmanager
  190. def encfs_mount(root_dir_path, mount_point_path, extpass=None):
  191. mount_arg_patterns = ['encfs', root_dir_path, mount_point_path]
  192. if extpass:
  193. mount_arg_patterns.extend(['--extpass', shlex_join(extpass)])
  194. with fuse_mount(mount_arg_patterns=mount_arg_patterns,
  195. mount_point_path=mount_point_path):
  196. yield mount_point_path
  197. @contextlib.contextmanager
  198. def fuse_mount(mount_arg_patterns, mount_point_path):
  199. import shlex
  200. mount_args = [a.format(mp=shlex.quote(mount_point_path))
  201. for a in mount_arg_patterns]
  202. sys.stderr.write('{}\n'.format(shlex_join(mount_args)))
  203. subprocess.check_call(mount_args)
  204. try:
  205. yield mount_point_path
  206. finally:
  207. umount_args = ['fusermount', '-u', '-z', mount_point_path]
  208. sys.stderr.write('{}\n'.format(shlex_join(umount_args)))
  209. subprocess.check_call(umount_args)
  210. def gpg_decrypt(path, verify=False):
  211. import gpg
  212. with gpg.Context() as gpg_ctx:
  213. with open(path, 'rb') as f:
  214. data, decrypt_result, verify_result = gpg_ctx.decrypt(f, verify=verify)
  215. return data
  216. def locate(*patterns, match_all=True, ignore_case=True):
  217. params = []
  218. if match_all:
  219. params.insert(0, '--all')
  220. if ignore_case:
  221. params.insert(0, '--ignore-case')
  222. return $(locate @(params) -- @(patterns)).split('\n')[:-1]
  223. def os_read_non_blocking(fd, buffer_size_bytes=8*1024, timeout_seconds=0.1):
  224. import select
  225. if fd in select.select([fd], [], [], timeout_seconds)[0]:
  226. return os.read(fd, buffer_size_bytes)
  227. else:
  228. return None
  229. def shlex_join(params):
  230. import shlex
  231. assert isinstance(params, list) or isinstance(params, tuple), params
  232. return ' '.join(shlex.quote(p) for p in params)
  233. def timestamp_now_utc():
  234. return dt.datetime.utcnow().replace(tzinfo=dt.timezone.utc)
  235. def timestamp_now_local():
  236. # if called without tz argument astimezone() assumes
  237. # the system local timezone for the target timezone
  238. return timestamp_now_utc().astimezone()
  239. def timestamp_iso_local():
  240. # if called without tz argument astimezone() assumes
  241. # the system local timezone for the target timezone
  242. return timestamp_now_local().strftime('%Y%m%dT%H%M%S%z')
  243. def yaml_load(path):
  244. import yaml
  245. with open(path, 'r') as f:
  246. return yaml.load(f.read())
  247. def yaml_write(path, data):
  248. import yaml
  249. with open(path, 'w') as f:
  250. f.write(yaml.dump(data, default_flow_style=False))
  251. aliases['d'] = ['sudo', 'docker']
  252. aliases['dpkg-welse'] = lambda args: '\n'.join(dpkg_welse(args[0]))
  253. aliases['dpkg-which'] = lambda args: '\t'.join(dpkg_which(args[0]))
  254. aliases['g'] = ['git']
  255. aliases['ll'] = ['ls', '-l', '--all', '--indicator-style=slash',
  256. '--human-readable', '--time-style=long-iso', '--color=auto']
  257. if shutil.which('startx') and $(tty).rstrip() == '/dev/tty1':
  258. startx
  259. # vim: filetype=python