Blame Lib/ntpath.py

rpm-build 2bd099
# Module 'ntpath' -- common operations on WinNT/Win95 pathnames
rpm-build 2bd099
"""Common pathname manipulations, WindowsNT/95 version.
rpm-build 2bd099
rpm-build 2bd099
Instead of importing this module directly, import os and refer to this
rpm-build 2bd099
module as os.path.
rpm-build 2bd099
"""
rpm-build 2bd099
rpm-build 2bd099
import os
rpm-build 2bd099
import sys
rpm-build 2bd099
import stat
rpm-build 2bd099
import genericpath
rpm-build 2bd099
from genericpath import *
rpm-build 2bd099
rpm-build 2bd099
__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
rpm-build 2bd099
           "basename","dirname","commonprefix","getsize","getmtime",
rpm-build 2bd099
           "getatime","getctime", "islink","exists","lexists","isdir","isfile",
rpm-build 2bd099
           "ismount", "expanduser","expandvars","normpath","abspath",
rpm-build 2bd099
           "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
rpm-build 2bd099
           "extsep","devnull","realpath","supports_unicode_filenames","relpath",
rpm-build 2bd099
           "samefile", "sameopenfile", "samestat", "commonpath"]
rpm-build 2bd099
rpm-build 2bd099
# strings representing various path-related bits and pieces
rpm-build 2bd099
# These are primarily for export; internally, they are hardcoded.
rpm-build 2bd099
curdir = '.'
rpm-build 2bd099
pardir = '..'
rpm-build 2bd099
extsep = '.'
rpm-build 2bd099
sep = '\\'
rpm-build 2bd099
pathsep = ';'
rpm-build 2bd099
altsep = '/'
rpm-build 2bd099
defpath = '.;C:\\bin'
rpm-build 2bd099
devnull = 'nul'
rpm-build 2bd099
rpm-build 2bd099
def _get_bothseps(path):
rpm-build 2bd099
    if isinstance(path, bytes):
rpm-build 2bd099
        return b'\\/'
rpm-build 2bd099
    else:
rpm-build 2bd099
        return '\\/'
rpm-build 2bd099
rpm-build 2bd099
# Normalize the case of a pathname and map slashes to backslashes.
rpm-build 2bd099
# Other normalizations (such as optimizing '../' away) are not done
rpm-build 2bd099
# (this is done by normpath).
rpm-build 2bd099
rpm-build 2bd099
def normcase(s):
rpm-build 2bd099
    """Normalize case of pathname.
rpm-build 2bd099
rpm-build 2bd099
    Makes all characters lowercase and all slashes into backslashes."""
rpm-build 2bd099
    s = os.fspath(s)
rpm-build 2bd099
    try:
rpm-build 2bd099
        if isinstance(s, bytes):
rpm-build 2bd099
            return s.replace(b'/', b'\\').lower()
rpm-build 2bd099
        else:
rpm-build 2bd099
            return s.replace('/', '\\').lower()
rpm-build 2bd099
    except (TypeError, AttributeError):
rpm-build 2bd099
        if not isinstance(s, (bytes, str)):
rpm-build 2bd099
            raise TypeError("normcase() argument must be str or bytes, "
rpm-build 2bd099
                            "not %r" % s.__class__.__name__) from None
rpm-build 2bd099
        raise
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Return whether a path is absolute.
rpm-build 2bd099
# Trivial in Posix, harder on Windows.
rpm-build 2bd099
# For Windows it is absolute if it starts with a slash or backslash (current
rpm-build 2bd099
# volume), or if a pathname after the volume-letter-and-colon or UNC-resource
rpm-build 2bd099
# starts with a slash or backslash.
rpm-build 2bd099
rpm-build 2bd099
def isabs(s):
rpm-build 2bd099
    """Test whether a path is absolute"""
rpm-build 2bd099
    s = os.fspath(s)
rpm-build 2bd099
    s = splitdrive(s)[1]
rpm-build 2bd099
    return len(s) > 0 and s[0] in _get_bothseps(s)
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Join two (or more) paths.
rpm-build 2bd099
def join(path, *paths):
rpm-build 2bd099
    path = os.fspath(path)
rpm-build 2bd099
    if isinstance(path, bytes):
rpm-build 2bd099
        sep = b'\\'
rpm-build 2bd099
        seps = b'\\/'
rpm-build 2bd099
        colon = b':'
rpm-build 2bd099
    else:
rpm-build 2bd099
        sep = '\\'
rpm-build 2bd099
        seps = '\\/'
rpm-build 2bd099
        colon = ':'
rpm-build 2bd099
    try:
rpm-build 2bd099
        if not paths:
rpm-build 2bd099
            path[:0] + sep  #23780: Ensure compatible data type even if p is null.
