summaryrefslogtreecommitdiff
path: root/tickets/1113f1af92984443b3e44700449aadc1/Maildir/new/1466244005.M574950P15747Q2.hrun
blob: f2f5bc420bed04a45acf33afbbfa5788f81239da (plain)
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
Return-Path: <obnam-dev-bounces@obnam.org>
X-Original-To: distix@pieni.net
Delivered-To: distix@pieni.net
Received: from bagpuss.pepperfish.net (bagpuss.pepperfish.net [148.251.8.16])
	(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by pieni.net (Postfix) with ESMTPS id 35EA22036F
	for <distix@pieni.net>; Sat, 18 Jun 2016 11:57:09 +0200 (CEST)
Received: from platypus.pepperfish.net (unknown [10.112.100.20])
	by bagpuss.pepperfish.net (Postfix) with ESMTP id 94428CA;
	Sat, 18 Jun 2016 10:57:08 +0100 (BST)
Received: from ip6-localhost ([::1] helo=platypus.pepperfish.net)
	by platypus.pepperfish.net with esmtp (Exim 4.80 #2 (Debian))
	id 1bED00-0005rU-E1; Sat, 18 Jun 2016 10:57:08 +0100
Received: from inmail0 ([10.112.100.10] helo=mx0.pepperfish.net)
 by platypus.pepperfish.net with esmtp (Exim 4.80 #2 (Debian))
 id 1bECzy-0005rO-Se
 for <obnam-dev@obnam.org>; Sat, 18 Jun 2016 10:57:06 +0100
Received: from smtp.gentoo.org ([140.211.166.183])
 by mx0.pepperfish.net with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256)
 (Exim 4.80) (envelope-from <robbat2@gentoo.org>) id 1bECzw-0001JH-UU
 for obnam-dev@obnam.org; Sat, 18 Jun 2016 10:57:06 +0100
Received: from grubbs.orbis-terrarum.net (localhost [127.0.0.1])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA (256/256 bits))
 (No client certificate requested)
 by smtp.gentoo.org (Postfix) with ESMTPS id E202734076D
 for <obnam-dev@obnam.org>; Sat, 18 Jun 2016 09:56:51 +0000 (UTC)
Received: (qmail 22421 invoked by uid 129); 18 Jun 2016 09:56:52 -0000
X-HELO: bohr-int.orbis-terrarum.net
Authentication-Results: orbis-terrarum.net; auth=pass (plain)
 smtp.auth=robbat2-bohr@orbis-terrarum.net
Received: from Unknown (HELO bohr-int.orbis-terrarum.net) (2001:470:e889:1::8)
 by orbis-terrarum.net (qpsmtpd/0.95) with ESMTPSA
 (ECDHE-RSA-AES256-GCM-SHA384 encrypted); Sat, 18 Jun 2016 09:56:52 +0000
Received: (nullmailer pid 28320 invoked by uid 10000);
 Sat, 18 Jun 2016 09:48:44 -0000
From: "Robin H. Johnson" <robbat2@gentoo.org>
To: obnam-dev@obnam.org
Date: Sat, 18 Jun 2016 02:48:42 -0700
Message-Id: <20160618094844.28217-1-robbat2@gentoo.org>
X-Mailer: git-send-email 2.9.0
X-Virus-Checked: Checked by ClamAV on orbis-terrarum.net
X-Spam-Score: -8.3
X-Spam-Score-int: -82
X-Spam-Bar: --------
X-Scanned-By: pepperfish.net, Sat, 18 Jun 2016 10:57:06 +0100
X-Spam-Report: Content analysis details: (-8.3 points)
 pts rule name              description
 ---- ---------------------- --------------------------------------------------
 -5.0 RCVD_IN_DNSWL_HI       RBL: Sender listed at http://www.dnswl.org/, high
 trust [140.211.166.183 listed in list.dnswl.org]
 -0.0 SPF_PASS               SPF: sender matches SPF record
 -1.4 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain
 -1.9 BAYES_00               BODY: Bayes spam probability is 0 to 1%
 [score: 0.0000]
X-ACL-Warn: message may be spam
X-Scan-Signature: 28cac42b4624e15eecd62f442284eda4
Subject: [PATCH 1/3] ForceLock: convert boilerplate to ContextManager
X-BeenThere: obnam-dev@obnam.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Obnam development discussions <obnam-dev-obnam.org>
List-Unsubscribe: <http://listmaster.pepperfish.net/cgi-bin/mailman/listinfo/obnam-dev-obnam.org>,
 <mailto:obnam-dev-request@obnam.org?subject=unsubscribe>
List-Archive: <http://listmaster.pepperfish.net/pipermail/obnam-dev-obnam.org>
List-Post: <mailto:obnam-dev@obnam.org>
List-Help: <mailto:obnam-dev-request@obnam.org?subject=help>
List-Subscribe: <http://listmaster.pepperfish.net/cgi-bin/mailman/listinfo/obnam-dev-obnam.org>,
 <mailto:obnam-dev-request@obnam.org?subject=subscribe>
Sender: obnam-dev-bounces@obnam.org
Errors-To: obnam-dev-bounces@obnam.org

Reduce code duplication by introducing a repo context manager per
PEP-343. This paves the way to introduce more lock handling functions
without adding duplicate code.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
---
 obnamlib/plugins/force_lock_plugin.py | 62 ++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 34 deletions(-)

diff --git a/obnamlib/plugins/force_lock_plugin.py b/obnamlib/plugins/force_lock_plugin.py
index 85995be..28bf641 100644
--- a/obnamlib/plugins/force_lock_plugin.py
+++ b/obnamlib/plugins/force_lock_plugin.py
@@ -24,6 +24,24 @@ class RepositoryAccessError(obnamlib.ObnamError):
     msg = 'Repository does not exist or cannot be accessed: {error}'
 
 
+class RepoAccessWrapper(object):
+    def __init__(self, plugin):
+        self.plugin = plugin
+        self.repo = None
+
+    def __enter__(self):
+        self.plugin.app.settings.require('repository')
+        repourl = self.plugin.app.settings['repository']
+        logging.info('Repository: %s', repourl)
+        try:
+            self.repo = self.plugin.app.get_repository_object()
+        except OSError, e:
+            raise RepositoryAccessError(error=str(e))
+        return self.repo
+
+    def __exit__(self, type, value, traceback):
+        self.repo.close()
+
 class ForceLockPlugin(obnamlib.ObnamPlugin):
 
     def enable(self):
@@ -33,25 +51,12 @@ class ForceLockPlugin(obnamlib.ObnamPlugin):
     def force_lock(self, args):
         '''Force a locked repository to be open.'''
 
-        self.app.settings.require('repository')
-
-        repourl = self.app.settings['repository']
-        client_name = self.app.settings['client-name']
         logging.info('Forcing lock')
-        logging.info('Repository: %s', repourl)
-
-        try:
-            repo = self.app.get_repository_object()
-        except OSError, e:
-            raise RepositoryAccessError(error=str(e))
-
-        repo.force_client_list_lock()
-        for client_name in repo.get_client_names():
-            repo.force_client_lock(client_name)
-        repo.force_chunk_indexes_lock()
-
-        repo.close()
-
+        with RepoAccessWrapper(self) as repo:
+            repo.force_client_list_lock()
+            for client_name in repo.get_client_names():
+                repo.force_client_lock(client_name)
+            repo.force_chunk_indexes_lock()
         return 0
 
     def lock(self, args):
@@ -61,24 +66,13 @@ class ForceLockPlugin(obnamlib.ObnamPlugin):
 
         '''
 
-        self.app.settings.require('repository')
-
-        repourl = self.app.settings['repository']
         client_name = self.app.settings['client-name']
         logging.info('Creating lock')
-        logging.info('Repository: %s', repourl)
         logging.info('Client: %s', client_name)
-
-        try:
-            repo = self.app.get_repository_object()
-        except OSError, e:
-            raise RepositoryAccessError(error=str(e))
-
-        repo.lock_client_list()
-        if client_name:
-            repo.lock_client(client_name)
-        repo.lock_chunk_indexes()
-
-        repo.close()
+        with RepoAccessWrapper(self) as repo:
+            repo.lock_client_list()
+            if client_name:
+                repo.lock_client(client_name)
+            repo.lock_chunk_indexes()
 
         return 0
-- 
2.9.0


_______________________________________________
obnam-dev mailing list
obnam-dev@obnam.org
http://listmaster.pepperfish.net/cgi-bin/mailman/listinfo/obnam-dev-obnam.org