Skip to content

Commit

Permalink
Merge pull request #178 from pyiron/cmd_update
Browse files Browse the repository at this point in the history
Move command line interface from python -m pysqa.cmd to python -m pysqa
  • Loading branch information
jan-janssen committed May 4, 2023
2 parents 43cb5ff + c719d56 commit 908ed91
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 65 deletions.
17 changes: 6 additions & 11 deletions pysqa/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def command_line(argv, execute_command=execute_command):
],
)
except getopt.GetoptError:
print("cmd.py help")
sys.exit()
print("python -m pysqa --help")
else:
mode_submit = False
mode_delete = False
Expand Down Expand Up @@ -92,9 +91,6 @@ def command_line(argv, execute_command=execute_command):
dependency_list = [arg]
else:
dependency_list.append(arg)
elif opt in ("-h", "--help"):
print("cmd.py help ... coming soon.")
sys.exit()
if mode_submit or mode_delete or mode_reservation or mode_status:
qa = QueueAdapter(directory=directory, execute_command=execute_command)
if mode_submit:
Expand Down Expand Up @@ -122,9 +118,8 @@ def command_line(argv, execute_command=execute_command):
for p, folder, files in os.walk(working_directory):
remote_dirs.append(p)
remote_files += [os.path.join(p, f) for f in files]
print(json.dumps({"dirs": remote_dirs, "files": remote_files}))
sys.exit()


if __name__ == "__main__":
command_line(sys.argv[1:])
print(
json.dumps({"dirs": sorted(remote_dirs), "files": sorted(remote_files)})
)
else:
print("python -m pysqa --help ... coming soon.")
8 changes: 2 additions & 6 deletions pysqa/ext/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_job_from_remote(self, working_directory):
)
remote_dict = json.loads(
self._execute_remote_command(
command="python -m pysqa.cmd --list --working_directory "
command="python -m pysqa --list --working_directory "
+ remote_working_directory
)
)
Expand Down Expand Up @@ -191,11 +191,7 @@ def _open_ssh_connection(self):
return ssh

def _remote_command(self):
return (
"python -m pysqa.cmd --config_directory "
+ self._ssh_remote_config_dir
+ " "
)
return "python -m pysqa --config_directory " + self._ssh_remote_config_dir + " "

def _get_queue_status_command(self):
return self._remote_command() + "--status"
Expand Down
127 changes: 81 additions & 46 deletions tests/test_cmd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import io
import json
import unittest
import unittest.mock
from pysqa.cmd import command_line


Expand All @@ -8,13 +11,27 @@ class TestCMD(unittest.TestCase):
def setUpClass(cls):
cls.test_dir = os.path.abspath(os.path.dirname(__file__))

@unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
def assert_stdout_command_line(self, cmd_args, execute_command, expected_output, mock_stdout):
command_line(
argv=cmd_args,
execute_command=execute_command
)
self.assertEqual(mock_stdout.getvalue(), expected_output)

def test_help(self):
with self.assertRaises(SystemExit):
command_line(["--help"])
self.assert_stdout_command_line(
["--help"],
None,
"python -m pysqa --help ... coming soon.\n",
)

def test_wrong_option(self):
with self.assertRaises(SystemExit):
command_line(["--error"])
self.assert_stdout_command_line(
["--error"],
None,
"python -m pysqa --help\n",
)

def test_submit(self):
def execute_command(
Expand All @@ -26,21 +43,21 @@ def execute_command(
):
return "1\n"

with self.assertRaises(SystemExit):
command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--submit",
"--queue", "slurm",
"--job_name", "test",
"--working_directory", ".",
"--cores", "2",
"--memory", "1GB",
"--run_time", "10",
"--command", "echo hello"
],
execute_command=execute_command
)
self.assert_stdout_command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--submit",
"--queue", "slurm",
"--job_name", "test",
"--working_directory", ".",
"--cores", "2",
"--memory", "1GB",
"--run_time", "10",
"--command", "echo hello"
],
execute_command,
"1\n",
)
with open("run_queue.sh") as f:
output = f.readlines()
content = [
Expand Down Expand Up @@ -69,18 +86,17 @@ def execute_command(
):
return "Success\n"

with self.assertRaises(SystemExit):
command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--delete",
"--id", "1",
],
execute_command=execute_command
)
self.assert_stdout_command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--delete",
"--id", "1"
],
execute_command,
"S\n"
)

def test_status(self):

def execute_command(
commands,
working_directory=None,
Expand All @@ -91,14 +107,25 @@ def execute_command(
with open(os.path.join(self.test_dir, "config", "slurm", "squeue_output")) as f:
return f.read()

with self.assertRaises(SystemExit):
command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--status"
],
execute_command=execute_command
)
self.assert_stdout_command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--status"
],
execute_command,
json.dumps({
"jobid": [5322019, 5322016, 5322017, 5322018, 5322013], "user": ["janj", "janj", "janj", "janj", "maxi"],
"jobname": ["pi_19576488", "pi_19576485", "pi_19576486", "pi_19576487", "pi_19576482"],
"status": ["running", "running", "running", "running", "running"],
"working_directory": [
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_1",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_2",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_3",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_4",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_5"
]
}) +"\n"
)

def test_list(self):
def execute_command(
Expand All @@ -110,13 +137,21 @@ def execute_command(
):
pass

with self.assertRaises(SystemExit):
command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--list",
"--working_directory", os.path.join(self.test_dir, "config", "slurm"),
self.assert_stdout_command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--list",
"--working_directory", os.path.join(self.test_dir, "config", "slurm"),

],
execute_command=execute_command
)
],
execute_command,
json.dumps({
"dirs": [os.path.join(self.test_dir, "config", "slurm")],
"files": sorted([
os.path.join(self.test_dir, "config", "slurm", "squeue_output"),
os.path.join(self.test_dir, "config", "slurm", "slurm_extra.sh"),
os.path.join(self.test_dir, "config", "slurm", "slurm.sh"),
os.path.join(self.test_dir, "config", "slurm", "queue.yaml"),
])
}) + "\n"
)
4 changes: 2 additions & 2 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def test_convert_path_to_remote(self):

def test_delete_command(self):
self.assertEqual(
"python -m pysqa.cmd --config_directory /u/share/pysqa/resources/queues/ --delete --id 123",
"python -m pysqa --config_directory /u/share/pysqa/resources/queues/ --delete --id 123",
self.remote._adapter._delete_command(job_id=123)
)

def test_reservation_command(self):
self.assertEqual(
"python -m pysqa.cmd --config_directory /u/share/pysqa/resources/queues/ --reservation --id 123",
"python -m pysqa --config_directory /u/share/pysqa/resources/queues/ --reservation --id 123",
self.remote._adapter._reservation_command(job_id=123)
)

Expand Down

0 comments on commit 908ed91

Please sign in to comment.