aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2024-02-29 02:29:14 +0100
committerPaul Oliver <contact@pauloliver.dev>2024-02-29 02:29:14 +0100
commit736f86bf68d8e115c46ec30263acec3f3e0af2a0 (patch)
treefbb5d8f2ceb5649cd9e74dd93a23b366ad5aad65
parentbb9e0f1631dd0773ba24d023b57cafba4b283a74 (diff)
Plugin added to help run simulators in the background.
[#20] The 'fireup.py' plugin can load one, or several, pre-saved simulations and restart them automatically, each inside its own tmux session.
-rwxr-xr-xbin/plugins/fireup.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/bin/plugins/fireup.py b/bin/plugins/fireup.py
new file mode 100755
index 0000000..3e1c750
--- /dev/null
+++ b/bin/plugins/fireup.py
@@ -0,0 +1,78 @@
+#!/usr/bin/env python3
+
+""" FIREUP: Plugin for the SALIS simulator.
+
+File: fireup.py
+Author: Paul Oliver
+Email: paul.t.oliver.design@gmail.com
+
+NOTE: REQUIRES TMUX TO WORK!
+
+Use this script to run one (or several) previously saved simulations in the
+background. Each simulator will run inside a tmux session.
+
+Remember to give this file correct permissions (chmod +x). To use, call it
+followed by an autosave interval (mandatory) and the list of save-files to
+load. Save-file paths are always relative to the './bin/sims/' directory.
+For example:
+
+$ ./bin/plugins/fireup.py -a 0x100000 -f 20.01.sim 20.02.sim 20.03.sim
+
+"""
+
+import argparse
+import os
+import subprocess
+
+
+# Parse CLI arguments with the argparse module. Required arguments are the
+# autosave interval (zero means no autosaving) and a list of previously saved
+# simulation files.
+parser = argparse.ArgumentParser(
+ description="TMUX launcher for pre-saved Salis simulations."
+)
+parser.add_argument(
+ "-f", "--files", required=True, type=str, nargs="+", metavar="FILE",
+ help="File name(s) of simulation(s) to load."
+)
+parser.add_argument(
+ "-a", "--auto", required=True, type=lambda x: int(x, 0), metavar="INT",
+ help="Auto-save interval for the loaded simulation(s)."
+)
+args = parser.parse_args()
+
+
+# Store the path of this script and the main Salis simulation script.
+path = os.path.dirname(__file__)
+salis = os.path.join(path, "../salis.py")
+
+
+# Revise that *all* listed files exist inside the './bin/sims/' directory.
+# Otherwise throw an exception.
+for fname in args.files:
+ abs_path = os.path.join(path, "../sims", fname)
+
+ if not os.path.isfile(abs_path):
+ parser.error("Save file '{}' not found.".format(abs_path))
+
+
+# Also, check that no file names are repeated.
+if len(args.files) != len(set(args.files)):
+ parser.error("Repeated file name detected.")
+
+
+# Everything seems OK! Let's fire up the TMUX sessions, one for every saved
+# file. Tmux sessions will be named similarly to their contained simulations.
+# We can, at any time, re-attach to any running session, or make use of all
+# other tmux commands.
+print("Firing up Salis simulations.")
+
+for fname in args.files:
+ session = "salis-{}".format(fname).replace(".", "-")
+ salis_cmd = "{} load -f {} -a {}".format(salis, fname, args.auto)
+ subprocess.run(["tmux", "new-session", "-d", "-s", session])
+ subprocess.run(["tmux", "send-keys", "-t", session, "{}".format(salis_cmd)])
+ subprocess.run(["tmux", "send-keys", "-t", session, "Enter", "Space"])
+ print("New tmux session '{}' is running '{}' in the background.".format(
+ session, fname
+ ))