rpm-build 2bd099
        result_drive, result_path = splitdrive(path)
rpm-build 2bd099
        for p in map(os.fspath, paths):
rpm-build 2bd099
            p_drive, p_path = splitdrive(p)
rpm-build 2bd099
            if p_path and p_path[0] in seps:
rpm-build 2bd099
                # Second path is absolute
rpm-build 2bd099
                if p_drive or not result_drive:
rpm-build 2bd099
                    result_drive = p_drive
rpm-build 2bd099
                result_path = p_path
rpm-build 2bd099
                continue
rpm-build 2bd099
            elif p_drive and p_drive != result_drive:
rpm-build 2bd099
                if p_drive.lower() != result_drive.lower():
rpm-build 2bd099
                    # Different drives => ignore the first path entirely
rpm-build 2bd099
                    result_drive = p_drive
rpm-build 2bd099
                    result_path = p_path
rpm-build 2bd099
                    continue
rpm-build 2bd099
                # Same drive in different case
rpm-build 2bd099
                result_drive = p_drive
rpm-build 2bd099
            # Second path is relative to the first
rpm-build 2bd099
            if result_path and result_path[-1] not in seps:
rpm-build 2bd099
                result_path = result_path + sep
rpm-build 2bd099
            result_path = result_path + p_path
rpm-build 2bd099
        ## add separator between UNC and non-absolute path
rpm-build 2bd099
        if (result_path and result_path[0] not in seps and
rpm-build 2bd099
            result_drive and result_drive[-1:] != colon):
rpm-build 2bd099
            return result_drive + sep + result_path
rpm-build 2bd099
        return result_drive + result_path
rpm-build 2bd099
    except (TypeError, AttributeError, BytesWarning):
rpm-build 2bd099
        genericpath._check_arg_types('join', path, *paths)
