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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
13 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
14 # On MS-DOS this may also turn slashes into backslashes; however, other
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
15 # normalizations (such as optimizing '../' away) are not allowed
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
16 # (another function should be defined to do that).
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
17
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
18 def normcase(s):
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
19 return s
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
20
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
21
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
22 # Return wheter a path is absolute.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
23 # Trivial in Posix, harder on the Mac or MS-DOS.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
24
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
25 def isabs(s):
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
26 return s[:1] == '/'
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
27
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
31 # Insert a '/' unless the first part is empty or already ends in '/'.
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
49
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 return head, tail
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
361
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
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
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
62 # pathname component; the root is everything before that.
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
63 # It is always true that root + ext == p.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
64
361
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
65 def splitext(p):
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
66 root, ext = '', ''
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
67 for c in p:
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 528
diff changeset
68 if c == '/':
361
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
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
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
76 ext = ext + c
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
77 else:
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
78 root = root + c
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
79 return root, ext
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
80
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
81
2925
5eb8bde300b7 add splitdrive()
Guido van Rossum <guido@python.org>
parents: 2039
diff changeset
82 # Split a pathname into a drive specification and the rest of the
5eb8bde300b7 add splitdrive()
Guido van Rossum <guido@python.org>
parents: 2039
diff changeset
83 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
5eb8bde300b7 add splitdrive()
Guido van Rossum <guido@python.org>
parents: 2039
diff changeset
84
5eb8bde300b7 add splitdrive()
Guido van Rossum <guido@python.org>
parents: 2039
diff changeset
85 def splitdrive(p):
5eb8bde300b7 add splitdrive()
Guido van Rossum <guido@python.org>
parents: 2039
diff changeset
86 return '', p
5eb8bde300b7 add splitdrive()
Guido van Rossum <guido@python.org>
parents: 2039
diff changeset
87
5eb8bde300b7 add splitdrive()
Guido van Rossum <guido@python.org>
parents: 2039
diff changeset
88
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 # Return the tail (basename) part of a path.
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
90
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 def basename(p):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 return split(p)[1]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 # Return the longest prefix of all list elements.
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
102
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def commonprefix(m):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 if not m: return ''
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 prefix = m[0]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 for item in m:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 for i in range(len(prefix)):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 if prefix[:i+1] <> item[:i+1]:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 prefix = prefix[:i]
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 528
diff changeset
110 if i == 0: return ''
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 break
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 return prefix
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
117
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
118 def islink(path):
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
122 return 0
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
123 return stat.S_ISLNK(st[stat.ST_MODE])
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
124
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
125
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
126 # Does a path exist?
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
127 # This is false for dangling symbolic links.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
128
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 def exists(path):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 return 0
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 return 1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
138 # This follows symbolic links, so both islink() and isdir() can be true
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
139 # for the same path.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
140
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 def isdir(path):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
151 # for the same path.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
152
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
153 def isfile(path):
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 return 0
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
158 return stat.S_ISREG(st[stat.ST_MODE])
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
180
428
3c7b7caa9da5 Added samefile() function.
Guido van Rossum <guido@python.org>
parents: 361
diff changeset
181 def samestat(s1, s2):
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 528
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
188
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 # Directory tree walk.
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
207 # For each directory under top (including top itself, but excluding
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
208 # '.' and '..'), func(arg, dirname, filenames) is called, where
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
209 # dirname is the name of the directory and filenames is the list
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
210 # files files (and subdirectories etc.) in the directory.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
211 # The func may modify the filenames list, to implement a filter,
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 # or to impose a different order of visiting.
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
213
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 def walk(top, func, arg):
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 return
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 func(arg, top, names)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 exceptions = ('.', '..')
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 for name in names:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 if name not in exceptions:
361
c8584a1ba38a path.cat --> join
Guido van Rossum <guido@python.org>
parents: 21
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 walk(name, func, arg)
582
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
226
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
227
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
228 # Expand paths beginning with '~' or '~user'.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
229 # '~' means $HOME; '~user' means that user's home directory.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
230 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
231 # the path is returned unchanged (leaving error reporting to whatever
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
232 # function is called with the expanded path as argument).
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
233 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
234 # (A function should also be defined to do full *sh-style environment
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
235 # variable expansion.)
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
236
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
237 def expanduser(path):
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
238 if path[:1] <> '~':
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
239 return path
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
240 i, n = 1, len(path)
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
241 while i < n and path[i] <> '/':
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
242 i = i+1
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
247 else:
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
248 import pwd
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
249 try:
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
250 pwent = pwd.getpwnam(path[1:i])
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
251 except KeyError:
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
252 return path
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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
5f71f166079d Make nicer comments.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
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, '/')