From e7cc4b375841c7054fe91f20c3f0476585767479 Mon Sep 17 00:00:00 2001 From: echel0n Date: Sat, 12 Apr 2014 10:14:24 -0700 Subject: [PATCH] Cleaned up TorrentToMeda, moved CFG variables into our initalizer and made it so it exits with a return code. Fixed the remaining nzbTo* scripts to exit with return codes. --- TorrentToMedia.py | 61 ++++++++++++++++++++---------------------- nzbToHeadPhones.py | 2 +- nzbToMylar.py | 2 +- nzbToNzbDrone.py | 2 +- nzbToSickBeard.py | 2 +- nzbtomedia/__init__.py | 27 +++++++++++++++++-- 6 files changed, 58 insertions(+), 38 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index b16aeef6..8fd4fd48 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -21,8 +21,7 @@ from nzbtomedia.utorrent.client import UTorrentClient from nzbtomedia.transmissionrpc.client import Client as TransmissionClient from nzbtomedia import logger -def main(inputDirectory, inputName, inputCategory, inputHash, inputID): - +def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID): status = int(1) # 1 = failed | 0 = success root = int(0) video = int(0) @@ -176,7 +175,7 @@ def main(inputDirectory, inputName, inputCategory, inputHash, inputID): logger.warning("Found no media files in output.") if (inputCategory in nzbtomedia.USER_SCRIPT_CATEGORIES and not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES) or ("ALL" in nzbtomedia.USER_SCRIPT_CATEGORIES and not inputCategory in processCategories): - logger.postprocess("Processing user script %s.", user_script) + logger.postprocess("Processing user script %s.", nzbtomedia.USER_SCRIPT) result = external_script(outputDestination,inputName,inputCategory) elif status == int(0) or (nzbtomedia.CFG['HeadPhones','Mylar','Gamez'][inputCategory]): # if movies linked/extracted or for other categories. logger.debug("Calling autoProcess script for successful download.") @@ -211,7 +210,7 @@ def main(inputDirectory, inputName, inputCategory, inputHash, inputID): resume_torrent(nzbtomedia.CLIENTAGENT, TorrentClass, inputHash, inputID, result, inputName) cleanup_output(inputCategory, processCategories, result, outputDestination) - logger.postprocess("All done.") + return result def create_torrent_class(clientAgent, inputHash): # Hardlink solution for Torrents @@ -308,12 +307,12 @@ def external_script(outputDestination, torrentName, torrentLabel): filePath = os.path.join(dirpath, file) fileName, fileExtension = os.path.splitext(file) - if fileExtension in user_script_mediaExtensions or "ALL" in user_script_mediaExtensions: + if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or "ALL" in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS: num_files = num_files + 1 - if user_script_runOnce == 1 and num_files > 1: # we have already run once, so just continue to get number of files. + if nzbtomedia.USER_SCRIPT_RUNONCE == 1 and num_files > 1: # we have already run once, so just continue to get number of files. continue - command = [user_script] - for param in user_script_param: + command = [nzbtomedia.USER_SCRIPT] + for param in nzbtomedia.USER_SCRIPT_PARAM: if param == "FN": command.append(file) continue @@ -327,7 +326,7 @@ def external_script(outputDestination, torrentName, torrentLabel): command.append(torrentLabel) continue elif param == "DN": - if user_script_runOnce == 1: + if nzbtomedia.USER_SCRIPT_RUNONCE == 1: command.append(outputDestination) else: command.append(dirpath) @@ -342,7 +341,7 @@ def external_script(outputDestination, torrentName, torrentLabel): try: p = Popen(command) res = p.wait() - if str(res) in user_script_successCodes: # Linux returns 0 for successful. + if str(res) in nzbtomedia.USER_SCRIPT_SUCCESSCODES: # Linux returns 0 for successful. logger.postprocess("UserScript %s was successfull", command[0]) result = int(0) else: @@ -354,43 +353,32 @@ def external_script(outputDestination, torrentName, torrentLabel): result = int(1) final_result = final_result + result - time.sleep(user_delay) + time.sleep(nzbtomedia.USER_DELAY) num_files_new = int(0) 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 user_script_mediaExtensions or user_script_mediaExtensions == "ALL": + if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS == "ALL": num_files_new = num_files_new + 1 - if user_script_clean == int(1) and num_files_new == int(0) and final_result == int(0): + if nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new == int(0) and final_result == int(0): logger.postprocess("All files have been processed. Cleaning outputDirectory %s", outputDestination) shutil.rmtree(outputDestination) - elif user_script_clean == int(1) and num_files_new != int(0): + elif nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new != int(0): logger.postprocess("%s files were processed, but %s still remain. outputDirectory will not be cleaned.", num_files, num_files_new) return final_result -if __name__ == "__main__": +def main(): # Initialize the config nzbtomedia.initialize() - # EXAMPLE VALUES: - if not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES: - user_script_mediaExtensions = (nzbtomedia.CFG["UserScript"]["user_script_mediaExtensions"]) - user_script = nzbtomedia.CFG["UserScript"]["user_script_path"] - user_script_param = (nzbtomedia.CFG["UserScript"]["user_script_param"]) - user_script_successCodes = (nzbtomedia.CFG["UserScript"]["user_script_successCodes"]) - user_script_clean = int(nzbtomedia.CFG["UserScript"]["user_script_clean"]) - user_delay = int(nzbtomedia.CFG["UserScript"]["delay"]) - user_script_runOnce = int(nzbtomedia.CFG["UserScript"]["user_script_runOnce"]) + # debug command line options + logger.debug("Options passed into TorrentToMedia: " + str(sys.argv)) - transcode = int(nzbtomedia.CFG["Transcoder"]["transcode"]) - - n = 0 - for arg in sys.argv: - logger.debug("arg %s is: %s", n, arg) - n = n+1 + # Post-Processing Result + result = 0 try: inputDirectory, inputName, inputCategory, inputHash, inputID = parse_args(nzbtomedia.CLIENTAGENT) @@ -400,14 +388,23 @@ if __name__ == "__main__": # check if this is a manual run if inputDirectory is None: + logger.warning("Invalid number of arguments received from client, Switching to manual run mode ...") for section, subsection in nzbtomedia.SUBSECTIONS.items(): for category in subsection: if nzbtomedia.CFG[section][category].isenabled(): dirNames = get_dirnames(section, category) for dirName in dirNames: logger.postprocess("Running %s:%s as a manual run for folder %s ...", section, category, dirName) - main(dirName, os.path.basename(dirName), category, inputHash, inputID) + results = processTorrent(dirName, os.path.basename(dirName), category, inputHash, inputID) + if results != 0: + result = results + logger.error("A problem was reported when trying to manually run %s:%s.", section, category) else: logger.warning("%s:%s is DISABLED, you can enable this in autoProcessMedia.cfg ...", section, category) else: - main(inputDirectory, inputName, inputCategory, inputHash, inputID) + result = processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID) + + logger.postprocess("All done.") + return result +if __name__ == "__main__": + exit(main()) \ No newline at end of file diff --git a/nzbToHeadPhones.py b/nzbToHeadPhones.py index 0433ff90..fe663eaf 100755 --- a/nzbToHeadPhones.py +++ b/nzbToHeadPhones.py @@ -8,4 +8,4 @@ def main(): nzbToMedia.main() if __name__ == "__main__": - main() + exit(main()) diff --git a/nzbToMylar.py b/nzbToMylar.py index 0433ff90..fe663eaf 100755 --- a/nzbToMylar.py +++ b/nzbToMylar.py @@ -8,4 +8,4 @@ def main(): nzbToMedia.main() if __name__ == "__main__": - main() + exit(main()) diff --git a/nzbToNzbDrone.py b/nzbToNzbDrone.py index 0433ff90..fe663eaf 100755 --- a/nzbToNzbDrone.py +++ b/nzbToNzbDrone.py @@ -8,4 +8,4 @@ def main(): nzbToMedia.main() if __name__ == "__main__": - main() + exit(main()) diff --git a/nzbToSickBeard.py b/nzbToSickBeard.py index 0433ff90..fe663eaf 100755 --- a/nzbToSickBeard.py +++ b/nzbToSickBeard.py @@ -8,4 +8,4 @@ def main(): nzbToMedia.main() if __name__ == "__main__": - main() + exit(main()) diff --git a/nzbtomedia/__init__.py b/nzbtomedia/__init__.py index 44e79b6b..678340fb 100644 --- a/nzbtomedia/__init__.py +++ b/nzbtomedia/__init__.py @@ -84,7 +84,16 @@ SAMPLEIDS = None SECTIONS = [] SUBSECTIONS = {} +TRANSCODE = None + USER_SCRIPT_CATEGORIES = None +USER_SCRIPT_MEDIAEXTENSIONS = None +USER_SCRIPT = None +USER_SCRIPT_PARAM = None +USER_SCRIPT_SUCCESSCODES = None +USER_SCRIPT_CLEAN = None +USER_DELAY = None +USER_SCRIPT_RUNONCE = None __INITIALIZED__ = False @@ -97,7 +106,9 @@ def initialize(): UTORRENTPWD, UTORRENTUSR, UTORRENTWEBUI, DELUGEHOST, DELUGEPORT, DELUGEUSR, DELUGEPWD, TRANSMISSIONHOST, TRANSMISSIONPORT, \ TRANSMISSIONPWD, TRANSMISSIONUSR, COMPRESSEDCONTAINER, MEDIACONTAINER, METACONTAINER, MINSAMPLESIZE, SAMPLEIDS, \ SECTIONS, SUBSECTIONS, USER_SCRIPT_CATEGORIES, __INITIALIZED__, GIT_PATH, GIT_USER, GIT_BRANCH, AUTO_UPDATE, APP_FILENAME, \ - APP_NAME + APP_NAME,USER_SCRIPT_MEDIAEXTENSIONS, USER_SCRIPT, USER_SCRIPT_PARAM, USER_SCRIPT_SUCCESSCODES, USER_SCRIPT_CLEAN,\ + USER_DELAY, USER_SCRIPT_RUNONCE, TRANSCODE + if __INITIALIZED__: return False @@ -204,9 +215,21 @@ def initialize(): SUBSECTIONS = CFG[SECTIONS] CATEGORIES += SUBSECTIONS.sections - USER_SCRIPT_CATEGORIES = CFG["UserScript"]["user_script_categories"] # NONE + TRANSCODE = int(CFG["Transcoder"]["transcode"]) + + USER_SCRIPT_CATEGORIES = CFG["UserScript"]["user_script_categories"] + if not "NONE" in USER_SCRIPT_CATEGORIES: + USER_SCRIPT_MEDIAEXTENSIONS = (CFG["UserScript"]["user_script_mediaExtensions"]) + USER_SCRIPT = CFG["UserScript"]["user_script_path"] + USER_SCRIPT_PARAM = (CFG["UserScript"]["user_script_param"]) + USER_SCRIPT_SUCCESSCODES = (CFG["UserScript"]["user_script_successCodes"]) + USER_SCRIPT_CLEAN = int(CFG["UserScript"]["user_script_clean"]) + USER_DELAY = int(CFG["UserScript"]["delay"]) + USER_SCRIPT_RUNONCE = int(CFG["UserScript"]["user_script_runOnce"]) GIT_PATH = CFG["General"]["git_path"] + GIT_USER = CFG["General"]["git_user"] + GIT_BRANCH = CFG["General"]["git_branch"] __INITIALIZED__ = True return True