rpm-build 2bd099
        raise
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Split a path in a drive specification (a drive letter followed by a
rpm-build 2bd099
# colon) and the path specification.
rpm-build 2bd099
# It is always true that drivespec + pathspec == p
rpm-build 2bd099
def splitdrive(p):
rpm-build 2bd099
    """Split a pathname into drive/UNC sharepoint and relative path specifiers.
rpm-build 2bd099
    Returns a 2-tuple (drive_or_unc, path); either part may be empty.
rpm-build 2bd099
rpm-build 2bd099
    If you assign
rpm-build 2bd099
        result = splitdrive(p)
rpm-build 2bd099
    It is always true that:
rpm-build 2bd099
        result[0] + result[1] == p
rpm-build 2bd099
rpm-build 2bd099
    If the path contained a drive letter, drive_or_unc will contain everything
rpm-build 2bd099
    up to and including the colon.  e.g. splitdrive("c:/dir") returns ("c:", "/dir")
rpm-build 2bd099
rpm-build 2bd099
    If the path contained a UNC path, the drive_or_unc will contain the host name
rpm-build 2bd099
    and share up to but not including the fourth directory separator character.
rpm-build 2bd099
    e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir")
rpm-build 2bd099
rpm-build 2bd099
    Paths cannot contain both a drive letter and a UNC path.
rpm-build 2bd099
rpm-build 2bd099
    """
rpm-build 2bd099
    p = os.fspath(p)
rpm-build 2bd099
    if len(p) >= 2:
rpm-build 2bd099
        if isinstance(p, bytes):
rpm-build 2bd099
            sep = b'\\'
rpm-build 2bd099
            altsep = b'/'
rpm-build 2bd099
            colon = b':'
rpm-build 2bd099
        else:
rpm-build 2bd099
            sep = '\\'
rpm-build 2bd099
            altsep = '/'
rpm-build 2bd099
            colon = ':'
rpm-build 2bd099
        normp = p.replace(altsep, sep)
rpm-build 2bd099
        if (normp[0:2] == sep*2) and (normp[2:3] != sep):
rpm-build 2bd099
            # is a UNC path:
rpm-build 2bd099
            # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
rpm-build 2bd099
            # \\machine\mountpoint\directory\etc\...
rpm-build 2bd099
            #           directory ^^^^^^^^^^^^^^^
rpm-build 2bd099
            index = normp.find(sep, 2)
rpm-build 2bd099
            if index == -1:
rpm-build 2bd099
                return p[:0], p
rpm-build 2bd099
            index2 = normp.find(sep, index + 1)
rpm-build 2bd099
            # a UNC path can't have two slashes in a row
rpm-build 2bd099
            # (after the initial two)
rpm-build 2bd099
            if index2 == index + 1:
rpm-build 2bd099
                return p[:0], p
rpm-build 2bd099
            if index2 == -1:
rpm-build 2bd099
                index2 = len(p)
rpm-build 2bd099
            return p[:index2], p[index2:]
rpm-build 2bd099
        if normp[1:2] == colon:
rpm-build 2bd099
            return p[:2], p[2:]
rpm-build 2bd099
    return p[:0], p
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Parse UNC paths
rpm-build 2bd099
def splitunc(p):
rpm-build 2bd099
    """Deprecated since Python 3.1.  Please use splitdrive() instead;
rpm-build 2bd099
    it now handles UNC paths.
rpm-build 2bd099
rpm-build 2bd099
    Split a pathname into UNC mount point and relative path specifiers.
rpm-build 2bd099
rpm-build 2bd099
    Return a 2-tuple (unc, rest); either part may be empty.
rpm-build 2bd099
    If unc is not empty, it has the form '//host/mount' (or similar
rpm-build 2bd099
    using backslashes).  unc+rest is always the input path.
rpm-build 2bd099
    Paths containing drive letters never have a UNC part.
rpm-build 2bd099
    """
rpm-build 2bd099
    import warnings
rpm-build 2bd099
    warnings.warn("ntpath.splitunc is deprecated, use ntpath.splitdrive instead",
rpm-build 2bd099
                  DeprecationWarning, 2)
rpm-build 2bd099
    drive, path = splitdrive(p)
rpm-build 2bd099
    if len(drive) == 2:
rpm-build 2bd099
         # Drive letter present
rpm-build 2bd099
        return p[:0], p
rpm-build 2bd099
    return drive, path
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Split a path in head (everything up to the last '/') and tail (the
rpm-build 2bd099
# rest).  After the trailing '/' is stripped, the invariant
rpm-build 2bd099
# join(head, tail) == p holds.
rpm-build 2bd099
# The resulting head won't end in '/' unless it is the root.
rpm-build 2bd099
rpm-build 2bd099
def split(p):
rpm-build 2bd099
    """Split a pathname.
rpm-build 2bd099
rpm-build 2bd099
    Return tuple (head, tail) where tail is everything after the final slash.
rpm-build 2bd099
    Either part may be empty."""
rpm-build 2bd099
    p = os.fspath(p)
rpm-build 2bd099
    seps = _get_bothseps(p)
rpm-build 2bd099
    d, p = splitdrive(p)
rpm-build 2bd099
    # set i to index beyond p's last slash
rpm-build 2bd099
    i = len(p)
rpm-build 2bd099
    while i and p[i-1] not in seps:
rpm-build 2bd099
        i -= 1
rpm-build 2bd099
    head, tail = p[:i], p[i:]  # now tail has no slashes
rpm-build 2bd099
    # remove trailing slashes from head, unless it's all slashes
rpm-build 2bd099
    head = head.rstrip(seps) or head
rpm-build 2bd099
    return d + head, tail
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Split a path in root and extension.
rpm-build 2bd099
# The extension is everything starting at the last dot in the last
rpm-build 2bd099
# pathname component; the root is everything before that.
rpm-build 2bd099
# It is always true that root + ext == p.
rpm-build 2bd099
rpm-build 2bd099
def splitext(p):
rpm-build 2bd099
    p = os.fspath(p)
rpm-build 2bd099
    if isinstance(p, bytes):
rpm-build 2bd099
        return genericpath._splitext(p, b'\\', b'/', b'.')
rpm-build 2bd099
    else:
rpm-build 2bd099
        return genericpath._splitext(p, '\\', '/', '.')
rpm-build 2bd099
splitext.__doc__ = genericpath._splitext.__doc__
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Return the tail (basename) part of a path.
rpm-build 2bd099
rpm-build 2bd099
def basename(p):
rpm-build 2bd099
    """Returns the final component of a pathname"""
rpm-build 2bd099
    return split(p)[1]
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Return the head (dirname) part of a path.
rpm-build 2bd099
rpm-build 2bd099
def dirname(p):
rpm-build 2bd099
    """Returns the directory component of a pathname"""
rpm-build 2bd099
    return split(p)[0]
rpm-build 2bd099
rpm-build 2bd099
# Is a path a symbolic link?
rpm-build 2bd099
# This will always return false on systems where os.lstat doesn't exist.
rpm-build 2bd099
rpm-build 2bd099
def islink(path):
rpm-build 2bd099
    """Test whether a path is a symbolic link.
rpm-build 2bd099
    This will always return false for Windows prior to 6.0.
rpm-build 2bd099
    """
rpm-build 2bd099
    try:
rpm-build 2bd099
        st = os.lstat(path)
rpm-build 2bd099
    except (OSError, AttributeError):
rpm-build 2bd099
        return False
rpm-build 2bd099
    return stat.S_ISLNK(st.st_mode)
rpm-build 2bd099
rpm-build 2bd099
# Being true for dangling symbolic links is also useful.
rpm-build 2bd099
rpm-build 2bd099
def lexists(path):
rpm-build 2bd099
    """Test whether a path exists.  Returns True for broken symbolic links"""
rpm-build 2bd099
    try:
rpm-build 2bd099
        st = os.lstat(path)
rpm-build 2bd099
    except OSError:
rpm-build 2bd099
        return False
rpm-build 2bd099
    return True
rpm-build 2bd099
rpm-build 2bd099
# Is a path a mount point?
rpm-build 2bd099
# Any drive letter root (eg c:\)
rpm-build 2bd099
# Any share UNC (eg \\server\share)
rpm-build 2bd099
# Any volume mounted on a filesystem folder
rpm-build 2bd099
#
rpm-build 2bd099
# No one method detects all three situations. Historically we've lexically
rpm-build 2bd099
# detected drive letter roots and share UNCs. The canonical approach to
rpm-build 2bd099
# detecting mounted volumes (querying the reparse tag) fails for the most
rpm-build 2bd099
# common case: drive letter roots. The alternative which uses GetVolumePathName
rpm-build 2bd099
# fails if the drive letter is the result of a SUBST.
rpm-build 2bd099
try:
rpm-build 2bd099
    from nt import _getvolumepathname
rpm-build 2bd099
except ImportError:
rpm-build 2bd099
    _getvolumepathname = None
rpm-build 2bd099
def ismount(path):
rpm-build 2bd099
    """Test whether a path is a mount point (a drive root, the root of a
rpm-build 2bd099
    share, or a mounted volume)"""
rpm-build 2bd099
    path = os.fspath(path)
rpm-build 2bd099
    seps = _get_bothseps(path)
rpm-build 2bd099
    path = abspath(path)
rpm-build 2bd099
    root, rest = splitdrive(path)
rpm-build 2bd099
    if root and root[0] in seps:
rpm-build 2bd099
        return (not rest) or (rest in seps)
rpm-build 2bd099
    if rest in seps:
rpm-build 2bd099
        return True
rpm-build 2bd099
rpm-build 2bd099
    if _getvolumepathname:
rpm-build 2bd099
        return path.rstrip(seps) == _getvolumepathname(path).rstrip(seps)
rpm-build 2bd099
    else:
rpm-build 2bd099
        return False
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Expand paths beginning with '~' or '~user'.
rpm-build 2bd099
# '~' means $HOME; '~user' means that user's home directory.
rpm-build 2bd099
# If the path doesn't begin with '~', or if the user or $HOME is unknown,
rpm-build 2bd099
# the path is returned unchanged (leaving error reporting to whatever
rpm-build 2bd099
# function is called with the expanded path as argument).
rpm-build 2bd099
# See also module 'glob' for expansion of *, ? and [...] in pathnames.
rpm-build 2bd099
# (A function should also be defined to do full *sh-style environment
rpm-build 2bd099
# variable expansion.)
rpm-build 2bd099
rpm-build 2bd099
def expanduser(path):
rpm-build 2bd099
    """Expand ~ and ~user constructs.
rpm-build 2bd099
rpm-build 2bd099
    If user or $HOME is unknown, do nothing."""
rpm-build 2bd099
    path = os.fspath(path)
rpm-build 2bd099
    if isinstance(path, bytes):
rpm-build 2bd099
        tilde = b'~'
rpm-build 2bd099
    else:
rpm-build 2bd099
        tilde = '~'
rpm-build 2bd099
    if not path.startswith(tilde):
rpm-build 2bd099
        return path
rpm-build 2bd099
    i, n = 1, len(path)
rpm-build 2bd099
    while i < n and path[i] not in _get_bothseps(path):
rpm-build 2bd099
        i += 1
rpm-build 2bd099
rpm-build 2bd099
    if 'HOME' in os.environ:
rpm-build 2bd099
        userhome = os.environ['HOME']
rpm-build 2bd099
    elif 'USERPROFILE' in os.environ:
rpm-build 2bd099
        userhome = os.environ['USERPROFILE']
rpm-build 2bd099
    elif not 'HOMEPATH' in os.environ:
rpm-build 2bd099
        return path
rpm-build 2bd099
    else:
rpm-build 2bd099
        try:
rpm-build 2bd099
            drive = os.environ['HOMEDRIVE']
rpm-build 2bd099
        except KeyError:
rpm-build 2bd099
            drive = ''
rpm-build 2bd099
        userhome = join(drive, os.environ['HOMEPATH'])
rpm-build 2bd099
rpm-build 2bd099
    if isinstance(path, bytes):
rpm-build 2bd099
        userhome = os.fsencode(userhome)
rpm-build 2bd099
rpm-build 2bd099
    if i != 1: #~user
rpm-build 2bd099
        userhome = join(dirname(userhome), path[1:i])
rpm-build 2bd099
rpm-build 2bd099
    return userhome + path[i:]
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Expand paths containing shell variable substitutions.
rpm-build 2bd099
# The following rules apply:
rpm-build 2bd099
#       - no expansion within single quotes
rpm-build 2bd099
#       - '$$' is translated into '$'
rpm-build 2bd099
#       - '%%' is translated into '%' if '%%' are not seen in %var1%%var2%
rpm-build 2bd099
#       - ${varname} is accepted.
rpm-build 2bd099
#       - $varname is accepted.
rpm-build 2bd099
#       - %varname% is accepted.
rpm-build 2bd099
#       - varnames can be made out of letters, digits and the characters '_-'
rpm-build 2bd099
#         (though is not verified in the ${varname} and %varname% cases)
rpm-build 2bd099
# XXX With COMMAND.COM you can use any characters in a variable name,
rpm-build 2bd099
# XXX except '^|<>='.
rpm-build 2bd099
rpm-build 2bd099
def expandvars(path):
rpm-build 2bd099
    """Expand shell variables of the forms $var, ${var} and %var%.
rpm-build 2bd099
rpm-build 2bd099
    Unknown variables are left unchanged."""
rpm-build 2bd099
    path = os.fspath(path)
rpm-build 2bd099
    if isinstance(path, bytes):
rpm-build 2bd099
        if b'$' not in path and b'%' not in path:
rpm-build 2bd099
            return path
rpm-build 2bd099
        import string
rpm-build 2bd099
        varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
rpm-build 2bd099
        quote = b'\''
rpm-build 2bd099
        percent = b'%'
rpm-build 2bd099
        brace = b'{'
rpm-build 2bd099
        rbrace = b'}'
rpm-build 2bd099
        dollar = b'$'
rpm-build 2bd099
        environ = getattr(os, 'environb', None)
rpm-build 2bd099
    else:
rpm-build 2bd099
        if '$' not in path and '%' not in path:
rpm-build 2bd099
            return path
rpm-build 2bd099
        import string
rpm-build 2bd099
        varchars = string.ascii_letters + string.digits + '_-'
rpm-build 2bd099
        quote = '\''
rpm-build 2bd099
        percent = '%'
rpm-build 2bd099
        brace = '{'
rpm-build 2bd099
        rbrace = '}'
rpm-build 2bd099
        dollar = '$'
rpm-build 2bd099
        environ = os.environ
rpm-build 2bd099
    res = path[:0]
rpm-build 2bd099
    index = 0
rpm-build 2bd099
    pathlen = len(path)
rpm-build 2bd099
    while index < pathlen:
rpm-build 2bd099
        c = path[index:index+1]
rpm-build 2bd099
        if c == quote:   # no expansion within single quotes
rpm-build 2bd099
            path = path[index + 1:]
rpm-build 2bd099
            pathlen = len(path)
rpm-build 2bd099
            try:
rpm-build 2bd099
                index = path.index(c)
rpm-build 2bd099
                res += c + path[:index + 1]
rpm-build 2bd099
            except ValueError:
rpm-build 2bd099
                res += c + path
rpm-build 2bd099
                index = pathlen - 1
rpm-build 2bd099
        elif c == percent:  # variable or '%'
rpm-build 2bd099
            if path[index + 1:index + 2] == percent:
rpm-build 2bd099
                res += c
rpm-build 2bd099
                index += 1
rpm-build 2bd099
            else:
rpm-build 2bd099
                path = path[index+1:]
rpm-build 2bd099
                pathlen = len(path)
rpm-build 2bd099
                try:
rpm-build 2bd099
                    index = path.index(percent)
rpm-build 2bd099
                except ValueError:
rpm-build 2bd099
                    res += percent + path
rpm-build 2bd099
                    index = pathlen - 1
rpm-build 2bd099
                else:
rpm-build 2bd099
                    var = path[:index]
rpm-build 2bd099
                    try:
rpm-build 2bd099
                        if environ is None:
rpm-build 2bd099
                            value = os.fsencode(os.environ[os.fsdecode(var)])
rpm-build 2bd099
                        else:
rpm-build 2bd099
                            value = environ[var]
rpm-build 2bd099
                    except KeyError:
rpm-build 2bd099
                        value = percent + var + percent
rpm-build 2bd099
                    res += value
rpm-build 2bd099
        elif c == dollar:  # variable or '$$'
rpm-build 2bd099
            if path[index + 1:index + 2] == dollar:
rpm-build 2bd099
                res += c
rpm-build 2bd099
                index += 1
rpm-build 2bd099
            elif path[index + 1:index + 2] == brace:
rpm-build 2bd099
                path = path[index+2:]
rpm-build 2bd099
                pathlen = len(path)
rpm-build 2bd099
                try:
rpm-build 2bd099
                    index = path.index(rbrace)
rpm-build 2bd099
                except ValueError:
rpm-build 2bd099
                    res += dollar + brace + path
rpm-build 2bd099
                    index = pathlen - 1
rpm-build 2bd099
                else:
rpm-build 2bd099
                    var = path[:index]
rpm-build 2bd099
                    try:
rpm-build 2bd099
                        if environ is None:
rpm-build 2bd099
                            value = os.fsencode(os.environ[os.fsdecode(var)])
rpm-build 2bd099
                        else:
rpm-build 2bd099
                            value = environ[var]
rpm-build 2bd099
                    except KeyError:
rpm-build 2bd099
                        value = dollar + brace + var + rbrace
rpm-build 2bd099
                    res += value
rpm-build 2bd099
            else:
rpm-build 2bd099
                var = path[:0]
rpm-build 2bd099
                index += 1
rpm-build 2bd099
                c = path[index:index + 1]
rpm-build 2bd099
                while c and c in varchars:
rpm-build 2bd099
                    var += c
rpm-build 2bd099
                    index += 1
rpm-build 2bd099
                    c = path[index:index + 1]
rpm-build 2bd099
                try:
rpm-build 2bd099
                    if environ is None:
rpm-build 2bd099
                        value = os.fsencode(os.environ[os.fsdecode(var)])
rpm-build 2bd099
                    else:
rpm-build 2bd099
                        value = environ[var]
rpm-build 2bd099
                except KeyError:
rpm-build 2bd099
                    value = dollar + var
rpm-build 2bd099
                res += value
rpm-build 2bd099
                if c:
rpm-build 2bd099
                    index -= 1
rpm-build 2bd099
        else:
rpm-build 2bd099
            res += c
rpm-build 2bd099
        index += 1
rpm-build 2bd099
    return res
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
rpm-build 2bd099
# Previously, this function also truncated pathnames to 8+3 format,
rpm-build 2bd099
# but as this module is called "ntpath", that's obviously wrong!
rpm-build 2bd099
rpm-build 2bd099
def normpath(path):
rpm-build 2bd099
    """Normalize path, eliminating double slashes, etc."""
rpm-build 2bd099
    path = os.fspath(path)
rpm-build 2bd099
    if isinstance(path, bytes):
rpm-build 2bd099
        sep = b'\\'
rpm-build 2bd099
        altsep = b'/'
rpm-build 2bd099
        curdir = b'.'
rpm-build 2bd099
        pardir = b'..'
rpm-build 2bd099
        special_prefixes = (b'\\\\.\\', b'\\\\?\\')
rpm-build 2bd099
    else:
rpm-build 2bd099
        sep = '\\'
rpm-build 2bd099
        altsep = '/'
rpm-build 2bd099
        curdir = '.'
rpm-build 2bd099
        pardir = '..'
rpm-build 2bd099
        special_prefixes = ('\\\\.\\', '\\\\?\\')
rpm-build 2bd099
    if path.startswith(special_prefixes):
rpm-build 2bd099
        # in the case of paths with these prefixes:
rpm-build 2bd099
        # \\.\ -> device names
rpm-build 2bd099
        # \\?\ -> literal paths
rpm-build 2bd099
        # do not do any normalization, but return the path unchanged
rpm-build 2bd099
        return path
rpm-build 2bd099
    path = path.replace(altsep, sep)
rpm-build 2bd099
    prefix, path = splitdrive(path)
rpm-build 2bd099
rpm-build 2bd099
    # collapse initial backslashes
rpm-build 2bd099
    if path.startswith(sep):
rpm-build 2bd099
        prefix += sep
rpm-build 2bd099
        path = path.lstrip(sep)
rpm-build 2bd099
rpm-build 2bd099
    comps = path.split(sep)
rpm-build 2bd099
    i = 0
rpm-build 2bd099
    while i < len(comps):
rpm-build 2bd099
        if not comps[i] or comps[i] == curdir:
rpm-build 2bd099
            del comps[i]
rpm-build 2bd099
        elif comps[i] == pardir:
rpm-build 2bd099
            if i > 0 and comps[i-1] != pardir:
rpm-build 2bd099
                del comps[i-1:i+1]
rpm-build 2bd099
                i -= 1
rpm-build 2bd099
            elif i == 0 and prefix.endswith(sep):
rpm-build 2bd099
                del comps[i]
rpm-build 2bd099
            else:
rpm-build 2bd099
                i += 1
rpm-build 2bd099
        else:
rpm-build 2bd099
            i += 1
rpm-build 2bd099
    # If the path is now empty, substitute '.'
rpm-build 2bd099
    if not prefix and not comps:
rpm-build 2bd099
        comps.append(curdir)
rpm-build 2bd099
    return prefix + sep.join(comps)
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Return an absolute path.
rpm-build 2bd099
try:
rpm-build 2bd099
    from nt import _getfullpathname
rpm-build 2bd099
rpm-build 2bd099
except ImportError: # not running on Windows - mock up something sensible
rpm-build 2bd099
    def abspath(path):
rpm-build 2bd099
        """Return the absolute version of a path."""
rpm-build 2bd099
        path = os.fspath(path)
rpm-build 2bd099
        if not isabs(path):
rpm-build 2bd099
            if isinstance(path, bytes):
rpm-build 2bd099
                cwd = os.getcwdb()
rpm-build 2bd099
            else:
rpm-build 2bd099
                cwd = os.getcwd()
rpm-build 2bd099
            path = join(cwd, path)
rpm-build 2bd099
        return normpath(path)
rpm-build 2bd099
rpm-build 2bd099
else:  # use native Windows method on Windows
rpm-build 2bd099
    def abspath(path):
rpm-build 2bd099
        """Return the absolute version of a path."""
rpm-build 2bd099
rpm-build 2bd099
        if path: # Empty path must return current working directory.
rpm-build 2bd099
            path = os.fspath(path)
rpm-build 2bd099
            try:
rpm-build 2bd099
                path = _getfullpathname(path)
rpm-build 2bd099
            except OSError:
rpm-build 2bd099
                pass # Bad path - return unchanged.
rpm-build 2bd099
        elif isinstance(path, bytes):
rpm-build 2bd099
            path = os.getcwdb()
rpm-build 2bd099
        else:
rpm-build 2bd099
            path = os.getcwd()
rpm-build 2bd099
        return normpath(path)
rpm-build 2bd099
rpm-build 2bd099
# realpath is a no-op on systems without islink support
rpm-build 2bd099
realpath = abspath
rpm-build 2bd099
# Win9x family and earlier have no Unicode filename support.
rpm-build 2bd099
supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
rpm-build 2bd099
                              sys.getwindowsversion()[3] >= 2)
rpm-build 2bd099
rpm-build 2bd099
def relpath(path, start=None):
rpm-build 2bd099
    """Return a relative version of a path"""
rpm-build 2bd099
    path = os.fspath(path)
rpm-build 2bd099
    if isinstance(path, bytes):
rpm-build 2bd099
        sep = b'\\'
rpm-build 2bd099
        curdir = b'.'
rpm-build 2bd099
        pardir = b'..'
rpm-build 2bd099
    else:
rpm-build 2bd099
        sep = '\\'
rpm-build 2bd099
        curdir = '.'
rpm-build 2bd099
        pardir = '..'
rpm-build 2bd099
rpm-build 2bd099
    if start is None:
rpm-build 2bd099
        start = curdir
rpm-build 2bd099
rpm-build 2bd099
    if not path:
rpm-build 2bd099
        raise ValueError("no path specified")
rpm-build 2bd099
rpm-build 2bd099
    start = os.fspath(start)
rpm-build 2bd099
    try:
rpm-build 2bd099
        start_abs = abspath(normpath(start))
rpm-build 2bd099
        path_abs = abspath(normpath(path))
rpm-build 2bd099
        start_drive, start_rest = splitdrive(start_abs)
rpm-build 2bd099
        path_drive, path_rest = splitdrive(path_abs)
rpm-build 2bd099
        if normcase(start_drive) != normcase(path_drive):
rpm-build 2bd099
            raise ValueError("path is on mount %r, start on mount %r" % (
rpm-build 2bd099
                path_drive, start_drive))
rpm-build 2bd099
rpm-build 2bd099
        start_list = [x for x in start_rest.split(sep) if x]
rpm-build 2bd099
        path_list = [x for x in path_rest.split(sep) if x]
rpm-build 2bd099
        # Work out how much of the filepath is shared by start and path.
rpm-build 2bd099
        i = 0
rpm-build 2bd099
        for e1, e2 in zip(start_list, path_list):
rpm-build 2bd099
            if normcase(e1) != normcase(e2):
rpm-build 2bd099
                break
rpm-build 2bd099
            i += 1
rpm-build 2bd099
rpm-build 2bd099
        rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
rpm-build 2bd099
        if not rel_list:
rpm-build 2bd099
            return curdir
rpm-build 2bd099
        return join(*rel_list)
rpm-build 2bd099
    except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning):
