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