[PATCH 1 of 8] Add a new function, fspath

Patrick Mézard pmezard at gmail.com
Fri May 2 05:59:37 CDT 2008


Paul Moore a écrit :
> # HG changeset patch
> # User "Paul Moore <p.f.moore at gmail.com>"
> # Date 1209573914 -3600
> # Node ID 4c332d2df388086257be74380d53701631fd3a2e
> # Parent  7758fc67c39fd22b1025faa02a69b49acfdb557c
> Add a new function, fspath
> 
> The function, given a filename and an (optional) root, returns the filename
> modified to use the case actually stored in the filesystem (if the file does
> not exist, return a default value).
> 
> A generic implementation is provided in util.py, with a faster win32-specific
> implementation (using win32api.FindFiles) in util_win32.py.
> 
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -868,6 +868,45 @@
>          return True
>      except:
>          return True
> +
> +def fspath(name, root='', default=None):
> +    '''Get name in the case stored in the filesystem
> +
> +    The filename 'name' is relative to the path 'root' (if given).
> +    If the file does not exist, return default (or name unchanged if default
> +    is not given). Otherwise, return the version of name with case as stored
> +    in the filesystem.
> +    '''
> +    if not os.path.exists(os.path.join(root, name)):
> +        if default is None:
> +            return name
> +        return default
> +    parts = []
> +    while name:
> +        dir, leaf = os.path.split(name)
> +
> +        # Scan os.listdir for a name that matches leaf except for case
> +        leaf_l = leaf.lower()
> +        for n in os.listdir(os.path.join(root, dir)):

For the record:

"""
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.listdir('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: ''
"""

I added something like:

"""
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -887,7 +887,8 @@
 
         # Scan os.listdir for a name that matches leaf except for case
         leaf_l = leaf.lower()
-        for n in os.listdir(os.path.join(root, dir)):
+        # os.listdir('') fails on MacOSX
+        for n in os.listdir(os.path.join(root, dir) or '.'):
             if n.lower() == leaf_l:
                 parts.append(n)
                 break
"""

--
Patrick Mézard



More information about the Mercurial-devel mailing list