From 599115a20fe12fa714fd1ec9d05a759bae980a0f Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Sun, 11 Mar 2018 01:22:11 +0100 Subject: [PATCH] Use subprocess.run instead of custom wrapper --- sigal/utils.py | 10 ---------- sigal/video.py | 19 +++++++++++-------- tests/test_utils.py | 10 ---------- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/sigal/utils.py b/sigal/utils.py index 7ac80d9..411ac00 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -24,7 +24,6 @@ import os import shutil from markdown import Markdown from markupsafe import Markup -from subprocess import Popen, PIPE VIDEO_MIMES = {'.mp4': 'video/mp4', '.webm': 'video/webm', @@ -95,15 +94,6 @@ def read_markdown(filename): return output -def call_subprocess(cmd): - """Wrapper to call ``subprocess.Popen`` and return stdout & stderr.""" - p = Popen(cmd, stdout=PIPE, stderr=PIPE) - stdout, stderr = p.communicate() - stderr = stderr.decode('utf8') - stdout = stdout.decode('utf8') - return p.returncode, stdout, stderr - - def is_valid_html5_video(ext): """Checks if ext is a supported HTML5 video.""" return ext in VIDEO_MIMES.keys() diff --git a/sigal/video.py b/sigal/video.py index e615ee1..742b1b7 100644 --- a/sigal/video.py +++ b/sigal/video.py @@ -25,11 +25,12 @@ import logging import os import re import shutil +import subprocess from os.path import splitext from . import image, utils from .settings import get_thumb, Status -from .utils import call_subprocess, is_valid_html5_video +from .utils import is_valid_html5_video class SubprocessException(Exception): @@ -43,16 +44,17 @@ def check_subprocess(cmd, source, outname): """ logger = logging.getLogger(__name__) try: - returncode, stdout, stderr = call_subprocess(cmd) + res = subprocess.run(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, encoding='utf8') except KeyboardInterrupt: logger.debug('Process terminated, removing file %s', outname) if os.path.isfile(outname): os.remove(outname) raise - if returncode: - logger.debug('STDOUT:\n %s', stdout) - logger.debug('STDERR:\n %s', stderr) + if res.returncode: + logger.debug('STDOUT:\n %s', res.stdout) + logger.debug('STDERR:\n %s', res.stderr) if os.path.isfile(outname): logger.debug('Removing file %s', outname) os.remove(outname) @@ -62,11 +64,12 @@ def check_subprocess(cmd, source, outname): def video_size(source, converter='ffmpeg'): """Returns the dimensions of the video.""" - ret, stdout, stderr = call_subprocess([converter, '-i', source]) + res = subprocess.run([converter, '-i', source], stderr=subprocess.PIPE, + encoding='utf8') pattern = re.compile(r'Stream.*Video.* ([0-9]+)x([0-9]+)') - match = pattern.search(stderr) + match = pattern.search(res.stderr) rot_pattern = re.compile(r'rotate\s*:\s*-?(90|270)') - rot_match = rot_pattern.search(stderr) + rot_match = rot_pattern.search(res.stderr) if match: x, y = int(match.groups()[0]), int(match.groups()[1]) diff --git a/tests/test_utils.py b/tests/test_utils.py index 83e3f04..d04f446 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -70,16 +70,6 @@ def test_read_markdown_empty_file(tmpdir): assert m['description'] == '' -def test_call_subprocess(): - returncode, stdout, stderr = utils.call_subprocess(['echo', 'ok']) - assert returncode == 0 - assert stdout == 'ok\n' - assert stderr == '' - - # returncode, stdout, stderr = utils.call_subprocess(['/usr/bin/false']) - # assert returncode == 1 - - def test_is_valid_html5_video(): assert utils.is_valid_html5_video('.webm') is True assert utils.is_valid_html5_video('.mpeg') is False