Merge lp:~kissiel/checkbox/session-storage-renames into lp:checkbox

Proposed by Maciej Kisielewski
Status: Merged
Approved by: Chris Wayne
Approved revision: 4468
Merged at revision: 4466
Proposed branch: lp:~kissiel/checkbox/session-storage-renames
Merge into: lp:checkbox
Diff against target: 147 lines (+32/-8)
6 files modified
checkbox-ng/launchers/checkbox-cli (+7/-0)
plainbox/plainbox/impl/runner.py (+1/-1)
plainbox/plainbox/impl/session/assistant.py (+1/-1)
plainbox/plainbox/impl/session/manager.py (+2/-2)
plainbox/plainbox/impl/session/storage.py (+19/-3)
plainbox/plainbox/impl/session/test_manager.py (+2/-1)
To merge this branch: bzr merge lp:~kissiel/checkbox/session-storage-renames
Reviewer Review Type Date Requested Status
Pierre Equoy Approve
Paul Larson Approve
Review via email: mp+302149@code.launchpad.net

Description of the change

This MR brings changes to how sessions are stored in the filesystem.

Instead of using mkdtemp, the directory name that's used for storing session is now generated from two variables - session title and timestamp.

Title is determined by (in order of importance):

1) command line argument (--title)
2) basename of the launcher name
3) front-end name (e.g. checkbox-cli

To test it run checkbox-cli in different ways and observe ~/.config/plainbox/*

8a01902 plainbox:runner: fix typo in docstring
4573b73 plainbox:session: allow custom prefixes for session storage
265e26a plainbox:session: use timestamp in session storage path
4ed79a2 checkbox-ng: use launcher basename as sesion title
530faee checkbox-ng: add --title option for naming sessions

To post a comment you must log in.
Revision history for this message
Pierre Equoy (pieq) wrote :

Apart from the small typo (see below), it looks good to me!

I tried to brutalize it by passing weird characters but it slugifies everything, so it's OK (well, Chinese words end up like a bunch of underscores but I guess the point is to be able to quickly name a session when testing, so it's not a big deal)

review: Needs Fixing
4463. By Launchpad Translations on behalf of checkbox-dev

Launchpad automatic translations update.

4464. By Maciej Kisielewski

plainbox:runner: fix typo in docstring

Signed-off-by: Maciej Kisielewski <email address hidden>

4465. By Maciej Kisielewski

plainbox:session: allow custom prefixes for session storage

Signed-off-by: Maciej Kisielewski <email address hidden>

4466. By Maciej Kisielewski

plainbox:session: use timestamp in session storage path

Instead of using mkdtemp that gives somewhat enigmatic session dir names, this
patch makes plainbox use timestamp instead.

session directory is now in the format {prefix}{timestamp}{suffix}, where
prefix is determined by the session title, timestamp is in ISO8601 compliant
format (%Y-%m-%dT%H.%M.%S), and suffix is '.session'.

In the rare case where requested directory would already exist, _({num}) is
inserted between timestamp and suffix. {num} starts at 1.

Signed-off-by: Maciej Kisielewski <email address hidden>

4467. By Maciej Kisielewski

checkbox-ng: use launcher basename as sesion title

Signed-off-by: Maciej Kisielewski <email address hidden>

4468. By Maciej Kisielewski

checkbox-ng: add --title option for naming sessions

Signed-off-by: Maciej Kisielewski <email address hidden>

Revision history for this message
Maciej Kisielewski (kissiel) wrote :

> Apart from the small typo (see below), it looks good to me!
>
> I tried to brutalize it by passing weird characters but it slugifies
> everything, so it's OK (well, Chinese words end up like a bunch of underscores
> but I guess the point is to be able to quickly name a session when testing, so
> it's not a big deal)

All in all there was too much 'all' :)
Fixed and repushed

Revision history for this message
Paul Larson (pwlars) wrote :

It looks like you rebased, so it's hard to tell for sure, but I think the only thing that changed since the last push was the typo that epierre mentioned. So +1

review: Approve
Revision history for this message
Pierre Equoy (pieq) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'checkbox-ng/launchers/checkbox-cli'
--- checkbox-ng/launchers/checkbox-cli 2016-08-03 18:27:47 +0000
+++ checkbox-ng/launchers/checkbox-cli 2016-08-08 08:25:58 +0000
@@ -339,6 +339,10 @@
339 def _start_new_session(self):339 def _start_new_session(self):
340 print(_("Preparing..."))340 print(_("Preparing..."))
341 title = self.launcher.app_id341 title = self.launcher.app_id
342 if self.ctx.args.title:
343 title = self.ctx.args.title
344 elif self.ctx.args.launcher:
345 title = os.path.basename(self.ctx.args.launcher)
342 if self.launcher.app_version:346 if self.launcher.app_version:
343 title += ' {}'.format(self.launcher.app_version)347 title += ' {}'.format(self.launcher.app_version)
344 self.ctx.sa.start_new_session(title)348 self.ctx.sa.start_new_session(title)
@@ -813,6 +817,9 @@
813 parser.add_argument(817 parser.add_argument(
814 '--verify', action='store_true',818 '--verify', action='store_true',
815 help=_('only validate the launcher'))819 help=_('only validate the launcher'))
820 parser.add_argument(
821 '--title', action='store', metavar='SESSION_NAME',
822 help=_('title of the session to use'))
816823
817824
818if __name__ == '__main__':825if __name__ == '__main__':
819826
=== modified file 'plainbox/plainbox/impl/runner.py'
--- plainbox/plainbox/impl/runner.py 2016-07-12 11:47:54 +0000
+++ plainbox/plainbox/impl/runner.py 2016-08-08 08:25:58 +0000
@@ -51,7 +51,7 @@
5151
5252
53def slugify(_string):53def slugify(_string):
54 """Transform any string to onet that can be used in filenames."""54 """Transform any string to one that can be used in filenames."""
55 valid_chars = frozenset(55 valid_chars = frozenset(
56 "-_.{}{}".format(string.ascii_letters, string.digits))56 "-_.{}{}".format(string.ascii_letters, string.digits))
57 return ''.join(c if c in valid_chars else '_' for c in _string)57 return ''.join(c if c in valid_chars else '_' for c in _string)
5858
=== modified file 'plainbox/plainbox/impl/session/assistant.py'
--- plainbox/plainbox/impl/session/assistant.py 2016-07-04 13:37:03 +0000
+++ plainbox/plainbox/impl/session/assistant.py 2016-08-08 08:25:58 +0000
@@ -561,7 +561,7 @@
561 methods to see if session should be resumed instead.561 methods to see if session should be resumed instead.
562 """562 """
563 UsageExpectation.of(self).enforce()563 UsageExpectation.of(self).enforce()
564 self._manager = SessionManager.create(self._repo)564 self._manager = SessionManager.create(self._repo, prefix=title + '-')
565 self._context = self._manager.add_local_device_context()565 self._context = self._manager.add_local_device_context()
566 for provider in self._selected_providers:566 for provider in self._selected_providers:
567 if provider.problem_list:567 if provider.problem_list:
568568
=== modified file 'plainbox/plainbox/impl/session/manager.py'
--- plainbox/plainbox/impl/session/manager.py 2016-07-25 17:55:34 +0000
+++ plainbox/plainbox/impl/session/manager.py 2016-08-08 08:25:58 +0000
@@ -187,7 +187,7 @@
187 return self.default_device_context.state187 return self.default_device_context.state
188188
189 @classmethod189 @classmethod
190 def create(cls, repo=None, legacy_mode=False):190 def create(cls, repo=None, legacy_mode=False, prefix='pbox-'):
191 """191 """
192 Create an empty session manager.192 Create an empty session manager.
193193
@@ -217,7 +217,7 @@
217 logger.debug("SessionManager.create()")217 logger.debug("SessionManager.create()")
218 if repo is None:218 if repo is None:
219 repo = SessionStorageRepository()219 repo = SessionStorageRepository()
220 storage = SessionStorage.create(repo.location, legacy_mode)220 storage = SessionStorage.create(repo.location, legacy_mode, prefix)
221 WellKnownDirsHelper(storage).populate()221 WellKnownDirsHelper(storage).populate()
222 return cls([], storage)222 return cls([], storage)
223223
224224
=== modified file 'plainbox/plainbox/impl/session/storage.py'
--- plainbox/plainbox/impl/session/storage.py 2015-08-28 10:25:58 +0000
+++ plainbox/plainbox/impl/session/storage.py 2016-08-08 08:25:58 +0000
@@ -28,6 +28,7 @@
28associated with a particular session.28associated with a particular session.
29"""29"""
3030
31import datetime
31import errno32import errno
32import logging33import logging
33import os34import os
@@ -37,6 +38,7 @@
37import tempfile38import tempfile
3839
39from plainbox.i18n import gettext as _, ngettext40from plainbox.i18n import gettext as _, ngettext
41from plainbox.impl.runner import slugify
4042
41logger = logging.getLogger("plainbox.session.storage")43logger = logging.getLogger("plainbox.session.storage")
4244
@@ -249,7 +251,7 @@
249 return os.path.join(self._location, self._SESSION_FILE)251 return os.path.join(self._location, self._SESSION_FILE)
250252
251 @classmethod253 @classmethod
252 def create(cls, base_dir, legacy_mode=False):254 def create(cls, base_dir, legacy_mode=False, prefix='pbox-'):
253 """255 """
254 Create a new :class:`SessionStorage` in a random subdirectory256 Create a new :class:`SessionStorage` in a random subdirectory
255 of the specified base directory. The base directory is also257 of the specified base directory. The base directory is also
@@ -264,6 +266,10 @@
264 If False (defaults to True) then the caller is expected to266 If False (defaults to True) then the caller is expected to
265 handle multiple sessions by itself.267 handle multiple sessions by itself.
266268
269 :param prefix:
270 String which should prefix all session filenames. The prefix is
271 sluggified before use.
272
267 .. note::273 .. note::
268 Legacy mode is where applications using PlainBox API can only274 Legacy mode is where applications using PlainBox API can only
269 handle one session. Creating another session replaces whatever was275 handle one session. Creating another session replaces whatever was
@@ -278,8 +284,18 @@
278 """284 """
279 if not os.path.exists(base_dir):285 if not os.path.exists(base_dir):
280 os.makedirs(base_dir)286 os.makedirs(base_dir)
281 location = tempfile.mkdtemp(287 isoformat = "%Y-%m-%dT%H.%M.%S"
282 prefix='pbox-', suffix='.session', dir=base_dir)288 timestamp = datetime.datetime.utcnow().strftime(isoformat)
289 location = os.path.join(base_dir, "{prefix}{timestamp}{suffix}".format(
290 prefix=slugify(prefix), timestamp=timestamp, suffix='.session'))
291 uniq = 1
292 while os.path.exists(location):
293 location = os.path.join(
294 base_dir, "{prefix}{timestamp}_({uniq}){suffix}".format(
295 prefix=slugify(prefix), timestamp=timestamp,
296 uniq=uniq, suffix='.session'))
297 uniq += 1
298 os.mkdir(location)
283 logger.debug(_("Created new storage in %r"), location)299 logger.debug(_("Created new storage in %r"), location)
284 self = cls(location)300 self = cls(location)
285 if legacy_mode:301 if legacy_mode:
286302
=== modified file 'plainbox/plainbox/impl/session/test_manager.py'
--- plainbox/plainbox/impl/session/test_manager.py 2015-06-09 18:25:56 +0000
+++ plainbox/plainbox/impl/session/test_manager.py 2016-08-08 08:25:58 +0000
@@ -148,7 +148,8 @@
148 repo = mocks['SessionStorageRepository']()148 repo = mocks['SessionStorageRepository']()
149 # Ensure that a storage was created, with repository location and149 # Ensure that a storage was created, with repository location and
150 # without legacy mode turned on150 # without legacy mode turned on
151 mocks['SessionStorage'].create.assert_called_with(repo.location, False)151 mocks['SessionStorage'].create.assert_called_with(
152 repo.location, False, 'pbox-')
152 storage = mocks['SessionStorage'].create()153 storage = mocks['SessionStorage'].create()
153 # Ensure that a default directories were created154 # Ensure that a default directories were created
154 mocks['WellKnownDirsHelper'].assert_called_with(storage)155 mocks['WellKnownDirsHelper'].assert_called_with(storage)

Subscribers

People subscribed via source and target branches