Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """Common operations on DOS pathnames.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import stat |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 5 | |
Skip Montanaro | eccd02a | 2001-01-20 23:34:12 +0000 | [diff] [blame] | 6 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 7 | "basename","dirname","commonprefix","getsize","getmtime", |
| 8 | "getatime","islink","exists","isdir","isfile","ismount", |
| 9 | "walk","expanduser","expandvars","normpath","abspath"] |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 11 | def normcase(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 12 | """Normalize the case of a pathname. |
| 13 | On MS-DOS it maps the pathname to lowercase, turns slashes into |
| 14 | backslashes. |
| 15 | Other normalizations (such as optimizing '../' away) are not allowed |
| 16 | (this is done by normpath). |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 17 | Previously, this version mapped invalid consecutive characters to a |
| 18 | single '_', but this has been removed. This functionality should |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 19 | possibly be added as a new function.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 20 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 21 | return s.replace("/", "\\").lower() |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 23 | |
| 24 | def isabs(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 25 | """Return whether a path is absolute. |
| 26 | Trivial in Posix, harder on the Mac or MS-DOS. |
| 27 | For DOS it is absolute if it starts with a slash or backslash (current |
| 28 | volume), or if a pathname after the volume letter and colon starts with |
| 29 | a slash or backslash.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 31 | s = splitdrive(s)[1] |
| 32 | return s != '' and s[:1] in '/\\' |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | ae590db | 1997-10-07 14:48:23 +0000 | [diff] [blame] | 35 | def join(a, *p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 36 | """Join two (or more) paths.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 38 | path = a |
| 39 | for b in p: |
| 40 | if isabs(b): |
| 41 | path = b |
Tim Peters | ceeda0e | 2000-09-19 23:46:56 +0000 | [diff] [blame] | 42 | elif path == '' or path[-1:] in '/\\:': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 43 | path = path + b |
| 44 | else: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 45 | path = path + "\\" + b |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 46 | return path |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | fda5c1a | 1995-08-10 19:27:42 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 49 | def splitdrive(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 50 | """Split a path into a drive specification (a drive letter followed |
| 51 | by a colon) and path specification. |
| 52 | It is always true that drivespec + pathspec == p.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 54 | if p[1:2] == ':': |
| 55 | return p[0:2], p[2:] |
| 56 | return '', p |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 58 | |
| 59 | def split(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 60 | """Split a path into head (everything up to the last '/') and tail |
| 61 | (the rest). After the trailing '/' is stripped, the invariant |
| 62 | join(head, tail) == p holds. |
| 63 | The resulting head won't end in '/' unless it is the root.""" |
| 64 | |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 65 | d, p = splitdrive(p) |
| 66 | # set i to index beyond p's last slash |
| 67 | i = len(p) |
| 68 | while i and p[i-1] not in '/\\': |
| 69 | i = i - 1 |
| 70 | head, tail = p[:i], p[i:] # now tail has no slashes |
| 71 | # remove trailing slashes from head, unless it's all slashes |
| 72 | head2 = head |
| 73 | while head2 and head2[-1] in '/\\': |
| 74 | head2 = head2[:-1] |
| 75 | head = head2 or head |
| 76 | return d + head, tail |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 77 | |
| 78 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 79 | def splitext(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 80 | """Split a path into root and extension. |
| 81 | The extension is everything starting at the first dot in the last |
| 82 | pathname component; the root is everything before that. |
| 83 | It is always true that root + ext == p.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 85 | root, ext = '', '' |
| 86 | for c in p: |
| 87 | if c in '/\\': |
| 88 | root, ext = root + ext + c, '' |
| 89 | elif c == '.' or ext: |
| 90 | ext = ext + c |
| 91 | else: |
| 92 | root = root + c |
| 93 | return root, ext |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 95 | |
| 96 | def basename(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 97 | """Return the tail (basename) part of a path.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 99 | return split(p)[1] |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 101 | |
| 102 | def dirname(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 103 | """Return the head (dirname) part of a path.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 105 | return split(p)[0] |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 107 | |
Skip Montanaro | 97bc98a | 2000-07-12 16:55:57 +0000 | [diff] [blame] | 108 | def commonprefix(m): |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 109 | """Return the longest prefix of all list elements.""" |
| 110 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 111 | if not m: return '' |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 112 | prefix = m[0] |
| 113 | for item in m: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 114 | for i in range(len(prefix)): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 115 | if prefix[:i+1] != item[:i+1]: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 116 | prefix = prefix[:i] |
| 117 | if i == 0: return '' |
| 118 | break |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 119 | return prefix |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 120 | |
| 121 | |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 122 | # Get size, mtime, atime of files. |
| 123 | |
| 124 | def getsize(filename): |
| 125 | """Return the size of a file, reported by os.stat().""" |
| 126 | st = os.stat(filename) |
| 127 | return st[stat.ST_SIZE] |
| 128 | |
| 129 | def getmtime(filename): |
| 130 | """Return the last modification time of a file, reported by os.stat().""" |
| 131 | st = os.stat(filename) |
| 132 | return st[stat.ST_MTIME] |
| 133 | |
| 134 | def getatime(filename): |
| 135 | """Return the last access time of a file, reported by os.stat().""" |
| 136 | st = os.stat(filename) |
Guido van Rossum | 46d5651 | 2000-07-01 10:52:49 +0000 | [diff] [blame] | 137 | return st[stat.ST_ATIME] |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 138 | |
| 139 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 140 | def islink(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 141 | """Is a path a symbolic link? |
| 142 | This will always return false on systems where posix.lstat doesn't exist.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 144 | return 0 |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 146 | |
| 147 | def exists(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 148 | """Does a path exist? |
| 149 | This is false for dangling symbolic links.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 151 | try: |
| 152 | st = os.stat(path) |
| 153 | except os.error: |
| 154 | return 0 |
| 155 | return 1 |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 157 | |
| 158 | def isdir(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 159 | """Is a path a dos directory?""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 161 | try: |
| 162 | st = os.stat(path) |
| 163 | except os.error: |
| 164 | return 0 |
| 165 | return stat.S_ISDIR(st[stat.ST_MODE]) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 167 | |
| 168 | def isfile(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 169 | """Is a path a regular file?""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 171 | try: |
| 172 | st = os.stat(path) |
| 173 | except os.error: |
| 174 | return 0 |
| 175 | return stat.S_ISREG(st[stat.ST_MODE]) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 176 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 177 | |
| 178 | def ismount(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 179 | """Is a path a mount point?""" |
| 180 | # XXX This degenerates in: 'is this the root?' on DOS |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 182 | return isabs(splitdrive(path)[1]) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 184 | |
| 185 | def walk(top, func, arg): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 186 | """Directory tree walk. |
| 187 | For each directory under top (including top itself, but excluding |
| 188 | '.' and '..'), func(arg, dirname, filenames) is called, where |
| 189 | dirname is the name of the directory and filenames is the list |
| 190 | files files (and subdirectories etc.) in the directory. |
| 191 | The func may modify the filenames list, to implement a filter, |
| 192 | or to impose a different order of visiting.""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 194 | try: |
| 195 | names = os.listdir(top) |
| 196 | except os.error: |
| 197 | return |
| 198 | func(arg, top, names) |
| 199 | exceptions = ('.', '..') |
| 200 | for name in names: |
| 201 | if name not in exceptions: |
| 202 | name = join(top, name) |
| 203 | if isdir(name): |
| 204 | walk(name, func, arg) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 206 | |
| 207 | def expanduser(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 208 | """Expand paths beginning with '~' or '~user'. |
| 209 | '~' means $HOME; '~user' means that user's home directory. |
| 210 | If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 211 | the path is returned unchanged (leaving error reporting to whatever |
| 212 | function is called with the expanded path as argument). |
| 213 | See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 214 | (A function should also be defined to do full *sh-style environment |
| 215 | variable expansion.)""" |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 216 | |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 217 | if path[:1] != '~': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 218 | return path |
| 219 | i, n = 1, len(path) |
| 220 | while i < n and path[i] not in '/\\': |
| 221 | i = i+1 |
| 222 | if i == 1: |
| 223 | if not os.environ.has_key('HOME'): |
| 224 | return path |
| 225 | userhome = os.environ['HOME'] |
| 226 | else: |
| 227 | return path |
| 228 | return userhome + path[i:] |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 231 | def expandvars(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 232 | """Expand paths containing shell variable substitutions. |
| 233 | The following rules apply: |
| 234 | - no expansion within single quotes |
| 235 | - no escape character, except for '$$' which is translated into '$' |
| 236 | - ${varname} is accepted. |
| 237 | - varnames can be made out of letters, digits and the character '_'""" |
| 238 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 239 | # XXX except '^|<>='. |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 241 | if '$' not in path: |
| 242 | return path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 243 | import string |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 244 | varchars = string.ascii_letters + string.digits + "_-" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 245 | res = '' |
| 246 | index = 0 |
| 247 | pathlen = len(path) |
| 248 | while index < pathlen: |
| 249 | c = path[index] |
| 250 | if c == '\'': # no expansion within single quotes |
| 251 | path = path[index + 1:] |
| 252 | pathlen = len(path) |
| 253 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 254 | index = path.index('\'') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 255 | res = res + '\'' + path[:index + 1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 256 | except ValueError: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 257 | res = res + path |
| 258 | index = pathlen -1 |
| 259 | elif c == '$': # variable or '$$' |
| 260 | if path[index + 1:index + 2] == '$': |
| 261 | res = res + c |
| 262 | index = index + 1 |
| 263 | elif path[index + 1:index + 2] == '{': |
| 264 | path = path[index+2:] |
| 265 | pathlen = len(path) |
| 266 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 267 | index = path.index('}') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 268 | var = path[:index] |
| 269 | if os.environ.has_key(var): |
| 270 | res = res + os.environ[var] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 271 | except ValueError: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 272 | res = res + path |
| 273 | index = pathlen - 1 |
| 274 | else: |
| 275 | var = '' |
| 276 | index = index + 1 |
| 277 | c = path[index:index + 1] |
| 278 | while c != '' and c in varchars: |
| 279 | var = var + c |
| 280 | index = index + 1 |
| 281 | c = path[index:index + 1] |
| 282 | if os.environ.has_key(var): |
| 283 | res = res + os.environ[var] |
| 284 | if c != '': |
| 285 | res = res + c |
| 286 | else: |
| 287 | res = res + c |
| 288 | index = index + 1 |
| 289 | return res |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 291 | |
| 292 | def normpath(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 293 | """Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
| 294 | Also, components of the path are silently truncated to 8+3 notation.""" |
| 295 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 296 | path = path.replace("/", "\\") |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 297 | prefix, path = splitdrive(path) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 298 | while path[:1] == "\\": |
| 299 | prefix = prefix + "\\" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 300 | path = path[1:] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 301 | comps = path.split("\\") |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 302 | i = 0 |
| 303 | while i < len(comps): |
| 304 | if comps[i] == '.': |
| 305 | del comps[i] |
| 306 | elif comps[i] == '..' and i > 0 and \ |
| 307 | comps[i-1] not in ('', '..'): |
| 308 | del comps[i-1:i+1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 309 | i = i - 1 |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 310 | elif comps[i] == '' and i > 0 and comps[i-1] != '': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 311 | del comps[i] |
| 312 | elif '.' in comps[i]: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 313 | comp = comps[i].split('.') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 314 | comps[i] = comp[0][:8] + '.' + comp[1][:3] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 315 | i = i + 1 |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 316 | elif len(comps[i]) > 8: |
| 317 | comps[i] = comps[i][:8] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 318 | i = i + 1 |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 319 | else: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 320 | i = i + 1 |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 321 | # If the path is now empty, substitute '.' |
| 322 | if not prefix and not comps: |
| 323 | comps.append('.') |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 324 | return prefix + "\\".join(comps) |
Guido van Rossum | 3ed23cc | 1994-02-15 15:57:15 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 326 | |
| 327 | |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 328 | def abspath(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 329 | """Return an absolute path.""" |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 330 | if not isabs(path): |
| 331 | path = join(os.getcwd(), path) |
| 332 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 333 | |
| 334 | # realpath is a no-op on systems without islink support |
| 335 | realpath = abspath |