rpm-build 2bd099
        genericpath._check_arg_types('relpath', path, start)
rpm-build 2bd099
        raise
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Return the longest common sub-path of the sequence of paths given as input.
rpm-build 2bd099
# The function is case-insensitive and 'separator-insensitive', i.e. if the
rpm-build 2bd099
# only difference between two paths is the use of '\' versus '/' as separator,
rpm-build 2bd099
# they are deemed to be equal.
rpm-build 2bd099
#
rpm-build 2bd099
# However, the returned path will have the standard '\' separator (even if the
rpm-build 2bd099
# given paths had the alternative '/' separator) and will have the case of the
rpm-build 2bd099
# first path given in the sequence. Additionally, any trailing separator is
rpm-build 2bd099
# stripped from the returned path.
rpm-build 2bd099
rpm-build 2bd099
def commonpath(paths):
rpm-build 2bd099
    """Given a sequence of path names, returns the longest common sub-path."""
rpm-build 2bd099
rpm-build 2bd099
    if not paths:
rpm-build 2bd099
        raise ValueError('commonpath() arg is an empty sequence')
rpm-build 2bd099
rpm-build 2bd099
    paths = tuple(map(os.fspath, paths))
rpm-build 2bd099
    if isinstance(paths[0], bytes):
rpm-build 2bd099
        sep = b'\\'
