Skip to content

Commit 908ed91

Browse files
authored
Merge pull request #178 from pyiron/cmd_update
Move command line interface from python -m pysqa.cmd to python -m pysqa
2 parents 43cb5ff + c719d56 commit 908ed91

4 files changed

Lines changed: 91 additions & 65 deletions

File tree

pysqa/cmd.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def command_line(argv, execute_command=execute_command):
4848
],
4949
)
5050
except getopt.GetoptError:
51-
print("cmd.py help")
52-
sys.exit()
51+
print("python -m pysqa --help")
5352
else:
5453
mode_submit = False
5554
mode_delete = False
@@ -92,9 +91,6 @@ def command_line(argv, execute_command=execute_command):
9291
dependency_list = [arg]
9392
else:
9493
dependency_list.append(arg)
95-
elif opt in ("-h", "--help"):
96-
print("cmd.py help ... coming soon.")
97-
sys.exit()
9894
if mode_submit or mode_delete or mode_reservation or mode_status:
9995
qa = QueueAdapter(directory=directory, execute_command=execute_command)
10096
if mode_submit:
@@ -122,9 +118,8 @@ def command_line(argv, execute_command=execute_command):
122118
for p, folder, files in os.walk(working_directory):
123119
remote_dirs.append(p)
124120
remote_files += [os.path.join(p, f) for f in files]
125-
print(json.dumps({"dirs": remote_dirs, "files": remote_files}))
126-
sys.exit()
127-
128-
129-
if __name__ == "__main__":
130-
command_line(sys.argv[1:])
121+
print(
122+
json.dumps({"dirs": sorted(remote_dirs), "files": sorted(remote_files)})
123+
)
124+
else:
125+
print("python -m pysqa --help ... coming soon.")

pysqa/ext/remote.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def get_job_from_remote(self, working_directory):
117117
)
118118
remote_dict = json.loads(
119119
self._execute_remote_command(
120-
command="python -m pysqa.cmd --list --working_directory "
120+
command="python -m pysqa --list --working_directory "
121121
+ remote_working_directory
122122
)
123123
)
@@ -191,11 +191,7 @@ def _open_ssh_connection(self):
191191
return ssh
192192

193193
def _remote_command(self):
194-
return (
195-
"python -m pysqa.cmd --config_directory "
196-
+ self._ssh_remote_config_dir
197-
+ " "
198-
)
194+
return "python -m pysqa --config_directory " + self._ssh_remote_config_dir + " "
199195

200196
def _get_queue_status_command(self):
201197
return self._remote_command() + "--status"

tests/test_cmd.py

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
2+
import io
3+
import json
24
import unittest
5+
import unittest.mock
36
from pysqa.cmd import command_line
47

58

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

14+
@unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
15+
def assert_stdout_command_line(self, cmd_args, execute_command, expected_output, mock_stdout):
16+
command_line(
17+
argv=cmd_args,
18+
execute_command=execute_command
19+
)
20+
self.assertEqual(mock_stdout.getvalue(), expected_output)
21+
1122
def test_help(self):
12-
with self.assertRaises(SystemExit):
13-
command_line(["--help"])
23+
self.assert_stdout_command_line(
24+
["--help"],
25+
None,
26+
"python -m pysqa --help ... coming soon.\n",
27+
)
1428

1529
def test_wrong_option(self):
16-
with self.assertRaises(SystemExit):
17-
command_line(["--error"])
30+
self.assert_stdout_command_line(
31+
["--error"],
32+
None,
33+
"python -m pysqa --help\n",
34+
)
1835

1936
def test_submit(self):
2037
def execute_command(
@@ -26,21 +43,21 @@ def execute_command(
2643
):
2744
return "1\n"
2845

29-
with self.assertRaises(SystemExit):
30-
command_line(
31-
[
32-
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
33-
"--submit",
34-
"--queue", "slurm",
35-
"--job_name", "test",
36-
"--working_directory", ".",
37-
"--cores", "2",
38-
"--memory", "1GB",
39-
"--run_time", "10",
40-
"--command", "echo hello"
41-
],
42-
execute_command=execute_command
43-
)
46+
self.assert_stdout_command_line(
47+
[
48+
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
49+
"--submit",
50+
"--queue", "slurm",
51+
"--job_name", "test",
52+
"--working_directory", ".",
53+
"--cores", "2",
54+
"--memory", "1GB",
55+
"--run_time", "10",
56+
"--command", "echo hello"
57+
],
58+
execute_command,
59+
"1\n",
60+
)
4461
with open("run_queue.sh") as f:
4562
output = f.readlines()
4663
content = [
@@ -69,18 +86,17 @@ def execute_command(
6986
):
7087
return "Success\n"
7188

72-
with self.assertRaises(SystemExit):
73-
command_line(
74-
[
75-
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
76-
"--delete",
77-
"--id", "1",
78-
],
79-
execute_command=execute_command
80-
)
89+
self.assert_stdout_command_line(
90+
[
91+
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
92+
"--delete",
93+
"--id", "1"
94+
],
95+
execute_command,
96+
"S\n"
97+
)
8198

8299
def test_status(self):
83-
84100
def execute_command(
85101
commands,
86102
working_directory=None,
@@ -91,14 +107,25 @@ def execute_command(
91107
with open(os.path.join(self.test_dir, "config", "slurm", "squeue_output")) as f:
92108
return f.read()
93109

94-
with self.assertRaises(SystemExit):
95-
command_line(
96-
[
97-
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
98-
"--status"
99-
],
100-
execute_command=execute_command
101-
)
110+
self.assert_stdout_command_line(
111+
[
112+
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
113+
"--status"
114+
],
115+
execute_command,
116+
json.dumps({
117+
"jobid": [5322019, 5322016, 5322017, 5322018, 5322013], "user": ["janj", "janj", "janj", "janj", "maxi"],
118+
"jobname": ["pi_19576488", "pi_19576485", "pi_19576486", "pi_19576487", "pi_19576482"],
119+
"status": ["running", "running", "running", "running", "running"],
120+
"working_directory": [
121+
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_1",
122+
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_2",
123+
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_3",
124+
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_4",
125+
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_5"
126+
]
127+
}) +"\n"
128+
)
102129

103130
def test_list(self):
104131
def execute_command(
@@ -110,13 +137,21 @@ def execute_command(
110137
):
111138
pass
112139

113-
with self.assertRaises(SystemExit):
114-
command_line(
115-
[
116-
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
117-
"--list",
118-
"--working_directory", os.path.join(self.test_dir, "config", "slurm"),
140+
self.assert_stdout_command_line(
141+
[
142+
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
143+
"--list",
144+
"--working_directory", os.path.join(self.test_dir, "config", "slurm"),
119145

120-
],
121-
execute_command=execute_command
122-
)
146+
],
147+
execute_command,
148+
json.dumps({
149+
"dirs": [os.path.join(self.test_dir, "config", "slurm")],
150+
"files": sorted([
151+
os.path.join(self.test_dir, "config", "slurm", "squeue_output"),
152+
os.path.join(self.test_dir, "config", "slurm", "slurm_extra.sh"),
153+
os.path.join(self.test_dir, "config", "slurm", "slurm.sh"),
154+
os.path.join(self.test_dir, "config", "slurm", "queue.yaml"),
155+
])
156+
}) + "\n"
157+
)

tests/test_remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def test_convert_path_to_remote(self):
4343

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

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

0 commit comments

Comments
 (0)