Skip to content

Instantly share code, notes, and snippets.

@maxfischer2781
Created July 13, 2023 16:15
Show Gist options
  • Save maxfischer2781/0db28a85dd8992f36586724e7a722311 to your computer and use it in GitHub Desktop.
Save maxfischer2781/0db28a85dd8992f36586724e7a722311 to your computer and use it in GitHub Desktop.
APEL gap publish CLI
#!/usr/bin/python3
from typing import Generator
import argparse
import configparser
import contextlib
import datetime
import pathlib
import tempfile
import subprocess
TODAY = datetime.date.today()
CLI = argparse.ArgumentParser(
description="publish APEL records from specific time range",
epilog="The APEL programs 'apelclient' and 'ssmsend' are picked up from $PATH."
)
CLI.add_argument("start", help="oldest date as YYYY-MM-DD")
CLI.add_argument("end", help="newest date as YYYY-MM-DD", nargs="?", default=TODAY.strftime("%Y-%m-%d"))
CLI.add_argument(
"--client-config",
type=pathlib.Path,
help="Path to APEL client config",
default=pathlib.Path("/etc/apel/client.cfg"),
)
def gap_publish(start: str, end: str, base_config: pathlib.Path):
with gap_config(start, end, base_config) as config_path:
subprocess.run(
["apelclient", f"--config={config_path}"]
)
subprocess.run(["ssmsend"])
@contextlib.contextmanager
def gap_config(start: str, end: str, base_config: pathlib.Path) -> Generator[pathlib.Path, None, None]:
config_content = configparser.ConfigParser()
with base_config.open("r") as in_stream:
config_content.read_file(in_stream)
config_content["unloader"]["interval"] = "gap"
config_content["unloader"]["gap_start"] = start
config_content["unloader"]["gap_end"] = end
with tempfile.NamedTemporaryFile(mode="w+") as config_file:
config_content.write(config_file)
config_file.flush()
yield pathlib.Path(config_file.name)
if __name__ == "__main__":
arguments = CLI.parse_args()
gap_publish(arguments.start, arguments.end, arguments.client_config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment