Merge pull request #897 from ZyX-I/shell

Shell: some refactoring and Windows hack
This commit is contained in:
ZyX-I 2014-06-25 21:27:15 +04:00
commit f4c62b8f83
2 changed files with 44 additions and 12 deletions

View File

@ -1,22 +1,62 @@
# vim:fileencoding=utf-8:noet
from __future__ import absolute_import, unicode_literals, division, print_function
from subprocess import Popen, PIPE
from locale import getlocale, getdefaultlocale, LC_MESSAGES
from functools import partial
import sys
if sys.platform.startswith('win32'):
# Prevent windows from launching consoles when calling commands
# http://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx
Popen = partial(Popen, creationflags=0x08000000)
def _get_shell_encoding():
return getlocale(LC_MESSAGES)[1] or getdefaultlocale()[1] or 'utf-8'
def run_cmd(pl, cmd, stdin=None):
'''Run command and return its stdout, stripped
If running command fails returns None and logs failure to ``pl`` argument.
:param PowerlineLogger pl:
Logger used to log failures.
:param list cmd:
Command which will be run.
:param str stdin:
String passed to command. May be None.
'''
try:
p = Popen(cmd, stdout=PIPE, stdin=PIPE)
p = Popen(cmd, shell=False, stdout=PIPE, stdin=PIPE)
except OSError as e:
pl.exception('Could not execute command ({0}): {1}', e, cmd)
return None
else:
stdout, err = p.communicate(stdin)
encoding = getlocale(LC_MESSAGES)[1] or getdefaultlocale()[1] or 'utf-8'
stdout = stdout.decode(encoding)
stdout = stdout.decode(_get_shell_encoding())
return stdout.strip()
def asrun(pl, ascript):
'''Run the given AppleScript and return the standard output and error.'''
return run_cmd(pl, ['osascript', '-'], ascript)
def readlines(cmd, cwd):
'''Run command and read its output, line by line
:param list cmd:
Command which will be run.
:param str cwd:
Working directory of the command which will be run.
'''
p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE, cwd=cwd)
encoding = _get_shell_encoding()
p.stderr.close()
with p.stdout:
for line in p.stdout:
yield line[:-1].decode(encoding)

View File

@ -7,6 +7,7 @@ import sys
import re
from powerline.lib.vcs import get_branch_name as _get_branch_name, get_file_status
from powerline.lib.shell import readlines
_ref_pat = re.compile(br'ref:\s*refs/heads/(.+)')
@ -144,15 +145,6 @@ try:
def branch(self):
return get_branch_name(self.directory)
except ImportError:
from subprocess import Popen, PIPE
def readlines(cmd, cwd):
p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE, cwd=cwd)
p.stderr.close()
with p.stdout:
for line in p.stdout:
yield line[:-1].decode('utf-8')
class Repository(object):
__slots__ = ('directory', 'ignore_event')