Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 1 | # module 'macpath' -- pathname (or -related) operations for the Macintosh |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 2 | |
| 3 | import mac |
| 4 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 5 | from stat import * |
| 6 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 7 | |
| 8 | # Return true if a path is absolute. |
| 9 | # On the Mac, relative paths begin with a colon, |
| 10 | # but as a special case, paths with no colons at all are also relative. |
| 11 | # Anything else is absolute (the string up to the first colon is the |
| 12 | # volume name). |
| 13 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 14 | def isabs(s): |
| 15 | return ':' in s and s[0] <> ':' |
| 16 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 17 | |
| 18 | # Concatenate two pathnames. |
| 19 | # The result is equivalent to what the second pathname would refer to |
| 20 | # if the first pathname were the current directory. |
| 21 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 22 | def cat(s, t): |
| 23 | if (not s) or isabs(t): return t |
| 24 | if t[:1] = ':': t = t[1:] |
| 25 | if ':' not in s: |
| 26 | s = ':' + s |
| 27 | if s[-1:] <> ':': |
| 28 | s = s + ':' |
| 29 | return s + t |
| 30 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 31 | |
| 32 | # Split a pathname in two parts: the directory leading up to the final bit, |
| 33 | # and the basename (the filename, without colons, in that directory). |
| 34 | # The result (s, t) is such that cat(s, t) yields the original argument. |
| 35 | |
| 36 | def split(s): |
| 37 | if ':' not in s: return '', s |
| 38 | colon = 0 |
| 39 | for i in range(len(s)): |
| 40 | if s[i] = ':': colon = i+1 |
| 41 | return s[:colon], s[colon:] |
| 42 | |
| 43 | |
| 44 | # Normalize a pathname: get rid of '::' sequences by backing up, |
| 45 | # e.g., 'foo:bar::bletch' becomes 'foo:bletch'. |
| 46 | # Raise the exception norm_error below if backing up is impossible, |
| 47 | # e.g., for '::foo'. |
| 48 | |
| 49 | norm_error = 'macpath.norm_error: path cannot be normalized' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 50 | |
| 51 | def norm(s): |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 52 | import string |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 53 | if ':' not in s: |
| 54 | return ':' + s |
| 55 | f = string.splitfields(s, ':') |
| 56 | pre = [] |
| 57 | post = [] |
| 58 | if not f[0]: |
| 59 | pre = f[:1] |
| 60 | f = f[1:] |
| 61 | if not f[len(f)-1]: |
| 62 | post = f[-1:] |
| 63 | f = f[:-1] |
| 64 | res = [] |
| 65 | for seg in f: |
| 66 | if seg: |
| 67 | res.append(seg) |
| 68 | else: |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 69 | if not res: raise norm_error, 'path starts with ::' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 70 | del res[len(res)-1] |
| 71 | if not (pre or res): |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 72 | raise norm_error, 'path starts with volume::' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 73 | if pre: res = pre + res |
| 74 | if post: res = res + post |
| 75 | s = res[0] |
| 76 | for seg in res[1:]: |
| 77 | s = s + ':' + seg |
| 78 | return s |
| 79 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 80 | |
| 81 | # Return true if the pathname refers to an existing directory. |
| 82 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 83 | def isdir(s): |
| 84 | try: |
| 85 | st = mac.stat(s) |
| 86 | except mac.error: |
| 87 | return 0 |
| 88 | return S_ISDIR(st[ST_MODE]) |
| 89 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 90 | |
| 91 | # Return true if the pathname refers to an existing regular file. |
| 92 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 93 | def isfile(s): |
| 94 | try: |
| 95 | st = mac.stat(s) |
| 96 | except mac.error: |
| 97 | return 0 |
| 98 | return S_ISREG(st[ST_MODE]) |
| 99 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 100 | |
| 101 | # Return true if the pathname refers to an existing file or directory. |
| 102 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 103 | def exists(s): |
| 104 | try: |
| 105 | st = mac.stat(s) |
| 106 | except mac.error: |
| 107 | return 0 |
| 108 | return 1 |