paul@6 | 1 | """ |
paul@6 | 2 | Path operations common to more than one OS |
paul@6 | 3 | Do not use directly. The OS specific modules import the appropriate |
paul@6 | 4 | functions from this module themselves. |
paul@6 | 5 | """ |
paul@6 | 6 | from os import error, stat |
paul@6 | 7 | from stat import * |
paul@6 | 8 | |
paul@6 | 9 | __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', |
paul@6 | 10 | 'getsize', 'isdir', 'isfile'] |
paul@6 | 11 | |
paul@6 | 12 | |
paul@6 | 13 | # Does a path exist? |
paul@6 | 14 | # This is false for dangling symbolic links on systems that support them. |
paul@6 | 15 | def exists(path): |
paul@6 | 16 | """Test whether a path exists. Returns False for broken symbolic links""" |
paul@6 | 17 | try: |
paul@6 | 18 | stat(path) |
paul@6 | 19 | except error: |
paul@6 | 20 | return False |
paul@6 | 21 | return True |
paul@6 | 22 | |
paul@6 | 23 | |
paul@6 | 24 | # This follows symbolic links, so both islink() and isdir() can be true |
paul@6 | 25 | # for the same path ono systems that support symlinks |
paul@6 | 26 | def isfile(path): |
paul@6 | 27 | """Test whether a path is a regular file""" |
paul@6 | 28 | try: |
paul@6 | 29 | st = stat(path) |
paul@6 | 30 | except error: |
paul@6 | 31 | return False |
paul@6 | 32 | return stat.S_ISREG(st.st_mode) |
paul@6 | 33 | |
paul@6 | 34 | |
paul@6 | 35 | # Is a path a directory? |
paul@6 | 36 | # This follows symbolic links, so both islink() and isdir() |
paul@6 | 37 | # can be true for the same path on systems that support symlinks |
paul@6 | 38 | def isdir(s): |
paul@6 | 39 | """Return true if the pathname refers to an existing directory.""" |
paul@6 | 40 | try: |
paul@6 | 41 | st = stat(s) |
paul@6 | 42 | except error: |
paul@6 | 43 | return False |
paul@6 | 44 | return stat.S_ISDIR(st.st_mode) |
paul@6 | 45 | |
paul@6 | 46 | |
paul@6 | 47 | def getsize(filename): |
paul@6 | 48 | """Return the size of a file, reported by os.stat().""" |
paul@6 | 49 | return stat(filename).st_size |
paul@6 | 50 | |
paul@6 | 51 | |
paul@6 | 52 | def getmtime(filename): |
paul@6 | 53 | """Return the last modification time of a file, reported by os.stat().""" |
paul@6 | 54 | return stat(filename).st_mtime |
paul@6 | 55 | |
paul@6 | 56 | |
paul@6 | 57 | def getatime(filename): |
paul@6 | 58 | """Return the last access time of a file, reported by os.stat().""" |
paul@6 | 59 | return stat(filename).st_atime |
paul@6 | 60 | |
paul@6 | 61 | |
paul@6 | 62 | def getctime(filename): |
paul@6 | 63 | """Return the metadata change time of a file, reported by os.stat().""" |
paul@6 | 64 | return stat(filename).st_ctime |
paul@6 | 65 | |
paul@6 | 66 | |
paul@6 | 67 | # Return the longest prefix of all list elements. |
paul@6 | 68 | def commonprefix(m): |
paul@6 | 69 | "Given a list of pathnames, returns the longest common leading component" |
paul@6 | 70 | if not m: return '' |
paul@6 | 71 | s1 = min(m) |
paul@6 | 72 | s2 = max(m) |
paul@6 | 73 | for i, c in enumerate(s1): |
paul@6 | 74 | if c != s2[i]: |
paul@6 | 75 | return s1[:i] |
paul@6 | 76 | return s1 |
paul@6 | 77 | |
paul@6 | 78 | # Split a path in root and extension. |
paul@6 | 79 | # The extension is everything starting at the last dot in the last |
paul@6 | 80 | # pathname component; the root is everything before that. |
paul@6 | 81 | # It is always true that root + ext == p. |
paul@6 | 82 | |
paul@6 | 83 | # Generic implementation of splitext, to be parametrized with |
paul@6 | 84 | # the separators |
paul@6 | 85 | def _splitext(p, sep, altsep, extsep): |
paul@6 | 86 | """Split the extension from a pathname. |
paul@6 | 87 | |
paul@6 | 88 | Extension is everything from the last dot to the end, ignoring |
paul@6 | 89 | leading dots. Returns "(root, ext)"; ext may be empty.""" |
paul@6 | 90 | |
paul@6 | 91 | sepIndex = p.rfind(sep) |
paul@6 | 92 | if altsep: |
paul@6 | 93 | altsepIndex = p.rfind(altsep) |
paul@6 | 94 | sepIndex = max(sepIndex, altsepIndex) |
paul@6 | 95 | |
paul@6 | 96 | dotIndex = p.rfind(extsep) |
paul@6 | 97 | if dotIndex > sepIndex: |
paul@6 | 98 | # skip all leading dots |
paul@6 | 99 | filenameIndex = sepIndex + 1 |
paul@6 | 100 | while filenameIndex < dotIndex: |
paul@6 | 101 | if p[filenameIndex] != extsep: |
paul@6 | 102 | return p[:dotIndex], p[dotIndex:] |
paul@6 | 103 | filenameIndex += 1 |
paul@6 | 104 | |
paul@6 | 105 | return p, '' |