From 788cf1e0c38f52f0b303d082fe47468f2a3d5991 Mon Sep 17 00:00:00 2001 From: David Maisonave <47364845+David-Maisonave@users.noreply.github.com> Date: Fri, 16 Aug 2024 12:10:49 -0400 Subject: [PATCH] Added validation check for scheduled plugins --- plugins/DupFileManager/StashPluginHelper.py | 3 + plugins/FileMonitor/README.md | 2 +- plugins/FileMonitor/StashPluginHelper.py | 3 + plugins/FileMonitor/filemonitor.py | 21 +++++-- plugins/FileMonitor/filemonitor.yml | 2 +- plugins/FileMonitor/filemonitor_config.py | 66 ++++++++++----------- 6 files changed, 58 insertions(+), 39 deletions(-) diff --git a/plugins/DupFileManager/StashPluginHelper.py b/plugins/DupFileManager/StashPluginHelper.py index 218e055..c32fe77 100644 --- a/plugins/DupFileManager/StashPluginHelper.py +++ b/plugins/DupFileManager/StashPluginHelper.py @@ -27,6 +27,7 @@ class StashPluginHelper(StashInterface): PLUGIN_TASK_NAME = None PLUGIN_ID = None PLUGIN_CONFIGURATION = None + PLUGINS_PATH = None pluginSettings = None pluginConfig = None STASH_INTERFACE_INIT = False @@ -165,6 +166,8 @@ class StashPluginHelper(StashInterface): self.PLUGIN_CONFIGURATION = self.get_configuration()["plugins"] self.STASH_CONFIGURATION = self.get_configuration()["general"] self.STASHPATHSCONFIG = self.STASH_CONFIGURATION['stashes'] + if 'pluginsPath' in self.STASH_CONFIGURATION: + self.PLUGINS_PATH = self.STASH_CONFIGURATION['pluginsPath'] for item in self.STASHPATHSCONFIG: self.STASH_PATHS.append(item["path"]) if settings: diff --git a/plugins/FileMonitor/README.md b/plugins/FileMonitor/README.md index a196509..400614a 100644 --- a/plugins/FileMonitor/README.md +++ b/plugins/FileMonitor/README.md @@ -1,4 +1,4 @@ -# FileMonitor: Ver 0.8.2 (By David Maisonave) +# FileMonitor: Ver 0.8.3 (By David Maisonave) FileMonitor is a [Stash](https://github.com/stashapp/stash) plugin with the following two main features: - Updates Stash when any file changes occurs in the Stash library. - **Task Scheduler**: Runs scheduled task based on the scheduler configuration in **filemonitor_config.py**. diff --git a/plugins/FileMonitor/StashPluginHelper.py b/plugins/FileMonitor/StashPluginHelper.py index 218e055..c32fe77 100644 --- a/plugins/FileMonitor/StashPluginHelper.py +++ b/plugins/FileMonitor/StashPluginHelper.py @@ -27,6 +27,7 @@ class StashPluginHelper(StashInterface): PLUGIN_TASK_NAME = None PLUGIN_ID = None PLUGIN_CONFIGURATION = None + PLUGINS_PATH = None pluginSettings = None pluginConfig = None STASH_INTERFACE_INIT = False @@ -165,6 +166,8 @@ class StashPluginHelper(StashInterface): self.PLUGIN_CONFIGURATION = self.get_configuration()["plugins"] self.STASH_CONFIGURATION = self.get_configuration()["general"] self.STASHPATHSCONFIG = self.STASH_CONFIGURATION['stashes'] + if 'pluginsPath' in self.STASH_CONFIGURATION: + self.PLUGINS_PATH = self.STASH_CONFIGURATION['pluginsPath'] for item in self.STASHPATHSCONFIG: self.STASH_PATHS.append(item["path"]) if settings: diff --git a/plugins/FileMonitor/filemonitor.py b/plugins/FileMonitor/filemonitor.py index 6d6752d..e23fbd4 100644 --- a/plugins/FileMonitor/filemonitor.py +++ b/plugins/FileMonitor/filemonitor.py @@ -45,8 +45,6 @@ stash = StashPluginHelper( stash.Status() stash.Log(f"\nStarting (__file__={__file__}) (stash.CALLED_AS_STASH_PLUGIN={stash.CALLED_AS_STASH_PLUGIN}) (stash.DEBUG_TRACING={stash.DEBUG_TRACING}) (stash.DRY_RUN={stash.DRY_RUN}) (stash.PLUGIN_TASK_NAME={stash.PLUGIN_TASK_NAME})************************************************") -# stash.Log(f"{stash.find_duplicate_scenes()}") - exitMsg = "Change success!!" mutex = Lock() signal = Condition(mutex) @@ -260,8 +258,23 @@ class StashScheduler: # Stash Scheduler # ToDo: Add code to check if plugin is installed. try: if 'pluginId' in task and task['pluginId'] != "": - stash.Trace(f"Running plugin task pluginID={task['pluginId']}, task name = {task['task']}") - stash.run_plugin_task(plugin_id=task['pluginId'], task_name=task['task']) + invalidDir = False + validDirMsg = "" + if 'validateDir' in task and task['validateDir'] != "": + invalidDir = True + communityPluginPath = f"{stash.PLUGINS_PATH}{os.sep}community{os.sep}{task['validateDir']}" + basePluginPath = f"{stash.PLUGINS_PATH}{os.sep}{task['validateDir']}" + if os.path.exists(communityPluginPath): + invalidDir = False + validDirMsg = f"Valid path in {communityPluginPath}" + elif os.path.exists(basePluginPath): + invalidDir = False + validDirMsg = f"Valid path in {basePluginPath}" + if invalidDir: + stash.Error(f"Could not run task '{task['task']}' because sub directory '{task['validateDir']}' does not exist under path '{stash.PLUGINS_PATH}'") + else: + stash.Trace(f"Running plugin task pluginID={task['pluginId']}, task name = {task['task']}. {validDirMsg}") + stash.run_plugin_task(plugin_id=task['pluginId'], task_name=task['task']) else: stash.Error(f"Can not run task '{task['task']}', because it's an invalid task.") stash.LogOnce(f"If task '{task['task']}' is supposed to be a built-in task, check for correct task name spelling.") diff --git a/plugins/FileMonitor/filemonitor.yml b/plugins/FileMonitor/filemonitor.yml index 4d2adff..b30f935 100644 --- a/plugins/FileMonitor/filemonitor.yml +++ b/plugins/FileMonitor/filemonitor.yml @@ -1,6 +1,6 @@ name: FileMonitor description: Monitors the Stash library folders, and updates Stash if any changes occurs in the Stash library paths. -version: 0.8.2 +version: 0.8.3 url: https://github.com/David-Maisonave/Axter-Stash/tree/main/plugins/FileMonitor settings: recursiveDisabled: diff --git a/plugins/FileMonitor/filemonitor_config.py b/plugins/FileMonitor/filemonitor_config.py index a5f6f00..add1c3d 100644 --- a/plugins/FileMonitor/filemonitor_config.py +++ b/plugins/FileMonitor/filemonitor_config.py @@ -11,12 +11,13 @@ config = { # Note: The hour section in time MUST be a two digit number, and use military time format. Example: 1PM = "13:00" and 1AM = "01:00" "task_scheduler": [ # To create a daily task, include each day of the week for the weekday field. - {"task" : "Auto Tag", "weekday" : "monday,tuesday,wednesday,thursday,friday,saturday,sunday", "time" : "06:00"}, # Auto Tag -> [Auto Tag] (Daily at 6AM) - {"task" : "Optimise Database", "weekday" : "monday,tuesday,wednesday,thursday,friday,saturday,sunday", "time" : "07:00"}, # Maintenance -> [Optimise Database] (Daily at 7AM) + {"task" : "Auto Tag", "weekday" : "monday,tuesday,wednesday,thursday,friday,saturday,sunday", "time" : "06:00"}, # Auto Tag -> [Auto Tag] (Daily at 6AM) + {"task" : "Optimise Database", "weekday" : "monday,tuesday,wednesday,thursday,friday,saturday,sunday", "time" : "07:00"}, # Maintenance -> [Optimise Database] (Daily at 7AM) + {"task" : "Create Tags", "pluginId" : "pathParser", "validateDir" : "pathParser", "weekday" : "monday,tuesday,wednesday,thursday,friday,saturday,sunday", "time" : "05:00"}, # [Plugin Tasks] - > [Path Parser] -> [Create Tags] (Daily at 5AM) : This task requires plugin [Path Parser] # The following tasks are scheduled for 3 days out of the week. - {"task" : "Clean", "weekday" : "monday,wednesday,friday", "time" : "08:00"}, # Maintenance -> [Clean] (3 days per week at 8AM) - {"task" : "Clean Generated Files", "weekday" : "tuesday,thursday,saturday", "time" : "08:00"}, # Maintenance -> [Clean Generated Files] (3 days per week at 8AM) + {"task" : "Clean", "weekday" : "monday,wednesday,friday", "time" : "08:00"}, # Maintenance -> [Clean] (3 days per week at 8AM) + {"task" : "Clean Generated Files", "weekday" : "tuesday,thursday,saturday", "time" : "08:00"}, # Maintenance -> [Clean Generated Files] (3 days per week at 8AM) # The following tasks are scheduled weekly {"task" : "Generate", "weekday" : "sunday", "time" : "07:00"}, # Generated Content-> [Generate] (Every Sunday at 7AM) @@ -29,11 +30,7 @@ config = { # 3 = 3rd specified weekday of the month. # 4 = 4th specified weekday of the month. # The following task is scheduled monthly - {"task" : "Backup", "weekday" : "sunday", "time" : "01:00", "monthly" : 2}, # Backup -> [Backup] 2nd sunday of the month at 1AM (01:00) - - # The following task is the syntax used for a plugins. A plugin task requires the plugin name for the [task] field, and the plugin-ID for the [pluginId] field. - # This task requires plugin [Path Parser], and it's disabled by default. - {"task" : "Create Tags", "pluginId" : "pathParser", "weekday" : "monday,tuesday,wednesday,thursday,friday,saturday,sunday", "time" : "DISABLED"}, # To enable this task change time "DISABLED" to a valid time. + {"task" : "Backup", "weekday" : "sunday", "time" : "01:00", "monthly" : 2}, # Backup -> [Backup] 2nd sunday of the month at 1AM (01:00) # Example#A1: Task to call call_GQL API with custom input {"task" : "GQL", "input" : "mutation OptimiseDatabase { optimiseDatabase }", "weekday" : "sunday", "time" : "DISABLED"}, # To enable, change "DISABLED" to valid time @@ -62,13 +59,15 @@ config = { # And days usage is discourage, because it only works if FileMonitor is running for X many days non-stop. # The below example tasks are done using hours and minutes, however any of these task types can be converted to a daily, weekly, or monthly syntax. - # Example#B1: Task for calling another Stash plugin, which needs plugin name and plugin ID. + # Example#B1: The following task is the syntax used for a plugin. A plugin task requires the plugin name for the [task] field, and the plugin-ID for the [pluginId] field. {"task" : "PluginButtonName_Here", "pluginId" : "PluginId_Here", "hours" : 0}, # The zero frequency value makes this task disabled. + # Example#B2: Optionally, the validateDir field can be included which is used to validate that the plugin is installed either under the plugins folder or under the plugins-community folder. + {"task" : "PluginButtonName_Here", "pluginId" : "PluginId_Here", "validateDir" : "UsuallySameAsPluginID", "hours" : 0}, # The zero frequency value makes this task disabled. - # Example#B2: Task to execute a command + # Example#B3: Task to execute a command {"task" : "execute", "command" : "C:\\MyPath\\HelloWorld.bat", "hours" : 0}, - # Example#B3: Task to execute a command with optional args field, and using keyword , which gets replaced with filemonitor.py current directory. + # Example#B4: Task to execute a command with optional args field, and using keyword , which gets replaced with filemonitor.py current directory. {"task" : "execute", "command" : "HelloWorld.cmd", "args" : "--name David", "minutes" : 0}, # Comment out **test** tasks. @@ -77,27 +76,28 @@ config = { # These tasks are usually executed before updating major releases on https://github.com/David-Maisonave/Axter-Stash/blob/main/plugins/FileMonitor # These tasks are ALWAYS executed before updating to https://github.com/stashapp/CommunityScripts # MUST ToDo: Always comment out below test task before checking in this code!!! - # {"task" : "TestBadTaskNameError", "minutes" : 1}, # Test invalid task name - # {"task" : "execute", "minutes" : 1}, # Test invalid task (missing command) - # {"task" : "python", "minutes" : 1}, # Test invalid task (missing scripts) - # {"task" : "PluginWithOutID", "minutes" : 1}, # Test invalid task (missing pluginId) - # {"task" : "execute", "command" : "", "minutes" : 1}, # Test invalid task (missing command) - # {"task" : "python", "script" : "", "minutes" : 1}, # Test invalid task (missing scripts) - # {"task" : "PluginWithOutID", "pluginId" : "", "minutes" : 1}, # Test invalid task (missing pluginId) - # {"task" : "Generate", "weekday" : "friday", "time" : "00:00"}, - # {"task" : "Clean", "weekday" : "friday", "time" : "00:00"}, - # {"task" : "Auto Tag", "weekday" : "friday", "time" : "00:00"}, - # {"task" : "Optimise Database", "weekday" : "friday", "time" : "00:00"}, - # {"task" : "Create Tags", "pluginId" : "pathParser", "weekday" : "friday", "time" : "00:00"}, # In task queue as -> Running plugin task: Create Tags - # {"task" : "Scan","paths": [r"B:\_\SpecialSet", r"C:\foo"], "weekday" : "friday", "time" : "00:00"}, - # {"task" : "GQL", "input" : "mutation OptimiseDatabase { optimiseDatabase }", "weekday" : "friday", "time" : "00:00"}, # In task queue as -> Optimising database... - # {"task" : "Clean Generated Files", "weekday" : "friday", "time" : "00:00"}, - # {"task" : "RenameGeneratedFiles", "weekday" : "friday", "time" : "00:00"}, # In task queue as -> Migrating scene hashes... - # {"task" : "Backup", "maxBackups" : 0, "weekday" : "friday", "time" : "00:00"}, # Does NOT show up in the Task Queue. Must check STASH log file to verify run. - # {"task" : "python", "script" : "test_hello_world2.py", "weekday" : "friday", "time" : "00:00"}, # Does NOT show up in the Task Queue. Check FileMonitor log file, and look for -> Task 'python' result=??? - # {"task" : "python", "script" : "test_hello_world.py", "detach" : False, "weekday" : "friday", "time" : "00:00"}, # Does NOT show up in the Task Queue. Check FileMonitor log file, and look for -> Task 'python' result=??? - # {"task" : "execute", "command" : "test_hello_world2.cmd", "weekday" : "friday", "time" : "00:00"}, # Does NOT show up in the Task Queue. Check FileMonitor log file, and look for -> Task 'execute' result=??? - # {"task" : "execute", "command" : "test_hello_world.bat", "args" : "--name David", "weekday" : "friday", "time" : "00:00"}, # Does NOT show up in the Task Queue. Check FileMonitor log file, and look for -> Task 'execute' result=??? + # {"task" : "TestBadTaskNameError", "minutes" : 1}, # Test invalid task name + # {"task" : "execute", "minutes" : 1}, # Test invalid task (missing command) + # {"task" : "python", "minutes" : 1}, # Test invalid task (missing scripts) + # {"task" : "PluginWithOutID", "minutes" : 1}, # Test invalid task (missing pluginId) + # {"task" : "execute", "command" : "", "minutes" : 1}, # Test invalid task (missing command) + # {"task" : "python", "script" : "", "minutes" : 1}, # Test invalid task (missing scripts) + # {"task" : "PluginWithOutID", "pluginId" : "", "minutes" : 1}, # Test invalid task (missing pluginId) + # {"task" : "Foo","pluginId":"foo","validateDir":"foo", "minutes" : 1}, # Test invalid task (missing plugin directory) + # {"task" : "Generate", "weekday" : "friday", "time" : "12:03"}, + # {"task" : "Clean", "weekday" : "friday", "time" : "12:03"}, + # {"task" : "Auto Tag", "weekday" : "friday", "time" : "12:03"}, + # {"task" : "Optimise Database", "weekday" : "friday", "time" : "12:03"}, + # {"task" : "Create Tags", "pluginId" : "pathParser", "validateDir" : "pathParser", "weekday" : "friday", "time" : "12:03"}, # In task queue as -> Running plugin task: Create Tags + # {"task" : "Scan","paths": [r"B:\_\SpecialSet", r"C:\foo"], "weekday" : "friday", "time" : "12:03"}, + # {"task" : "GQL", "input" : "mutation OptimiseDatabase { optimiseDatabase }", "weekday" : "friday", "time" : "12:03"}, # In task queue as -> Optimising database... + # {"task" : "Clean Generated Files", "weekday" : "friday", "time" : "12:03"}, + # {"task" : "RenameGeneratedFiles", "weekday" : "friday", "time" : "12:03"}, # In task queue as -> Migrating scene hashes... + # {"task" : "Backup", "maxBackups" : 0, "weekday" : "friday", "time" : "12:03"}, # Does NOT show up in the Task Queue. Must check STASH log file to verify run. + # {"task" : "python", "script" : "test_hello_world2.py", "weekday" : "friday", "time" : "12:03"}, # Does NOT show up in the Task Queue. Check FileMonitor log file, and look for -> Task 'python' result=??? + # {"task" : "python", "script" : "test_hello_world.py", "detach" : False, "weekday" : "friday", "time" : "12:03"}, # Does NOT show up in the Task Queue. Check FileMonitor log file, and look for -> Task 'python' result=??? + # {"task" : "execute", "command" : "test_hello_world2.cmd", "weekday" : "friday", "time" : "12:03"}, # Does NOT show up in the Task Queue. Check FileMonitor log file, and look for -> Task 'execute' result=??? + # {"task" : "execute", "command" : "test_hello_world.bat", "args" : "--name David", "weekday" : "friday", "time" : "12:03"}, # Does NOT show up in the Task Queue. Check FileMonitor log file, and look for -> Task 'execute' result=??? ], # Timeout in seconds. This is how often FileMonitor will check the scheduler and (in-plugin mode) check if another job (Task) is in the queue.