Merge lp:~satwell/duplicity/caching into lp:duplicity/0.6

Proposed by Kenneth Loafman
Status: Merged
Merged at revision: 894
Proposed branch: lp:~satwell/duplicity/caching
Merge into: lp:duplicity/0.6
Diff against target: 134 lines (+70/-9)
3 files modified
duplicity/cached_ops.py (+62/-0)
duplicity/path.py (+6/-5)
duplicity/tarfile.py (+2/-4)
To merge this branch: bzr merge lp:~satwell/duplicity/caching
Reviewer Review Type Date Requested Status
Kenneth Loafman Pending
Review via email: mp+133810@code.launchpad.net

Description of the change

Add a cache for password and group lookups. This significantly improves runtime with very large password and group configurations.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'duplicity/cached_ops.py'
2--- duplicity/cached_ops.py 1970-01-01 00:00:00 +0000
3+++ duplicity/cached_ops.py 2012-11-11 01:32:21 +0000
4@@ -0,0 +1,62 @@
5+# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
6+#
7+# Copyright 2012 Google Inc.
8+#
9+# This file is part of duplicity.
10+#
11+# Duplicity is free software; you can redistribute it and/or modify it
12+# under the terms of the GNU General Public License as published by the
13+# Free Software Foundation; either version 2 of the License, or (at your
14+# option) any later version.
15+#
16+# Duplicity is distributed in the hope that it will be useful, but
17+# WITHOUT ANY WARRANTY; without even the implied warranty of
18+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+# General Public License for more details.
20+#
21+# You should have received a copy of the GNU General Public License
22+# along with duplicity; if not, write to the Free Software Foundation,
23+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24+
25+"""Cache-wrapped functions for grp and pwd lookups."""
26+
27+import grp
28+import pwd
29+
30+
31+class CachedCall(object):
32+ """Decorator for caching the results of function calls."""
33+
34+ def __init__(self, f):
35+ self.cache = {}
36+ self.f = f
37+
38+ def __call__(self, *args):
39+ try:
40+ return self.cache[args]
41+ except (KeyError, TypeError), e:
42+ result = self.f(*args)
43+ if not isinstance(e, TypeError):
44+ # TypeError most likely means that args is not hashable
45+ self.cache[args] = result
46+ return result
47+
48+
49+@CachedCall
50+def getgrgid(gid):
51+ return grp.getgrgid(gid)
52+
53+
54+@CachedCall
55+def getgrnam(name):
56+ return grp.getgrnam(name)
57+
58+
59+@CachedCall
60+def getpwnam(name):
61+ return pwd.getpwnam(name)
62+
63+
64+@CachedCall
65+def getpwuid(uid):
66+ return pwd.getpwuid(uid)
67
68=== modified file 'duplicity/path.py'
69--- duplicity/path.py 2011-08-23 18:14:17 +0000
70+++ duplicity/path.py 2012-11-11 01:32:21 +0000
71@@ -26,7 +26,7 @@
72
73 """
74
75-import stat, errno, socket, time, re, gzip, pwd, grp
76+import stat, errno, socket, time, re, gzip
77
78 from duplicity import tarfile
79 from duplicity import file_naming
80@@ -36,6 +36,7 @@
81 from duplicity import librsync
82 from duplicity import log #@UnusedImport
83 from duplicity import dup_time
84+from duplicity import cached_ops
85 from duplicity.lazy import * #@UnusedWildImport
86
87 _copy_blocksize = 64 * 1024
88@@ -206,13 +207,13 @@
89 try:
90 if globals.numeric_owner:
91 raise KeyError
92- self.stat.st_uid = pwd.getpwnam(tarinfo.uname)[2]
93+ self.stat.st_uid = cached_ops.getpwnam(tarinfo.uname)[2]
94 except KeyError:
95 self.stat.st_uid = tarinfo.uid
96 try:
97 if globals.numeric_owner:
98 raise KeyError
99- self.stat.st_gid = grp.getgrnam(tarinfo.gname)[2]
100+ self.stat.st_gid = cached_ops.getgrnam(tarinfo.gname)[2]
101 except KeyError:
102 self.stat.st_gid = tarinfo.gid
103
104@@ -284,11 +285,11 @@
105 ti.mtime = int(self.stat.st_mtime)
106
107 try:
108- ti.uname = pwd.getpwuid(ti.uid)[0]
109+ ti.uname = cached_ops.getpwuid(ti.uid)[0]
110 except KeyError:
111 ti.uname = ''
112 try:
113- ti.gname = grp.getgrgid(ti.gid)[0]
114+ ti.gname = cached_ops.getgrgid(ti.gid)[0]
115 except KeyError:
116 ti.gname = ''
117
118
119=== modified file 'duplicity/tarfile.py'
120--- duplicity/tarfile.py 2011-10-08 17:40:11 +0000
121+++ duplicity/tarfile.py 2012-11-11 01:32:21 +0000
122@@ -53,10 +53,8 @@
123 import re
124 import operator
125
126-try:
127- import grp, pwd
128-except ImportError:
129- grp = pwd = None
130+from duplicity import cached_ops
131+grp = pwd = cached_ops
132
133 # from tarfile import *
134 __all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"]

Subscribers

People subscribed via source and target branches

to all changes: