Use with for resource access

This commit is contained in:
Labrys of Knossos 2022-12-18 12:55:16 -05:00
commit 1938fcc66a
7 changed files with 104 additions and 124 deletions

View file

@ -37,7 +37,7 @@ except ImportError:
def which(name): def which(name):
proc = subprocess.Popen(['which', name], stdout=PIPE) with subprocess.Popen(['which', name], stdout=PIPE) as proc:
try: try:
proc_out, proc_err = proc.communicate() proc_out, proc_err = proc.communicate()
except Exception: except Exception:
@ -544,7 +544,7 @@ def configure_remote_paths():
def configure_niceness(): def configure_niceness():
global NICENESS global NICENESS
try: try:
proc = subprocess.Popen(['nice'], stdout=DEVNULL, stderr=DEVNULL) with subprocess.Popen(['nice'], stdout=DEVNULL, stderr=DEVNULL) as proc:
proc.communicate() proc.communicate()
niceness = CFG['Posix']['niceness'] niceness = CFG['Posix']['niceness']
if ( if (
@ -556,7 +556,7 @@ def configure_niceness():
except Exception: except Exception:
pass pass
try: try:
proc = subprocess.Popen(['ionice'], stdout=DEVNULL, stderr=DEVNULL) with subprocess.Popen(['ionice'], stdout=DEVNULL, stderr=DEVNULL) as proc:
proc.communicate() proc.communicate()
try: try:
ionice = CFG['Posix']['ionice_class'] ionice = CFG['Posix']['ionice_class']

View file

@ -8,6 +8,7 @@ import stat
import subprocess import subprocess
from subprocess import call from subprocess import call
from subprocess import Popen from subprocess import Popen
from subprocess import DEVNULL
from time import sleep from time import sleep
import nzb2media import nzb2media
@ -87,35 +88,23 @@ def extract(file_path, output_destination):
} }
# Test command exists and if not, remove # Test command exists and if not, remove
if not os.getenv('TR_TORRENT_DIR'): if not os.getenv('TR_TORRENT_DIR'):
devnull = open(os.devnull, 'w')
for cmd in required_cmds: for cmd in required_cmds:
if call( if call(['which', cmd], stdout=DEVNULL, stderr=DEVNULL):
['which', cmd], # note, returns 0 if exists, or 1 if doesn't exist.
stdout=devnull,
stderr=devnull,
): # note, returns 0 if exists, or 1 if doesn't exist.
for key, val in extract_commands.items(): for key, val in extract_commands.items():
if cmd in val[0]: if cmd in val[0]:
if not call( if not call(['which', '7zr'], stdout=DEVNULL, stderr=DEVNULL):
['which', '7zr'], # we do have '7zr'
stdout=devnull,
stderr=devnull,
): # we do have '7zr'
extract_commands[key] = ['7zr', 'x', '-y'] extract_commands[key] = ['7zr', 'x', '-y']
elif not call( elif not call(['which', '7z'], stdout=DEVNULL, stderr=DEVNULL):
['which', '7z'], stdout=devnull, stderr=devnull, # we do have '7z'
): # we do have '7z'
extract_commands[key] = ['7z', 'x', '-y'] extract_commands[key] = ['7z', 'x', '-y']
elif not call( elif not call(['which', '7za'], stdout=DEVNULL, stderr=DEVNULL):
['which', '7za'], # we do have '7za'
stdout=devnull,
stderr=devnull,
): # we do have '7za'
extract_commands[key] = ['7za', 'x', '-y'] extract_commands[key] = ['7za', 'x', '-y']
else: else:
log.error(f'EXTRACTOR: {cmd} not found, disabling support for {key}') log.error(f'EXTRACTOR: {cmd} not found, disabling support for {key}')
del extract_commands[key] del extract_commands[key]
devnull.close()
else: else:
log.warning('EXTRACTOR: Cannot determine which tool to use when called from Transmission') log.warning('EXTRACTOR: Cannot determine which tool to use when called from Transmission')
@ -130,19 +119,10 @@ def extract(file_path, output_destination):
cmd = extract_commands[f'.tar{ext[1]}'] cmd = extract_commands[f'.tar{ext[1]}']
else: # Try gunzip else: # Try gunzip
cmd = extract_commands[ext[1]] cmd = extract_commands[ext[1]]
elif ext[1] in ('.1', '.01', '.001') and os.path.splitext(ext[0])[1] in ( elif ext[1] in ('.1', '.01', '.001') and os.path.splitext(ext[0])[1] in ('.rar', '.zip', '.7z'):
'.rar',
'.zip',
'.7z',
):
cmd = extract_commands[os.path.splitext(ext[0])[1]] cmd = extract_commands[os.path.splitext(ext[0])[1]]
elif ext[1] in ( elif ext[1] in ('.cb7', '.cba', '.cbr', '.cbt', '.cbz'):
'.cb7', # don't extract these comic book archives.
'.cba',
'.cbr',
'.cbt',
'.cbz',
): # don't extract these comic book archives.
return False return False
else: else:
if ext[1] in extract_commands: if ext[1] in extract_commands:
@ -157,9 +137,10 @@ def extract(file_path, output_destination):
if nzb2media.PASSWORDS_FILE and os.path.isfile( if nzb2media.PASSWORDS_FILE and os.path.isfile(
os.path.normpath(nzb2media.PASSWORDS_FILE), os.path.normpath(nzb2media.PASSWORDS_FILE),
): ):
with open(os.path.normpath(nzb2media.PASSWORDS_FILE)) as fin:
passwords = [ passwords = [
line.strip() line.strip()
for line in open(os.path.normpath(nzb2media.PASSWORDS_FILE)) for line in fin
] ]
else: else:
passwords = [] passwords = []
@ -179,7 +160,6 @@ def extract(file_path, output_destination):
os.chdir( os.chdir(
output_destination, output_destination,
) # Not all unpack commands accept full paths, so just extract into this directory ) # Not all unpack commands accept full paths, so just extract into this directory
devnull = open(os.devnull, 'w')
try: # now works same for nt and *nix try: # now works same for nt and *nix
info = None info = None
@ -192,9 +172,8 @@ 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.
res = Popen( with Popen(cmd2, stdout=DEVNULL, stderr=DEVNULL, startupinfo=info) as proc:
cmd2, stdout=devnull, stderr=devnull, startupinfo=info, res = proc.wait() # should extract files fine.
).wait() # should extract files fine.
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
@ -209,9 +188,7 @@ 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)
proc = Popen( with Popen(cmd2, stdout=DEVNULL, stderr=DEVNULL, startupinfo=info) as proc:
cmd2, stdout=devnull, stderr=devnull, startupinfo=info,
)
res = proc.wait() # should extract files fine. 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}')