rpm-build 2bd099
        altsep = b'/'
rpm-build 2bd099
        curdir = b'.'
rpm-build 2bd099
    else:
rpm-build 2bd099
        sep = '\\'
rpm-build 2bd099
        altsep = '/'
rpm-build 2bd099
        curdir = '.'
rpm-build 2bd099
rpm-build 2bd099
    try:
rpm-build 2bd099
        drivesplits = [splitdrive(p.replace(altsep, sep).lower()) for p in paths]
rpm-build 2bd099
        split_paths = [p.split(sep) for d, p in drivesplits]
rpm-build 2bd099
rpm-build 2bd099
        try:
rpm-build 2bd099
            isabs, = set(p[:1] == sep for d, p in drivesplits)
rpm-build 2bd099
        except ValueError:
rpm-build 2bd099
            raise ValueError("Can't mix absolute and relative paths") from None
rpm-build 2bd099
rpm-build 2bd099
        # Check that all drive letters or UNC paths match. The check is made only
rpm-build 2bd099
        # now otherwise type errors for mixing strings and bytes would not be
rpm-build 2bd099
        # caught.
rpm-build 2bd099
        if len(set(d for d, p in drivesplits)) != 1:
rpm-build 2bd099
            raise ValueError("Paths don't have the same drive")
