Skip to content

Commit cb4eab6

Browse files
committed
do not import LOCAL_CLUSTER directly, access it via settings module
1 parent 74744aa commit cb4eab6

File tree

11 files changed

+47
-38
lines changed

11 files changed

+47
-38
lines changed

pyslurm/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
sys.setdlopenflags(sys.getdlopenflags() | os.RTLD_GLOBAL | os.RTLD_DEEPBIND)
99

10-
# Initialize slurm api
11-
from pyslurm.api import slurm_init, slurm_fini
12-
slurm_init()
13-
1410
from .version import __version__
1511

1612
from pyslurm import db
@@ -35,3 +31,7 @@
3531

3632
# The old API in deprecated.pyx
3733
from pyslurm.deprecated import *
34+
35+
# Initialize slurm api
36+
from pyslurm.api import slurm_init, slurm_fini
37+
slurm_init()

pyslurm/api.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
# cython: c_string_type=unicode, c_string_encoding=default
2323
# cython: language_level=3
2424

25+
from pyslurm import settings
26+
2527

2628
def slurm_init(config_path=None):
2729
"""Initialize the Slurm API.
@@ -36,6 +38,7 @@ def slurm_init(config_path=None):
3638
None, so libslurm will automatically detect its config.
3739
"""
3840
slurm.slurm_init(cstr.from_unicode(config_path))
41+
settings.init()
3942

4043

4144
def slurm_fini():

