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