Lichen

lib/posixpath.py

175:9633900a1220
2016-11-12 Paul Boddie Retain alias information for invoked objects, providing correct origin details for instances imported from other modules.
     1 """Common operations on Posix pathnames.     2      3 Instead of importing this module directly, import os and refer to     4 this module as os.path.  The "os.path" name is an alias for this     5 module on Posix systems; on other systems (e.g. Mac, Windows),     6 os.path provides the same operations in a manner specific to that     7 platform, and is an alias to another module (e.g. macpath, ntpath).     8      9 Some of this can actually be useful on non-Posix systems too, e.g.    10 for manipulation of the pathname component of URLs.    11 """    12     13 from genericos import environ, error, fstat, getcwd, getcwdu, getuid, listdir, lstat, readlink    14 import sys    15 import stat    16 import genericpath    17 from genericpath import *    18     19 try:    20     _unicode = unicode    21 except NameError:    22     # If Python is built without Unicode support, the unicode type    23     # will not exist. Fake one.    24     class _unicode(object):    25         pass    26     27 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",    28            "basename","dirname","commonprefix","getsize","getmtime",    29            "getatime","getctime","islink","exists","lexists","isdir","isfile",    30            "ismount","walk","expanduser","expandvars","normpath","abspath",    31            "samefile","sameopenfile","samestat",    32            "curdir","pardir","sep","pathsep","defpath","altsep","extsep",    33            "devnull","realpath","supports_unicode_filenames","relpath"]    34     35 # strings representing various path-related bits and pieces    36 curdir = '.'    37 pardir = '..'    38 extsep = '.'    39 sep = '/'    40 pathsep = ':'    41 defpath = ':/bin:/usr/bin'    42 altsep = None    43 devnull = '/dev/null'    44     45 # Normalize the case of a pathname.  Trivial in Posix, string.lower on Mac.    46 # On MS-DOS this may also turn slashes into backslashes; however, other    47 # normalizations (such as optimizing '../' away) are not allowed    48 # (another function should be defined to do that).    49     50 def normcase(s):    51     """Normalize case of pathname.  Has no effect under Posix"""    52     return s    53     54     55 # Return whether a path is absolute.    56 # Trivial in Posix, harder on the Mac or MS-DOS.    57     58 def isabs(s):    59     """Test whether a path is absolute"""    60     return s.startswith('/')    61     62     63 # Join pathnames.    64 # Ignore the previous parts if a part is absolute.    65 # Insert a '/' unless the first part is empty or already ends in '/'.    66     67 def join(a, *p):    68     """Join two or more pathname components, inserting '/' as needed.    69     If any component is an absolute path, all previous path components    70     will be discarded.  An empty last part will result in a path that    71     ends with a separator."""    72     path = a    73     for b in p:    74         if b.startswith('/'):    75             path = b    76         elif path == '' or path.endswith('/'):    77             path +=  b    78         else:    79             path += '/' + b    80     return path    81     82     83 # Split a path in head (everything up to the last '/') and tail (the    84 # rest).  If the path ends in '/', tail will be empty.  If there is no    85 # '/' in the path, head  will be empty.    86 # Trailing '/'es are stripped from head unless it is the root.    87     88 def split(p):    89     """Split a pathname.  Returns tuple "(head, tail)" where "tail" is    90     everything after the final slash.  Either part may be empty."""    91     i = p.rfind('/') + 1    92     head, tail = p[:i], p[i:]    93     if head and head != '/'*len(head):    94         head = head.rstrip('/')    95     return head, tail    96     97     98 # Split a path in root and extension.    99 # The extension is everything starting at the last dot in the last   100 # pathname component; the root is everything before that.   101 # It is always true that root + ext == p.   102    103 def splitext(p):   104     return genericpath._splitext(p, sep, altsep, extsep)   105    106 # Split a pathname into a drive specification and the rest of the   107 # path.  Useful on DOS/Windows/NT; on Unix, the drive is always empty.   108    109 def splitdrive(p):   110     """Split a pathname into drive and path. On Posix, drive is always   111     empty."""   112     return '', p   113    114    115 # Return the tail (basename) part of a path, same as split(path)[1].   116    117 def basename(p):   118     """Returns the final component of a pathname"""   119     i = p.rfind('/') + 1   120     return p[i:]   121    122    123 # Return the head (dirname) part of a path, same as split(path)[0].   124    125 def dirname(p):   126     """Returns the directory component of a pathname"""   127     i = p.rfind('/') + 1   128     head = p[:i]   129     if head and head != '/'*len(head):   130         head = head.rstrip('/')   131     return head   132    133    134 # Is a path a symbolic link?   135 # This will always return false on systems where os.lstat doesn't exist.   136    137 def islink(path):   138     """Test whether a path is a symbolic link"""   139     try:   140         st = lstat(path)   141     except (error, AttributeError):   142         return False   143     return stat.S_ISLNK(st.st_mode)   144    145 # Being true for dangling symbolic links is also useful.   146    147 def lexists(path):   148     """Test whether a path exists.  Returns True for broken symbolic links"""   149     try:   150         lstat(path)   151     except error:   152         return False   153     return True   154    155    156 # Are two filenames really pointing to the same file?   157    158 def samefile(f1, f2):   159     """Test whether two pathnames reference the same actual file"""   160     s1 = stat(f1)   161     s2 = stat(f2)   162     return samestat(s1, s2)   163    164    165 # Are two open files really referencing the same file?   166 # (Not necessarily the same file descriptor!)   167    168 def sameopenfile(fp1, fp2):   169     """Test whether two open file objects reference the same file"""   170     s1 = fstat(fp1)   171     s2 = fstat(fp2)   172     return samestat(s1, s2)   173    174    175 # Are two stat buffers (obtained from stat, fstat or lstat)   176 # describing the same file?   177    178 def samestat(s1, s2):   179     """Test whether two stat buffers reference the same file"""   180     return s1.st_ino == s2.st_ino and \   181            s1.st_dev == s2.st_dev   182    183    184 # Is a path a mount point?   185 # (Does this work for all UNIXes?  Is it even guaranteed to work by Posix?)   186    187 def ismount(path):   188     """Test whether a path is a mount point"""   189     if islink(path):   190         # A symlink can never be a mount point   191         return False   192     try:   193         s1 = lstat(path)   194         s2 = lstat(join(path, '..'))   195     except error:   196         return False # It doesn't exist -- so not a mount point :-)   197     dev1 = s1.st_dev   198     dev2 = s2.st_dev   199     if dev1 != dev2:   200         return True     # path/.. on a different device as path   201     ino1 = s1.st_ino   202     ino2 = s2.st_ino   203     if ino1 == ino2:   204         return True     # path/.. is the same i-node as path   205     return False   206    207    208 # Directory tree walk.   209 # For each directory under top (including top itself, but excluding   210 # '.' and '..'), func(arg, dirname, filenames) is called, where   211 # dirname is the name of the directory and filenames is the list   212 # of files (and subdirectories etc.) in the directory.   213 # The func may modify the filenames list, to implement a filter,   214 # or to impose a different order of visiting.   215    216 def walk(top, func, arg):   217     """Directory tree walk with callback function.   218    219     For each directory in the directory tree rooted at top (including top   220     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).   221     dirname is the name of the directory, and fnames a list of the names of   222     the files and subdirectories in dirname (excluding '.' and '..').  func   223     may modify the fnames list in-place (e.g. via del or slice assignment),   224     and walk will only recurse into the subdirectories whose names remain in   225     fnames; this can be used to implement a filter, or to impose a specific   226     order of visiting.  No semantics are defined for, or required of, arg,   227     beyond that arg is always passed to func.  It can be used, e.g., to pass   228     a filename pattern, or a mutable object designed to accumulate   229     statistics.  Passing None for arg is common."""   230     try:   231         names = listdir(top)   232     except error:   233         return   234     func(arg, top, names)   235     for name in names:   236         name = join(top, name)   237         try:   238             st = lstat(name)   239         except error:   240             continue   241         if stat.S_ISDIR(st.st_mode):   242             walk(name, func, arg)   243    244    245 # Expand paths beginning with '~' or '~user'.   246 # '~' means $HOME; '~user' means that user's home directory.   247 # If the path doesn't begin with '~', or if the user or $HOME is unknown,   248 # the path is returned unchanged (leaving error reporting to whatever   249 # function is called with the expanded path as argument).   250 # See also module 'glob' for expansion of *, ? and [...] in pathnames.   251 # (A function should also be defined to do full *sh-style environment   252 # variable expansion.)   253    254 def expanduser(path):   255     """Expand ~ and ~user constructions.  If user or $HOME is unknown,   256     do nothing."""   257     if not path.startswith('~'):   258         return path   259     i = path.find('/', 1)   260     if i < 0:   261         i = len(path)   262     if i == 1:   263         if 'HOME' not in environ:   264             import pwd   265             userhome = pwd.getpwuid(getuid()).pw_dir   266         else:   267             userhome = environ['HOME']   268     else:   269         import pwd   270         try:   271             pwent = pwd.getpwnam(path[1:i])   272         except KeyError:   273             return path   274         userhome = pwent.pw_dir   275     userhome = userhome.rstrip('/')   276     return (userhome + path[i:]) or '/'   277    278    279 # Expand paths containing shell variable substitutions.   280 # This expands the forms $variable and ${variable} only.   281 # Non-existent variables are left unchanged.   282    283 _varprog = None   284    285 def expandvars(path):   286     """Expand shell variables of form $var and ${var}.  Unknown variables   287     are left unchanged."""   288     global _varprog   289     if '$' not in path:   290         return path   291     if not _varprog:   292         import re   293         _varprog = re.compile(r'\$(\w+|\{[^}]*\})')   294     i = 0   295     while True:   296         m = _varprog.search(path, i)   297         if not m:   298             break   299         i, j = m.span(0)   300         name = m.group(1)   301         if name.startswith('{') and name.endswith('}'):   302             name = name[1:-1]   303         if name in environ:   304             tail = path[j:]   305             path = path[:i] + environ[name]   306             i = len(path)   307             path += tail   308         else:   309             i = j   310     return path   311    312    313 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.   314 # It should be understood that this may change the meaning of the path   315 # if it contains symbolic links!   316    317 def normpath(path):   318     """Normalize path, eliminating double slashes, etc."""   319     # Preserve unicode (if path is unicode)   320     slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.')   321     if path == '':   322         return dot   323     initial_slashes = path.startswith('/')   324     # POSIX allows one or two initial slashes, but treats three or more   325     # as single slash.   326     if (initial_slashes and   327         path.startswith('//') and not path.startswith('///')):   328         initial_slashes = 2   329     comps = path.split('/')   330     new_comps = []   331     for comp in comps:   332         if comp in ('', '.'):   333             continue   334         if (comp != '..' or (not initial_slashes and not new_comps) or   335              (new_comps and new_comps[-1] == '..')):   336             new_comps.append(comp)   337         elif new_comps:   338             new_comps.pop()   339     comps = new_comps   340     path = slash.join(comps)   341     if initial_slashes:   342         path = slash*initial_slashes + path   343     return path or dot   344    345    346 def abspath(path):   347     """Return an absolute path."""   348     if not isabs(path):   349         if isinstance(path, _unicode):   350             cwd = getcwdu()   351         else:   352             cwd = getcwd()   353         path = join(cwd, path)   354     return normpath(path)   355    356    357 # Return a canonical path (i.e. the absolute location of a file on the   358 # filesystem).   359    360 def realpath(filename):   361     """Return the canonical path of the specified filename, eliminating any   362 symbolic links encountered in the path."""   363     if isabs(filename):   364         bits = ['/'] + filename.split('/')[1:]   365     else:   366         bits = [''] + filename.split('/')   367    368     for i in range(2, len(bits)+1):   369         component = join(*bits[0:i])   370         # Resolve symbolic links.   371         if islink(component):   372             resolved = _resolve_link(component)   373             if resolved is None:   374                 # Infinite loop -- return original component + rest of the path   375                 return abspath(join(*([component] + bits[i:])))   376             else:   377                 newpath = join(*([resolved] + bits[i:]))   378                 return realpath(newpath)   379    380     return abspath(filename)   381    382    383 def _resolve_link(path):   384     """Internal helper function.  Takes a path and follows symlinks   385     until we either arrive at something that isn't a symlink, or   386     encounter a path we've seen before (meaning that there's a loop).   387     """   388     paths_seen = set()   389     while islink(path):   390         if path in paths_seen:   391             # Already seen this path, so we must have a symlink loop   392             return None   393         paths_seen.add(path)   394         # Resolve where the link points to   395         resolved = readlink(path)   396         if not isabs(resolved):   397             dir = dirname(path)   398             path = normpath(join(dir, resolved))   399         else:   400             path = normpath(resolved)   401     return path   402    403 supports_unicode_filenames = (sys.platform == 'darwin')   404    405 def relpath(path, start=curdir):   406     """Return a relative version of a path"""   407    408     if not path:   409         raise ValueError("no path specified")   410    411     start_list = [x for x in abspath(start).split(sep) if x]   412     path_list = [x for x in abspath(path).split(sep) if x]   413    414     # Work out how much of the filepath is shared by start and path.   415     i = len(commonprefix([start_list, path_list]))   416    417     rel_list = [pardir] * (len(start_list)-i) + path_list[i:]   418     if not rel_list:   419         return curdir   420     return join(*rel_list)