Use subprocess.run instead of custom wrapper
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user