##// END OF EJS Templates
store: simplify walking...
Matt Mackall -
r6899:56a7a54e default
parent child Browse files
Show More
@@ -1,125 +1,117 b''
1 # store.py - repository store handling for Mercurial
1 # store.py - repository store handling for Mercurial
2 #
2 #
3 # Copyright 2008 Matt Mackall <mpm@selenic.com>
3 # Copyright 2008 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 from i18n import _
8 from i18n import _
9 import os, stat, osutil, util
9 import os, stat, osutil, util
10
10
11 def _buildencodefun():
11 def _buildencodefun():
12 e = '_'
12 e = '_'
13 win_reserved = [ord(x) for x in '\\:*?"<>|']
13 win_reserved = [ord(x) for x in '\\:*?"<>|']
14 cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ])
14 cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ])
15 for x in (range(32) + range(126, 256) + win_reserved):
15 for x in (range(32) + range(126, 256) + win_reserved):
16 cmap[chr(x)] = "~%02x" % x
16 cmap[chr(x)] = "~%02x" % x
17 for x in range(ord("A"), ord("Z")+1) + [ord(e)]:
17 for x in range(ord("A"), ord("Z")+1) + [ord(e)]:
18 cmap[chr(x)] = e + chr(x).lower()
18 cmap[chr(x)] = e + chr(x).lower()
19 dmap = {}
19 dmap = {}
20 for k, v in cmap.iteritems():
20 for k, v in cmap.iteritems():
21 dmap[v] = k
21 dmap[v] = k
22 def decode(s):
22 def decode(s):
23 i = 0
23 i = 0
24 while i < len(s):
24 while i < len(s):
25 for l in xrange(1, 4):
25 for l in xrange(1, 4):
26 try:
26 try:
27 yield dmap[s[i:i+l]]
27 yield dmap[s[i:i+l]]
28 i += l
28 i += l
29 break
29 break
30 except KeyError:
30 except KeyError:
31 pass
31 pass
32 else:
32 else:
33 raise KeyError
33 raise KeyError
34 return (lambda s: "".join([cmap[c] for c in s]),
34 return (lambda s: "".join([cmap[c] for c in s]),
35 lambda s: "".join(list(decode(s))))
35 lambda s: "".join(list(decode(s))))
36
36
37 encodefilename, decodefilename = _buildencodefun()
37 encodefilename, decodefilename = _buildencodefun()
38
38
39 def _dirwalk(path, recurse):
40 '''yields (filename, size)'''
41 for e, kind, st in osutil.listdir(path, stat=True):
42 pe = os.path.join(path, e)
43 if kind == stat.S_IFDIR:
44 if recurse:
45 for x in _dirwalk(pe, True):
46 yield x
47 elif kind == stat.S_IFREG:
48 yield pe, st.st_size
49
50 def _calcmode(path):
39 def _calcmode(path):
51 try:
40 try:
52 # files in .hg/ will be created using this mode
41 # files in .hg/ will be created using this mode
53 mode = os.stat(path).st_mode
42 mode = os.stat(path).st_mode
54 # avoid some useless chmods
43 # avoid some useless chmods
55 if (0777 & ~util._umask) == (0777 & mode):
44 if (0777 & ~util._umask) == (0777 & mode):
56 mode = None
45 mode = None
57 except OSError:
46 except OSError:
58 mode = None
47 mode = None
59 return mode
48 return mode
60
49
61 class basicstore:
50 class basicstore:
62 '''base class for local repository stores'''
51 '''base class for local repository stores'''
63 def __init__(self, path, opener):
52 def __init__(self, path, opener):
64 self.path = path
53 self.path = path
65 self.createmode = _calcmode(path)
54 self.createmode = _calcmode(path)
66 self.opener = opener(self.path)
55 self.opener = opener(self.path)
67 self.opener.createmode = self.createmode
56 self.opener.createmode = self.createmode
68
57
69 def join(self, f):
58 def join(self, f):
70 return os.path.join(self.path, f)
59 return os.path.join(self.path, f)
71
60
72 def _revlogfiles(self, relpath='', recurse=False):
61 def _walk(self, relpath, recurse):
73 '''yields (filename, size)'''
62 '''yields (filename, size)'''
74 if relpath:
63 path = os.path.join(self.path, relpath)
75 path = os.path.join(self.path, relpath)
76 else:
77 path = self.path
78 if not os.path.isdir(path):
79 return
80 striplen = len(self.path) + len(os.sep)
64 striplen = len(self.path) + len(os.sep)
81 filetypes = ('.d', '.i')
65 prefix = path[striplen:]
82 for f, size in _dirwalk(path, recurse):
66 l = []
83 if (len(f) > 2) and f[-2:] in filetypes:
67 if os.path.isdir(path):
84 yield util.pconvert(f[striplen:]), size
68 visit = [path]
69 while visit:
70 p = visit.pop()
71 for f, kind, st in osutil.listdir(p, stat=True):
72 fp = os.path.join(p, f)
73 if kind == stat.S_IFREG and f[-2:] in ('.d', '.i'):
74 l.append((util.pconvert(fp[striplen:]), st.st_size))
75 elif kind == stat.S_IFDIR and recurse:
76 visit.append(fp)
77 return util.sort(l)
85
78
86 def datafiles(self, reporterror=None):
79 def datafiles(self, reporterror=None):
87 for x in self._revlogfiles('data', True):
80 return self._walk('data', True)
88 yield x
89
81
90 def walk(self):
82 def walk(self):
91 '''yields (direncoded filename, size)'''
83 '''yields (direncoded filename, size)'''
92 # yield data files first
84 # yield data files first
93 for x in self.datafiles():
85 for x in self.datafiles():
94 yield x
86 yield x
95 # yield manifest before changelog
87 # yield manifest before changelog
96 meta = util.sort(self._revlogfiles())
88 meta = self._walk('', False)
97 meta.reverse()
89 meta.reverse()
98 for x in meta:
90 for x in meta:
99 yield x
91 yield x
100
92
101 class encodedstore(basicstore):
93 class encodedstore(basicstore):
102 def __init__(self, path, opener):
94 def __init__(self, path, opener):
103 self.path = os.path.join(path, 'store')
95 self.path = os.path.join(path, 'store')
104 self.createmode = _calcmode(self.path)
96 self.createmode = _calcmode(self.path)
105 self.encodefn = encodefilename
97 self.encodefn = encodefilename
106 op = opener(self.path)
98 op = opener(self.path)
107 op.createmode = self.createmode
99 op.createmode = self.createmode
108 self.opener = lambda f, *args, **kw: op(self.encodefn(f), *args, **kw)
100 self.opener = lambda f, *args, **kw: op(self.encodefn(f), *args, **kw)
109
101
110 def datafiles(self, reporterror=None):
102 def datafiles(self, reporterror=None):
111 for f, size in self._revlogfiles('data', True):
103 for f, size in self._walk('data', True):
112 try:
104 try:
113 yield decodefilename(f), size
105 yield decodefilename(f), size
114 except KeyError:
106 except KeyError:
115 if not reporterror:
107 if not reporterror:
116 raise
108 raise
117 reporterror(_("cannot decode filename '%s'") % f)
109 reporterror(_("cannot decode filename '%s'") % f)
118
110
119 def join(self, f):
111 def join(self, f):
120 return os.path.join(self.path, self.encodefn(f))
112 return os.path.join(self.path, self.encodefn(f))
121
113
122 def store(requirements, path, opener):
114 def store(requirements, path, opener):
123 if 'store' in requirements:
115 if 'store' in requirements:
124 return encodedstore(path, opener)
116 return encodedstore(path, opener)
125 return basicstore(path, opener)
117 return basicstore(path, opener)
General Comments 0
You need to be logged in to leave comments. Login now