comparison mercurial/store.py @ 13949:ba43aa1e173c

store: add some doctests
author Adrian Buehlmann <adrian@cadifra.com>
date Sun, 17 Apr 2011 17:55:34 +0200
parents 643b8212813e
children e24b5e3c2f27
comparison
equal deleted inserted replaced
13948:3fc5420016cc 13949:ba43aa1e173c
12 _sha = util.sha1 12 _sha = util.sha1
13 13
14 # This avoids a collision between a file named foo and a dir named 14 # This avoids a collision between a file named foo and a dir named
15 # foo.i or foo.d 15 # foo.i or foo.d
16 def encodedir(path): 16 def encodedir(path):
17 '''
18 >>> encodedir('data/foo.i')
19 'data/foo.i'
20 >>> encodedir('data/foo.i/bla.i')
21 'data/foo.i.hg/bla.i'
22 >>> encodedir('data/foo.i.hg/bla.i')
23 'data/foo.i.hg.hg/bla.i'
24 '''
17 if not path.startswith('data/'): 25 if not path.startswith('data/'):
18 return path 26 return path
19 return (path 27 return (path
20 .replace(".hg/", ".hg.hg/") 28 .replace(".hg/", ".hg.hg/")
21 .replace(".i/", ".i.hg/") 29 .replace(".i/", ".i.hg/")
22 .replace(".d/", ".d.hg/")) 30 .replace(".d/", ".d.hg/"))
23 31
24 def decodedir(path): 32 def decodedir(path):
33 '''
34 >>> decodedir('data/foo.i')
35 'data/foo.i'
36 >>> decodedir('data/foo.i.hg/bla.i')
37 'data/foo.i/bla.i'
38 >>> decodedir('data/foo.i.hg.hg/bla.i')
39 'data/foo.i.hg/bla.i'
40 '''
25 if not path.startswith('data/') or ".hg/" not in path: 41 if not path.startswith('data/') or ".hg/" not in path:
26 return path 42 return path
27 return (path 43 return (path
28 .replace(".d.hg/", ".d/") 44 .replace(".d.hg/", ".d/")
29 .replace(".i.hg/", ".i/") 45 .replace(".i.hg/", ".i/")
30 .replace(".hg.hg/", ".hg/")) 46 .replace(".hg.hg/", ".hg/"))
31 47
32 def _buildencodefun(): 48 def _buildencodefun():
49 '''
50 >>> enc, dec = _buildencodefun()
51
52 >>> enc('nothing/special.txt')
53 'nothing/special.txt'
54 >>> dec('nothing/special.txt')
55 'nothing/special.txt'
56
57 >>> enc('HELLO')
58 '_h_e_l_l_o'
59 >>> dec('_h_e_l_l_o')
60 'HELLO'
61
62 >>> enc('hello:world?')
63 'hello~3aworld~3f'
64 >>> dec('hello~3aworld~3f')
65 'hello:world?'
66
67 >>> enc('the\x07quick\xADshot')
68 'the~07quick~adshot'
69 >>> dec('the~07quick~adshot')
70 'the\\x07quick\\xadshot'
71 '''
33 e = '_' 72 e = '_'
34 win_reserved = [ord(x) for x in '\\:*?"<>|'] 73 win_reserved = [ord(x) for x in '\\:*?"<>|']
35 cmap = dict([(chr(x), chr(x)) for x in xrange(127)]) 74 cmap = dict([(chr(x), chr(x)) for x in xrange(127)])
36 for x in (range(32) + range(126, 256) + win_reserved): 75 for x in (range(32) + range(126, 256) + win_reserved):
37 cmap[chr(x)] = "~%02x" % x 76 cmap[chr(x)] = "~%02x" % x
56 lambda s: decodedir("".join(list(decode(s))))) 95 lambda s: decodedir("".join(list(decode(s)))))
57 96
58 encodefilename, decodefilename = _buildencodefun() 97 encodefilename, decodefilename = _buildencodefun()
59 98
60 def _build_lower_encodefun(): 99 def _build_lower_encodefun():
100 '''
101 >>> f = _build_lower_encodefun()
102 >>> f('nothing/special.txt')
103 'nothing/special.txt'
104 >>> f('HELLO')
105 'hello'
106 >>> f('hello:world?')
107 'hello~3aworld~3f'
108 >>> f('the\x07quick\xADshot')
109 'the~07quick~adshot'
110 '''
61 win_reserved = [ord(x) for x in '\\:*?"<>|'] 111 win_reserved = [ord(x) for x in '\\:*?"<>|']
62 cmap = dict([(chr(x), chr(x)) for x in xrange(127)]) 112 cmap = dict([(chr(x), chr(x)) for x in xrange(127)])
63 for x in (range(32) + range(126, 256) + win_reserved): 113 for x in (range(32) + range(126, 256) + win_reserved):
64 cmap[chr(x)] = "~%02x" % x 114 cmap[chr(x)] = "~%02x" % x
65 for x in range(ord("A"), ord("Z")+1): 115 for x in range(ord("A"), ord("Z")+1):
70 120
71 _windows_reserved_filenames = '''con prn aux nul 121 _windows_reserved_filenames = '''con prn aux nul
72 com1 com2 com3 com4 com5 com6 com7 com8 com9 122 com1 com2 com3 com4 com5 com6 com7 com8 com9
73 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split() 123 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split()
74 def _auxencode(path, dotencode): 124 def _auxencode(path, dotencode):
125 '''
126 Encodes filenames containing names reserved by Windows or which end in
127 period or space. Does not touch other single reserved characters c.
128 Specifically, c in '\\:*?"<>|' or ord(c) <= 31 are *not* encoded here.
129 Additionally encodes space or period at the beginning, if dotencode is
130 True.
131 path is assumed to be all lowercase.
132
133 >>> _auxencode('.foo/aux.txt/txt.aux/con/prn/nul/foo.', True)
134 '~2efoo/au~78.txt/txt.aux/co~6e/pr~6e/nu~6c/foo~2e'
135 >>> _auxencode('.com1com2/lpt9.lpt4.lpt1/conprn/foo.', False)
136 '.com1com2/lp~749.lpt4.lpt1/conprn/foo~2e'
137 >>> _auxencode('foo. ', True)
138 'foo.~20'
139 >>> _auxencode(' .foo', True)
140 '~20.foo'
141 '''
75 res = [] 142 res = []
76 for n in path.split('/'): 143 for n in path.split('/'):
77 if n: 144 if n:
78 base = n.split('.')[0] 145 base = n.split('.')[0]
79 if base and (base in _windows_reserved_filenames): 146 if base and (base in _windows_reserved_filenames):