don't use subprocess.check_output

This function was added in Python 2.7 and we're currently targetting 2.6 as a
minimum version. Replaced with a function in util.
This commit is contained in:
Adrian Sampson 2012-11-01 11:59:02 -07:00
parent 07d3f3e066
commit f4fa11f8ca
4 changed files with 25 additions and 9 deletions

View file

@ -22,6 +22,7 @@ import shutil
import fnmatch
from collections import defaultdict
import traceback
import subprocess
MAX_FILENAME_LENGTH = 200
WINDOWS_MAGIC_PREFIX = u'\\\\?\\'
@ -573,3 +574,20 @@ def cpu_count():
return num
else:
return 1
def command_output(cmd):
"""Wraps the `subprocess` module to invoke a command (given as a
list of arguments starting with the command name) and collect
stdout. The stderr stream is ignored. May raise
`subprocess.CalledProcessError` or an `OSError`.
This replaces `subprocess.check_output`, which isn't available in
Python 2.6 and which can have problems if lots of output is sent to
stderr.
"""
with open(os.devnull, 'w') as devnull:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=devnull)
stdout, _ = proc.communicate()
if proc.returncode:
raise subprocess.CalledProcessError(proc.returncode, cmd)
return stdout

View file

@ -20,6 +20,7 @@ import subprocess
import os
from tempfile import NamedTemporaryFile
import logging
from beets.util import command_output
# Resizing methods
PIL = 1
@ -42,8 +43,7 @@ def call(args):
command exits abnormally, a ArtResizerError is raised.
"""
try:
with open(os.devnull, 'w') as devnull:
return subprocess.check_output(args, stderr=devnull)
return command_output(args)
except subprocess.CalledProcessError as e:
raise ArtResizerError(
"{0} exited with status {1}".format(args[0], e.returncode)

View file

@ -21,7 +21,7 @@ import threading
from subprocess import Popen, PIPE
from beets.plugins import BeetsPlugin
from beets import ui, library, util
from beets import ui, util
from beetsplug.embedart import _embed
log = logging.getLogger('beets')

View file

@ -18,7 +18,7 @@ import os
from beets import ui
from beets.plugins import BeetsPlugin
from beets.util import syspath
from beets.util import syspath, command_output
from beets.ui import commands
log = logging.getLogger('beets')
@ -30,13 +30,11 @@ class ReplayGainError(Exception):
"""
def call(args):
"""Execute the command indicated by `args` (a list of strings) and
return the command's output. The stderr stream is ignored. If the
command exits abnormally, a ReplayGainError is raised.
"""Execute the command and return its output or raise a
ReplayGainError on failure.
"""
try:
with open(os.devnull, 'w') as devnull:
return subprocess.check_output(args, stderr=devnull)
return command_output(args)
except subprocess.CalledProcessError as e:
raise ReplayGainError(
"{0} exited with status {1}".format(args[0], e.returncode)