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