2013-07-31 22:15:35 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# vim:fileencoding=utf-8:noet
|
|
|
|
from __future__ import (unicode_literals, division, absolute_import, print_function)
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import errno
|
2014-08-27 19:34:18 +02:00
|
|
|
import os
|
2014-08-27 19:36:19 +02:00
|
|
|
|
2014-08-27 19:34:18 +02:00
|
|
|
try:
|
|
|
|
from posix import environ
|
|
|
|
except ImportError:
|
|
|
|
from os import environ
|
2013-07-31 22:15:35 +02:00
|
|
|
|
2014-09-16 21:59:11 +02:00
|
|
|
# XXX Hack for importing powerline modules to work.
|
|
|
|
sys.path.pop(0)
|
|
|
|
|
2014-09-16 18:51:39 +02:00
|
|
|
try:
|
|
|
|
from powerline.lib.encoding import get_preferred_output_encoding
|
|
|
|
except ImportError:
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(os.path.realpath(__file__)))))
|
|
|
|
from powerline.lib.encoding import get_preferred_output_encoding
|
|
|
|
|
2014-08-23 02:10:47 +02:00
|
|
|
|
2013-07-31 22:15:35 +02:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print('Must provide at least one argument.', file=sys.stderr)
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
2014-12-05 22:05:25 +01:00
|
|
|
use_filesystem = not sys.platform.lower().startswith('linux')
|
2013-07-31 22:15:35 +02:00
|
|
|
|
2014-08-23 12:55:59 +02:00
|
|
|
if sys.argv[1] == '--socket':
|
|
|
|
address = sys.argv[2]
|
|
|
|
if not use_filesystem:
|
|
|
|
address = '\0' + address
|
|
|
|
del sys.argv[1:3]
|
|
|
|
else:
|
|
|
|
address = ('/tmp/powerline-ipc-%d' if use_filesystem else '\0powerline-ipc-%d') % os.getuid()
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
sock = socket.socket(family=socket.AF_UNIX)
|
|
|
|
|
|
|
|
|
|
|
|
def eintr_retry_call(func, *args, **kwargs):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
except EnvironmentError as e:
|
|
|
|
if getattr(e, 'errno', None) == errno.EINTR:
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
|
2014-08-27 19:33:07 +02:00
|
|
|
|
2013-07-31 22:15:35 +02:00
|
|
|
try:
|
|
|
|
eintr_retry_call(sock.connect, address)
|
|
|
|
except Exception:
|
|
|
|
# Run the powerline renderer
|
|
|
|
args = ['powerline-render'] + sys.argv[1:]
|
|
|
|
os.execvp('powerline-render', args)
|
|
|
|
|
2014-09-16 18:51:39 +02:00
|
|
|
fenc = get_preferred_output_encoding()
|
2013-07-31 22:15:35 +02:00
|
|
|
|
2014-08-27 19:33:07 +02:00
|
|
|
|
|
|
|
def tobytes(s):
|
|
|
|
if isinstance(s, bytes):
|
|
|
|
return s
|
|
|
|
else:
|
|
|
|
return s.encode(fenc)
|
|
|
|
|
2014-08-23 02:10:47 +02:00
|
|
|
|
|
|
|
args = [tobytes('%x' % (len(sys.argv) - 1))]
|
|
|
|
args.extend((tobytes(s) for s in sys.argv[1:]))
|
|
|
|
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
cwd = os.getcwd()
|
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
|
|
|
else:
|
2014-08-23 02:10:47 +02:00
|
|
|
if not isinstance(cwd, bytes):
|
2013-07-31 22:15:35 +02:00
|
|
|
cwd = cwd.encode(fenc)
|
2014-08-04 00:59:02 +02:00
|
|
|
args.append(cwd)
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
|
2014-08-27 19:34:18 +02:00
|
|
|
args.extend((tobytes(k) + b'=' + tobytes(v) for k, v in environ.items()))
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
EOF = b'\0\0'
|
|
|
|
|
|
|
|
for a in args:
|
2014-08-04 00:59:02 +02:00
|
|
|
eintr_retry_call(sock.sendall, a + b'\0')
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
eintr_retry_call(sock.sendall, EOF)
|
|
|
|
|
|
|
|
received = []
|
|
|
|
while True:
|
|
|
|
r = sock.recv(4096)
|
|
|
|
if not r:
|
|
|
|
break
|
|
|
|
received.append(r)
|
|
|
|
|
|
|
|
sock.close()
|
|
|
|
|
2014-08-23 02:10:47 +02:00
|
|
|
if sys.version_info < (3,):
|
|
|
|
sys.stdout.write(b''.join(received))
|
|
|
|
else:
|
|
|
|
sys.stdout.buffer.write(b''.join(received))
|