| 1 | n/a | """ |
|---|
| 2 | n/a | Path operations common to more than one OS |
|---|
| 3 | n/a | Do not use directly. The OS specific modules import the appropriate |
|---|
| 4 | n/a | functions from this module themselves. |
|---|
| 5 | n/a | """ |
|---|
| 6 | n/a | import os |
|---|
| 7 | n/a | import stat |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', |
|---|
| 10 | n/a | 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile', |
|---|
| 11 | n/a | 'samestat'] |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | # Does a path exist? |
|---|
| 15 | n/a | # This is false for dangling symbolic links on systems that support them. |
|---|
| 16 | n/a | def exists(path): |
|---|
| 17 | n/a | """Test whether a path exists. Returns False for broken symbolic links""" |
|---|
| 18 | n/a | try: |
|---|
| 19 | n/a | os.stat(path) |
|---|
| 20 | n/a | except OSError: |
|---|
| 21 | n/a | return False |
|---|
| 22 | n/a | return True |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | # This follows symbolic links, so both islink() and isdir() can be true |
|---|
| 26 | n/a | # for the same path on systems that support symlinks |
|---|
| 27 | n/a | def isfile(path): |
|---|
| 28 | n/a | """Test whether a path is a regular file""" |
|---|
| 29 | n/a | try: |
|---|
| 30 | n/a | st = os.stat(path) |
|---|
| 31 | n/a | except OSError: |
|---|
| 32 | n/a | return False |
|---|
| 33 | n/a | return stat.S_ISREG(st.st_mode) |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | # Is a path a directory? |
|---|
| 37 | n/a | # This follows symbolic links, so both islink() and isdir() |
|---|
| 38 | n/a | # can be true for the same path on systems that support symlinks |
|---|
| 39 | n/a | def isdir(s): |
|---|
| 40 | n/a | """Return true if the pathname refers to an existing directory.""" |
|---|
| 41 | n/a | try: |
|---|
| 42 | n/a | st = os.stat(s) |
|---|
| 43 | n/a | except OSError: |
|---|
| 44 | n/a | return False |
|---|
| 45 | n/a | return stat.S_ISDIR(st.st_mode) |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | def getsize(filename): |
|---|
| 49 | n/a | """Return the size of a file, reported by os.stat().""" |
|---|
| 50 | n/a | return os.stat(filename).st_size |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | def getmtime(filename): |
|---|
| 54 | n/a | """Return the last modification time of a file, reported by os.stat().""" |
|---|
| 55 | n/a | return os.stat(filename).st_mtime |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def getatime(filename): |
|---|
| 59 | n/a | """Return the last access time of a file, reported by os.stat().""" |
|---|
| 60 | n/a | return os.stat(filename).st_atime |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | def getctime(filename): |
|---|
| 64 | n/a | """Return the metadata change time of a file, reported by os.stat().""" |
|---|
| 65 | n/a | return os.stat(filename).st_ctime |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | # Return the longest prefix of all list elements. |
|---|
| 69 | n/a | def commonprefix(m): |
|---|
| 70 | n/a | "Given a list of pathnames, returns the longest common leading component" |
|---|
| 71 | n/a | if not m: return '' |
|---|
| 72 | n/a | # Some people pass in a list of pathname parts to operate in an OS-agnostic |
|---|
| 73 | n/a | # fashion; don't try to translate in that case as that's an abuse of the |
|---|
| 74 | n/a | # API and they are already doing what they need to be OS-agnostic and so |
|---|
| 75 | n/a | # they most likely won't be using an os.PathLike object in the sublists. |
|---|
| 76 | n/a | if not isinstance(m[0], (list, tuple)): |
|---|
| 77 | n/a | m = tuple(map(os.fspath, m)) |
|---|
| 78 | n/a | s1 = min(m) |
|---|
| 79 | n/a | s2 = max(m) |
|---|
| 80 | n/a | for i, c in enumerate(s1): |
|---|
| 81 | n/a | if c != s2[i]: |
|---|
| 82 | n/a | return s1[:i] |
|---|
| 83 | n/a | return s1 |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | # Are two stat buffers (obtained from stat, fstat or lstat) |
|---|
| 86 | n/a | # describing the same file? |
|---|
| 87 | n/a | def samestat(s1, s2): |
|---|
| 88 | n/a | """Test whether two stat buffers reference the same file""" |
|---|
| 89 | n/a | return (s1.st_ino == s2.st_ino and |
|---|
| 90 | n/a | s1.st_dev == s2.st_dev) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | # Are two filenames really pointing to the same file? |
|---|
| 94 | n/a | def samefile(f1, f2): |
|---|
| 95 | n/a | """Test whether two pathnames reference the same actual file""" |
|---|
| 96 | n/a | s1 = os.stat(f1) |
|---|
| 97 | n/a | s2 = os.stat(f2) |
|---|
| 98 | n/a | return samestat(s1, s2) |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | # Are two open files really referencing the same file? |
|---|
| 102 | n/a | # (Not necessarily the same file descriptor!) |
|---|
| 103 | n/a | def sameopenfile(fp1, fp2): |
|---|
| 104 | n/a | """Test whether two open file objects reference the same file""" |
|---|
| 105 | n/a | s1 = os.fstat(fp1) |
|---|
| 106 | n/a | s2 = os.fstat(fp2) |
|---|
| 107 | n/a | return samestat(s1, s2) |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | # Split a path in root and extension. |
|---|
| 111 | n/a | # The extension is everything starting at the last dot in the last |
|---|
| 112 | n/a | # pathname component; the root is everything before that. |
|---|
| 113 | n/a | # It is always true that root + ext == p. |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | # Generic implementation of splitext, to be parametrized with |
|---|
| 116 | n/a | # the separators |
|---|
| 117 | n/a | def _splitext(p, sep, altsep, extsep): |
|---|
| 118 | n/a | """Split the extension from a pathname. |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | Extension is everything from the last dot to the end, ignoring |
|---|
| 121 | n/a | leading dots. Returns "(root, ext)"; ext may be empty.""" |
|---|
| 122 | n/a | # NOTE: This code must work for text and bytes strings. |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | sepIndex = p.rfind(sep) |
|---|
| 125 | n/a | if altsep: |
|---|
| 126 | n/a | altsepIndex = p.rfind(altsep) |
|---|
| 127 | n/a | sepIndex = max(sepIndex, altsepIndex) |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | dotIndex = p.rfind(extsep) |
|---|
| 130 | n/a | if dotIndex > sepIndex: |
|---|
| 131 | n/a | # skip all leading dots |
|---|
| 132 | n/a | filenameIndex = sepIndex + 1 |
|---|
| 133 | n/a | while filenameIndex < dotIndex: |
|---|
| 134 | n/a | if p[filenameIndex:filenameIndex+1] != extsep: |
|---|
| 135 | n/a | return p[:dotIndex], p[dotIndex:] |
|---|
| 136 | n/a | filenameIndex += 1 |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | return p, p[:0] |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | def _check_arg_types(funcname, *args): |
|---|
| 141 | n/a | hasstr = hasbytes = False |
|---|
| 142 | n/a | for s in args: |
|---|
| 143 | n/a | if isinstance(s, str): |
|---|
| 144 | n/a | hasstr = True |
|---|
| 145 | n/a | elif isinstance(s, bytes): |
|---|
| 146 | n/a | hasbytes = True |
|---|
| 147 | n/a | else: |
|---|
| 148 | n/a | raise TypeError('%s() argument must be str or bytes, not %r' % |
|---|
| 149 | n/a | (funcname, s.__class__.__name__)) from None |
|---|
| 150 | n/a | if hasstr and hasbytes: |
|---|
| 151 | n/a | raise TypeError("Can't mix strings and bytes in path components") from None |
|---|