Skip to content

Commit 9c1655d

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 0ddd81a commit 9c1655d

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
@@ -199,7 +200,6 @@ def from_uuid(cls, sr_uuid):
199200

200201
@classmethod
201202
def from_session(cls, session):
202-
import util
203203
import SR as sm
204204

205205
host_ref = util.get_localhost_ref(session)
@@ -221,7 +221,6 @@ def from_session(cls, session):
221221

222222
@classmethod
223223
def from_cli(cls):
224-
import util
225224
with util.APISession("SM-lcache-CacheFileSR") as session:
226225
return cls.from_session(session)
227226

@@ -236,8 +235,6 @@ def _fast_find_nodes(self):
236235
return list(found)
237236

238237
def xapi_vfs_stats(self):
239-
import util
240-
241238
f = self.statvfs()
242239
if not f.f_frsize:
243240
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
@@ -47,7 +47,8 @@
4747
import traceback
4848
import glob
4949
import copy
50-
import tempfile
50+
import contextlib
51+
from sm_typing import override
5152

5253
from functools import reduce
5354
from sm_typing import List, Optional
@@ -769,11 +770,11 @@ def getrootdevID():
769770
return rootdevID
770771

771772

772-
class APISession:
773+
class APISession(contextlib.AbstractContextManager):
773774
def __init__(self, originator="SM"):
774775
self.originator = originator
775776
self.session = self.login()
776-
SMlog("APISession [{}] login".format(self.originator))
777+
SMlog("APISession [{}] login".format(self.originator), priority=LOG_DEBUG)
777778
atexit.register(self.atexit)
778779

779780
def login(self):
@@ -783,17 +784,17 @@ def login(self):
783784
session.xenapi.login_with_password('root', '', '', self.originator)
784785
except Exception as exc:
785786
msg = f"APISession [{self.originator}] Unable to open local XAPI session"
786-
SMlog(msg)
787+
SMlog(msg, priority=LOG_ERR)
787788
raise xs_errors.XenError(msg) from exc
788789
return session
789790

790791
def _logout(self, log):
791792
"""Closes an API session"""
792793
if self.session is None:
793-
SMlog("APISession [{}] session is None {}".format(self.originator, log))
794+
SMlog("APISession [{}] session is None {}".format(self.originator, log), priority=LOG_DEBUG)
794795
return
795796
self.session.xenapi.session.logout()
796-
SMlog("APISession [{}] {}".format(self.originator, log))
797+
SMlog("APISession [{}] {}".format(self.originator, log), priority=LOG_DEBUG)
797798
self.session = None
798799

799800
def logout(self, log="logout"):
@@ -807,9 +808,13 @@ def __del__(self):
807808
def atexit(self):
808809
self._logout(log="logout atexit")
809810

811+
@override
810812
def __enter__(self):
813+
if not self.session:
814+
raise SyntaxError("Session is already closed: wrong usage of context.")
811815
return self.session
812816

817+
@override
813818
def __exit__(self, _type, _value, _traceback):
814819
self.logout(log=f"logout exception[{_type}] {_value}" if _type else "logout context")
815820

0 commit comments

Comments
 (0)