##// END OF EJS Templates
Update util.py docstrings, fix walk test
mpm@selenic.com -
r1082:ce96e316 default
parent child Browse files
Show More
@@ -1,20 +1,27 b''
1 # util.py - utility functions and platform specfic implementations
1 """
2 #
2 util.py - Mercurial utility functions and platform specfic implementations
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
3
4 #
4 Copyright 2005 K. Thananchayan <thananck@yahoo.com>
5 # This software may be used and distributed according to the terms
5
6 # of the GNU General Public License, incorporated herein by reference.
6 This software may be used and distributed according to the terms
7 of the GNU General Public License, incorporated herein by reference.
8
9 This contains helper routines that are independent of the SCM core and hide
10 platform-specific details from the core.
11 """
7
12
8 import os, errno
13 import os, errno
9 from demandload import *
14 from demandload import *
10 demandload(globals(), "re")
15 demandload(globals(), "re")
11
16
12 def binary(s):
17 def binary(s):
18 """return true if a string is binary data using diff's heuristic"""
13 if s and '\0' in s[:4096]:
19 if s and '\0' in s[:4096]:
14 return True
20 return True
15 return False
21 return False
16
22
17 def unique(g):
23 def unique(g):
24 """return the uniq elements of iterable g"""
18 seen = {}
25 seen = {}
19 for f in g:
26 for f in g:
20 if f not in seen:
27 if f not in seen:
@@ -86,6 +93,7 b' def pathto(n1, n2):'
86 return os.sep.join((['..'] * len(a)) + b)
93 return os.sep.join((['..'] * len(a)) + b)
87
94
88 def canonpath(root, cwd, myname):
95 def canonpath(root, cwd, myname):
96 """return the canonical path of myname, given cwd and root"""
89 rootsep = root + os.sep
97 rootsep = root + os.sep
90 name = myname
98 name = myname
91 if not name.startswith(os.sep):
99 if not name.startswith(os.sep):
@@ -99,6 +107,33 b' def canonpath(root, cwd, myname):'
99 raise Abort('%s not under root' % myname)
107 raise Abort('%s not under root' % myname)
100
108
101 def matcher(canonroot, cwd, names, inc, exc, head=''):
109 def matcher(canonroot, cwd, names, inc, exc, head=''):
110 """build a function to match a set of file patterns
111
112 arguments:
113 canonroot - the canonical root of the tree you're matching against
114 cwd - the current working directory, if relevant
115 names - patterns to find
116 inc - patterns to include
117 exc - patterns to exclude
118 head - a regex to prepend to patterns to control whether a match is rooted
119
120 a pattern is one of:
121 're:<regex>'
122 'glob:<shellglob>'
123 'path:<explicit path>'
124 'relpath:<relative path>'
125 '<relative path>'
126
127 returns:
128 a 3-tuple containing
129 - list of explicit non-pattern names passed in
130 - a bool match(filename) function
131 - a bool indicating if any patterns were passed in
132
133 todo:
134 make head regex a rooted bool
135 """
136
102 def patkind(name):
137 def patkind(name):
103 for prefix in 're:', 'glob:', 'path:', 'relpath:':
138 for prefix in 're:', 'glob:', 'path:', 'relpath:':
104 if name.startswith(prefix): return name.split(':', 1)
139 if name.startswith(prefix): return name.split(':', 1)
@@ -175,6 +210,7 b' def system(cmd, errprefix=None):'
175 raise Abort(errmsg)
210 raise Abort(errmsg)
176
211
177 def rename(src, dst):
212 def rename(src, dst):
213 """forcibly rename a file"""
178 try:
214 try:
179 os.rename(src, dst)
215 os.rename(src, dst)
180 except:
216 except:
@@ -204,7 +240,7 b' def _makelock_file(info, pathname):'
204 def _readlock_file(pathname):
240 def _readlock_file(pathname):
205 return file(pathname).read()
241 return file(pathname).read()
206
242
207 # Platfor specific varients
243 # Platform specific variants
208 if os.name == 'nt':
244 if os.name == 'nt':
209 nulldev = 'NUL:'
245 nulldev = 'NUL:'
210
246
@@ -233,6 +269,7 b' else:'
233 nulldev = '/dev/null'
269 nulldev = '/dev/null'
234
270
235 def is_exec(f, last):
271 def is_exec(f, last):
272 """check whether a file is executable"""
236 return (os.stat(f).st_mode & 0100 != 0)
273 return (os.stat(f).st_mode & 0100 != 0)
237
274
238 def set_exec(f, mode):
275 def set_exec(f, mode):
@@ -63,8 +63,8 b' f mammals/skunk mamma'
63 f mammals/Procyonidae/cacomistle mammals/Procyonidae/cacomistle
63 f mammals/Procyonidae/cacomistle mammals/Procyonidae/cacomistle
64 f mammals/Procyonidae/coatimundi mammals/Procyonidae/coatimundi
64 f mammals/Procyonidae/coatimundi mammals/Procyonidae/coatimundi
65 f mammals/Procyonidae/raccoon mammals/Procyonidae/raccoon
65 f mammals/Procyonidae/raccoon mammals/Procyonidae/raccoon
66 abort: .. not under repository root
66 abort: .. not under root
67 abort: beans/../.. not under repository root
67 abort: beans/../.. not under root
68 f fennel fennel
68 f fennel fennel
69 f fenugreek fenugreek
69 f fenugreek fenugreek
70 f fiddlehead fiddlehead
70 f fiddlehead fiddlehead
General Comments 0
You need to be logged in to leave comments. Login now