Skip to content

Commit 119cae3

Browse files
committed
APISession inherits from AbstractContextManager, and SMLog priorities are set.
SMlogs are set to LOG_DEBUG, except on failure to create a session. Fixed some less readable code: - lcache.py: import util on top instead of inside a method - util.py; removed double tempfile import Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
1 parent 4a333e5 commit 119cae3

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

drivers/lcache.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# along with this program; if not, write to the Free Software Foundation, Inc.,
1616
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1717

18+
import util
1819
from sm_typing import override
1920

2021
import os
@@ -197,7 +198,6 @@ def from_uuid(cls, sr_uuid):
197198

198199
@classmethod
199200
def from_session(cls, session):
200-
import util
201201
import SR as sm
202202

203203
host_ref = util.get_localhost_ref(session)
@@ -219,7 +219,6 @@ def from_session(cls, session):
219219

220220
@classmethod
221221
def from_cli(cls):
222-
import util
223222
with util.APISession("SM-lcache-CacheFileSR") as session:
224223
return cls.from_session(session)
225224

@@ -234,8 +233,6 @@ def _fast_find_nodes(self):
234233
return list(found)
235234

236235
def xapi_vfs_stats(self):
237-
import util
238-
239236
f = self.statvfs()
240237
if not f.f_frsize:
241238
raise util.SMException("Cache FS does not report utilization.")

drivers/util.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
import traceback
4343
import glob
4444
import copy
45-
import tempfile
45+
import contextlib
46+
from sm_typing import override
4647

4748
from functools import reduce
4849

@@ -701,11 +702,11 @@ def getrootdevID():
701702
return rootdevID
702703

703704

704-
class APISession:
705+
class APISession(contextlib.AbstractContextManager):
705706
def __init__(self, originator="SM"):
706707
self.originator = originator
707708
self.session = self.login()
708-
SMlog("APISession [{}] login".format(self.originator))
709+
SMlog("APISession [{}] login".format(self.originator), priority=LOG_DEBUG)
709710
atexit.register(self.atexit)
710711

711712
def login(self):
@@ -715,17 +716,17 @@ def login(self):
715716
session.xenapi.login_with_password('root', '', '', self.originator)
716717
except Exception as exc:
717718
msg = f"APISession [{self.originator}] Unable to open local XAPI session"
718-
SMlog(msg)
719+
SMlog(msg, priority=LOG_ERR)
719720
raise xs_errors.XenError(msg) from exc
720721
return session
721722

722723
def _logout(self, log):
723724
"""Closes an API session"""
724725
if self.session is None:
725-
SMlog("APISession [{}] session is None {}".format(self.originator, log))
726+
SMlog("APISession [{}] session is None {}".format(self.originator, log), priority=LOG_DEBUG)
726727
return
727728
self.session.xenapi.session.logout()
728-
SMlog("APISession [{}] {}".format(self.originator, log))
729+
SMlog("APISession [{}] {}".format(self.originator, log), priority=LOG_DEBUG)
729730
self.session = None
730731

731732
def logout(self, log="logout"):
@@ -739,9 +740,13 @@ def __del__(self):
739740
def atexit(self):
740741
self._logout(log="logout atexit")
741742

743+
@override
742744
def __enter__(self):
745+
if not self.session:
746+
raise SyntaxError("Session is already closed: wrong usage of context.")
743747
return self.session
744748

749+
@override
745750
def __exit__(self, _type, _value, _traceback):
746751
self.logout(log=f"logout exception[{_type}] {_value}" if _type else "logout context")
747752

0 commit comments

Comments
 (0)