Skip to content

Commit

Permalink
New command run to run a user-defined command on each event
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodsf committed Dec 2, 2024
1 parent 227a546 commit 8ae6ace
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Copyright (c) 2022-2024 Claudio Satriano <[email protected]>
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

Expand Down
3 changes: 3 additions & 0 deletions seiscat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
14 changes: 14 additions & 0 deletions seiscat/parse_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
36 changes: 36 additions & 0 deletions seiscat/run_command.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
: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}')

0 comments on commit 8ae6ace

Please sign in to comment.