Mercurial > cpython-lru_cache-weakref
annotate Lib/posixpath.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 2f54f7e9c2b1 |
| children | 7ab63884d68b |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
1 """Common operations on Posix pathnames. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
2 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
3 Instead of importing this module directly, import os and refer to |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
4 this module as os.path. The "os.path" name is an alias for this |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
5 module on Posix systems; on other systems (e.g. Mac, Windows), |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
6 os.path provides the same operations in a manner specific to that |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
7 platform, and is an alias to another module (e.g. macpath, ntpath). |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
8 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
9 Some of this can actually be useful on non-Posix systems too, e.g. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
10 for manipulation of the pathname component of URLs. |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
11 """ |
| 5 | 12 |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
13 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
|
14 import stat |
|
41057
98bbc93bd55b
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents:
40114
diff
changeset
|
15 import genericpath |
|
46771
b93005712e39
Add Py3k warnings to os.path.walk
Benjamin Peterson <benjamin@python.org>
parents:
44522
diff
changeset
|
16 import warnings |
|
39689
d0ed7dbaad56
- Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
35377
diff
changeset
|
17 from genericpath import * |
| 5 | 18 |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
19 __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
|
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
20 "basename","dirname","commonprefix","getsize","getmtime", |
|
35377
448c6fbff641
backport bug #1266283: add lexists to os.path.__all__
Georg Brandl <georg@python.org>
parents:
35007
diff
changeset
|
21 "getatime","getctime","islink","exists","lexists","isdir","isfile", |
|
448c6fbff641
backport bug #1266283: add lexists to os.path.__all__
Georg Brandl <georg@python.org>
parents:
35007
diff
changeset
|
22 "ismount","walk","expanduser","expandvars","normpath","abspath", |
|
27056
94b9cf4274de
Fix SF #659228, 'realpath' function missing from os.path
Neal Norwitz <nnorwitz@gmail.com>
parents:
26946
diff
changeset
|
23 "samefile","sameopenfile","samestat", |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
24 "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
25 "devnull","realpath","supports_unicode_filenames","relpath"] |
| 5 | 26 |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
27 # strings representing various path-related bits and pieces |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
28 curdir = '.' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
29 pardir = '..' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
30 extsep = '.' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
31 sep = '/' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
32 pathsep = ':' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
33 defpath = ':/bin:/usr/bin' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
34 altsep = None |
|
32302
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32077
diff
changeset
|
35 devnull = '/dev/null' |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
36 |
| 582 | 37 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. |
| 38 # On MS-DOS this may also turn slashes into backslashes; however, other | |
| 39 # normalizations (such as optimizing '../' away) are not allowed | |
| 40 # (another function should be defined to do that). | |
| 41 | |
| 42 def normcase(s): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
43 """Normalize case of pathname. Has no effect under Posix""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
44 return s |
| 582 | 45 |
| 46 | |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11881
diff
changeset
|
47 # Return whether a path is absolute. |
| 582 | 48 # Trivial in Posix, harder on the Mac or MS-DOS. |
| 49 | |
| 50 def isabs(s): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
51 """Test whether a path is absolute""" |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
52 return s.startswith('/') |
| 582 | 53 |
| 54 | |
|
5133
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
55 # Join pathnames. |
|
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
56 # Ignore the previous parts if a part is absolute. |
| 361 | 57 # Insert a '/' unless the first part is empty or already ends in '/'. |
| 582 | 58 |
|
5133
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
59 def join(a, *p): |
|
42747
9a347ba17cab
Bug #1688564: document os.path.join's absolute path behavior in the docstring.
Georg Brandl <georg@python.org>
parents:
41532
diff
changeset
|
60 """Join two or more pathname components, inserting '/' as needed. |
|
9a347ba17cab
Bug #1688564: document os.path.join's absolute path behavior in the docstring.
Georg Brandl <georg@python.org>
parents:
41532
diff
changeset
|
61 If any component is an absolute path, all previous path components |
|
9a347ba17cab
Bug #1688564: document os.path.join's absolute path behavior in the docstring.
Georg Brandl <georg@python.org>
parents:
41532
diff
changeset
|
62 will be discarded.""" |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
63 path = a |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
64 for b in p: |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
65 if b.startswith('/'): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
66 path = b |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
67 elif path == '' or path.endswith('/'): |
|
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
68 path += b |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
69 else: |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
70 path += '/' + b |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
71 return path |
| 5 | 72 |
| 73 | |
|
769
f8421da4efa7
Renamed to posixpath; changed def'n of split().
Guido van Rossum <guido@python.org>
parents:
582
diff
changeset
|
74 # 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
|
75 # 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
|
76 # '/' 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
|
77 # Trailing '/'es are stripped from head unless it is the root. |
| 582 | 78 |
| 5 | 79 def split(p): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
80 """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
| 14909 | 81 everything after the final slash. Either part may be empty.""" |
|
14908
8f59bd957952
Remove imports of string when string methods will do.
Fred Drake <fdrake@acm.org>
parents:
14121
diff
changeset
|
82 i = p.rfind('/') + 1 |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
83 head, tail = p[:i], p[i:] |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
14909
diff
changeset
|
84 if head and head != '/'*len(head): |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
85 head = head.rstrip('/') |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
86 return head, tail |
| 5 | 87 |
| 88 | |
| 361 | 89 # Split a path in root and extension. |
|
4074
7b6eefac6e49
Correct description of splitext().
Guido van Rossum <guido@python.org>
parents:
3795
diff
changeset
|
90 # The extension is everything starting at the last dot in the last |
| 361 | 91 # pathname component; the root is everything before that. |
| 582 | 92 # It is always true that root + ext == p. |
| 93 | |
| 361 | 94 def splitext(p): |
|
41057
98bbc93bd55b
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents:
40114
diff
changeset
|
95 return genericpath._splitext(p, sep, altsep, extsep) |
|
98bbc93bd55b
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents:
40114
diff
changeset
|
96 splitext.__doc__ = genericpath._splitext.__doc__ |
| 361 | 97 |
| 2925 | 98 # Split a pathname into a drive specification and the rest of the |
| 99 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. | |
| 100 | |
| 101 def splitdrive(p): | |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
102 """Split a pathname into drive and path. On Posix, drive is always |
| 14909 | 103 empty.""" |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
104 return '', p |
| 2925 | 105 |
| 106 | |
|
40114
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
107 # Return the tail (basename) part of a path, same as split(path)[1]. |
| 582 | 108 |
| 5 | 109 def basename(p): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
110 """Returns the final component of a pathname""" |
|
40114
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
111 i = p.rfind('/') + 1 |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
112 return p[i:] |
| 5 | 113 |
| 114 | |
|
40114
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
115 # Return the head (dirname) part of a path, same as split(path)[0]. |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
116 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
117 def dirname(p): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
118 """Returns the directory component of a pathname""" |
|
40114
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
119 i = p.rfind('/') + 1 |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
120 head = p[:i] |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
121 if head and head != '/'*len(head): |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
122 head = head.rstrip('/') |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
123 return head |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
124 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
125 |
| 582 | 126 # 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
|
127 # This will always return false on systems where os.lstat doesn't exist. |
| 582 | 128 |
| 129 def islink(path): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
130 """Test whether a path is a symbolic link""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
131 try: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
132 st = os.lstat(path) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
133 except (os.error, AttributeError): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
134 return False |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
135 return stat.S_ISLNK(st.st_mode) |
| 582 | 136 |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
137 # Being true for dangling symbolic links is also useful. |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
138 |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
139 def lexists(path): |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
140 """Test whether a path exists. Returns True for broken symbolic links""" |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
141 try: |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
142 st = os.lstat(path) |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
143 except os.error: |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
144 return False |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
145 return True |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
146 |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
147 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
148 # Are two filenames really pointing to the same file? |
| 582 | 149 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
150 def samefile(f1, f2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
151 """Test whether two pathnames reference the same actual file""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
152 s1 = os.stat(f1) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
153 s2 = os.stat(f2) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
154 return samestat(s1, s2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
155 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
156 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
157 # Are two open files really referencing the same file? |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
158 # (Not necessarily the same file descriptor!) |
| 582 | 159 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
160 def sameopenfile(fp1, fp2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
161 """Test whether two open file objects reference the same file""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
162 s1 = os.fstat(fp1) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
163 s2 = os.fstat(fp2) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
164 return samestat(s1, s2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
165 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
166 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
167 # Are two stat buffers (obtained from stat, fstat or lstat) |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
168 # describing the same file? |
| 582 | 169 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
170 def samestat(s1, s2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
171 """Test whether two stat buffers reference the same file""" |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
172 return s1.st_ino == s2.st_ino and \ |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
173 s1.st_dev == s2.st_dev |
| 5 | 174 |
| 175 | |
| 176 # 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
|
177 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?) |
| 582 | 178 |
| 5 | 179 def ismount(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
180 """Test whether a path is a mount point""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
181 try: |
|
44467
dce25948918d
Bug #1713: posixpath.ismount() claims symlink to a mountpoint is a mountpoint.
Christian Heimes <christian@cheimes.de>
parents:
42747
diff
changeset
|
182 s1 = os.lstat(path) |
|
dce25948918d
Bug #1713: posixpath.ismount() claims symlink to a mountpoint is a mountpoint.
Christian Heimes <christian@cheimes.de>
parents:
42747
diff
changeset
|
183 s2 = os.lstat(join(path, '..')) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
184 except os.error: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
185 return False # It doesn't exist -- so not a mount point :-) |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
186 dev1 = s1.st_dev |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
187 dev2 = s2.st_dev |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
188 if dev1 != dev2: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
189 return True # path/.. on a different device as path |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
190 ino1 = s1.st_ino |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
191 ino2 = s2.st_ino |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
192 if ino1 == ino2: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
193 return True # path/.. is the same i-node as path |
|
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
194 return False |
| 5 | 195 |
| 196 | |
| 197 # Directory tree walk. | |
| 582 | 198 # For each directory under top (including top itself, but excluding |
| 199 # '.' and '..'), func(arg, dirname, filenames) is called, where | |
| 200 # dirname is the name of the directory and filenames is the list | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
201 # of files (and subdirectories etc.) in the directory. |
| 582 | 202 # The func may modify the filenames list, to implement a filter, |
| 5 | 203 # or to impose a different order of visiting. |
| 582 | 204 |
| 5 | 205 def walk(top, func, arg): |
|
20361
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
206 """Directory tree walk with callback function. |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
207 |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
208 For each directory in the directory tree rooted at top (including top |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
209 itself, but excluding '.' and '..'), call func(arg, dirname, fnames). |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
210 dirname is the name of the directory, and fnames a list of the names of |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
211 the files and subdirectories in dirname (excluding '.' and '..'). func |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
212 may modify the fnames list in-place (e.g. via del or slice assignment), |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
213 and walk will only recurse into the subdirectories whose names remain in |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
214 fnames; this can be used to implement a filter, or to impose a specific |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
215 order of visiting. No semantics are defined for, or required of, arg, |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
216 beyond that arg is always passed to func. It can be used, e.g., to pass |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
217 a filename pattern, or a mutable object designed to accumulate |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
218 statistics. Passing None for arg is common.""" |
|
53903
a36fa2ce61b1
Merged revisions 72458 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents:
52903
diff
changeset
|
219 warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.", |
|
a36fa2ce61b1
Merged revisions 72458 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents:
52903
diff
changeset
|
220 stacklevel=2) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
221 try: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
222 names = os.listdir(top) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
223 except os.error: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
224 return |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
225 func(arg, top, names) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
226 for name in names: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
227 name = join(top, name) |
|
17549
693fa6b41dfa
In walk(), don't die when os.lstat() raises os.error, e.g. because a
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
228 try: |
|
693fa6b41dfa
In walk(), don't die when os.lstat() raises os.error, e.g. because a
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
229 st = os.lstat(name) |
|
693fa6b41dfa
In walk(), don't die when os.lstat() raises os.error, e.g. because a
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
230 except os.error: |
|
693fa6b41dfa
In walk(), don't die when os.lstat() raises os.error, e.g. because a
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
231 continue |
|
23709
3b379ae3569d
Remove another reference to stat.ST_MODE
Neal Norwitz <nnorwitz@gmail.com>
parents:
23625
diff
changeset
|
232 if stat.S_ISDIR(st.st_mode): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
233 walk(name, func, arg) |
| 582 | 234 |
| 235 | |
| 236 # Expand paths beginning with '~' or '~user'. | |
| 237 # '~' means $HOME; '~user' means that user's home directory. | |
| 238 # If the path doesn't begin with '~', or if the user or $HOME is unknown, | |
| 239 # the path is returned unchanged (leaving error reporting to whatever | |
| 240 # function is called with the expanded path as argument). | |
| 241 # See also module 'glob' for expansion of *, ? and [...] in pathnames. | |
| 242 # (A function should also be defined to do full *sh-style environment | |
| 243 # variable expansion.) | |
| 244 | |
| 245 def expanduser(path): | |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
246 """Expand ~ and ~user constructions. If user or $HOME is unknown, |
| 14909 | 247 do nothing.""" |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
248 if not path.startswith('~'): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
249 return path |
|
29540
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
250 i = path.find('/', 1) |
|
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
251 if i < 0: |
|
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
252 i = len(path) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
253 if i == 1: |
|
29540
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
254 if 'HOME' not in os.environ: |
|
25126
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
255 import pwd |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
256 userhome = pwd.getpwuid(os.getuid()).pw_dir |
|
25126
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
257 else: |
|
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
258 userhome = os.environ['HOME'] |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
259 else: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
260 import pwd |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
261 try: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
262 pwent = pwd.getpwnam(path[1:i]) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
263 except KeyError: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
264 return path |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
265 userhome = pwent.pw_dir |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46771
diff
changeset
|
266 userhome = userhome.rstrip('/') or userhome |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
267 return userhome + path[i:] |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
268 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
269 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
270 # 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
|
271 # This expands the forms $variable and ${variable} only. |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11881
diff
changeset
|
272 # Non-existent variables are left unchanged. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
273 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
274 _varprog = None |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
275 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
276 def expandvars(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
277 """Expand shell variables of form $var and ${var}. Unknown variables |
| 14909 | 278 are left unchanged.""" |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
279 global _varprog |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
280 if '$' not in path: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
281 return path |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
282 if not _varprog: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
283 import re |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
284 _varprog = re.compile(r'\$(\w+|\{[^}]*\})') |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
285 i = 0 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
286 while True: |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
287 m = _varprog.search(path, i) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
288 if not m: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
289 break |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
290 i, j = m.span(0) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
291 name = m.group(1) |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
292 if name.startswith('{') and name.endswith('}'): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
293 name = name[1:-1] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22867
diff
changeset
|
294 if name in os.environ: |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
295 tail = path[j:] |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
296 path = path[:i] + os.environ[name] |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
297 i = len(path) |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
298 path += tail |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
299 else: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
300 i = j |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
301 return path |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
302 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
303 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
304 # 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
|
305 # 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
|
306 # 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
|
307 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
308 def normpath(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
309 """Normalize path, eliminating double slashes, etc.""" |
|
58270
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
310 # Preserve unicode (if path is unicode) |
|
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
311 slash, dot = (u'/', u'.') if isinstance(path, unicode) else ('/', '.') |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
312 if path == '': |
|
58270
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
313 return dot |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
314 initial_slashes = path.startswith('/') |
|
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
315 # POSIX allows one or two initial slashes, but treats three or more |
|
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
316 # as single slash. |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16333
diff
changeset
|
317 if (initial_slashes and |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
318 path.startswith('//') and not path.startswith('///')): |
|
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
319 initial_slashes = 2 |
|
14908
8f59bd957952
Remove imports of string when string methods will do.
Fred Drake <fdrake@acm.org>
parents:
14121
diff
changeset
|
320 comps = path.split('/') |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
321 new_comps = [] |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
322 for comp in comps: |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
323 if comp in ('', '.'): |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
324 continue |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
325 if (comp != '..' or (not initial_slashes and not new_comps) or |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
326 (new_comps and new_comps[-1] == '..')): |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
327 new_comps.append(comp) |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
328 elif new_comps: |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
329 new_comps.pop() |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
330 comps = new_comps |
|
58270
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
331 path = slash.join(comps) |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
332 if initial_slashes: |
|
58270
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
333 path = slash*initial_slashes + path |
|
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
334 return path or dot |
| 10018 | 335 |
| 336 | |
| 337 def abspath(path): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
338 """Return an absolute path.""" |
| 10018 | 339 if not isabs(path): |
| 340 path = join(os.getcwd(), path) | |
| 341 return normpath(path) | |
|
19934
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
342 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
343 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
344 # Return a canonical path (i.e. the absolute location of a file on the |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
345 # filesystem). |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
346 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
347 def realpath(filename): |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
348 """Return the canonical path of the specified filename, eliminating any |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
349 symbolic links encountered in the path.""" |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
350 if isabs(filename): |
|
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
351 bits = ['/'] + filename.split('/')[1:] |
|
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
352 else: |
|
35007
de76b343d9c8
Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents:
33332
diff
changeset
|
353 bits = [''] + filename.split('/') |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
354 |
|
19934
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
355 for i in range(2, len(bits)+1): |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
356 component = join(*bits[0:i]) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
357 # Resolve symbolic links. |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
358 if islink(component): |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
359 resolved = _resolve_link(component) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
360 if resolved is None: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
361 # Infinite loop -- return original component + rest of the path |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
362 return abspath(join(*([component] + bits[i:]))) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
363 else: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
364 newpath = join(*([resolved] + bits[i:])) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
365 return realpath(newpath) |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19934
diff
changeset
|
366 |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
367 return abspath(filename) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
368 |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
369 |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
370 def _resolve_link(path): |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
371 """Internal helper function. Takes a path and follows symlinks |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32621
diff
changeset
|
372 until we either arrive at something that isn't a symlink, or |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
373 encounter a path we've seen before (meaning that there's a loop). |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
374 """ |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
375 paths_seen = [] |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
376 while islink(path): |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
377 if path in paths_seen: |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
378 # Already seen this path, so we must have a symlink loop |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
379 return None |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
380 paths_seen.append(path) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
381 # Resolve where the link points to |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
382 resolved = os.readlink(path) |
|
32901
30d86e356bf2
Use isabs() in conditional, not abspath
Andrew M. Kuchling <amk@amk.ca>
parents:
32736
diff
changeset
|
383 if not isabs(resolved): |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
384 dir = dirname(path) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
385 path = normpath(join(dir, resolved)) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
386 else: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
387 path = normpath(resolved) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
388 return path |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
389 |
|
29921
2a40ca06a22c
back out the darwin supports_unicode_filenames patch; it causes deep problems with the tests
Just van Rossum <just@letterror.com>
parents:
29810
diff
changeset
|
390 supports_unicode_filenames = False |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
391 |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
392 def relpath(path, start=curdir): |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
393 """Return a relative version of a path""" |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
394 |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
395 if not path: |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
396 raise ValueError("no path specified") |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41218
diff
changeset
|
397 |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
398 start_list = abspath(start).split(sep) |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
399 path_list = abspath(path).split(sep) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41218
diff
changeset
|
400 |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
401 # Work out how much of the filepath is shared by start and path. |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
402 i = len(commonprefix([start_list, path_list])) |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
403 |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
404 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
|
44522
4321a5abd6ce
#1742: don't raise exception on os.path.relpath("a", "a"), but return os.curdir.
Georg Brandl <georg@python.org>
parents:
44467
diff
changeset
|
405 if not rel_list: |
|
4321a5abd6ce
#1742: don't raise exception on os.path.relpath("a", "a"), but return os.curdir.
Georg Brandl <georg@python.org>
parents:
44467
diff
changeset
|
406 return curdir |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
407 return join(*rel_list) |
