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
1=== modified file 'checkbox-ng/launchers/checkbox-cli'
2--- checkbox-ng/launchers/checkbox-cli 2016-08-03 18:27:47 +0000
3+++ checkbox-ng/launchers/checkbox-cli 2016-08-08 08:25:58 +0000
4@@ -339,6 +339,10 @@
5 def _start_new_session(self):
6 print(_("Preparing..."))
7 title = self.launcher.app_id
8+ if self.ctx.args.title:
9+ title = self.ctx.args.title
10+ elif self.ctx.args.launcher:
11+ title = os.path.basename(self.ctx.args.launcher)
12 if self.launcher.app_version:
13 title += ' {}'.format(self.launcher.app_version)
14 self.ctx.sa.start_new_session(title)
15@@ -813,6 +817,9 @@
16 parser.add_argument(
17 '--verify', action='store_true',
18 help=_('only validate the launcher'))
19+ parser.add_argument(
20+ '--title', action='store', metavar='SESSION_NAME',
21+ help=_('title of the session to use'))
22
23
24 if __name__ == '__main__':
25
26=== modified file 'plainbox/plainbox/impl/runner.py'
27--- plainbox/plainbox/impl/runner.py 2016-07-12 11:47:54 +0000
28+++ plainbox/plainbox/impl/runner.py 2016-08-08 08:25:58 +0000
29@@ -51,7 +51,7 @@
30
31
32 def slugify(_string):
33- """Transform any string to onet that can be used in filenames."""
34+ """Transform any string to one that can be used in filenames."""
35 valid_chars = frozenset(
36 "-_.{}{}".format(string.ascii_letters, string.digits))
37 return ''.join(c if c in valid_chars else '_' for c in _string)
38
39=== modified file 'plainbox/plainbox/impl/session/assistant.py'
40--- plainbox/plainbox/impl/session/assistant.py 2016-07-04 13:37:03 +0000
41+++ plainbox/plainbox/impl/session/assistant.py 2016-08-08 08:25:58 +0000
42@@ -561,7 +561,7 @@
43 methods to see if session should be resumed instead.
44 """
45 UsageExpectation.of(self).enforce()
46- self._manager = SessionManager.create(self._repo)
47+ self._manager = SessionManager.create(self._repo, prefix=title + '-')
48 self._context = self._manager.add_local_device_context()
49 for provider in self._selected_providers:
50 if provider.problem_list:
51
52=== modified file 'plainbox/plainbox/impl/session/manager.py'
53--- plainbox/plainbox/impl/session/manager.py 2016-07-25 17:55:34 +0000
54+++ plainbox/plainbox/impl/session/manager.py 2016-08-08 08:25:58 +0000
55@@ -187,7 +187,7 @@
56 return self.default_device_context.state
57
58 @classmethod
59- def create(cls, repo=None, legacy_mode=False):
60+ def create(cls, repo=None, legacy_mode=False, prefix='pbox-'):
61 """
62 Create an empty session manager.
63
64@@ -217,7 +217,7 @@
65 logger.debug("SessionManager.create()")
66 if repo is None:
67 repo = SessionStorageRepository()
68- storage = SessionStorage.create(repo.location, legacy_mode)
69+ storage = SessionStorage.create(repo.location, legacy_mode, prefix)
70 WellKnownDirsHelper(storage).populate()
71 return cls([], storage)
72
73
74=== modified file 'plainbox/plainbox/impl/session/storage.py'
75--- plainbox/plainbox/impl/session/storage.py 2015-08-28 10:25:58 +0000
76+++ plainbox/plainbox/impl/session/storage.py 2016-08-08 08:25:58 +0000
77@@ -28,6 +28,7 @@
78 associated with a particular session.
79 """
80
81+import datetime
82 import errno
83 import logging
84 import os
85@@ -37,6 +38,7 @@
86 import tempfile
87
88 from plainbox.i18n import gettext as _, ngettext
89+from plainbox.impl.runner import slugify
90
91 logger = logging.getLogger("plainbox.session.storage")
92
93@@ -249,7 +251,7 @@
94 return os.path.join(self._location, self._SESSION_FILE)
95
96 @classmethod
97- def create(cls, base_dir, legacy_mode=False):
98+ def create(cls, base_dir, legacy_mode=False, prefix='pbox-'):
99 """
100 Create a new :class:`SessionStorage` in a random subdirectory
101 of the specified base directory. The base directory is also
102@@ -264,6 +266,10 @@
103 If False (defaults to True) then the caller is expected to
104 handle multiple sessions by itself.
105
106+ :param prefix:
107+ String which should prefix all session filenames. The prefix is
108+ sluggified before use.
109+
110 .. note::
111 Legacy mode is where applications using PlainBox API can only
112 handle one session. Creating another session replaces whatever was
113@@ -278,8 +284,18 @@
114 """
115 if not os.path.exists(base_dir):
116 os.makedirs(base_dir)
117- location = tempfile.mkdtemp(
118- prefix='pbox-', suffix='.session', dir=base_dir)
119+ isoformat = "%Y-%m-%dT%H.%M.%S"
120+ timestamp = datetime.datetime.utcnow().strftime(isoformat)
121+ location = os.path.join(base_dir, "{prefix}{timestamp}{suffix}".format(
122+ prefix=slugify(prefix), timestamp=timestamp, suffix='.session'))
123+ uniq = 1
124+ while os.path.exists(location):
125+ location = os.path.join(
126+ base_dir, "{prefix}{timestamp}_({uniq}){suffix}".format(
127+ prefix=slugify(prefix), timestamp=timestamp,
128+ uniq=uniq, suffix='.session'))
129+ uniq += 1
130+ os.mkdir(location)
131 logger.debug(_("Created new storage in %r"), location)
132 self = cls(location)
133 if legacy_mode:
134
135=== modified file 'plainbox/plainbox/impl/session/test_manager.py'
136--- plainbox/plainbox/impl/session/test_manager.py 2015-06-09 18:25:56 +0000
137+++ plainbox/plainbox/impl/session/test_manager.py 2016-08-08 08:25:58 +0000
138@@ -148,7 +148,8 @@
139 repo = mocks['SessionStorageRepository']()
140 # Ensure that a storage was created, with repository location and
141 # without legacy mode turned on
142- mocks['SessionStorage'].create.assert_called_with(repo.location, False)
143+ mocks['SessionStorage'].create.assert_called_with(
144+ repo.location, False, 'pbox-')
145 storage = mocks['SessionStorage'].create()
146 # Ensure that a default directories were created
147 mocks['WellKnownDirsHelper'].assert_called_with(storage)

Subscribers

People subscribed via source and target branches