From 260b66265c1b88b00051b2ce2f562fc00ec67c8c Mon Sep 17 00:00:00 2001
From: Jeremy Auguste <jeremy.auguste@lis-lab.fr>
Date: Mon, 24 Sep 2018 10:10:06 +0200
Subject: [PATCH] Added way to specify multiple hosts (and ignore hosts)

---
 batchoar.py |  9 ++++++---
 oargen.py   | 17 ++++++++++++-----
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/batchoar.py b/batchoar.py
index 6b52d35..ee906ba 100755
--- a/batchoar.py
+++ b/batchoar.py
@@ -23,8 +23,10 @@ def argparser():
                         help="If True, reserves only cores with GPUs")
     parser.add_argument('-c', '--core', default=1, type=int,
                         help="Number of cores to reserve")
-    parser.add_argument('-H', '--host',
-                        help="Name of the host (SQL LIKE syntax accepted)")
+    parser.add_argument('-H', '--host', nargs="+", default=[],
+                        help="Name of the hosts (SQL 'LIKE' syntax accepted)")
+    parser.add_argument('-I', '--ignore-host', nargs="+", default=[],
+                        help="Name of the hosts to ignore (SQL 'NOT LIKE' syntax accepted)")
     parser.add_argument('-i', '--interactive', action="store_true",
                         help="Launch job in interactive mode")
     parser.add_argument('-C', '--checkpoint', type=int, metavar="SECONDS",
@@ -95,7 +97,7 @@ def load_jobs(yaml_in, args):
     yaml_jobs = yaml.load(yaml_in)
 
     var_names, var_tuples = load_variables(yaml_jobs["variables"])
-    constants = yaml_jobs["constants"]
+    constants = yaml_jobs["constants"] if "constants" in yaml_jobs else {}
     flags = load_flags(yaml_jobs["flags"], args)
     try:
         precommand = yaml_jobs["environment"]["precommand"]
@@ -142,6 +144,7 @@ def main():
             anterior = anteriors.pop()
 
         oar_command = oargen.prepare_oarsub(args.gpu, args.host, args.core, args.time,
+                                            ignore_hosts=args.ignore_host,
                                             command=job, name=job_name,
                                             output_directory=args.directory,
                                             besteffort=args.besteffort,
diff --git a/oargen.py b/oargen.py
index 0b24f6d..4d273fa 100755
--- a/oargen.py
+++ b/oargen.py
@@ -27,8 +27,10 @@ def argparser():
                         help="If True, reserves only cores with GPUs")
     parser.add_argument('-c', '--core', default=1, type=int,
                         help="Number of cores to reserve")
-    parser.add_argument('-H', '--host',
-                        help="Name of the host (SQL LIKE syntax accepted)")
+    parser.add_argument('-H', '--host', nargs="+", default=[],
+                        help="Name of the hosts (SQL 'LIKE' syntax accepted)")
+    parser.add_argument('-I', '--ignore-host', nargs="+", default=[],
+                        help="Name of the hosts to ignore (SQL 'NOT LIKE' syntax accepted)")
     parser.add_argument('-i', '--interactive', action="store_true",
                         help="Launch job in interactive mode")
     parser.add_argument('-C', '--checkpoint', type=int, metavar="SECONDS",
@@ -42,7 +44,8 @@ def argparser():
     return args
 
 
-def prepare_oarsub(gpu, host, core, time,
+def prepare_oarsub(gpu, hosts, core, time,
+                   ignore_hosts=[],
                    command=None,
                    interactive=False,
                    name=None, output_directory=None,
@@ -55,8 +58,12 @@ def prepare_oarsub(gpu, host, core, time,
         properties += "(gpu IS NOT NULL)"
     else:
         properties += "(gpu IS NULL)"
-    if host is not None:
-        properties += " AND host LIKE '{}'".format(host)
+    if hosts:
+        for host in hosts:
+            properties += " AND host LIKE '{}'".format(host)
+    if ignore_hosts:
+        for host in ignore_hosts:
+            properties += " AND host NOT LIKE '{}'".format(host)
     properties += ""
     oar_cmd.append(properties)
 
-- 
GitLab