Mercurial > cpython-lru_cache-weakref
annotate Lib/posixpath.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 7b6eefac6e49 |
| children | 666152b77027 |
| rev | line source |
|---|---|
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
1 # Module 'posixpath' -- common operations on Posix pathnames. |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
2 # Some of this can actually be useful on non-Posix systems too, e.g. |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
3 # for manipulation of the pathname component of URLs. |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
4 # The "os.path" name is an alias for this module on Posix systems; |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
5 # on other systems (e.g. Mac, Windows), os.path provides the same |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
6 # operations in a manner specific to that platform, and is an alias |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
7 # to another module (e.g. macpath, ntpath). |
| 5 | 8 |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
9 import os |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
10 import stat |
| 5 | 11 |
| 12 | |
| 582 | 13 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. |
| 14 # On MS-DOS this may also turn slashes into backslashes; however, other | |
| 15 # normalizations (such as optimizing '../' away) are not allowed | |
| 16 # (another function should be defined to do that). | |
| 17 | |
| 18 def normcase(s): | |
| 19 return s | |
| 20 | |
| 21 | |
| 22 # Return wheter a path is absolute. | |
| 23 # Trivial in Posix, harder on the Mac or MS-DOS. | |
| 24 | |
| 25 def isabs(s): | |
| 26 return s[:1] == '/' | |
| 27 | |
| 28 | |
| 361 | 29 # Join two pathnames. |
| 582 | 30 # Ignore the first part if the second part is absolute. |
| 361 | 31 # Insert a '/' unless the first part is empty or already ends in '/'. |
| 582 | 32 |
| 361 | 33 def join(a, b): |
| 559 | 34 if b[:1] == '/': return b |
| 35 if a == '' or a[-1:] == '/': return a + b | |
| 361 | 36 # Note: join('x', '') returns 'x/'; is this what we want? |
| 5 | 37 return a + '/' + b |
| 38 | |
| 39 | |
|
769
f8421da4efa7
Renamed to posixpath; changed def'n of split().
Guido van Rossum <guido@python.org>
parents:
582
diff
changeset
|
40 # Split a path in head (everything up to the last '/') and tail (the |
|
3064
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
41 # rest). If the path ends in '/', tail will be empty. If there is no |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
42 # '/' in the path, head will be empty. |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
43 # Trailing '/'es are stripped from head unless it is the root. |
| 582 | 44 |
| 5 | 45 def split(p): |
|
3064
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
46 import string |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
47 i = string.rfind(p, '/') + 1 |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
48 head, tail = p[:i], p[i:] |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
49 if head and head <> '/'*len(head): |
|
769
f8421da4efa7
Renamed to posixpath; changed def'n of split().
Guido van Rossum <guido@python.org>
parents:
582
diff
changeset
|
50 while head[-1] == '/': |
|
f8421da4efa7
Renamed to posixpath; changed def'n of split().
Guido van Rossum <guido@python.org>
parents:
582
diff
changeset
|
51 head = head[:-1] |
| 5 | 52 return head, tail |
| 53 | |
| 54 | |
| 361 | 55 # Split a path in root and extension. |
|
4074
7b6eefac6e49
Correct description of splitext().
Guido van Rossum <guido@python.org>
parents:
3795
diff
changeset
|
56 # The extension is everything starting at the last dot in the last |
| 361 | 57 # pathname component; the root is everything before that. |
| 582 | 58 # It is always true that root + ext == p. |
| 59 | |
| 361 | 60 def splitext(p): |
| 61 root, ext = '', '' | |
| 62 for c in p: | |
| 559 | 63 if c == '/': |
| 361 | 64 root, ext = root + ext + c, '' |
|
2039
0701769090f4
Let extensions start at the last period after the last slash in the
Sjoerd Mullender <sjoerd@acm.org>
parents:
1746
diff
changeset
|
65 elif c == '.': |
|
0701769090f4
Let extensions start at the last period after the last slash in the
Sjoerd Mullender <sjoerd@acm.org>
parents:
1746
diff
changeset
|
66 if ext: |
|
0701769090f4
Let extensions start at the last period after the last slash in the
Sjoerd Mullender <sjoerd@acm.org>
parents:
1746
diff
changeset
|
67 root, ext = root + ext, c |
|
0701769090f4
Let extensions start at the last period after the last slash in the
Sjoerd Mullender <sjoerd@acm.org>
parents:
1746
diff
changeset
|
68 else: |
|
0701769090f4
Let extensions start at the last period after the last slash in the
Sjoerd Mullender <sjoerd@acm.org>
parents:
1746
diff
changeset
|
69 ext = c |
|
0701769090f4
Let extensions start at the last period after the last slash in the
Sjoerd Mullender <sjoerd@acm.org>
parents:
1746
diff
changeset
|
70 elif ext: |
| 361 | 71 ext = ext + c |
| 72 else: | |
| 73 root = root + c | |
| 74 return root, ext | |
| 75 | |
| 76 | |
| 2925 | 77 # Split a pathname into a drive specification and the rest of the |
| 78 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. | |
| 79 | |
| 80 def splitdrive(p): | |
| 81 return '', p | |
| 82 | |
| 83 | |
| 5 | 84 # Return the tail (basename) part of a path. |
| 582 | 85 |
| 5 | 86 def basename(p): |
| 87 return split(p)[1] | |
| 88 | |
| 89 | |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
90 # Return the head (dirname) part of a path. |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
91 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
92 def dirname(p): |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
93 return split(p)[0] |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
94 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
95 |
| 5 | 96 # Return the longest prefix of all list elements. |
| 582 | 97 |
| 5 | 98 def commonprefix(m): |
| 99 if not m: return '' | |
| 100 prefix = m[0] | |
| 101 for item in m: | |
| 102 for i in range(len(prefix)): | |
| 103 if prefix[:i+1] <> item[:i+1]: | |
| 104 prefix = prefix[:i] | |
| 559 | 105 if i == 0: return '' |
| 5 | 106 break |
| 107 return prefix | |
| 108 | |
| 109 | |
| 582 | 110 # Is a path a symbolic link? |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
111 # This will always return false on systems where os.lstat doesn't exist. |
| 582 | 112 |
| 113 def islink(path): | |
| 114 try: | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
115 st = os.lstat(path) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
116 except (os.error, AttributeError): |
| 582 | 117 return 0 |
| 118 return stat.S_ISLNK(st[stat.ST_MODE]) | |
| 119 | |
| 120 | |
| 121 # Does a path exist? | |
| 122 # This is false for dangling symbolic links. | |
| 123 | |
| 5 | 124 def exists(path): |
| 125 try: | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
126 st = os.stat(path) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
127 except os.error: |
| 5 | 128 return 0 |
| 129 return 1 | |
| 130 | |
| 131 | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
132 # Is a path a directory? |
| 582 | 133 # This follows symbolic links, so both islink() and isdir() can be true |
| 134 # for the same path. | |
| 135 | |
| 5 | 136 def isdir(path): |
| 137 try: | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
138 st = os.stat(path) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
139 except os.error: |
| 5 | 140 return 0 |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
141 return stat.S_ISDIR(st[stat.ST_MODE]) |
| 5 | 142 |
| 143 | |
|
769
f8421da4efa7
Renamed to posixpath; changed def'n of split().
Guido van Rossum <guido@python.org>
parents:
582
diff
changeset
|
144 # Is a path a regular file? |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
145 # This follows symbolic links, so both islink() and isfile() can be true |
| 582 | 146 # for the same path. |
| 147 | |
| 148 def isfile(path): | |
| 5 | 149 try: |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
150 st = os.stat(path) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
151 except os.error: |
| 5 | 152 return 0 |
| 582 | 153 return stat.S_ISREG(st[stat.ST_MODE]) |
| 5 | 154 |
| 155 | |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
156 # Are two filenames really pointing to the same file? |
| 582 | 157 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
158 def samefile(f1, f2): |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
159 s1 = os.stat(f1) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
160 s2 = os.stat(f2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
161 return samestat(s1, s2) |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
162 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
163 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
164 # Are two open files really referencing the same file? |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
165 # (Not necessarily the same file descriptor!) |
| 582 | 166 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
167 def sameopenfile(fp1, fp2): |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
168 s1 = os.fstat(fp1) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
169 s2 = os.fstat(fp2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
170 return samestat(s1, s2) |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
171 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
172 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
173 # Are two stat buffers (obtained from stat, fstat or lstat) |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
174 # describing the same file? |
| 582 | 175 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
176 def samestat(s1, s2): |
| 559 | 177 return s1[stat.ST_INO] == s2[stat.ST_INO] and \ |
|
850
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
178 s1[stat.ST_DEV] == s2[stat.ST_DEV] |
| 5 | 179 |
| 180 | |
| 181 # Is a path a mount point? | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
182 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?) |
| 582 | 183 |
| 5 | 184 def ismount(path): |
|
850
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
185 try: |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
186 s1 = os.stat(path) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
187 s2 = os.stat(join(path, '..')) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
188 except os.error: |
|
850
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
189 return 0 # It doesn't exist -- so not a mount point :-) |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
190 dev1 = s1[stat.ST_DEV] |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
191 dev2 = s2[stat.ST_DEV] |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
192 if dev1 != dev2: |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
193 return 1 # path/.. on a different device as path |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
194 ino1 = s1[stat.ST_INO] |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
195 ino2 = s2[stat.ST_INO] |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
196 if ino1 == ino2: |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
197 return 1 # path/.. is the same i-node as path |
|
872a3caeff07
fix a typo in samestat() (ST_DEV, not STD_DEV);
Guido van Rossum <guido@python.org>
parents:
769
diff
changeset
|
198 return 0 |
| 5 | 199 |
| 200 | |
| 201 # Directory tree walk. | |
| 582 | 202 # For each directory under top (including top itself, but excluding |
| 203 # '.' and '..'), func(arg, dirname, filenames) is called, where | |
| 204 # dirname is the name of the directory and filenames is the list | |
| 205 # files files (and subdirectories etc.) in the directory. | |
| 206 # The func may modify the filenames list, to implement a filter, | |
| 5 | 207 # or to impose a different order of visiting. |
| 582 | 208 |
| 5 | 209 def walk(top, func, arg): |
| 210 try: | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
211 names = os.listdir(top) |
|
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
212 except os.error: |
| 5 | 213 return |
| 214 func(arg, top, names) | |
| 215 exceptions = ('.', '..') | |
| 216 for name in names: | |
| 217 if name not in exceptions: | |
| 361 | 218 name = join(top, name) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
219 if isdir(name) and not islink(name): |
| 5 | 220 walk(name, func, arg) |
| 582 | 221 |
| 222 | |
| 223 # Expand paths beginning with '~' or '~user'. | |
| 224 # '~' means $HOME; '~user' means that user's home directory. | |
| 225 # If the path doesn't begin with '~', or if the user or $HOME is unknown, | |
| 226 # the path is returned unchanged (leaving error reporting to whatever | |
| 227 # function is called with the expanded path as argument). | |
| 228 # See also module 'glob' for expansion of *, ? and [...] in pathnames. | |
| 229 # (A function should also be defined to do full *sh-style environment | |
| 230 # variable expansion.) | |
| 231 | |
| 232 def expanduser(path): | |
| 233 if path[:1] <> '~': | |
| 234 return path | |
| 235 i, n = 1, len(path) | |
| 236 while i < n and path[i] <> '/': | |
| 237 i = i+1 | |
| 238 if i == 1: | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
239 if not os.environ.has_key('HOME'): |
| 582 | 240 return path |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
241 userhome = os.environ['HOME'] |
| 582 | 242 else: |
| 243 import pwd | |
| 244 try: | |
| 245 pwent = pwd.getpwnam(path[1:i]) | |
| 246 except KeyError: | |
| 247 return path | |
| 248 userhome = pwent[5] | |
|
3483
fed93d2d88fc
Subtlety: ~root/a should expand to /a, not //a. Everything else unchanged.
Guido van Rossum <guido@python.org>
parents:
3064
diff
changeset
|
249 if userhome[-1:] == '/': i = i+1 |
| 582 | 250 return userhome + path[i:] |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
251 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
252 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
253 # Expand paths containing shell variable substitutions. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
254 # This expands the forms $variable and ${variable} only. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
255 # Non-existant variables are left unchanged. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
256 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
257 _varprog = None |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
258 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
259 def expandvars(path): |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
260 global _varprog |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
261 if '$' not in path: |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
262 return path |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
263 if not _varprog: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
264 import regex |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
265 _varprog = regex.compile('$\([a-zA-Z0-9_]+\|{[^}]*}\)') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
266 i = 0 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
267 while 1: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
268 i = _varprog.search(path, i) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
269 if i < 0: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
270 break |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
271 name = _varprog.group(1) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
272 j = i + len(_varprog.group(0)) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
273 if name[:1] == '{' and name[-1:] == '}': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
274 name = name[1:-1] |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
275 if os.environ.has_key(name): |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
276 tail = path[j:] |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
277 path = path[:i] + os.environ[name] |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
278 i = len(path) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
279 path = path + tail |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
280 else: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
281 i = j |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
282 return path |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
283 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
284 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
285 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
286 # It should be understood that this may change the meaning of the path |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
287 # if it contains symbolic links! |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
288 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
289 def normpath(path): |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
290 import string |
|
1320
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
291 # Treat initial slashes specially |
|
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
292 slashes = '' |
|
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
293 while path[:1] == '/': |
|
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
294 slashes = slashes + '/' |
|
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
295 path = path[1:] |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
296 comps = string.splitfields(path, '/') |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
297 i = 0 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
298 while i < len(comps): |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
299 if comps[i] == '.': |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
300 del comps[i] |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
301 elif comps[i] == '..' and i > 0 and \ |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
302 comps[i-1] not in ('', '..'): |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
303 del comps[i-1:i+1] |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
304 i = i-1 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
305 elif comps[i] == '' and i > 0 and comps[i-1] <> '': |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
306 del comps[i] |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
307 else: |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
308 i = i+1 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
309 # If the path is now empty, substitute '.' |
|
1320
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
310 if not comps and not slashes: |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
311 comps.append('.') |
|
1320
907c3996b15c
* posixpath.py: Fix border cases in normpath ('/foo/..' should return '/')
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
312 return slashes + string.joinfields(comps, '/') |
