Mercurial > hg
comparison mercurial/scmutil.py @ 24725:ee751d47cf2c
vfs: add walk
To eliminate "path prefix" (= "the root of vfs") part from "dirpath"
yielded by "os.walk()" correctly, "path prefix" should have "os.sep"
at the end of own string, but it isn't easy to ensure it, because:
- examination by "path.endswith(os.sep)" isn't portable
Some problematic encodings use 0x5c (= "os.sep" on Windows) as the
tail byte of some multi-byte characters.
- "os.path.join(path, '')" isn't portable
With Python 2.7.9, this invocation doesn't add "os.sep" at the end
of UNC path (see issue4557 for detail).
Python 2.7.9 changed also behavior of "os.path.normpath()" (see *) and
"os.path.splitdrive()" for UNC path.
vfs root normpath splitdrive os.sep required
=============== ============== =================== ============
z:\ z:\ z: + \ no
z:\foo z:\foo z: + \foo yes
z:\foo\ z:\foo z: + \foo yes
[before Python 2.7.9]
\\foo\bar \\foo\bar '' + \\foo\bar yes
\\foo\bar\ \\foo\bar (*) '' + \\foo\bar yes
\\foo\bar\baz \\foo\bar\baz '' + \\foo\bar\baz yes
\\foo\bar\baz\ \\foo\bar\baz '' + \\foo\bar\baz yes
[Python 2.7.9]
\\foo\bar \\foo\bar \\foo\bar + '' yes
\\foo\bar\ \\foo\bar\ (*) \\foo\bar + \ no
\\foo\bar\baz \\foo\bar\baz \\foo\bar + \baz yes
\\foo\bar\baz\ \\foo\bar\baz \\foo\bar + \baz yes
If it is ensured that "normpath()"-ed vfs root is passed to
"splitdrive()", adding "os.sep" is required only when "path" part of
"splitdrive()" result isn't "os.sep" itself. This is just what
"pathutil.nameasprefix()" examines.
This patch applies "os.path.normpath()" on "self.join(None)"
explicitly, because it isn't ensured that vfs root is already
normalized: vfs itself is constructed with "realpath=False" (= avoid
normalizing in "vfs.__init__()") in many code paths.
This normalization should be much cheaper than subsequent file I/O for
directory traversal.
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Sat, 11 Apr 2015 23:00:04 +0900 |
parents | 467a33142425 |
children | bef8b17443a3 |
comparison
equal
deleted
inserted
replaced
24724:95eb067b2b5e | 24725:ee751d47cf2c |
---|---|
377 def unlinkpath(self, path=None, ignoremissing=False): | 377 def unlinkpath(self, path=None, ignoremissing=False): |
378 return util.unlinkpath(self.join(path), ignoremissing) | 378 return util.unlinkpath(self.join(path), ignoremissing) |
379 | 379 |
380 def utime(self, path=None, t=None): | 380 def utime(self, path=None, t=None): |
381 return os.utime(self.join(path), t) | 381 return os.utime(self.join(path), t) |
382 | |
383 def walk(self, path=None, onerror=None): | |
384 """Yield (dirpath, dirs, files) tuple for each directories under path | |
385 | |
386 ``dirpath`` is relative one from the root of this vfs. This | |
387 uses ``os.sep`` as path separator, even you specify POSIX | |
388 style ``path``. | |
389 | |
390 "The root of this vfs" is represented as empty ``dirpath``. | |
391 """ | |
392 root = os.path.normpath(self.join(None)) | |
393 # when dirpath == root, dirpath[prefixlen:] becomes empty | |
394 # because len(dirpath) < prefixlen. | |
395 prefixlen = len(pathutil.normasprefix(root)) | |
396 for dirpath, dirs, files in os.walk(self.join(path), onerror=onerror): | |
397 yield (dirpath[prefixlen:], dirs, files) | |
382 | 398 |
383 class vfs(abstractvfs): | 399 class vfs(abstractvfs): |
384 '''Operate files relative to a base directory | 400 '''Operate files relative to a base directory |
385 | 401 |
386 This class is used to hide the details of COW semantics and | 402 This class is used to hide the details of COW semantics and |