Mercurial > cpython-lru_cache-weakref
annotate Lib/dospath.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 | 11d89914c7b9 |
| children | 5f0216302cd8 |
| rev | line source |
|---|---|
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 # Module 'dospath' -- common operations on DOS pathnames |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 import os |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 import stat |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import string |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 # Normalize the case of a pathname. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 # On MS-DOS it maps the pathname to lowercase, turns slashes into |
| 2951 | 10 # backslashes. |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 # Other normalizations (such as optimizing '../' away) are not allowed |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 # (this is done by normpath). |
| 2951 | 13 # Previously, this version mapped invalid consecutive characters to a |
| 14 # single '_', but this has been removed. This functionality should | |
| 15 # possibly be added as a new function. | |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 def normcase(s): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 res, s = splitdrive(s) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 for c in s: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 if c in '/\\': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 res = res + os.sep |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 else: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 res = res + c |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 return string.lower(res) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 # Return wheter a path is absolute. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 # Trivial in Posix, harder on the Mac or MS-DOS. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 # For DOS it is absolute if it starts with a slash or backslash (current |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 # volume), or if a pathname after the volume letter and colon starts with |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 # a slash or backslash. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 def isabs(s): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 s = splitdrive(s)[1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 return s != '' and s[:1] in '/\\' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 # Join two pathnames. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 # Ignore the first part if the second part is absolute. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 # Insert a '/' unless the first part is empty or already ends in '/'. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 def join(a, b): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 if isabs(b): return b |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 if a == '' or a[-1:] in '/\\': return a + b |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 # Note: join('x', '') returns 'x/'; is this what we want? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 return a + os.sep + b |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 # Split a path in a drive specification (a drive letter followed by a |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 # colon) and the path specification. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 # It is always true that drivespec + pathspec == p |
| 2951 | 52 |
|
1549
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 def splitdrive(p): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 if p[1:2] == ':': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 return p[0:2], p[2:] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 return '', p |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 # Split a path in head (everything up to the last '/') and tail (the |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 # rest). If the original path ends in '/' but is not the root, this |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 # '/' is stripped. After the trailing '/' is stripped, the invariant |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 # join(head, tail) == p holds. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 # The resulting head won't end in '/' unless it is the root. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 def split(p): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 d, p = splitdrive(p) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 slashes = '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 while p and p[-1:] in '/\\': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 slashes = slashes + p[-1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 p = p[:-1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 if p == '': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 p = p + slashes |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 head, tail = '', '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 for c in p: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 tail = tail + c |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 if c in '/\\': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 head, tail = head + tail, '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 slashes = '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 while head and head[-1:] in '/\\': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 slashes = slashes + head[-1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 head = head[:-1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 if head == '': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 head = head + slashes |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 return d + head, tail |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 # Split a path in root and extension. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 # The extension is everything starting at the first dot in the last |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 # pathname component; the root is everything before that. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 # It is always true that root + ext == p. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 def splitext(p): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 root, ext = '', '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 for c in p: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 if c in '/\\': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 root, ext = root + ext + c, '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 elif c == '.' or ext: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 ext = ext + c |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 else: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 root = root + c |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 return root, ext |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 # Return the tail (basename) part of a path. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 def basename(p): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 return split(p)[1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 # Return the head (dirname) part of a path. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 def dirname(p): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 return split(p)[0] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 # Return the longest prefix of all list elements. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 def commonprefix(m): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 if not m: return '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 prefix = m[0] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 for item in m: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 for i in range(len(prefix)): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 if prefix[:i+1] <> item[:i+1]: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 prefix = prefix[:i] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 if i == 0: return '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 break |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 return prefix |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 # Is a path a symbolic link? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 # This will always return false on systems where posix.lstat doesn't exist. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 def islink(path): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 return false |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 # Does a path exist? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 # This is false for dangling symbolic links. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 def exists(path): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 try: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 st = os.stat(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 except os.error: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 return 0 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 return 1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 # Is a path a dos directory? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 # This follows symbolic links, so both islink() and isdir() can be true |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 # for the same path. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 def isdir(path): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 try: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 st = os.stat(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 except os.error: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 return 0 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 return stat.S_ISDIR(st[stat.ST_MODE]) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 # Is a path a regular file? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 # This follows symbolic links, so both islink() and isdir() can be true |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 # for the same path. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 def isfile(path): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 try: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 st = os.stat(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 except os.error: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 return 0 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 return stat.S_ISREG(st[stat.ST_MODE]) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 # Are two filenames really pointing to the same file? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 def samefile(f1, f2): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 s1 = os.stat(f1) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 s2 = os.stat(f2) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 return samestat(s1, s2) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 # Are two open files really referencing the same file? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 # (Not necessarily the same file descriptor!) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 # XXX THIS IS BROKEN UNDER DOS! ST_INO seems to indicate number of reads? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 def sameopenfile(fp1, fp2): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 s1 = os.fstat(fp1.fileno()) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 s2 = os.fstat(fp2.fileno()) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 return samestat(s1, s2) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 # Are two stat buffers (obtained from stat, fstat or lstat) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 # describing the same file? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 def samestat(s1, s2): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 return s1[stat.ST_INO] == s2[stat.ST_INO] and \ |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 s1[stat.ST_DEV] == s2[stat.ST_DEV] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 # Is a path a mount point? |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 # XXX This degenerates in: 'is this the root?' on DOS |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 def ismount(path): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 return isabs(splitdrive(path)[1]) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 # Directory tree walk. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 # For each directory under top (including top itself, but excluding |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 # '.' and '..'), func(arg, dirname, filenames) is called, where |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 # dirname is the name of the directory and filenames is the list |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 # files files (and subdirectories etc.) in the directory. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 # The func may modify the filenames list, to implement a filter, |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 # or to impose a different order of visiting. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 def walk(top, func, arg): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 try: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 names = os.listdir(top) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 except os.error: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 return |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 func(arg, top, names) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 exceptions = ('.', '..') |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 for name in names: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 if name not in exceptions: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 name = join(top, name) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 if isdir(name): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 walk(name, func, arg) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 # Expand paths beginning with '~' or '~user'. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 # '~' means $HOME; '~user' means that user's home directory. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 # the path is returned unchanged (leaving error reporting to whatever |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 # function is called with the expanded path as argument). |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 # (A function should also be defined to do full *sh-style environment |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 # variable expansion.) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 def expanduser(path): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 if path[:1] <> '~': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 return path |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 i, n = 1, len(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 while i < n and path[i] not in '/\\': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 i = i+1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 if i == 1: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 if not os.environ.has_key('HOME'): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 return path |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 userhome = os.environ['HOME'] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 else: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 return path |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 return userhome + path[i:] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 # Expand paths containing shell variable substitutions. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 # The following rules apply: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 # - no expansion within single quotes |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 # - no escape character, except for '$$' which is translated into '$' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 # - ${varname} is accepted. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 # - varnames can be made out of letters, digits and the character '_' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 # XXX With COMMAND.COM you can use any characters in a variable name, |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 # XXX except '^|<>='. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 varchars = string.letters + string.digits + '_-' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 def expandvars(path): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 if '$' not in path: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 return path |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 res = '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 index = 0 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 pathlen = len(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 while index < pathlen: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 c = path[index] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 if c == '\'': # no expansion within single quotes |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 path = path[index + 1:] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 pathlen = len(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 try: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 index = string.index(path, '\'') |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 res = res + '\'' + path[:index + 1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 except string.index_error: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 res = res + path |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 index = pathlen -1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 elif c == '$': # variable or '$$' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 if path[index + 1:index + 2] == '$': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 res = res + c |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 index = index + 1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 elif path[index + 1:index + 2] == '{': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 path = path[index+2:] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 pathlen = len(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 try: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 index = string.index(path, '}') |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 var = path[:index] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 if os.environ.has_key(var): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 res = res + os.environ[var] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 except string.index_error: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 res = res + path |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 index = pathlen - 1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 else: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 var = '' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 index = index + 1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 c = path[index:index + 1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 while c != '' and c in varchars: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 var = var + c |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 index = index + 1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 c = path[index:index + 1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 if os.environ.has_key(var): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 res = res + os.environ[var] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 if c != '': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 res = res + c |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 else: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 res = res + c |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 index = index + 1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 return res |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 # Also, components of the path are silently truncated to 8+3 notation. |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 def normpath(path): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 path = normcase(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 prefix, path = splitdrive(path) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 while path[:1] == os.sep: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 prefix = prefix + os.sep |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 path = path[1:] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 comps = string.splitfields(path, os.sep) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 i = 0 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 while i < len(comps): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 if comps[i] == '.': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 del comps[i] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 elif comps[i] == '..' and i > 0 and \ |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 comps[i-1] not in ('', '..'): |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 del comps[i-1:i+1] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 i = i-1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 elif comps[i] == '' and i > 0 and comps[i-1] <> '': |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 del comps[i] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 elif '.' in comps[i]: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 comp = string.splitfields(comps[i], '.') |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 comps[i] = comp[0][:8] + '.' + comp[1][:3] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 i = i+1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 elif len(comps[i]) > 8: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 comps[i] = comps[i][:8] |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 i = i+1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 else: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 i = i+1 |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 # If the path is now empty, substitute '.' |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 if not prefix and not comps: |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 comps.append('.') |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 return prefix + string.joinfields(comps, os.sep) |
|
81f26bf1178f
aifc, sunau: make rate an int; os: restruct; ospath: obsolete; rfc822:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 |
