Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cleanpp #3447

Merged
merged 5 commits into from
Dec 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 47 additions & 51 deletions medusa/post_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
ShowDirectoryNotFoundException,
)
from medusa.helpers import is_subtitle, verify_freespace
from medusa.helpers.utils import generate
from medusa.name_parser.parser import (
InvalidNameException,
InvalidShowException,
Expand Down Expand Up @@ -316,38 +317,36 @@ def _delete(self, files, associated_files=False):
:param files: path(s) to file(s) that should be deleted
:param associated_files: True to delete all files which differ only by extension, False to leave them
"""
if not files:
return

# Check if files is a list, if not, make it one
if not isinstance(files, list):
file_list = [files]
else:
file_list = files
gen_files = generate(files or [])
files = list(gen_files)

# also delete associated files, works only for 1 file
if associated_files and len(file_list) == 1:
file_list += self.list_associated_files(file_list[0], subfolders=True)
if associated_files and len(files) == 1:
files += self.list_associated_files(files[0], subfolders=True)

for cur_file in file_list:
if os.path.isfile(cur_file):
self.log(u'Deleting file: {0}'.format(cur_file), logger.DEBUG)
for filename in files:
if os.path.isfile(filename):
self.log(u'Deleting file: {0}'.format(filename), logger.DEBUG)
# check first the read-only attribute
file_attribute = os.stat(cur_file)[0]
file_attribute = os.stat(filename)[0]
if not file_attribute & stat.S_IWRITE:
# File is read-only, so make it writeable
self.log(u'Read only mode on file {0}. Will try to make it writeable'.format
(cur_file), logger.DEBUG)
self.log(u'Read only mode on file {0}. '
u'Will try to make it writeable'.format(filename),
logger.DEBUG)
try:
os.chmod(cur_file, stat.S_IWRITE)
os.chmod(filename, stat.S_IWRITE)
except OSError as error:
self.log(u'Cannot change permissions of {filename}. Error: {msg}'.format
(filename=cur_file, msg=error), logger.WARNING)
self.log(
u'Cannot change permissions for {path}. '
u'Error: {msg}'.format(path=filename, msg=error),
logger.WARNING
)

os.remove(cur_file)
os.remove(filename)

# do the library update for synoindex
notifiers.synoindex_notifier.deleteFile(cur_file)
notifiers.synoindex_notifier.deleteFile(filename)

@staticmethod
def rename_associated_file(new_path, new_basename, filepath):
Expand Down Expand Up @@ -907,53 +906,50 @@ def _run_extra_scripts(self, ep_obj):
if not app.EXTRA_SCRIPTS:
return

file_path = self.file_path
if isinstance(file_path, text_type):
try:
file_path = file_path.encode(app.SYS_ENCODING)
except UnicodeEncodeError:
# ignore it
pass
def _attempt_to_encode(item, _encoding):
if isinstance(item, text_type):
try:
item = item.encode(_encoding)
except UnicodeEncodeError:
pass # ignore it
finally:
return item

ep_location = ep_obj.location
if isinstance(ep_location, text_type):
try:
ep_location = ep_location.encode(app.SYS_ENCODING)
except UnicodeEncodeError:
# ignore it
pass
encoding = app.SYS_ENCODING

file_path = _attempt_to_encode(self.file_path, encoding)
ep_location = _attempt_to_encode(ep_obj.location, encoding)
indexer_id = str(ep_obj.series.indexerid)
season = str(ep_obj.season)
episode = str(ep_obj.episode)
airdate = str(ep_obj.airdate)

for cur_script_name in app.EXTRA_SCRIPTS:
if isinstance(cur_script_name, text_type):
try:
cur_script_name = cur_script_name.encode(app.SYS_ENCODING)
except UnicodeEncodeError:
# ignore it
pass
cur_script_name = _attempt_to_encode(cur_script_name, encoding)

# generate a safe command line string to execute the script and provide all the parameters
script_cmd = [piece for piece in re.split(r'(\'.*?\'|".*?"| )', cur_script_name) if piece.strip()]
script_cmd[0] = os.path.abspath(script_cmd[0])
self.log(u'Absolute path to script: {0}'.format(script_cmd[0]), logger.DEBUG)

script_cmd += [
ep_location, file_path, str(ep_obj.series.indexerid),
str(ep_obj.season), str(ep_obj.episode), str(ep_obj.airdate)
]
script_cmd += [ep_location, file_path, indexer_id, season, episode, airdate]

# use subprocess to run the command and capture output
self.log(u'Executing command: {0}'.format(script_cmd))
try:
p = subprocess.Popen(
script_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=app.PROG_DIR
process = subprocess.Popen(
script_cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=app.PROG_DIR
)
out, _ = p.communicate()
out, _ = process.communicate()

self.log(u'Script result: {0}'.format(out), logger.DEBUG)

except Exception as e:
self.log(u'Unable to run extra_script: {0!r}'.format(e))
except Exception as error:
self.log(u'Unable to run extra_script: {0!r}'.format(error))

def flag_kodi_clean_library(self):
"""Set flag to clean Kodi's library if Kodi is enabled."""
Expand Down