Updated post-process code.

This commit is contained in:
echel0n 2014-06-06 17:44:44 -07:00
parent 64b857ee57
commit 08159e2872

View file

@ -37,6 +37,64 @@ from lib.unrar2 import RarFile, RarInfo
from lib.unrar2.rar_exceptions import *
def delete_folder(folder, check_empty=True):
# check if it's a folder
if not ek.ek(os.path.isdir, folder):
return False
# check if it isn't TV_DOWNLOAD_DIR
if sickbeard.TV_DOWNLOAD_DIR:
if helpers.real_path(folder) == helpers.real_path(sickbeard.TV_DOWNLOAD_DIR):
return False
# check if it's empty folder when wanted checked
if check_empty:
check_files = ek.ek(os.listdir, folder)
if check_files:
return False
# try deleting folder
try:
logger.log(u"Deleting folder: " + folder)
shutil.rmtree(folder)
except (OSError, IOError), e:
logger.log(u"Warning: unable to delete folder: " + folder + ": " + ex(e), logger.WARNING)
return False
return True
def delete_files(processPath, notwantedFiles):
global returnStr, process_result
if not process_result:
return
#Delete all file not needed
for cur_file in notwantedFiles:
cur_file_path = ek.ek(os.path.join, processPath, cur_file)
if not ek.ek(os.path.isfile, cur_file_path):
continue #Prevent error when a notwantedfiles is an associated files
returnStr += logHelper(u"Deleting file " + cur_file, logger.DEBUG)
#check first the read-only attribute
file_attribute = ek.ek(os.stat, cur_file_path)[0]
if (not file_attribute & stat.S_IWRITE):
# File is read-only, so make it writeable
returnStr += logHelper(u"Changing ReadOnly Flag for file " + cur_file, logger.DEBUG)
try:
ek.ek(os.chmod, cur_file_path, stat.S_IWRITE)
except OSError, e:
returnStr += logHelper(u"Cannot change permissions of " + cur_file_path + ': ' + str(e.strerror),
logger.DEBUG)
try:
ek.ek(os.remove, cur_file_path)
except OSError, e:
returnStr += logHelper(u"Unable to delete file " + cur_file + ': ' + str(e.strerror), logger.DEBUG)
def logHelper(logMessage, logLevel=logger.MESSAGE):
logger.log(logMessage, logLevel)
return logMessage + u"\n"
@ -106,13 +164,13 @@ def processDir(dirName, nzbName=None, process_method=None, force=False, is_prior
#Don't Link media when the media is extracted from a rar in the same path
if process_method in ('hardlink', 'symlink') and videoInRar:
process_media(path, videoInRar, nzbName, 'move', force, is_priority)
process_result = process_media(path, videoInRar, nzbName, 'move', force, is_priority)
delete_files(path, rarContent)
for video in set(videoFiles) - set(videoInRar):
process_media(path, [video], nzbName, process_method, force, is_priority)
process_result = process_media(path, [video], nzbName, process_method, force, is_priority)
else:
for video in videoFiles:
process_media(path, [video], nzbName, process_method, force, is_priority)
process_result = process_media(path, [video], nzbName, process_method, force, is_priority)
#Process Video File in all TV Subdir
for dir in [x for x in dirs if validateDir(path, x, nzbNameOriginal, failed)]:
@ -147,8 +205,8 @@ def processDir(dirName, nzbName=None, process_method=None, force=False, is_prior
if process_method == "move" and \
ek.ek(os.path.normpath, processPath) != ek.ek(os.path.normpath,
sickbeard.TV_DOWNLOAD_DIR):
if processPath != sickbeard.TV_DOWNLOAD_DIR:
delete_dir(processPath)
if delete_folder(processPath, check_empty=False):
returnStr += logHelper(u"Deleted folder: " + processPath, logger.DEBUG)
if process_result:
returnStr += logHelper(u"Successfully processed")
@ -320,6 +378,7 @@ def already_postprocessed(dirName, videofile, force):
def process_media(processPath, videoFiles, nzbName, process_method, force, is_priority):
global process_result, returnStr
processor = None
for cur_video_file in videoFiles:
if already_postprocessed(processPath, cur_video_file, force):
@ -335,6 +394,7 @@ def process_media(processPath, videoFiles, nzbName, process_method, force, is_pr
process_result = False
process_fail_message = ex(e)
if processor:
returnStr += processor.log
if process_result:
@ -347,57 +407,10 @@ def process_media(processPath, videoFiles, nzbName, process_method, force, is_pr
if not process_result:
break
def delete_files(processPath, notwantedFiles):
global returnStr, process_result
if not process_result:
return
#Delete all file not needed
for cur_file in notwantedFiles:
cur_file_path = ek.ek(os.path.join, processPath, cur_file)
if not ek.ek(os.path.isfile, cur_file_path):
continue #Prevent error when a notwantedfiles is an associated files
returnStr += logHelper(u"Deleting file " + cur_file, logger.DEBUG)
#check first the read-only attribute
file_attribute = ek.ek(os.stat, cur_file_path)[0]
if (not file_attribute & stat.S_IWRITE):
# File is read-only, so make it writeable
returnStr += logHelper(u"Changing ReadOnly Flag for file " + cur_file, logger.DEBUG)
try:
ek.ek(os.chmod, cur_file_path, stat.S_IWRITE)
except OSError, e:
returnStr += logHelper(u"Cannot change permissions of " + cur_file_path + ': ' + str(e.strerror),
logger.DEBUG)
try:
ek.ek(os.remove, cur_file_path)
except OSError, e:
returnStr += logHelper(u"Unable to delete file " + cur_file + ': ' + str(e.strerror), logger.DEBUG)
def delete_dir(processPath):
global returnStr
if not ek.ek(os.listdir, processPath) == []:
returnStr += logHelper(
u"Skipping Deleting folder " + processPath + ' because some files was not deleted/processed', logger.DEBUG)
return
returnStr += logHelper(u"Deleting folder " + processPath, logger.DEBUG)
try:
shutil.rmtree(processPath)
except (OSError, IOError), e:
returnStr += logHelper(u"Warning: unable to remove the folder " + processPath + ": " + ex(e), logger.WARNING)
def get_path_dir_files(dirName, nzbName, type):
path = ""
dirs = []
files = []
if dirName == sickbeard.TV_DOWNLOAD_DIR and not nzbName or type == "manual": #Scheduled Post Processing Active
#Get at first all the subdir in the dirName
@ -436,7 +449,8 @@ def process_failed(dirName, nzbName):
returnStr += processor.log
if sickbeard.DELETE_FAILED and process_result:
delete_dir(dirName)
if delete_folder(dirName, check_empty=False):
returnStr += logHelper(u"Deleted folder: " + dirName, logger.DEBUG)
if process_result:
returnStr += logHelper(u"Failed Download Processing succeeded: (" + str(nzbName) + ", " + dirName + ")")