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 | |
Guido van Rossum | fbe0a8e | 1991-08-16 13:27:45 +0000 | [diff] [blame] | 18 | # Join two pathnames. |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 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 | fbe0a8e | 1991-08-16 13:27:45 +0000 | [diff] [blame] | 22 | def join(s, t): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | fbe0a8e | 1991-08-16 13:27:45 +0000 | [diff] [blame] | 32 | cat = join # For compatibility |
| 33 | |
| 34 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 35 | # Split a pathname in two parts: the directory leading up to the final bit, |
| 36 | # and the basename (the filename, without colons, in that directory). |
Guido van Rossum | fbe0a8e | 1991-08-16 13:27:45 +0000 | [diff] [blame] | 37 | # The result (s, t) is such that join(s, t) yields the original argument. |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 38 | |
| 39 | def split(s): |
| 40 | if ':' not in s: return '', s |
| 41 | colon = 0 |
| 42 | for i in range(len(s)): |
| 43 | if s[i] = ':': colon = i+1 |
| 44 | return s[:colon], s[colon:] |
| 45 | |
| 46 | |
| 47 | # Normalize a pathname: get rid of '::' sequences by backing up, |
| 48 | # e.g., 'foo:bar::bletch' becomes 'foo:bletch'. |
| 49 | # Raise the exception norm_error below if backing up is impossible, |
| 50 | # e.g., for '::foo'. |
| 51 | |
| 52 | norm_error = 'macpath.norm_error: path cannot be normalized' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 53 | |
| 54 | def norm(s): |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 55 | import string |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 56 | if ':' not in s: |
| 57 | return ':' + s |
| 58 | f = string.splitfields(s, ':') |
| 59 | pre = [] |
| 60 | post = [] |
| 61 | if not f[0]: |
| 62 | pre = f[:1] |
| 63 | f = f[1:] |
| 64 | if not f[len(f)-1]: |
| 65 | post = f[-1:] |
| 66 | f = f[:-1] |
| 67 | res = [] |
| 68 | for seg in f: |
| 69 | if seg: |
| 70 | res.append(seg) |
| 71 | else: |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 72 | if not res: raise norm_error, 'path starts with ::' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 73 | del res[len(res)-1] |
| 74 | if not (pre or res): |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 75 | raise norm_error, 'path starts with volume::' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 76 | if pre: res = pre + res |
| 77 | if post: res = res + post |
| 78 | s = res[0] |
| 79 | for seg in res[1:]: |
| 80 | s = s + ':' + seg |
| 81 | return s |
| 82 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 83 | |
| 84 | # Return true if the pathname refers to an existing directory. |
| 85 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 86 | def isdir(s): |
| 87 | try: |
| 88 | st = mac.stat(s) |
| 89 | except mac.error: |
| 90 | return 0 |
| 91 | return S_ISDIR(st[ST_MODE]) |
| 92 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 93 | |
| 94 | # Return true if the pathname refers to an existing regular file. |
| 95 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 96 | def isfile(s): |
| 97 | try: |
| 98 | st = mac.stat(s) |
| 99 | except mac.error: |
| 100 | return 0 |
| 101 | return S_ISREG(st[ST_MODE]) |
| 102 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 103 | |
| 104 | # Return true if the pathname refers to an existing file or directory. |
| 105 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 106 | def exists(s): |
| 107 | try: |
| 108 | st = mac.stat(s) |
| 109 | except mac.error: |
| 110 | return 0 |
| 111 | return 1 |