Refactor subprocess.Popen calls

This commit is contained in:
Labrys of Knossos 2022-12-18 03:56:58 -05:00
commit 71a242ccc1
7 changed files with 127 additions and 239 deletions

View file

@ -11,6 +11,7 @@ import subprocess
import sys import sys
import time import time
import typing import typing
from subprocess import PIPE, DEVNULL
from nzb2media import main_db from nzb2media import main_db
from nzb2media import version_check from nzb2media import version_check
@ -35,6 +36,16 @@ except ImportError:
sys.exit('Please install pywin32') sys.exit('Please install pywin32')
def which(name):
proc = subprocess.Popen(['which', name], stdout=PIPE)
try:
proc_out, proc_err = proc.communicate()
except Exception:
return ''
else:
return proc_out.strip().decode()
def module_path(module=__file__): def module_path(module=__file__):
try: try:
path = pathlib.Path(module.__file__) path = pathlib.Path(module.__file__)
@ -288,7 +299,7 @@ MOUNTED = None
GETSUBS = False GETSUBS = False
TRANSCODE = None TRANSCODE = None
CONCAT = None CONCAT = None
FFMPEG_PATH = None FFMPEG_PATH = ''
SYS_PATH = None SYS_PATH = None
DUPLICATE = None DUPLICATE = None
IGNOREEXTENSIONS = [] IGNOREEXTENSIONS = []
@ -532,40 +543,36 @@ def configure_remote_paths():
def configure_niceness(): def configure_niceness():
global NICENESS global NICENESS
try:
with open(os.devnull, 'w') as devnull: proc = subprocess.Popen(['nice'], stdout=DEVNULL, stderr=DEVNULL)
proc.communicate()
niceness = CFG['Posix']['niceness']
if (
len(niceness.split(',')) > 1
): # Allow passing of absolute command, not just value.
NICENESS.extend(niceness.split(','))
else:
NICENESS.extend(['nice', f'-n{int(niceness)}'])
except Exception:
pass
try:
proc = subprocess.Popen(['ionice'], stdout=DEVNULL, stderr=DEVNULL)
proc.communicate()
try: try:
subprocess.Popen( ionice = CFG['Posix']['ionice_class']
['nice'], stdout=devnull, stderr=devnull, NICENESS.extend(['ionice', f'-c{int(ionice)}'])
).communicate() except Exception:
niceness = CFG['Posix']['niceness'] pass
if ( try:
len(niceness.split(',')) > 1 if 'ionice' in NICENESS:
): # Allow passing of absolute command, not just value. ionice = CFG['Posix']['ionice_classdata']
NICENESS.extend(niceness.split(',')) NICENESS.extend([f'-n{int(ionice)}'])
else: else:
NICENESS.extend(['nice', f'-n{int(niceness)}']) NICENESS.extend(['ionice', f'-n{int(ionice)}'])
except Exception:
pass
try:
subprocess.Popen(
['ionice'], stdout=devnull, stderr=devnull,
).communicate()
try:
ionice = CFG['Posix']['ionice_class']
NICENESS.extend(['ionice', f'-c{int(ionice)}'])
except Exception:
pass
try:
if 'ionice' in NICENESS:
ionice = CFG['Posix']['ionice_classdata']
NICENESS.extend([f'-n{int(ionice)}'])
else:
NICENESS.extend(['ionice', f'-n{int(ionice)}'])
except Exception:
pass
except Exception: except Exception:
pass pass
except Exception:
pass
def configure_containers(): def configure_containers():
@ -1413,123 +1420,36 @@ def configure_utility_locations():
else: else:
if SYS_PATH: if SYS_PATH:
os.environ['PATH'] += ':' + SYS_PATH os.environ['PATH'] += ':' + SYS_PATH
try:
SEVENZIP = ( SEVENZIP = which('7z') or which('7zr') or which('7za')
subprocess.Popen(['which', '7z'], stdout=subprocess.PIPE)
.communicate()[0]
.strip()
.decode()
)
except Exception:
pass
if not SEVENZIP: if not SEVENZIP:
try:
SEVENZIP = (
subprocess.Popen(['which', '7zr'], stdout=subprocess.PIPE)
.communicate()[0]
.strip()
.decode()
)
except Exception:
pass
if not SEVENZIP:
try:
SEVENZIP = (
subprocess.Popen(['which', '7za'], stdout=subprocess.PIPE)
.communicate()[0]
.strip()
.decode()
)
except Exception:
pass
if not SEVENZIP:
SEVENZIP = None
log.warning('Failed to locate 7zip. Transcoding of disk images and extraction of .7z files will not be possible!') log.warning('Failed to locate 7zip. Transcoding of disk images and extraction of .7z files will not be possible!')
try:
PAR2CMD = ( PAR2CMD = which('par2')
subprocess.Popen(['which', 'par2'], stdout=subprocess.PIPE)
.communicate()[0]
.strip()
.decode()
)
except Exception:
pass
if not PAR2CMD: if not PAR2CMD:
PAR2CMD = None PAR2CMD = None
log.warning('Failed to locate par2. Repair and rename using par files will not be possible!') log.warning('Failed to locate par2. Repair and rename using par files will not be possible!')
if os.path.isfile(os.path.join(FFMPEG_PATH, 'ffmpeg')) or os.access(
os.path.join(FFMPEG_PATH, 'ffmpeg'), ffmpeg_bin = os.path.join(FFMPEG_PATH, 'ffmpeg')
os.X_OK, avconv_bin = os.path.join(FFMPEG_PATH, 'avconv')
): if os.path.isfile(ffmpeg_bin) or os.access(ffmpeg_bin, os.X_OK):
FFMPEG = os.path.join(FFMPEG_PATH, 'ffmpeg') FFMPEG = ffmpeg_bin
elif os.path.isfile(os.path.join(FFMPEG_PATH, 'avconv')) or os.access( elif os.path.isfile(avconv_bin) or os.access(avconv_bin, os.X_OK):
os.path.join(FFMPEG_PATH, 'avconv'), FFMPEG = avconv_bin
os.X_OK,
):
FFMPEG = os.path.join(FFMPEG_PATH, 'avconv')
else: else:
try: FFMPEG = which('ffmpeg') or which('avconv')
FFMPEG = (
subprocess.Popen(
['which', 'ffmpeg'], stdout=subprocess.PIPE,
)
.communicate()[0]
.strip()
.decode()
)
except Exception:
pass
if not FFMPEG:
try:
FFMPEG = (
subprocess.Popen(
['which', 'avconv'], stdout=subprocess.PIPE,
)
.communicate()[0]
.strip()
.decode()
)
except Exception:
pass
if not FFMPEG: if not FFMPEG:
FFMPEG = None FFMPEG = None
log.warning('Failed to locate ffmpeg. Transcoding disabled!') log.warning('Failed to locate ffmpeg. Transcoding disabled!')
log.warning('Install ffmpeg with x264 support to enable this feature ...') log.warning('Install ffmpeg with x264 support to enable this feature ...')
ffprobe_bin = os.path.join(FFMPEG_PATH, 'ffprobe')
if os.path.isfile(os.path.join(FFMPEG_PATH, 'ffprobe')) or os.access( avprobe_bin = os.path.join(FFMPEG_PATH, 'avprobe')
os.path.join(FFMPEG_PATH, 'ffprobe'), if os.path.isfile(ffprobe_bin) or os.access(ffprobe_bin, os.X_OK):
os.X_OK, FFPROBE = ffprobe_bin
): elif os.path.isfile(avprobe_bin) or os.access(avprobe_bin, os.X_OK):
FFPROBE = os.path.join(FFMPEG_PATH, 'ffprobe') FFPROBE = avprobe_bin
elif os.path.isfile(os.path.join(FFMPEG_PATH, 'avprobe')) or os.access(
os.path.join(FFMPEG_PATH, 'avprobe'),
os.X_OK,
):
FFPROBE = os.path.join(FFMPEG_PATH, 'avprobe')
else: else:
try: FFPROBE = which('ffprobe') or which('avprobe')
FFPROBE = (
subprocess.Popen(
['which', 'ffprobe'], stdout=subprocess.PIPE,
)
.communicate()[0]
.strip()
.decode()
)
except Exception:
pass
if not FFPROBE:
try:
FFPROBE = (
subprocess.Popen(
['which', 'avprobe'], stdout=subprocess.PIPE,
)
.communicate()[0]
.strip()
.decode()
)
except Exception:
pass
if not FFPROBE: if not FFPROBE:
FFPROBE = None FFPROBE = None
if CHECK_MEDIA: if CHECK_MEDIA:

View file

@ -192,10 +192,9 @@ def extract(file_path, output_destination):
cmd2 = cmd cmd2 = cmd
if 'gunzip' not in cmd: # gunzip doesn't support password if 'gunzip' not in cmd: # gunzip doesn't support password
cmd2.append('-p-') # don't prompt for password. cmd2.append('-p-') # don't prompt for password.
p = Popen( res = Popen(
cmd2, stdout=devnull, stderr=devnull, startupinfo=info, cmd2, stdout=devnull, stderr=devnull, startupinfo=info,
) # should extract files fine. ).wait() # should extract files fine.
res = p.wait()
if res == 0: # Both Linux and Windows return 0 for successful. if res == 0: # Both Linux and Windows return 0 for successful.
log.info(f'EXTRACTOR: Extraction was successful for {file_path} to {output_destination}') log.info(f'EXTRACTOR: Extraction was successful for {file_path} to {output_destination}')
success = 1 success = 1
@ -210,10 +209,10 @@ def extract(file_path, output_destination):
# append password here. # append password here.
passcmd = f'-p{password}' passcmd = f'-p{password}'
cmd2.append(passcmd) cmd2.append(passcmd)
p = Popen( proc = Popen(
cmd2, stdout=devnull, stderr=devnull, startupinfo=info, cmd2, stdout=devnull, stderr=devnull, startupinfo=info,
) # should extract files fine. )
res = p.wait() res = proc.wait() # should extract files fine.
if (res >= 0 and platform == 'Windows') or res == 0: if (res >= 0 and platform == 'Windows') or res == 0:
log.info(f'EXTRACTOR: Extraction was successful for {file_path} to {output_destination} using password: {password}') log.info(f'EXTRACTOR: Extraction was successful for {file_path} to {output_destination} using password: {password}')
success = 1 success = 1

View file

@ -2,10 +2,10 @@ from __future__ import annotations
import logging import logging
import os import os
import platform
import re import re
import shlex import shlex
import subprocess import subprocess
from subprocess import DEVNULL
import nzb2media import nzb2media
from nzb2media.utils.files import list_media_files from nzb2media.utils.files import list_media_files
@ -212,10 +212,6 @@ def par2(dirname):
if nzb2media.PAR2CMD and parfile: if nzb2media.PAR2CMD and parfile:
pwd = os.getcwd() # Get our Present Working Directory pwd = os.getcwd() # Get our Present Working Directory
os.chdir(dirname) # set directory to run par on. os.chdir(dirname) # set directory to run par on.
if platform.system() == 'Windows':
bitbucket = open('NUL')
else:
bitbucket = open('/dev/null')
log.info(f'Running par2 on file {parfile}.') log.info(f'Running par2 on file {parfile}.')
command = [nzb2media.PAR2CMD, 'r', parfile, '*'] command = [nzb2media.PAR2CMD, 'r', parfile, '*']
cmd = '' cmd = ''
@ -223,9 +219,7 @@ def par2(dirname):
cmd = f'{cmd} {item}' cmd = f'{cmd} {item}'
log.debug(f'calling command:{cmd}') log.debug(f'calling command:{cmd}')
try: try:
proc = subprocess.Popen( proc = subprocess.Popen(command, stdout=DEVNULL, stderr=DEVNULL)
command, stdout=bitbucket, stderr=bitbucket,
)
proc.communicate() proc.communicate()
result = proc.returncode result = proc.returncode
except Exception: except Exception:
@ -233,7 +227,6 @@ def par2(dirname):
if result == 0: if result == 0:
log.info('par2 file processing succeeded') log.info('par2 file processing succeeded')
os.chdir(pwd) os.chdir(pwd)
bitbucket.close()
# dict for custom groups # dict for custom groups

View file

@ -11,6 +11,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import time import time
from subprocess import PIPE, DEVNULL
from babelfish import Language from babelfish import Language
@ -100,22 +101,20 @@ def is_video_good(video: pathlib.Path, status, require_lan=None):
return False return False
def zip_out(file, img, bitbucket): def zip_out(file, img):
procin = None proc = None
if os.path.isfile(file): if os.path.isfile(file):
cmd = ['cat', file] cmd = ['cat', file]
else: else:
cmd = [nzb2media.SEVENZIP, '-so', 'e', img, file] cmd = [nzb2media.SEVENZIP, '-so', 'e', img, file]
try: try:
procin = subprocess.Popen( proc = subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL)
cmd, stdout=subprocess.PIPE, stderr=bitbucket,
)
except Exception: except Exception:
log.error(f'Extracting [{file}] has failed') log.error(f'Extracting [{file}] has failed')
return procin return proc
def get_video_details(videofile, img=None, bitbucket=None): def get_video_details(videofile, img=None):
video_details = {} video_details = {}
result = 1 result = 1
file = videofile file = videofile
@ -138,13 +137,11 @@ def get_video_details(videofile, img=None, bitbucket=None):
] ]
print_cmd(command) print_cmd(command)
if img: if img:
procin = zip_out(file, img, bitbucket) procin = zip_out(file, img)
proc = subprocess.Popen( proc = subprocess.Popen(command, stdout=PIPE, stdin=procin.stdout)
command, stdout=subprocess.PIPE, stdin=procin.stdout,
)
procin.stdout.close() procin.stdout.close()
else: else:
proc = subprocess.Popen(command, stdout=subprocess.PIPE) proc = subprocess.Popen(command, stdout=PIPE)
out, err = proc.communicate() out, err = proc.communicate()
result = proc.returncode result = proc.returncode
video_details = json.loads(out.decode()) video_details = json.loads(out.decode())
@ -162,13 +159,11 @@ def get_video_details(videofile, img=None, bitbucket=None):
] ]
print_cmd(command) print_cmd(command)
if img: if img:
procin = zip_out(file, img, bitbucket) procin = zip_out(file, img)
proc = subprocess.Popen( proc = subprocess.Popen(command, stdout=PIPE, stdin=procin.stdout)
command, stdout=subprocess.PIPE, stdin=procin.stdout,
)
procin.stdout.close() procin.stdout.close()
else: else:
proc = subprocess.Popen(command, stdout=subprocess.PIPE) proc = subprocess.Popen(command, stdout=PIPE)
out, err = proc.communicate() out, err = proc.communicate()
result = proc.returncode result = proc.returncode
video_details = json.loads(out.decode()) video_details = json.loads(out.decode())
@ -200,7 +195,7 @@ def check_vid_file(video_details, result):
return False return False
def build_commands(file, new_dir, movie_name, bitbucket): def build_commands(file, new_dir, movie_name):
if isinstance(file, str): if isinstance(file, str):
input_file = file input_file = file
if 'concat:' in file: if 'concat:' in file:
@ -228,16 +223,14 @@ def build_commands(file, new_dir, movie_name, bitbucket):
new_file = [] new_file = []
rem_vid = [] rem_vid = []
for vid in data['files']: for vid in data['files']:
video_details, result = get_video_details(vid, img, bitbucket) video_details, result = get_video_details(vid, img)
if not check_vid_file( if not check_vid_file(
video_details, result, video_details, result,
): # lets not transcode menu or other clips that don't have audio and video. ): # lets not transcode menu or other clips that don't have audio and video.
rem_vid.append(vid) rem_vid.append(vid)
data['files'] = [f for f in data['files'] if f not in rem_vid] data['files'] = [f for f in data['files'] if f not in rem_vid]
new_file = {img: {'name': data['name'], 'files': data['files']}} new_file = {img: {'name': data['name'], 'files': data['files']}}
video_details, result = get_video_details( video_details, result = get_video_details(data['files'][0], img)
data['files'][0], img, bitbucket,
)
input_file = '-' input_file = '-'
file = '-' file = '-'
@ -752,7 +745,7 @@ def get_subs(file):
return subfiles return subfiles
def extract_subs(file, newfile_path, bitbucket): def extract_subs(file, newfile_path):
video_details, result = get_video_details(file) video_details, result = get_video_details(file)
if not video_details: if not video_details:
return return
@ -815,9 +808,9 @@ def extract_subs(file, newfile_path, bitbucket):
result = 1 # set result to failed in case call fails. result = 1 # set result to failed in case call fails.
try: try:
proc = subprocess.Popen( proc = subprocess.Popen(
command, stdout=bitbucket, stderr=bitbucket, command, stdout=DEVNULL, stderr=DEVNULL,
) )
out, err = proc.communicate() proc_out, proc_error = proc.communicate()
result = proc.returncode result = proc.returncode
except Exception: except Exception:
log.error('Extracting subtitle has failed') log.error('Extracting subtitle has failed')
@ -832,7 +825,7 @@ def extract_subs(file, newfile_path, bitbucket):
log.error('Extracting subtitles has failed') log.error('Extracting subtitles has failed')
def process_list(it, new_dir, bitbucket): def process_list(it, new_dir):
rem_list = [] rem_list = []
new_list = [] new_list = []
combine = [] combine = []
@ -846,7 +839,7 @@ def process_list(it, new_dir, bitbucket):
and ext not in nzb2media.IGNOREEXTENSIONS and ext not in nzb2media.IGNOREEXTENSIONS
): ):
log.debug(f'Attempting to rip disk image: {item}') log.debug(f'Attempting to rip disk image: {item}')
new_list.extend(rip_iso(item, new_dir, bitbucket)) new_list.extend(rip_iso(item, new_dir))
rem_list.append(item) rem_list.append(item)
elif ( elif (
re.match('.+VTS_[0-9][0-9]_[0-9].[Vv][Oo][Bb]', item) re.match('.+VTS_[0-9][0-9]_[0-9].[Vv][Oo][Bb]', item)
@ -907,9 +900,7 @@ def process_list(it, new_dir, bitbucket):
return it, rem_list, new_list, success return it, rem_list, new_list, success
def mount_iso( def mount_iso(item, new_dir): # Currently only supports Linux Mount when permissions allow.
item, new_dir, bitbucket,
): # Currently only supports Linux Mount when permissions allow.
if platform.system() == 'Windows': if platform.system() == 'Windows':
log.error(f'No mounting options available under Windows for image file {item}') log.error(f'No mounting options available under Windows for image file {item}')
return [] return []
@ -917,7 +908,7 @@ def mount_iso(
make_dir(mount_point) make_dir(mount_point)
cmd = ['mount', '-o', 'loop', item, mount_point] cmd = ['mount', '-o', 'loop', item, mount_point]
print_cmd(cmd) print_cmd(cmd)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=bitbucket) proc = subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL)
out, err = proc.communicate() out, err = proc.communicate()
nzb2media.MOUNTED = ( nzb2media.MOUNTED = (
mount_point # Allows us to verify this has been done and then cleanup. mount_point # Allows us to verify this has been done and then cleanup.
@ -951,16 +942,15 @@ def mount_iso(
return ['failure'] # If we got here, nothing matched our criteria return ['failure'] # If we got here, nothing matched our criteria
def rip_iso(item, new_dir, bitbucket): def rip_iso(item, new_dir):
new_files = [] new_files = []
failure_dir = 'failure' failure_dir = 'failure'
# Mount the ISO in your OS and call combineVTS. # Mount the ISO in your OS and call combineVTS.
if not nzb2media.SEVENZIP: if not nzb2media.SEVENZIP:
log.debug(f'No 7zip installed. Attempting to mount image file {item}') log.debug(f'No 7zip installed. Attempting to mount image file {item}')
try: try:
new_files = mount_iso( # Currently only works for Linux.
item, new_dir, bitbucket, new_files = mount_iso(item, new_dir)
) # Currently only works for Linux.
except Exception: except Exception:
log.error(f'Failed to mount and extract from image file {item}') log.error(f'Failed to mount and extract from image file {item}')
new_files = [failure_dir] new_files = [failure_dir]
@ -969,7 +959,7 @@ def rip_iso(item, new_dir, bitbucket):
try: try:
log.debug(f'Attempting to extract .vob or .mts from image file {item}') log.debug(f'Attempting to extract .vob or .mts from image file {item}')
print_cmd(cmd) print_cmd(cmd)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=bitbucket) proc = subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL)
out, err = proc.communicate() out, err = proc.communicate()
file_match_gen = ( file_match_gen = (
re.match( re.match(
@ -1040,7 +1030,7 @@ def rip_iso(item, new_dir, bitbucket):
new_files.append({item: {'name': name, 'files': combined}}) new_files.append({item: {'name': name, 'files': combined}})
if not new_files: if not new_files:
log.error(f'No VIDEO_TS or BDMV/SOURCE folder found in image file. Attempting to mount and scan {item}') log.error(f'No VIDEO_TS or BDMV/SOURCE folder found in image file. Attempting to mount and scan {item}')
new_files = mount_iso(item, new_dir, bitbucket) new_files = mount_iso(item, new_dir)
except Exception: except Exception:
log.error(f'Failed to extract from image file {item}') log.error(f'Failed to extract from image file {item}')
new_files = [failure_dir] new_files = [failure_dir]
@ -1159,19 +1149,12 @@ def transcode_directory(dir_name):
make_dir(new_dir) make_dir(new_dir)
else: else:
new_dir = dir_name new_dir = dir_name
if platform.system() == 'Windows':
bitbucket = open('NUL')
else:
bitbucket = open('/dev/null')
movie_name = os.path.splitext(os.path.split(dir_name)[1])[0] movie_name = os.path.splitext(os.path.split(dir_name)[1])[0]
file_list = nzb2media.list_media_files( file_list = nzb2media.list_media_files(
dir_name, media=True, audio=False, meta=False, archives=False, dir_name, media=True, audio=False, meta=False, archives=False,
) )
file_list, rem_list, new_list, success = process_list( file_list, rem_list, new_list, success = process_list(file_list, new_dir)
file_list, new_dir, bitbucket,
)
if not success: if not success:
bitbucket.close()
return 1, dir_name return 1, dir_name
for file in file_list: for file in file_list:
@ -1180,12 +1163,12 @@ def transcode_directory(dir_name):
and os.path.splitext(file)[1] in nzb2media.IGNOREEXTENSIONS and os.path.splitext(file)[1] in nzb2media.IGNOREEXTENSIONS
): ):
continue continue
command, file = build_commands(file, new_dir, movie_name, bitbucket) command, file = build_commands(file, new_dir, movie_name)
newfile_path = command[-1] newfile_path = command[-1]
# transcoding files may remove the original file, so make sure to extract subtitles first # transcoding files may remove the original file, so make sure to extract subtitles first
if nzb2media.SEXTRACT and isinstance(file, str): if nzb2media.SEXTRACT and isinstance(file, str):
extract_subs(file, newfile_path, bitbucket) extract_subs(file, newfile_path)
try: # Try to remove the file that we're transcoding to just in case. (ffmpeg will return an error if it already exists for some reason) try: # Try to remove the file that we're transcoding to just in case. (ffmpeg will return an error if it already exists for some reason)
os.remove(newfile_path) os.remove(newfile_path)
@ -1202,19 +1185,12 @@ def transcode_directory(dir_name):
result = 1 # set result to failed in case call fails. result = 1 # set result to failed in case call fails.
try: try:
if isinstance(file, str): if isinstance(file, str):
proc = subprocess.Popen( proc = subprocess.Popen(command, stdout=DEVNULL, stderr=PIPE)
command, stdout=bitbucket, stderr=subprocess.PIPE,
)
else: else:
img, data = next(file.items()) img, data = next(file.items())
proc = subprocess.Popen( proc = subprocess.Popen(command, stdout=DEVNULL, stderr=PIPE, stdin=PIPE)
command,
stdout=bitbucket,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
)
for vob in data['files']: for vob in data['files']:
procin = zip_out(vob, img, bitbucket) procin = zip_out(vob, img)
if procin: if procin:
log.debug(f'Feeding in file: {vob} to Transcoder') log.debug(f'Feeding in file: {vob} to Transcoder')
shutil.copyfileobj(procin.stdout, proc.stdin) shutil.copyfileobj(procin.stdout, proc.stdin)
@ -1258,7 +1234,7 @@ def transcode_directory(dir_name):
time.sleep(5) # play it safe and avoid failing to unmount. time.sleep(5) # play it safe and avoid failing to unmount.
cmd = ['umount', '-l', nzb2media.MOUNTED] cmd = ['umount', '-l', nzb2media.MOUNTED]
print_cmd(cmd) print_cmd(cmd)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=bitbucket) proc = subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL)
out, err = proc.communicate() out, err = proc.communicate()
time.sleep(5) time.sleep(5)
os.rmdir(nzb2media.MOUNTED) os.rmdir(nzb2media.MOUNTED)
@ -1278,5 +1254,4 @@ def transcode_directory(dir_name):
not nzb2media.PROCESSOUTPUT and nzb2media.DUPLICATE not nzb2media.PROCESSOUTPUT and nzb2media.DUPLICATE
): # We postprocess the original files to CP/SB ): # We postprocess the original files to CP/SB
new_dir = dir_name new_dir = dir_name
bitbucket.close()
return final_result, new_dir return final_result, new_dir

View file

@ -118,8 +118,8 @@ def external_script(output_destination, torrent_name, torrent_label, settings):
cmd = f'{cmd} {item}' cmd = f'{cmd} {item}'
log.info(f'Running script {cmd} on file {file_path}.') log.info(f'Running script {cmd} on file {file_path}.')
try: try:
p = Popen(command) proc = Popen(command)
res = p.wait() res = proc.wait()
if ( if (
str(res) in nzb2media.USER_SCRIPT_SUCCESSCODES str(res) in nzb2media.USER_SCRIPT_SUCCESSCODES
): # Linux returns 0 for successful. ): # Linux returns 0 for successful.

View file

@ -111,8 +111,8 @@ def restart():
if popen_list: if popen_list:
popen_list += nzb2media.SYS_ARGV popen_list += nzb2media.SYS_ARGV
log.info(f'Restarting nzbToMedia with {popen_list}') log.info(f'Restarting nzbToMedia with {popen_list}')
p = subprocess.Popen(popen_list, cwd=os.getcwd()) proc = subprocess.Popen(popen_list, cwd=os.getcwd())
p.wait() proc.wait()
status = p.returncode status = proc.returncode
os._exit(status) os._exit(status)

View file

@ -11,6 +11,7 @@ import stat
import subprocess import subprocess
import tarfile import tarfile
import traceback import traceback
from subprocess import PIPE, STDOUT
from urllib.request import urlretrieve from urllib.request import urlretrieve
import nzb2media import nzb2media
@ -161,52 +162,52 @@ class GitUpdateManager(UpdateManager):
def _run_git(self, git_path, args): def _run_git(self, git_path, args):
output = None proc_out = None
err = None proc_err = None
if not git_path: if not git_path:
log.debug('No git specified, can\'t use git commands') log.debug('No git specified, can\'t use git commands')
exit_status = 1 proc_status = 1
return output, err, exit_status return proc_out, proc_err, proc_status
cmd = f'{git_path} {args}' cmd = f'{git_path} {args}'
try: try:
log.debug(f'Executing {cmd} with your shell in {nzb2media.APP_ROOT}') log.debug(f'Executing {cmd} with your shell in {nzb2media.APP_ROOT}')
p = subprocess.Popen( proc = subprocess.Popen(
cmd, cmd,
stdin=subprocess.PIPE, stdin=PIPE,
stdout=subprocess.PIPE, stdout=PIPE,
stderr=subprocess.STDOUT, stderr=STDOUT,
shell=True, shell=True,
cwd=nzb2media.APP_ROOT, cwd=nzb2media.APP_ROOT,
) )
output, err = p.communicate() proc_out, proc_err = proc.communicate()
exit_status = p.returncode proc_status = proc.returncode
output = output.decode('utf-8') proc_out = proc_out.decode('utf-8')
if output: if proc_out:
output = output.strip() proc_out = proc_out.strip()
if nzb2media.LOG_GIT: if nzb2media.LOG_GIT:
log.debug(f'git output: {output}') log.debug(f'git output: {proc_out}')
except OSError: except OSError:
log.error(f'Command {cmd} didn\'t work') log.error(f'Command {cmd} didn\'t work')
exit_status = 1 proc_status = 1
exit_status = 128 if ('fatal:' in output) or err else exit_status proc_status = 128 if ('fatal:' in proc_out) or proc_err else proc_status
if exit_status == 0: if proc_status == 0:
log.debug(f'{cmd} : returned successful') log.debug(f'{cmd} : returned successful')
exit_status = 0 proc_status = 0
elif nzb2media.LOG_GIT and exit_status in (1, 128): elif nzb2media.LOG_GIT and proc_status in (1, 128):
log.debug(f'{cmd} returned : {output}') log.debug(f'{cmd} returned : {proc_out}')
else: else:
if nzb2media.LOG_GIT: if nzb2media.LOG_GIT:
log.debug(f'{cmd} returned : {output}, treat as error for now') log.debug(f'{cmd} returned : {proc_out}, treat as error for now')
exit_status = 1 proc_status = 1
return output, err, exit_status return proc_out, proc_err, proc_status
def _find_installed_version(self): def _find_installed_version(self):
""" """