View file

@ -177,7 +177,11 @@ def rename_script(dirname):
dirname = directory dirname = directory
break break
if rename_file: if rename_file:
rename_lines = [line.strip() for line in open(rename_file)] with open(rename_file) as fin:
rename_lines = [
line.strip()
for line in fin
]
for line in rename_lines: for line in rename_lines:
if re.search('^(mv|Move)', line, re.IGNORECASE): if re.search('^(mv|Move)', line, re.IGNORECASE):
cmd = shlex.split(line)[1:] cmd = shlex.split(line)[1:]
@ -219,7 +223,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(command, stdout=DEVNULL, stderr=DEVNULL) with subprocess.Popen(command, stdout=DEVNULL, stderr=DEVNULL) as proc:
proc.communicate() proc.communicate()
result = proc.returncode result = proc.returncode
except Exception: except Exception:

View file

@ -100,16 +100,16 @@ def is_video_good(video: pathlib.Path, status, require_lan=None):
def zip_out(file, img): def zip_out(file, img):
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:
proc = subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL) with subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL) as proc:
return proc
except Exception: except Exception:
log.error(f'Extracting [{file}] has failed') log.error(f'Extracting [{file}] has failed')
return proc return None
def get_video_details(videofile, img=None): def get_video_details(videofile, img=None):
@ -136,13 +136,15 @@ def get_video_details(videofile, img=None):
print_cmd(command) print_cmd(command)
if img: if img:
procin = zip_out(file, img) procin = zip_out(file, img)
proc = subprocess.Popen(command, stdout=PIPE, stdin=procin.stdout) with subprocess.Popen(command, stdout=PIPE, stdin=procin.stdout) as proc:
proc_out, proc_err = proc.communicate()
result = proc.returncode
procin.stdout.close() procin.stdout.close()
else: else:
proc = subprocess.Popen(command, stdout=PIPE) with subprocess.Popen(command, stdout=PIPE) as proc:
out, err = proc.communicate() proc_out, proc_err = proc.communicate()
result = proc.returncode result = proc.returncode
video_details = json.loads(out.decode()) video_details = json.loads(proc_out.decode())
except Exception: except Exception:
try: # try this again without -show error in case of ffmpeg limitation try: # try this again without -show error in case of ffmpeg limitation
command = [ command = [
@ -158,13 +160,15 @@ def get_video_details(videofile, img=None):
print_cmd(command) print_cmd(command)
if img: if img:
procin = zip_out(file, img) procin = zip_out(file, img)
proc = subprocess.Popen(command, stdout=PIPE, stdin=procin.stdout) with subprocess.Popen(command, stdout=PIPE, stdin=procin.stdout) as proc:
proc_out, proc_err = proc.communicate()
result = proc.returncode
procin.stdout.close() procin.stdout.close()
else: else:
proc = subprocess.Popen(command, stdout=PIPE) with subprocess.Popen(command, stdout=PIPE) as proc:
out, err = proc.communicate() proc_out, proc_err = proc.communicate()
result = proc.returncode result = proc.returncode
video_details = json.loads(out.decode()) video_details = json.loads(proc_out.decode())
except Exception: except Exception:
log.error(f'Checking [{file}] has failed') log.error(f'Checking [{file}] has failed')
return video_details, result return video_details, result
@ -804,9 +808,7 @@ def extract_subs(file, newfile_path):
print_cmd(command) print_cmd(command)
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( with subprocess.Popen(command, stdout=DEVNULL, stderr=DEVNULL) as proc:
command, stdout=DEVNULL, stderr=DEVNULL,
)
proc_out, proc_error = proc.communicate() proc_out, proc_error = proc.communicate()
result = proc.returncode result = proc.returncode
except Exception: except Exception:
@ -905,8 +907,8 @@ def mount_iso(item, new_dir): # Currently only supports Linux Mount when permis
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=PIPE, stderr=DEVNULL) with subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL) as proc:
out, err = proc.communicate() proc_out, proc_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.
) )
@ -956,13 +958,13 @@ def rip_iso(item, new_dir):
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=PIPE, stderr=DEVNULL) with subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL) as proc:
out, err = proc.communicate() proc_out, proc_err = proc.communicate()
file_match_gen = ( file_match_gen = (
re.match( re.match(
r'.+(VIDEO_TS[/\\]VTS_[0-9][0-9]_[0-9].[Vv][Oo][Bb])', line, r'.+(VIDEO_TS[/\\]VTS_[0-9][0-9]_[0-9].[Vv][Oo][Bb])', line,
) )
for line in out.decode().splitlines() for line in proc_out.decode().splitlines()
) )
file_list = [ file_list = [
file_match.groups()[0] file_match.groups()[0]
@ -994,7 +996,7 @@ def rip_iso(item, new_dir):
else: # check BlueRay for BDMV/STREAM/XXXX.MTS else: # check BlueRay for BDMV/STREAM/XXXX.MTS
mts_list_gen = ( mts_list_gen = (
re.match(r'.+(BDMV[/\\]STREAM[/\\][0-9]+[0-9].[Mm]).', line) re.match(r'.+(BDMV[/\\]STREAM[/\\][0-9]+[0-9].[Mm]).', line)
for line in out.decode().splitlines() for line in proc_out.decode().splitlines()
) )
mts_list = [ mts_list = [
file_match.groups()[0] file_match.groups()[0]
@ -1182,10 +1184,11 @@ 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(command, stdout=DEVNULL, stderr=PIPE) with subprocess.Popen(command, stdout=DEVNULL, stderr=PIPE) as proc:
out, err = proc.communicate()
else: else:
img, data = next(file.items()) img, data = next(file.items())
proc = subprocess.Popen(command, stdout=DEVNULL, stderr=PIPE, stdin=PIPE) with subprocess.Popen(command, stdout=DEVNULL, stderr=PIPE, stdin=PIPE) as proc:
for vob in data['files']: for vob in data['files']:
procin = zip_out(vob, img) procin = zip_out(vob, img)
if procin: if procin:
@ -1231,8 +1234,8 @@ 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=PIPE, stderr=DEVNULL) with subprocess.Popen(cmd, stdout=PIPE, stderr=DEVNULL) as proc:
out, err = proc.communicate() proc_out, proc_err = proc.communicate()
time.sleep(5) time.sleep(5)
os.rmdir(nzb2media.MOUNTED) os.rmdir(nzb2media.MOUNTED)
nzb2media.MOUNTED = None nzb2media.MOUNTED = None

View file

@ -118,20 +118,20 @@ 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:
proc = Popen(command) with Popen(command) as proc:
res = proc.wait() res = proc.wait()
if ( except Exception:
str(res) in nzb2media.USER_SCRIPT_SUCCESSCODES log.error(f'UserScript {command[0]} has failed')
): # Linux returns 0 for successful. result = 1
else:
if str(res) in nzb2media.USER_SCRIPT_SUCCESSCODES:
# Linux returns 0 for successful.
log.info(f'UserScript {command[0]} was successfull') log.info(f'UserScript {command[0]} was successfull')
result = 0 result = 0
else: else:
log.error(f'UserScript {command[0]} has failed with return code: {res}') log.error(f'UserScript {command[0]} has failed with return code: {res}')
log.info(f'If the UserScript completed successfully you should add {res} to the user_script_successCodes') log.info(f'If the UserScript completed successfully you should add {res} to the user_script_successCodes')
result = int(1) result = 1
except Exception:
log.error(f'UserScript {command[0]} has failed')
result = int(1)
final_result += result final_result += result
num_files_new = 0 num_files_new = 0

View file

@ -110,7 +110,7 @@ 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}')
proc = subprocess.Popen(popen_list, cwd=os.getcwd()) with subprocess.Popen(popen_list, cwd=os.getcwd()) as proc:
proc.wait() proc.wait()
status = proc.returncode status = proc.returncode

View file

@ -160,52 +160,49 @@ class GitUpdateManager(UpdateManager):
def _run_git(self, git_path, args): def _run_git(self, git_path, args):
proc_out = None result = None
proc_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')
proc_status = 1 proc_status = 1
return proc_out, proc_err, proc_status return result, 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}')
proc = subprocess.Popen( with subprocess.Popen(
cmd, cmd,
stdin=PIPE, stdin=PIPE,
stdout=PIPE, stdout=PIPE,
stderr=STDOUT, stderr=STDOUT,
shell=True, shell=True,
cwd=nzb2media.APP_ROOT, cwd=nzb2media.APP_ROOT,
) ) as proc:
proc_out, proc_err = proc.communicate() proc_out, proc_err = proc.communicate()
proc_status = proc.returncode proc_status = proc.returncode
proc_out = proc_out.decode('utf-8')
if proc_out:
proc_out = proc_out.strip()
if nzb2media.LOG_GIT: if nzb2media.LOG_GIT:
log.debug(f'git output: {proc_out}') msg = proc_out.decode('utf-8').strip()
log.debug(f'git output: {msg}')
except OSError: except OSError:
log.error(f'Command {cmd} didn\'t work') log.error(f'Command {cmd} didn\'t work')
proc_status = 1 proc_status = 1
proc_status = 128 if ('fatal:' in proc_out) or proc_err else proc_status proc_status = 128 if ('fatal:' in result) or proc_err else proc_status
if proc_status == 0: if proc_status == 0:
log.debug(f'{cmd} : returned successful') log.debug(f'{cmd} : returned successful')
proc_status = 0 proc_status = 0
elif nzb2media.LOG_GIT and proc_status in (1, 128): elif nzb2media.LOG_GIT and proc_status in (1, 128):
log.debug(f'{cmd} returned : {proc_out}') log.debug(f'{cmd} returned : {result}')
else: else:
if nzb2media.LOG_GIT: if nzb2media.LOG_GIT:
log.debug(f'{cmd} returned : {proc_out}, treat as error for now') log.debug(f'{cmd} returned : {result}, treat as error for now')
proc_status = 1 proc_status = 1
return proc_out, proc_err, proc_status return result, proc_err, proc_status
def _find_installed_version(self): def _find_installed_version(self):
""" """
@ -480,9 +477,8 @@ class SourceUpdateManager(UpdateManager):
# extract to sb-update dir # extract to sb-update dir
log.info(f'Extracting file {tar_download_path}') log.info(f'Extracting file {tar_download_path}')
tar = tarfile.open(tar_download_path) with tarfile.open(tar_download_path) as tar:
tar.extractall(sb_update_dir) tar.extractall(sb_update_dir)
tar.close()
# delete .tar.gz # delete .tar.gz
log.info(f'Deleting file {tar_download_path}') log.info(f'Deleting file {tar_download_path}')