Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1 | """ |
| 2 | Path operations common to more than one OS |
| 3 | Do not use directly. The OS specific modules import the appropriate |
| 4 | functions from this module themselves. |
| 5 | """ |
| 6 | import os |
| 7 | import stat |
| 8 | |
| 9 | __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 10 | 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile', |
| 11 | 'samestat'] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | # Does a path exist? |
| 15 | # This is false for dangling symbolic links on systems that support them. |
| 16 | def exists(path): |
| 17 | """Test whether a path exists. Returns False for broken symbolic links""" |
| 18 | try: |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 19 | os.stat(path) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 20 | except OSError: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 21 | return False |
| 22 | return True |
| 23 | |
| 24 | |
| 25 | # This follows symbolic links, so both islink() and isdir() can be true |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 26 | # for the same path on systems that support symlinks |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 27 | def isfile(path): |
| 28 | """Test whether a path is a regular file""" |
| 29 | try: |
| 30 | st = os.stat(path) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 31 | except OSError: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 32 | return False |
| 33 | return stat.S_ISREG(st.st_mode) |
| 34 | |
| 35 | |
| 36 | # Is a path a directory? |
| 37 | # This follows symbolic links, so both islink() and isdir() |
| 38 | # can be true for the same path on systems that support symlinks |
| 39 | def isdir(s): |
| 40 | """Return true if the pathname refers to an existing directory.""" |
| 41 | try: |
| 42 | st = os.stat(s) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 43 | except OSError: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 44 | return False |
| 45 | return stat.S_ISDIR(st.st_mode) |
| 46 | |
| 47 | |
| 48 | def getsize(filename): |
| 49 | """Return the size of a file, reported by os.stat().""" |
| 50 | return os.stat(filename).st_size |
| 51 | |
| 52 | |
| 53 | def getmtime(filename): |
| 54 | """Return the last modification time of a file, reported by os.stat().""" |
| 55 | return os.stat(filename).st_mtime |
| 56 | |
| 57 | |
| 58 | def getatime(filename): |
| 59 | """Return the last access time of a file, reported by os.stat().""" |
| 60 | return os.stat(filename).st_atime |
| 61 | |
| 62 | |
| 63 | def getctime(filename): |
| 64 | """Return the metadata change time of a file, reported by os.stat().""" |
| 65 | return os.stat(filename).st_ctime |
| 66 | |
| 67 | |
| 68 | # Return the longest prefix of all list elements. |
| 69 | def commonprefix(m): |
| 70 | "Given a list of pathnames, returns the longest common leading component" |
| 71 | if not m: return '' |
| 72 | s1 = min(m) |
| 73 | s2 = max(m) |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 74 | for i, c in enumerate(s1): |
| 75 | if c != s2[i]: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 76 | return s1[:i] |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 77 | return s1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 78 | |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 79 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 80 | # describing the same file? |
| 81 | def samestat(s1, s2): |
| 82 | """Test whether two stat buffers reference the same file""" |
| 83 | return (s1.st_ino == s2.st_ino and |
| 84 | s1.st_dev == s2.st_dev) |
| 85 | |
| 86 | |
| 87 | # Are two filenames really pointing to the same file? |
| 88 | def samefile(f1, f2): |
| 89 | """Test whether two pathnames reference the same actual file""" |
| 90 | s1 = os.stat(f1) |
| 91 | s2 = os.stat(f2) |
| 92 | return samestat(s1, s2) |
| 93 | |
| 94 | |
| 95 | # Are two open files really referencing the same file? |
| 96 | # (Not necessarily the same file descriptor!) |
| 97 | def sameopenfile(fp1, fp2): |
| 98 | """Test whether two open file objects reference the same file""" |
| 99 | s1 = os.fstat(fp1) |
| 100 | s2 = os.fstat(fp2) |
| 101 | return samestat(s1, s2) |
| 102 | |
| 103 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 104 | # Split a path in root and extension. |
| 105 | # The extension is everything starting at the last dot in the last |
| 106 | # pathname component; the root is everything before that. |
| 107 | # It is always true that root + ext == p. |
| 108 | |
| 109 | # Generic implementation of splitext, to be parametrized with |
| 110 | # the separators |
| 111 | def _splitext(p, sep, altsep, extsep): |
| 112 | """Split the extension from a pathname. |
| 113 | |
| 114 | Extension is everything from the last dot to the end, ignoring |
| 115 | leading dots. Returns "(root, ext)"; ext may be empty.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 116 | # NOTE: This code must work for text and bytes strings. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 117 | |
| 118 | sepIndex = p.rfind(sep) |
| 119 | if altsep: |
| 120 | altsepIndex = p.rfind(altsep) |
| 121 | sepIndex = max(sepIndex, altsepIndex) |
| 122 | |
| 123 | dotIndex = p.rfind(extsep) |
| 124 | if dotIndex > sepIndex: |
| 125 | # skip all leading dots |
| 126 | filenameIndex = sepIndex + 1 |
| 127 | while filenameIndex < dotIndex: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 128 | if p[filenameIndex:filenameIndex+1] != extsep: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 129 | return p[:dotIndex], p[dotIndex:] |
| 130 | filenameIndex += 1 |
| 131 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 132 | return p, p[:0] |