rpm-build 2bd099
rpm-build 2bd099
        drive, path = splitdrive(paths[0].replace(altsep, sep))
rpm-build 2bd099
        common = path.split(sep)
rpm-build 2bd099
        common = [c for c in common if c and c != curdir]
rpm-build 2bd099
rpm-build 2bd099
        split_paths = [[c for c in s if c and c != curdir] for s in split_paths]
rpm-build 2bd099
        s1 = min(split_paths)
rpm-build 2bd099
        s2 = max(split_paths)
rpm-build 2bd099
        for i, c in enumerate(s1):
rpm-build 2bd099
            if c != s2[i]:
rpm-build 2bd099
                common = common[:i]
rpm-build 2bd099
                break
rpm-build 2bd099
        else:
rpm-build 2bd099
            common = common[:len(s1)]
rpm-build 2bd099
rpm-build 2bd099
        prefix = drive + sep if isabs else drive
rpm-build 2bd099
        return prefix + sep.join(common)
rpm-build 2bd099
    except (TypeError, AttributeError):
rpm-build 2bd099
        genericpath._check_arg_types('commonpath', *paths)
rpm-build 2bd099
        raise
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# determine if two files are in fact the same file
rpm-build 2bd099
try:
rpm-build 2bd099
    # GetFinalPathNameByHandle is available starting with Windows 6.0.
