Execute a script at the end of a run
Last updated
Last updated
Running a script at the end of a named-paths group run is a common need. Setting it up is a straightforward configuration file change plus one PathsManager
method call.
In config/config.ini
you need a couple of things:
The listener enabled
The [scripts]
section with two keys
First, make sure you have the [scripts]
section. If your config.ini
file is newly generated from the most recent point release, it will be there. Otherwise, add it like this:
The run_scripts
key is enables or blocks all script running. By default script running is blocked. To run scripts you must have the value yes
. The shell
key is optional. CsvPath Framework uses it to create a shebang in the first line of your script if it doesn't see one. You can set shell
to blank or remove the key, if you don't think it would be helpful.
You also need the listener import line at [listeners] scripts.results
. Again, if your project is new you may have it. Otherwise, copy and paste from the code below.
Next, let's make sure the listener is active.
The listener group is scripts
. There is only one event type that executes scripts: results
. You need the scripts.results
key under [listeners]
to import the class. And you need the groups
key to include scripts
. If you have multiple listener groups enabled just remember to comma-separate them. Here's everything:
To add a script you do one of:
Add the script to the named-paths groups definition.json
in a text editor by hand
Call the PathsManager
's store_script_for_paths()
method
The second option is the better and easier choice. Using the PathsManager
method you have less chance for error. If the definitions file doesn't yet exist it is generated for you. However, should you want to add the script by hand, you can.
Open or create definitions.json wherever you keep your csvpaths prior to loading them. Your file doesn't need to be called definitions.json
, but when you load it that is the name CsvPath Framework will use
Create a _config
key with a dict
In the _config
dict add a key with the named-paths group name that holds a dict
In the dict add a script type key that holds a script name
The script type is one of:
on_complete_all_script
— executed on every run
on_complete_valid_script
— executed when all csvpaths in the run are fully valid
on_complete_invalid_script
— executed when any csvpath in the run is invaid
on_complete_errors_script
— executed if there were any errors
What you get should look something like:
That's not super hard, but it's harder than doing it the easy way.
You would then put your script file, in this example complete_script.sh
, in the named-paths home, the same directory as the definition.json ends up.
The easy way to add a script is to call a PathManager
method:
Here the named-paths group name is many
. If we didn't want to run the script every time using on_complete_all_script
, the default, we would add a parameter like script_type="on_complete_errors_script"
or or one of the other script types.
The outcome of the method call is the same as the example of doing it by hand — just much easier to set up.
When your named-paths group runs and the script type's condition is met, CsvPath Framework copies the script file into the run home directory and runs it. It captures the standard out and error out to a text file that has the same name as the script plus a timestamp.
And that's it.
The only caveat is that you cannot run scripts after named-paths group runs unless you are using the local filesystem backend — the default. If you are storing your archive in the cloud or on an SFTP server you will need to use another method to trigger actions. would be one option. You can to trigger actions and workflows in the cloud.