From 9808a8d95be816fd46989423b2abb7523688daf1 Mon Sep 17 00:00:00 2001
From: Daniel K Lyons <dlyons@nrao.edu>
Date: Thu, 4 Mar 2021 13:22:30 -0700
Subject: [PATCH] Make vulture resolve scripts adjacent to the condor submit
 file

---
 .../executables/vulture/test/localscript.condor    |  4 ++++
 apps/cli/executables/vulture/test/test.sh          |  3 +++
 apps/cli/executables/vulture/test/test_vulture.py  | 10 ++++++++++
 apps/cli/executables/vulture/vulture/condorlite.py | 14 +++++++++++++-
 4 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 apps/cli/executables/vulture/test/localscript.condor
 create mode 100755 apps/cli/executables/vulture/test/test.sh

diff --git a/apps/cli/executables/vulture/test/localscript.condor b/apps/cli/executables/vulture/test/localscript.condor
new file mode 100644
index 000000000..757b5d3f4
--- /dev/null
+++ b/apps/cli/executables/vulture/test/localscript.condor
@@ -0,0 +1,4 @@
+executable = test.sh
+arguments = hello
+
+queue
\ No newline at end of file
diff --git a/apps/cli/executables/vulture/test/test.sh b/apps/cli/executables/vulture/test/test.sh
new file mode 100755
index 000000000..3747a7ba0
--- /dev/null
+++ b/apps/cli/executables/vulture/test/test.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+echo $*
diff --git a/apps/cli/executables/vulture/test/test_vulture.py b/apps/cli/executables/vulture/test/test_vulture.py
index 32487089a..c987b2d78 100644
--- a/apps/cli/executables/vulture/test/test_vulture.py
+++ b/apps/cli/executables/vulture/test/test_vulture.py
@@ -56,6 +56,16 @@ def test_job_stdout():
     os.remove("null.error")
 
 
+def test_shell_script_in_working_directory(caplog):
+    test_job = Job("test/localscript.condor")
+    test_job.execute()
+    with open("test.sh.out", "r") as f:
+        contents = f.read()
+    assert "hello" in contents
+    os.remove("test.sh.out")
+    os.remove("test.sh.error")
+
+
 def test_job_stderr():
     """
     Test that vulture writes correct output from stderr to a log file
diff --git a/apps/cli/executables/vulture/vulture/condorlite.py b/apps/cli/executables/vulture/vulture/condorlite.py
index 1244613d2..ab3724735 100644
--- a/apps/cli/executables/vulture/vulture/condorlite.py
+++ b/apps/cli/executables/vulture/vulture/condorlite.py
@@ -133,6 +133,7 @@ class Job:
         :param write_log: Boolean that determines whether a log file will be written
         """
         self.fields, self.command = self.parse(file)
+        self.job_directory = Path(file).resolve().parent
         self.write_log = write_log
         if self.write_log:
             if self.fields.get("executable"):
@@ -216,8 +217,19 @@ class Job:
 
         # Execute
         if self.fields.get("executable"):
+            # So there is a thing that can happen here, where the condor file refers to a script in the same directory
+            # HTCondor will happily execute such a thing, but using subprocess.run like this will not. So we have to
+            # detect if there is a file in the current directory with the same name as the executable, and if there is,
+            # we must prefix with "./" to get the right behavior.
+            if (self.job_directory / self.fields["executable"]).exists():
+                executable = str(
+                    (self.job_directory / self.fields["executable"]).resolve()
+                )
+            else:
+                executable = self.fields["executable"]
+
             process = subprocess.run(
-                [self.fields["executable"], self.fields["arguments"]],
+                [executable, self.fields["arguments"]],
                 capture_output=capture_output,
                 text=True,
             )
-- 
GitLab