Changeset 348 for python


Ignore:
Timestamp:
Jun 5, 2013, 3:53:22 PM (12 years ago)
Author:
Yuri Dario
Message:

python: add samefile, sameopenfile, samestat to os.path module.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/trunk/Lib/os2knixpath.py

    r10 r348  
    1616           "getatime","getctime", "islink","exists","lexists","isdir","isfile",
    1717           "ismount","walk","expanduser","expandvars","normpath","abspath",
     18           "samefile","sameopenfile","samestat",
    1819           "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
    1920           "extsep","devnull","realpath","supports_unicode_filenames"]
     
    9899
    99100
    100 # alias exists to lexists
    101 lexists = exists
    102 
    103 
    104101# Is a path a directory?
    105102
     
    150147    return normpath(path)
    151148
    152 # realpath is a no-op on systems without islink support
    153 realpath = abspath
     149
     150# Return a canonical path (i.e. the absolute location of a file on the
     151# filesystem).
     152
     153def realpath(filename):
     154    """Return the canonical path of the specified filename, eliminating any
     155symbolic links encountered in the path."""
     156    if isabs(filename):
     157        bits = ['/'] + filename.split('/')[1:]
     158    else:
     159        bits = [''] + filename.split('/')
     160
     161    for i in range(2, len(bits)+1):
     162        component = join(*bits[0:i])
     163        # Resolve symbolic links.
     164        if islink(component):
     165            resolved = _resolve_link(component)
     166            if resolved is None:
     167                # Infinite loop -- return original component + rest of the path
     168                return abspath(join(*([component] + bits[i:])))
     169            else:
     170                newpath = join(*([resolved] + bits[i:]))
     171                return realpath(newpath)
     172
     173    return abspath(filename)
     174
     175
     176def _resolve_link(path):
     177    """Internal helper function.  Takes a path and follows symlinks
     178    until we either arrive at something that isn't a symlink, or
     179    encounter a path we've seen before (meaning that there's a loop).
     180    """
     181    paths_seen = []
     182    while islink(path):
     183        if path in paths_seen:
     184            # Already seen this path, so we must have a symlink loop
     185            return None
     186        paths_seen.append(path)
     187        # Resolve where the link points to
     188        resolved = os.readlink(path)
     189        if not isabs(resolved):
     190            dir = dirname(path)
     191            path = normpath(join(dir, resolved))
     192        else:
     193            path = normpath(resolved)
     194    return path
     195
     196
     197# Is a path a symbolic link?
     198# This will always return false on systems where os.lstat doesn't exist.
     199
     200def islink(path):
     201    """Test whether a path is a symbolic link"""
     202    try:
     203        st = os.lstat(path)
     204    except (os.error, AttributeError):
     205        return False
     206    return stat.S_ISLNK(st.st_mode)
     207
     208# Being true for dangling symbolic links is also useful.
     209
     210def lexists(path):
     211    """Test whether a path exists.  Returns True for broken symbolic links"""
     212    try:
     213        st = os.lstat(path)
     214    except os.error:
     215        return False
     216    return True
     217
     218
     219# Are two filenames really pointing to the same file?
     220
     221def samefile(f1, f2):
     222    """Test whether two pathnames reference the same actual file"""
     223    s1 = os.stat(f1)
     224    s2 = os.stat(f2)
     225    return samestat(s1, s2)
     226
     227
     228# Are two open files really referencing the same file?
     229# (Not necessarily the same file descriptor!)
     230
     231def sameopenfile(fp1, fp2):
     232    """Test whether two open file objects reference the same file"""
     233    s1 = os.fstat(fp1)
     234    s2 = os.fstat(fp2)
     235    return samestat(s1, s2)
     236
     237
     238# Are two stat buffers (obtained from stat, fstat or lstat)
     239# describing the same file?
     240
     241def samestat(s1, s2):
     242    """Test whether two stat buffers reference the same file"""
     243    return s1.st_ino == s2.st_ino and \
     244           s1.st_dev == s2.st_dev
     245
    154246
    155247supports_unicode_filenames = False
Note: See TracChangeset for help on using the changeset viewer.