From 8ae6acedbce44d6584f53e3b130ab8fb2a3bb8b9 Mon Sep 17 00:00:00 2001 From: Claudio Satriano Date: Mon, 2 Dec 2024 21:47:58 +0100 Subject: [PATCH] New command `run` to run a user-defined command on each event --- CHANGELOG.md | 1 + seiscat/main.py | 3 +++ seiscat/parse_arguments.py | 14 ++++++++++++++ seiscat/run_command.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 seiscat/run_command.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 426ec94..1e4046d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Copyright (c) 2022-2024 Claudio Satriano a local SDS archive - New option for `seiscat initdb` and `seiscat updatedb`: `--fromfile` to initialize or update the database from a CSV file +- New command `seiscat run` to run a user-defined command on each event ## v0.8 - 2024-10-28 diff --git a/seiscat/main.py b/seiscat/main.py index 66d1067..ee07138 100644 --- a/seiscat/main.py +++ b/seiscat/main.py @@ -52,6 +52,9 @@ def run(): elif args.action == 'plot': from .plot.plot_map import plot_catalog_map plot_catalog_map(config) + elif args.action == 'run': + from .run_command import run_command + run_command(config) def main(): diff --git a/seiscat/parse_arguments.py b/seiscat/parse_arguments.py index b72703b..5a86698 100644 --- a/seiscat/parse_arguments.py +++ b/seiscat/parse_arguments.py @@ -278,6 +278,20 @@ def parse_arguments(): default=10, help='scale factor for marker size (default: %(default)s)' ) + run_parser = subparser.add_parser( + 'run', parents=[versions_parser, where_parser], + help='run a user-defined command on each event') + run_parser.add_argument( + 'command', + type=str, + help='command to run. It can be any executable (e.g., shell script, ' + 'Python script, etc.). All the columns of the events table will ' + 'be available as environment variables (e.g., $evid, $time, etc.)' + ) + run_parser.add_argument( + 'eventid', nargs='?', + help='only run the command on this eventid' + ).completer = _evid_completer subparser.add_parser('sampleconfig', help='write sample config file') parser.add_argument( '-c', diff --git a/seiscat/run_command.py b/seiscat/run_command.py new file mode 100644 index 0000000..d117148 --- /dev/null +++ b/seiscat/run_command.py @@ -0,0 +1,36 @@ +# -*- coding: utf8 -*- +# SPDX-License-Identifier: GPL-3.0-or-later +""" +Run a user-defined command on a list of events. + +:copyright: + 2022-2024 Claudio Satriano +:license: + GNU General Public License v3.0 or later + (https://www.gnu.org/licenses/gpl-3.0-standalone.html) +""" +import os +import subprocess +from .database.dbfunctions import read_events_from_db +from .utils import ExceptionExit + + +def run_command(config): + """ + Run a user-defined command on a list of events. + + :param config: config object + :type config: dict + """ + args = config['args'] + command = args.command + with ExceptionExit(): + events = read_events_from_db(config) + for event in events: + print(f'Running {command} on event {event["evid"]}') + event_str = {k: str(v) for k, v in event.items()} + env = {**os.environ, **event_str} + try: + subprocess.run(command, shell=True, env=env, check=True) + except subprocess.CalledProcessError as e: + print(f'Command {command} failed with exit status {e.returncode}')