rpm-build 2bd099
    # Windows XP and non-Windows OS'es will mock _getfinalpathname.
rpm-build 2bd099
    if sys.getwindowsversion()[:2] >= (6, 0):
rpm-build 2bd099
        from nt import _getfinalpathname
rpm-build 2bd099
    else:
rpm-build 2bd099
        raise ImportError
rpm-build 2bd099
except (AttributeError, ImportError):
rpm-build 2bd099
    # On Windows XP and earlier, two files are the same if their absolute
rpm-build 2bd099
    # pathnames are the same.
rpm-build 2bd099
    # Non-Windows operating systems fake this method with an XP
rpm-build 2bd099
    # approximation.
rpm-build 2bd099
    def _getfinalpathname(f):
rpm-build 2bd099
        return normcase(abspath(f))
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
try:
rpm-build 2bd099
    # The genericpath.isdir implementation uses os.stat and checks the mode
rpm-build 2bd099
    # attribute to tell whether or not the path is a directory.
rpm-build 2bd099
    # This is overkill on Windows - just pass the path to GetFileAttributes
rpm-build 2bd099
    # and check the attribute from there.
rpm-build 2bd099
    from nt import _isdir as isdir
rpm-build 2bd099
except ImportError:
rpm-build 2bd099
    # Use genericpath.isdir as imported above.
rpm-build 2bd099
    pass