From 2fbc987e6f70621ceebd389aafba321726fdcaef Mon Sep 17 00:00:00 2001 From: echel0n Date: Sat, 12 Apr 2014 11:10:28 -0700 Subject: [PATCH] Moved option force_clean from Torrent section into General section. Moved cleanup function from TorrentToMedia into nzbToMediaUtils. Folder cleanup is performed automatically after a successful postprocess for both nzb and torrent processing scripts. --- TorrentToMedia.py | 35 +++++++--------------------------- autoProcessMedia.cfg.spec | 2 +- nzbToMedia.py | 28 +++++++++++++++++---------- nzbtomedia/nzbToMediaConfig.py | 11 ++++++----- nzbtomedia/nzbToMediaUtil.py | 19 ++++++++++++++++++ 5 files changed, 51 insertions(+), 44 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index f2a71be0..ed8b88dd 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -15,7 +15,7 @@ from nzbtomedia.autoProcess.autoProcessMusic import autoProcessMusic from nzbtomedia.autoProcess.autoProcessTV import autoProcessTV from nzbtomedia.extractor import extractor from nzbtomedia.nzbToMediaUtil import category_search, safeName, is_sample, copy_link, parse_args, flatten, get_dirnames, \ - remove_read_only + remove_read_only, cleanup_directories from nzbtomedia.synchronousdeluge.client import DelugeClient from nzbtomedia.utorrent.client import UTorrentClient from nzbtomedia.transmissionrpc.client import Client as TransmissionClient @@ -54,9 +54,8 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID) if result != 0: logger.error("A problem was reported in the autoProcessTV script.") resume_torrent(nzbtomedia.CLIENTAGENT, TorrentClass, inputHash, inputID, result, inputName) - cleanup_output(inputCategory, processCategories, result, outputDestination) - logger.postprocess("All done.") - sys.exit() + cleanup_directories(inputCategory, processCategories, result, outputDestination) + return result processOnly = nzbtomedia.CFG[nzbtomedia.SECTIONS].sections if not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES: # if None, we only process the 5 listed. @@ -66,8 +65,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID) if not inputCategory in processOnly: logger.postprocess("No processing to be done for category: %s. Exiting", inputCategory) - logger.postprocess("All done.") - sys.exit() + return logger.debug("Scanning files in directory: %s", inputDirectory) @@ -182,7 +180,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID) status = int(0) # hp, my, gz don't support failed. else: logger.error("Something failed! Please check logs. Exiting") - sys.exit(-1) + return status result = 0 if nzbtomedia.CFG['CouchPotato'][inputCategory]: @@ -209,7 +207,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID) logger.error("A problem was reported in the autoProcess* script. If torrent was paused we will resume seeding") resume_torrent(nzbtomedia.CLIENTAGENT, TorrentClass, inputHash, inputID, result, inputName) - cleanup_output(inputCategory, processCategories, result, outputDestination) + cleanup_directories(inputCategory, processCategories, result, outputDestination) return result def create_torrent_class(clientAgent, inputHash): @@ -278,25 +276,6 @@ def resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputN TorrentClass.core.resume_torrent([inputID]) time.sleep(5) -def cleanup_output(inputCategory, processCategories, result, outputDestination): - if inputCategory in processCategories and result == 0 and os.path.isdir(outputDestination): - num_files_new = int(0) - file_list = [] - for dirpath, dirnames, filenames in os.walk(outputDestination): - for file in filenames: - filePath = os.path.join(dirpath, file) - fileName, fileExtension = os.path.splitext(file) - if fileExtension in nzbtomedia.MEDIACONTAINER or fileExtension in nzbtomedia.METACONTAINER: - num_files_new += 1 - file_list.append(file) - if num_files_new is 0 or int(nzbtomedia.CFG["Torrent"]["forceClean"]) is 1: - logger.postprocess("All files have been processed. Cleaning outputDirectory %s", outputDestination) - shutil.rmtree(outputDestination) - else: - logger.postprocess("outputDirectory %s still contains %s media and/or meta files. This directory will not be removed.", outputDestination, num_files_new) - for item in file_list: - logger.debug("media/meta file found: %s", item) - def external_script(outputDestination, torrentName, torrentLabel): final_result = int(0) # start at 0. @@ -388,7 +367,7 @@ def main(): inputDirectory, inputName, inputCategory, inputHash, inputID = parse_args(nzbtomedia.CLIENTAGENT) except: logger.error("There was a problem loading variables") - sys.exit(-1) + return -1 # check if this is a manual run if inputDirectory is None: diff --git a/autoProcessMedia.cfg.spec b/autoProcessMedia.cfg.spec index 9eb94b36..27aeee23 100644 --- a/autoProcessMedia.cfg.spec +++ b/autoProcessMedia.cfg.spec @@ -6,6 +6,7 @@ git_path = git_user = git_branch = + force_clean = 0 [CouchPotato] #### autoProcessing for Movies @@ -137,7 +138,6 @@ DelugePWD = your password ###### ADVANCED USE - ONLY EDIT IF YOU KNOW WHAT YOU'RE DOING ###### deleteOriginal = 0 - forceClean = 0 [Extensions] compressedExtensions = .zip,.rar,.7z,.gz,.bz,.tar,.arj,.1,.01,.001 diff --git a/nzbToMedia.py b/nzbToMedia.py index 7f64037a..18098818 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -307,31 +307,39 @@ from nzbtomedia.autoProcess.autoProcessGames import autoProcessGames from nzbtomedia.autoProcess.autoProcessMovie import autoProcessMovie from nzbtomedia.autoProcess.autoProcessMusic import autoProcessMusic from nzbtomedia.autoProcess.autoProcessTV import autoProcessTV -from nzbtomedia.nzbToMediaUtil import get_dirnames +from nzbtomedia.nzbToMediaUtil import get_dirnames, cleanup_directories from nzbtomedia import logger # post-processing def process(nzbDir, inputName=None, status=0, clientAgent='manual', download_id=None, inputCategory=None): + # auto-detect section + section = nzbtomedia.CFG.findsection(inputCategory) if nzbtomedia.CFG["CouchPotato"][inputCategory]: logger.postprocess("Calling CouchPotatoServer to post-process: %s", inputName) - return autoProcessMovie().process(nzbDir, inputName, status, clientAgent, download_id, inputCategory) + result = autoProcessMovie().process(nzbDir, inputName, status, clientAgent, download_id, inputCategory) elif nzbtomedia.CFG["SickBeard", "NzbDrone"][inputCategory]: logger.postprocess("Calling Sick-Beard to post-process: %s", inputName) - return autoProcessTV().processEpisode(nzbDir, inputName, status, clientAgent, inputCategory) + result = autoProcessTV().processEpisode(nzbDir, inputName, status, clientAgent, inputCategory) elif nzbtomedia.CFG["HeadPhones"][inputCategory]: logger.postprocess("Calling HeadPhones to post-process: %s", inputName) - return autoProcessMusic().process(nzbDir, inputName, status, clientAgent, inputCategory) + result = autoProcessMusic().process(nzbDir, inputName, status, clientAgent, inputCategory) elif nzbtomedia.CFG["Mylar"][inputCategory]: logger.postprocess("Calling Mylar to post-process: %s", inputName) - return autoProcessComics().processEpisode(nzbDir, inputName, status, clientAgent, inputCategory) + result = autoProcessComics().processEpisode(nzbDir, inputName, status, clientAgent, inputCategory) elif nzbtomedia.CFG["Gamez"][inputCategory]: logger.postprocess("Calling Gamez to post-process: %s", inputName) - return autoProcessGames().process(nzbDir, inputName, status, clientAgent, inputCategory) + result = autoProcessGames().process(nzbDir, inputName, status, clientAgent, inputCategory) else: logger.postprocess("We could not find a section with the download category of %s in your autoProcessMedia.cfg. Exiting.", inputCategory) return -1 + if result == 0: + # Clean up any leftover files + cleanup_directories(inputCategory, section, result, nzbDir) + + return result + def main(): # Initialize the config nzbtomedia.initialize() @@ -432,21 +440,21 @@ def main(): logger.postprocess("nzbToMedia running %s:%s as a manual run on folder %s ...", section, category, dirName) results = process(dirName, os.path.basename(dirName), 0, inputCategory=category) if results != 0: - result = results logger.error("A problem was reported when trying to manually run %s:%s.", section, category) + result = results + else: logger.postprocess("nzbToMedia %s:%s is DISABLED, you can enable this in autoProcessMedia.cfg ...", section, category) if result == 0: logger.postprocess("The nzbToMedia script completed successfully.") if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11 - sys.exit(nzbtomedia.NZBGET_POSTPROCESS_SUCCESS) + return nzbtomedia.NZBGET_POSTPROCESS_SUCCESS else: logger.error("A problem was reported in the nzbToMedia script.") if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11 - sys.exit(nzbtomedia.NZBGET_POSTPROCESS_ERROR) + return nzbtomedia.NZBGET_POSTPROCESS_ERROR return result - if __name__ == '__main__': exit(main()) \ No newline at end of file diff --git a/nzbtomedia/nzbToMediaConfig.py b/nzbtomedia/nzbToMediaConfig.py index 76cca7aa..3b3c0c94 100644 --- a/nzbtomedia/nzbToMediaConfig.py +++ b/nzbtomedia/nzbToMediaConfig.py @@ -143,25 +143,28 @@ class ConfigObj(lib.configobj.ConfigObj, Section): value = 'hard' else: value = 'no' + if option == "forceClean": + CFG_NEW['General']['force_clean'] = value + values.pop(option) return values def process_section(section, subsections=None): if subsections: for subsection in subsections: if subsection in CFG_OLD.sections: - values = CFG_OLD[subsection] + values = cleanup_values(CFG_OLD[subsection], section) if subsection not in CFG_NEW[section].sections: CFG_NEW[section][subsection] = {} for option, value in values.items(): CFG_NEW[section][subsection][option] = value elif subsection in CFG_OLD[section].sections: - values = CFG_OLD[section][subsection] + values = cleanup_values(CFG_OLD[section][subsection], section) if subsection not in CFG_NEW[section].sections: CFG_NEW[section][subsection] = {} for option, value in values.items(): CFG_NEW[section][subsection][option] = value else: - values = CFG_OLD[section] + values = cleanup_values(CFG_OLD[section], section) if section not in CFG_NEW.sections: CFG_NEW[section] = {} for option, value in values.items(): @@ -174,11 +177,9 @@ class ConfigObj(lib.configobj.ConfigObj, Section): subsection = section section = ''.join([k for k,v in subsections.iteritems() if subsection in v]) process_section(section, subsection) - #[[v.remove(c) for c in v if c in subsection] for k, v in subsections.items() if k == section] elif section in subsections.keys(): subsection = subsections[section] process_section(section, subsection) - #[[v.remove(c) for c in v if c in subsection] for k,v in subsections.items() if k == section] elif section in CFG_OLD.keys(): process_section(section, subsection) diff --git a/nzbtomedia/nzbToMediaUtil.py b/nzbtomedia/nzbToMediaUtil.py index 6dd86a02..3fb03839 100644 --- a/nzbtomedia/nzbToMediaUtil.py +++ b/nzbtomedia/nzbToMediaUtil.py @@ -424,3 +424,22 @@ def delete(dirName): shutil.rmtree(dirName, True) except: logger.error("Unable to delete folder %s", dirName) + +def cleanup_directories(inputCategory, processCategories, result, directory): + if inputCategory in processCategories and result == 0 and os.path.isdir(directory): + num_files_new = int(0) + file_list = [] + for dirpath, dirnames, filenames in os.walk(directory): + for file in filenames: + filePath = os.path.join(dirpath, file) + fileName, fileExtension = os.path.splitext(file) + if fileExtension in nzbtomedia.MEDIACONTAINER or fileExtension in nzbtomedia.METACONTAINER: + num_files_new += 1 + file_list.append(file) + if num_files_new is 0 or int(nzbtomedia.CFG["General"]["force_clean"]) == 1: + logger.info("All files have been processed. Cleaning directory %s", directory) + shutil.rmtree(directory) + else: + logger.info("Directory %s still contains %s media and/or meta files. This directory will not be removed.", directory, num_files_new) + for item in file_list: + logger.debug("media/meta file found: %s", item)