/usr/lib/python3/dist-packages/plainbox/impl/session/legacy.py is in python3-plainbox 0.5.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | # This file is part of Checkbox.
#
# Copyright 2012, 2013 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
:mod:`plainbox.impl.session.legacy` -- Legacy suspend/resume API
================================================================
"""
import abc
import logging
import os
from plainbox.i18n import gettext as _
from plainbox.impl.session.manager import SessionManager
from plainbox.impl.session.state import SessionState
from plainbox.impl.session.storage import SessionStorageRepository
logger = logging.getLogger("plainbox.session.legacy")
class ISessionStateLegacyAPI(metaclass=abc.ABCMeta):
"""
Interface describing legacy parts of the SessionState API.
"""
session_data_filename = 'session.json'
@abc.abstractproperty
def session_dir(self):
"""
pathname of a temporary directory for this session
This is not None only between calls to open() / close().
"""
@abc.abstractproperty
def jobs_io_log_dir(self):
"""
pathname of the jobs IO logs directory
This is not None only between calls to open() / close().
"""
@abc.abstractproperty
def open(self):
"""
Open session state for running jobs.
This function creates the cache directory where jobs can store their
data. See:
http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
"""
@abc.abstractmethod
def clean(self):
"""
Clean the session directory.
"""
@abc.abstractmethod
def remove(self):
"""
Remove this session
"""
@abc.abstractmethod
def previous_session_file(self):
"""
Check the filesystem for previous session data
Returns the full pathname to the session file if it exists
"""
@abc.abstractmethod
def persistent_save(self):
"""
Save to disk the minimum needed to resume plainbox where it stopped
"""
@abc.abstractmethod
def resume(self):
"""
Erase the job_state_map and desired_job_list with the saved ones
"""
@abc.abstractmethod
def __enter__(self):
return self
@abc.abstractmethod
def __exit__(self, *args):
self.close()
class SessionStateLegacyAPICompatImpl(SessionState, ISessionStateLegacyAPI):
"""
Compatibility wrapper to use new suspend/resume implementation via the
original (legacy) suspend/resume API.
This subclass of SessionState implements the ISessionStateLegacyAPI
interface thus allowing applications to keep using suspend/resume as they
did before, without adjusting their code.
:ivar _manager:
Instance of SessionManager (this is a bit insane because
the manager actually knows about the session too)
:ivar _commit_hint:
Either None or a set of flags (strings) that determine what kind of
actions should take place before the next time the 'manager' property
gets accessed. This is used to implement lazy decision on how to
map the open/resume/clean methods onto the SessionManager API
"""
def __init__(self, job_list):
super(SessionStateLegacyAPICompatImpl, self).__init__(job_list)
self._manager = None
self._commit_hint = None
def open(self):
"""
Open session state for running jobs.
"""
logger.debug("SessionState.open()")
self._add_hint('open')
return self
def resume(self):
"""
Erase the job_state_map and desired_job_list with the saved ones
"""
logger.debug("SessionState.resume()")
self._add_hint('resume')
self._commit_manager()
def clean(self):
"""
Clean the session directory.
"""
logger.debug("SessionState.clean()")
self._add_hint('clean')
self._commit_manager()
def close(self):
"""
Close the session.
Legacy API, this function does absolutely nothing
"""
logger.debug("SessionState.close()")
self._manager = None
self._commit_hint = None
def remove(self):
logger.debug("SessionState.remove()")
self.manager.destroy()
self._manager = None
self._commit_hint = None
def _add_hint(self, hint):
if self._commit_hint is None:
self._commit_hint = set()
self._commit_hint.add(hint)
@property
def manager(self):
logger.debug(_(".manager accessed"))
if self._commit_hint is not None:
self._commit_manager()
if self._manager is None:
raise AttributeError(_("Session not ready, did you call open()?"))
return self._manager
def _commit_manager(self):
"""
Commit the new value of the '_manager' instance attribute.
This method looks at '_commit_hint' to figure out if the semantics
of open(), resume() or clean() should be applied on the SessionManager
instance that this class is tracking.
"""
logger.debug(
"_commit_manager(), _commit_hint: %r", self._commit_hint)
assert isinstance(self._commit_hint, set)
if 'open' in self._commit_hint:
if 'resume' in self._commit_hint:
self._commit_resume()
elif 'clean' in self._commit_hint:
self._commit_clean()
else:
self._commit_open()
self._commit_hint = None
def _commit_open(self):
logger.debug("_commit_open()")
self._manager = SessionManager.create_with_job_list(
self.job_list, legacy_mode=True)
# Compatibility hack. Since session manager is supposed to
# create and manage both session state and session storage
# we need to inject ourselves into its internal attribute.
# This way it will keep operating on this instance in the
# essential checkpoint() method.
self._manager._state = self
def _commit_clean(self):
logger.debug("_commit_clean()")
if self._manager:
self._manager.destroy()
self._manager.create_with_job_list(self.job_list)
self._manager = SessionManager.create_with_job_list(
self.job_list, legacy_mode=True)
self._manager._state = self
def _commit_resume(self):
logger.debug("_commit_resume()")
last_storage = SessionStorageRepository().get_last_storage()
assert last_storage is not None, "no saved session to resume"
self._manager = SessionManager.load_session(
self.job_list, last_storage, lambda session: self)
logger.debug(_("_commit_resume() finished"))
@property
def session_dir(self):
"""
pathname of a temporary directory for this session
This is not None only between calls to open() / close().
"""
if self._commit_hint is not None:
self._commit_manager()
if self._manager is None:
return None
else:
return self.manager.storage.location
@property
def jobs_io_log_dir(self):
"""
pathname of the jobs IO logs directory
This is not None only between calls to open() / close().
"""
# TODO: use well-known dir helper
return os.path.join(self.manager.storage.location, 'io-logs')
def previous_session_file(self):
"""
Check the filesystem for previous session data
Returns the full pathname to the session file if it exists
"""
last_storage = SessionStorageRepository().get_last_storage()
if last_storage and os.path.exists(last_storage.session_file):
return last_storage.session_file
def persistent_save(self):
"""
Save to disk the minimum needed to resume plainbox where it stopped
"""
self.manager.checkpoint()
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
SessionStateLegacyAPI = SessionStateLegacyAPICompatImpl
|