pyslurm/core/job/job.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ from typing import Union
3434
from pyslurm.utils import cstr, ctime
3535
from pyslurm.utils.uint import *
3636
from pyslurm.core.job.util import *
37-
from pyslurm.settings import LOCAL_CLUSTER
37+
from pyslurm import settings
3838
from pyslurm import xcollections
3939
from pyslurm.core.error import (
4040
RPCError,
@@ -236,7 +236,7 @@ cdef class Job:
236236
self.ptr.job_id = job_id
237237
self.passwd = {}
238238
self.groups = {}
239-
cstr.fmalloc(&self.ptr.cluster, LOCAL_CLUSTER)
239+
cstr.fmalloc(&self.ptr.cluster, settings.LOCAL_CLUSTER)
240240
self.steps = JobSteps()
241241
self.stats = JobStatistics()
242242
self.pids = {}

pyslurm/core/job/step.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ from typing import Union
2626
from pyslurm.utils import cstr, ctime
2727
from pyslurm.utils.uint import *
2828
from pyslurm.core.error import RPCError, verify_rpc
29-
from pyslurm.settings import LOCAL_CLUSTER
29+
from pyslurm import settings
3030
from pyslurm import xcollections
3131
from pyslurm.utils.helpers import (
3232
signal_to_num,
@@ -159,7 +159,7 @@ cdef class JobStep:
159159
self.id = step_id
160160
self.stats = JobStepStatistics()
161161
self.pids = {}
162-
cstr.fmalloc(&self.ptr.cluster, LOCAL_CLUSTER)
162+
cstr.fmalloc(&self.ptr.cluster, settings.LOCAL_CLUSTER)
163163

164164
# Initialize attributes, if any were provided
165165
for k, v in kwargs.items():

pyslurm/core/node.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ from pyslurm.utils import ctime
2828
from pyslurm.utils.uint import *
2929
from pyslurm.core.error import RPCError, verify_rpc
3030
from pyslurm.utils.ctime import timestamp_to_date, _raw_time
31-
from pyslurm.settings import LOCAL_CLUSTER
31+
from pyslurm import settings
3232
from pyslurm import xcollections
3333
from pyslurm.utils.helpers import (
3434
uid_to_name,
@@ -222,7 +222,7 @@ cdef class Node:
222222
def __init__(self, name=None, **kwargs):
223223
self._alloc_impl()
224224
self.name = name
225-
self.cluster = LOCAL_CLUSTER
225+
self.cluster = settings.LOCAL_CLUSTER
226226
for k, v in kwargs.items():
227227
setattr(self, k, v)
228228

@@ -270,7 +270,7 @@ cdef class Node:
270270
wrap._alloc_info()
271271
wrap.passwd = {}
272272
wrap.groups = {}
273-
wrap.cluster = LOCAL_CLUSTER
273+
wrap.cluster = settings.LOCAL_CLUSTER
274274
memcpy(wrap.info, in_ptr, sizeof(node_info_t))
275275
return wrap
276276

pyslurm/core/partition.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ from pyslurm.utils.uint import *
3030
from pyslurm.core.error import RPCError, verify_rpc
3131
from pyslurm.utils.ctime import timestamp_to_date, _raw_time
3232
from pyslurm.constants import UNLIMITED
33-
from pyslurm.settings import LOCAL_CLUSTER
33+
from pyslurm import settings
3434
from pyslurm.core import slurmctld
3535
from pyslurm.core.slurmctld.config import _get_memory
3636
from pyslurm import xcollections
@@ -168,7 +168,7 @@ cdef class Partition:
168168
def __init__(self, name=None, **kwargs):
169169
self._alloc_impl()
170170
self.name = name
171-
self.cluster = LOCAL_CLUSTER
171+
self.cluster = settings.LOCAL_CLUSTER
172172
for k, v in kwargs.items():
173173
setattr(self, k, v)
174174

@@ -194,7 +194,7 @@ cdef class Partition:
194194
cdef Partition from_ptr(partition_info_t *in_ptr):
195195
cdef Partition wrap = Partition.__new__(Partition)
196196
wrap._alloc_impl()
197-
wrap.cluster = LOCAL_CLUSTER
197+
wrap.cluster = settings.LOCAL_CLUSTER
198198
memcpy(wrap.ptr, in_ptr, sizeof(partition_info_t))
199199
return wrap
200200

pyslurm/db/assoc.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ from pyslurm.utils.helpers import (
2929
)
3030
from pyslurm.utils.uint import *
3131
from pyslurm.db.connection import _open_conn_or_error
32-
from pyslurm.settings import LOCAL_CLUSTER
32+
from pyslurm import settings
3333
from pyslurm import xcollections
3434

3535

@@ -65,7 +65,7 @@ cdef class Associations(MultiClusterMap):
6565
# Fetch Assoc Data
6666
assoc_data = SlurmList.wrap(slurmdb_associations_get(
6767
conn.ptr, cond.ptr))
68-
68+
6969
if assoc_data.is_null:
7070
raise RPCError(msg="Failed to get Association data from slurmdbd")
7171

@@ -135,7 +135,7 @@ cdef class Associations(MultiClusterMap):
135135
else:
136136
# Autodetects the last slurm error
137137
raise RPCError()
138-
138+
139139
if not db_connection:
140140
# Autocommit if no connection was explicitly specified.
141141
conn.commit()
@@ -185,7 +185,7 @@ cdef class Association:
185185
def __init__(self, **kwargs):
186186
self._alloc_impl()
187187
self.id = 0
188-
self.cluster = LOCAL_CLUSTER
188+
self.cluster = settings.LOCAL_CLUSTER
189189
for k, v in kwargs.items():
190190
setattr(self, k, v)
191191

pyslurm/db/job.pyx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424

2525
from typing import Union, Any
2626
from pyslurm.core.error import RPCError, PyslurmError
27-
from pyslurm.core import slurmctld
28-
from typing import Any
2927
from pyslurm.utils.uint import *
30-
from pyslurm.settings import LOCAL_CLUSTER
28+
from pyslurm import settings
3129
from pyslurm import xcollections
3230
from pyslurm.utils.ctime import (
3331
date_to_timestamp,
@@ -106,7 +104,7 @@ cdef class JobFilter:
106104
if not self.clusters:
107105
# This is a requirement for some other parameters to function
108106
# correctly, like self.nodelist
109-
return [LOCAL_CLUSTER]
107+
return [settings.LOCAL_CLUSTER]
110108
elif self.clusters == "all":
111109
return None
112110
else:
@@ -450,7 +448,7 @@ cdef class Job:
450448
self._alloc_impl()
451449
self.ptr.jobid = int(job_id)
452450
cstr.fmalloc(&self.ptr.cluster,
453-
LOCAL_CLUSTER if not cluster else cluster)
451+
settings.LOCAL_CLUSTER if not cluster else cluster)
454452
self.qos_data = QualitiesOfService()
455453
self.steps = JobSteps()
456454
self.stats = JobStatistics()
@@ -511,7 +509,7 @@ cdef class Job:
511509
>>> db_job = pyslurm.db.Job.load(10000, with_script=True)
512510
>>> print(db_job.script)
513511
"""
514-
cluster = LOCAL_CLUSTER if not cluster else cluster
512+
cluster = settings.LOCAL_CLUSTER if not cluster else cluster
515513
jfilter = JobFilter(ids=[int(job_id)], clusters=[cluster],
516514
with_script=with_script, with_env=with_env)
517515
job = Jobs.load(jfilter).get((cluster, int(job_id)))

pyslurm/settings.pyx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@
2222
# cython: c_string_type=unicode, c_string_encoding=default
2323
# cython: language_level=3
2424

25-
from pyslurm.core import slurmctld
2625
from pyslurm cimport slurm
2726
from pyslurm.utils cimport cstr
2827

2928

30-
LOCAL_CLUSTER = cstr.to_unicode(slurm.slurm_conf.cluster_name)
31-
if not LOCAL_CLUSTER:
32-
slurm_conf = slurmctld.Config.load()
33-
LOCAL_CLUSTER = slurm_conf.cluster_name
29+
LOCAL_CLUSTER = "UNKNOWN"
30+
31+
32+
def init():
33+
from pyslurm.core import slurmctld
34+
35+
global LOCAL_CLUSTER
36+
LOCAL_CLUSTER = cstr.to_unicode(slurm.slurm_conf.cluster_name)
37+
if not LOCAL_CLUSTER:
38+
slurm_conf = slurmctld.Config.load()
39+
LOCAL_CLUSTER = slurm_conf.cluster_name

pyslurm/xcollections.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ cdef class MultiClusterMap:
9595
_key_type
9696
_val_type
9797
_id_attr
98+
_cluster

0 commit comments

Comments
 (0)