Skip to content

Commit 3a603e1

Browse files
authored
Fix parsing user names in JobSearchFilter (#303)
Fixes #302 - also makes it clearer in the docs for some attributes which type is expected inside the lists.
1 parent 37f7e22 commit 3a603e1

File tree

5 files changed

+38
-52
lines changed

5 files changed

+38
-52
lines changed

pyslurm/db/job.pxd

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,72 +56,72 @@ from pyslurm.db.tres cimport TrackableResources, TrackableResource
5656

5757

5858
cdef class JobSearchFilter:
59-
"""Search conditions for Slurm database Jobs.
59+
"""Query-Conditions for Jobs in the Slurm Database.
6060
6161
Args:
6262
**kwargs (Any, optional=None):
6363
Any valid attribute of the object.
6464
6565
Attributes:
66-
ids (list):
66+
ids (list[int]):
6767
A list of Job ids to search for.
6868
start_time (Union[str, int, datetime.datetime]):
6969
Search for Jobs which started after this time.
7070
end_time (Union[str, int, datetime.datetime]):
7171
Search for Jobs which ended before this time.
72-
accounts (list):
72+
accounts (list[str]):
7373
Search for Jobs with these account names.
74-
association_ids (list):
74+
association_ids (list[int]):
7575
Search for Jobs with these association ids.
76-
clusters (list):
76+
clusters (list[str]):
7777
Search for Jobs running in these clusters.
78-
constraints (list):
78+
constraints (list[str]):
7979
Search for Jobs with these constraints.
8080
cpus (int):
8181
Search for Jobs with exactly this many CPUs.
82-
Note: If you also specify max_cpus, then this value will act as
82+
Note: If you also specify `max_cpus`, then this value will act as
8383
the minimum.
8484
max_cpus (int):
8585
Search for Jobs with no more than this amount of CPUs.
86-
Note: This value has no effect without also setting cpus.
86+
Note: This value has no effect without also setting `cpus`.
8787
nodes (int):
8888
Search for Jobs with exactly this many nodes.
89-
Note: If you also specify max_nodes, then this value will act as
89+
Note: If you also specify `max_nodes`, then this value will act as
9090
the minimum.
9191
max_nodes (int):
9292
Search for Jobs with no more than this amount of nodes.
93-
Note: This value has no effect without also setting nodes.
94-
qos (list):
93+
Note: This value has no effect without also setting `nodes`.
94+
qos (list[str]):
9595
Search for Jobs with these Qualities of Service.
96-
names (list):
96+
names (list[str]):
9797
Search for Jobs with these job names.
98-
partitions (list):
98+
partitions (list[str]):
9999
Search for Jobs with these partition names.
100-
groups (list):
101-
Search for Jobs with these group names. You can both specify the
102-
groups as string or by their GID.
100+
groups (list[str]):
101+
Search for Jobs with these group names. Alternatively, you can
102+
also specify the GIDs directly.
103103
timelimit (Union[str, int]):
104104
Search for Jobs with exactly this timelimit.
105-
Note: If you also specify max_timelimit, then this value will act
105+
Note: If you also specify `max_timelimit`, then this value will act
106106
as the minimum.
107107
max_timelimit (Union[str, int]):
108108
Search for Jobs which run no longer than this timelimit
109-
Note: This value has no effect without also setting timelimit
110-
users (list):
111-
Search for Jobs with these user names. You can both specify the
112-
users as string or by their UID.
113-
wckeys (list):
109+
Note: This value has no effect without also setting `timelimit`
110+
users (list[str]):
111+
Search for Jobs with these user names. Alternatively, you can also
112+
specify the UIDs directly.
113+
wckeys (list[str]):
114114
Search for Jobs with these WCKeys
115-
nodelist (list):
115+
nodelist (list[str]):
116116
Search for Jobs that ran on any of these Nodes
117117
with_script (bool):
118118
Instruct the slurmdbd to also send the job script(s)
119119
Note: This requires specifying explictiy job ids, and is mutually
120-
exclusive with with_env
120+
exclusive with `with_env`
121121
with_env (bool):
122122
Instruct the slurmdbd to also send the job environment(s)
123123
Note: This requires specifying explictiy job ids, and is mutually
124-
exclusive with with_script
124+
exclusive with `with_script`
125125
"""
126126
cdef slurmdb_job_cond_t *ptr
127127

pyslurm/db/job.pyx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,12 @@ cdef class JobSearchFilter:
8787
def _parse_groups(self):
8888
if not self.groups:
8989
return None
90-
91-
gid_list = []
92-
for group in self.groups:
93-
if isinstance(group, int):
94-
gid_list.append(group)
95-
else:
96-
gid_list.append(group_to_gid(group))
97-
98-
return gid_list
90+
return list({group_to_gid(group) for group in self.groups})
9991

10092
def _parse_users(self):
10193
if not self.users:
10294
return None
103-
104-
uid_list = []
105-
for user in self.users:
106-
if not isinstance(user, list):
107-
uid_list.append(int(user))
108-
elif user:
109-
uid_list.append(user_to_uid(user))
110-
111-
return uid_list
95+
return list({user_to_uid(user) for user in self.users})
11296

11397
def _parse_clusters(self):
11498
if not self.clusters:

pyslurm/utils/helpers.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ def user_to_uid(user, err_on_invalid=True):
9595
return slurm.NO_VAL
9696

9797
try:
98-
if isinstance(user, str):
98+
if isinstance(user, str) and not user.isdigit():
9999
return getpwnam(user).pw_uid
100100

101-
return getpwuid(user).pw_uid
101+
return getpwuid(int(user)).pw_uid
102102
except KeyError as e:
103103
if err_on_invalid:
104104
raise e
@@ -112,10 +112,10 @@ def group_to_gid(group, err_on_invalid=True):
112112
return slurm.NO_VAL
113113

114114
try:
115-
if isinstance(group, str):
115+
if isinstance(group, str) and not group.isdigit():
116116
return getgrnam(group).gr_gid
117117

118-
return getgrgid(group).gr_gid
118+
return getgrgid(int(group)).gr_gid
119119
except KeyError as e:
120120
if err_on_invalid:
121121
raise e

tests/unit/test_common.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ def test_parse_uid(self):
309309
name = uid_to_name(0, lookup=lookup)
310310
assert name == "root"
311311

312-
uid = user_to_uid("root")
313-
assert uid == 0
312+
assert user_to_uid("root") == 0
313+
assert user_to_uid(0) == 0
314+
assert user_to_uid("0") == 0
314315

315316
with pytest.raises(KeyError):
316317
name = uid_to_name(2**32-5)
@@ -326,8 +327,9 @@ def test_parse_gid(self):
326327
name = gid_to_name(0, lookup=lookup)
327328
assert name == "root"
328329

329-
gid = group_to_gid("root")
330-
assert gid == 0
330+
assert group_to_gid("root") == 0
331+
assert group_to_gid(0) == 0
332+
assert group_to_gid("0") == 0
331333

332334
with pytest.raises(KeyError):
333335
name = gid_to_name(2**32-5)

tests/unit/test_db_job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import pyslurm
2525

2626

27-
def test_search_filter():
27+
def test_filter():
2828
job_filter = pyslurm.db.JobSearchFilter()
2929

3030
job_filter.clusters = ["test1"]

0 commit comments

Comments
 (0)