Skip to content

Commit

Permalink
Provides check_output() for Python 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Dec 2, 2014
1 parent d166ec8 commit 0fb68ab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
12 changes: 6 additions & 6 deletions reprounzip-vagrant/reprounzip/unpackers/vagrant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
select_installer, busybox_url, join_root, FileUploader, FileDownloader, \
get_runs
from reprounzip.unpackers.vagrant.interaction import interactive_shell
from reprounzip.utils import unicode_, iteritems
from reprounzip.utils import unicode_, iteritems, check_output


class IgnoreMissingKey(MissingHostKeyPolicy):
Expand Down Expand Up @@ -115,16 +115,16 @@ def get_ssh_parameters(target):
"""Reads the SSH parameters from ``vagrant ssh`` command output.
"""
try:
stdout = subprocess.check_output(['vagrant', 'ssh-config'],
cwd=target.path,
stderr=subprocess.PIPE)
stdout = check_output(['vagrant', 'ssh-config'],
cwd=target.path,
stderr=subprocess.PIPE)
except subprocess.CalledProcessError:
# Makes sure the VM is running
subprocess.check_call(['vagrant', 'up'],
cwd=target.path)
# Try again
stdout = subprocess.check_output(['vagrant', 'ssh-config'],
cwd=target.path)
stdout = check_output(['vagrant', 'ssh-config'],
cwd=target.path)

info = {}
for line in stdout.split(b'\n'):
Expand Down
17 changes: 17 additions & 0 deletions reprounzip/reprounzip/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
from rpaths import Path
import stat
import subprocess
import sys


Expand Down Expand Up @@ -220,6 +221,22 @@ def rmtree_fixed(path):
path.rmdir()


def check_output(*popenargs, **kwargs):
"""Runs a command and returns its output, raising on non-zero exit code.
"""
if 'stdout' in kwargs:
raise ValueError("stdout argument not allowed")
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
stdout, stderr = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get('args')
if cmd is None:
cmd = [popenargs[0]]
raise subprocess.CalledProcessError(retcode, cmd, output=stdout)
return stdout


def download_file(url, dest, cachename=None):
"""Downloads a file using a local cache.
Expand Down
17 changes: 17 additions & 0 deletions reprozip/reprozip/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
from rpaths import Path
import stat
import subprocess
import sys


Expand Down Expand Up @@ -220,6 +221,22 @@ def rmtree_fixed(path):
path.rmdir()


def check_output(*popenargs, **kwargs):
"""Runs a command and returns its output, raising on non-zero exit code.
"""
if 'stdout' in kwargs:
raise ValueError("stdout argument not allowed")
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
stdout, stderr = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get('args')
if cmd is None:
cmd = [popenargs[0]]
raise subprocess.CalledProcessError(retcode, cmd, output=stdout)
return stdout


def download_file(url, dest, cachename=None):
"""Downloads a file using a local cache.
Expand Down
4 changes: 2 additions & 2 deletions tests/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import yaml

from reprounzip.unpackers.common import join_root
from reprounzip.utils import iteritems
from reprounzip.utils import iteritems, check_output as _check_output


tests = Path(__file__).parent.absolute()
Expand Down Expand Up @@ -58,7 +58,7 @@ def check_call(args):

@print_arg_list
def check_output(args):
output = subprocess.check_output(args)
output = _check_output(args)
sys.stdout.buffer.write(output)
return output

Expand Down

0 comments on commit 0fb68ab

Please sign in to comment.