comparison mercurial/pathutil.py @ 25285:46f2df2f0680

pathutil: restate dirname and join as forwards to posixpath I've done this as its own step so that it's easy to see that the posixpath implementations pass the doctests in this package. In a future patch I'll just make these pure forwards of the methods so that things using pathutil can be oblivious to the posix nature of these functions.
author Augie Fackler <augie@google.com>
date Tue, 26 May 2015 14:30:48 -0400
parents 660b178f49c7
children 127a11f705d9
comparison
equal deleted inserted replaced
25284:7072b91ccd20 25285:46f2df2f0680
1 import os, errno, stat 1 import os, errno, stat, posixpath
2 2
3 import encoding 3 import encoding
4 import util 4 import util
5 from i18n import _ 5 from i18n import _
6 6
186 if len(p) != len(os.sep): 186 if len(p) != len(os.sep):
187 return path + os.sep 187 return path + os.sep
188 else: 188 else:
189 return path 189 return path
190 190
191 def join(path, *paths): 191 def join(*args):
192 '''Join two or more pathname components, inserting '/' as needed. 192 '''Join two or more pathname components, inserting '/' as needed.
193 193
194 Based on the posix os.path.join() implementation. 194 Based on the posix os.path.join() implementation.
195 195
196 >>> join('foo', 'bar') 196 >>> join('foo', 'bar')
212 >>> join('', '', '') 212 >>> join('', '', '')
213 '' 213 ''
214 >>> join ('foo', '', '', 'bar') 214 >>> join ('foo', '', '', 'bar')
215 'foo/bar' 215 'foo/bar'
216 ''' 216 '''
217 sep = '/' 217 return posixpath.join(*args)
218 if not paths:
219 path[:0] + sep #23780: Ensure compatible data type even if p is null.
220 for piece in paths:
221 if piece.startswith(sep):
222 path = piece
223 elif not path or path.endswith(sep):
224 path += piece
225 else:
226 path += sep + piece
227 return path
228 218
229 def dirname(path): 219 def dirname(path):
230 '''returns the directory portion of the given path 220 '''returns the directory portion of the given path
231 221
232 Based on the posix os.path.split() implementation. 222 Based on the posix os.path.split() implementation.
244 >>> dirname('/foo//bar/poo') 234 >>> dirname('/foo//bar/poo')
245 '/foo//bar' 235 '/foo//bar'
246 >>> dirname('/foo//bar') 236 >>> dirname('/foo//bar')
247 '/foo' 237 '/foo'
248 ''' 238 '''
249 sep = '/' 239 return posixpath.dirname(path)
250 i = path.rfind(sep) + 1
251 dirname = path[:i]
252 if dirname and dirname != sep * len(dirname):
253 dirname = dirname.rstrip(sep)
254 return dirname