Skip to content

Commit

Permalink
Add support for coverage, fix run encodings
Browse files Browse the repository at this point in the history
- Fixes #3
- Fixes #4

Signed-off-by: Dan Ryan <dan@danryan.co>
  • Loading branch information
techalchemy committed Aug 14, 2018
1 parent c1f1557 commit 18c7e52
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions news/3.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added suport for coverage and tox builds.
1 change: 1 addition & 0 deletions news/4.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed encoding issues when passing commands and environments to ``vistir.misc.run()``.
28 changes: 15 additions & 13 deletions src/vistir/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
from __future__ import absolute_import, unicode_literals

import json
import locale
import os
import subprocess
import sys

from collections import OrderedDict

import six

from .cmdparse import Script
from .compat import Path, partialmethod, fs_str
from .compat import Path, fs_str, partialmethod


__all__ = [
Expand Down Expand Up @@ -63,25 +64,26 @@ def dedup(iterable):
return iter(OrderedDict.fromkeys(iterable))


def run(cmd, env=None):
def run(cmd, env={}):
"""Use `subprocess.Popen` to get the output of a command and decode it.
:param list cmd: A list representing the command you want to run.
:param dict env: Additional environment settings to pass through to the subprocess.
:returns: A 2-tuple of (output, error)
"""
encoding = locale.getdefaultlocale()[1] or "utf-8"
if not env:
env = os.environ.copy()
else:
env = env.copy()
_env = {}
for key, val in _env.items():
_env[fs_str(key)] = fs_str(val)
_env = {k: fs_str(v) for k, v in os.environ.items()}
for key, val in env.items():
_env[key] = fs_str(val)
if six.PY2:
if isinstance(cmd, six.string_types):
cmd = cmd.encode("utf-8")
elif isinstance(cmd, (list, tuple)):
cmd = [c.encode("utf-8") for c in cmd]
c = subprocess.Popen(
cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
cmd, env=_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
)
out, err = c.communicate()
return out.decode(encoding).strip(), err.decode(encoding).strip()
return out.strip(), err.strip()


def load_path(python):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_unnest(seed_ints, additional_lists):
list_copies[i - 1].append(additional_lists[i])
else:
composite_list.append(list_copies[i])
assert all(x.isdigit() for x in flattened_list), flattened_list
assert all((isinstance(x, int) or x.isdigit()) for x in flattened_list), flattened_list
assert sorted(list(vistir.misc.unnest(composite_list))) == sorted(flattened_list)


Expand Down

0 comments on commit 18c7e52

Please sign in to comment.