ยปCore Development>Code coverage>Lib/os2emxpath.py

Python code coverage for Lib/os2emxpath.py

#countcontent
1n/a# Module 'os2emxpath' -- common operations on OS/2 pathnames
2n/a"""Common pathname manipulations, OS/2 EMX version.
3n/a
4n/aInstead of importing this module directly, import os and refer to this
5n/amodule as os.path.
6n/a"""
7n/a
8n/aimport os
9n/aimport stat
10n/afrom genericpath import *
11n/afrom ntpath import (expanduser, expandvars, isabs, islink, splitdrive,
12n/a splitext, split)
13n/a
14n/a__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
15n/a "basename","dirname","commonprefix","getsize","getmtime",
16n/a "getatime","getctime", "islink","exists","lexists","isdir","isfile",
17n/a "ismount","expanduser","expandvars","normpath","abspath",
18n/a "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
19n/a "extsep","devnull","realpath","supports_unicode_filenames"]
20n/a
21n/a# strings representing various path-related bits and pieces
22n/acurdir = '.'
23n/apardir = '..'
24n/aextsep = '.'
25n/asep = '/'
26n/aaltsep = '\\'
27n/apathsep = ';'
28n/adefpath = '.;C:\\bin'
29n/adevnull = 'nul'
30n/a
31n/a# Normalize the case of a pathname and map slashes to backslashes.
32n/a# Other normalizations (such as optimizing '../' away) are not done
33n/a# (this is done by normpath).
34n/a
35n/adef normcase(s):
36n/a """Normalize case of pathname.
37n/a
38n/a Makes all characters lowercase and all altseps into seps."""
39n/a if not isinstance(s, (bytes, str)):
40n/a raise TypeError("normcase() argument must be str or bytes, "
41n/a "not '{}'".format(s.__class__.__name__))
42n/a return s.replace('\\', '/').lower()
43n/a
44n/a
45n/a# Join two (or more) paths.
46n/a
47n/adef join(a, *p):
48n/a """Join two or more pathname components, inserting sep as needed"""
49n/a path = a
50n/a for b in p:
51n/a if isabs(b):
52n/a path = b
53n/a elif path == '' or path[-1:] in '/\\:':
54n/a path = path + b
55n/a else:
56n/a path = path + '/' + b
57n/a return path
58n/a
59n/a
60n/a# Parse UNC paths
61n/adef splitunc(p):
62n/a """Split a pathname into UNC mount point and relative path specifiers.
63n/a
64n/a Return a 2-tuple (unc, rest); either part may be empty.
65n/a If unc is not empty, it has the form '//host/mount' (or similar
66n/a using backslashes). unc+rest is always the input path.
67n/a Paths containing drive letters never have an UNC part.
68n/a """
69n/a if p[1:2] == ':':
70n/a return '', p # Drive letter present
71n/a firstTwo = p[0:2]
72n/a if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
73n/a # is a UNC path:
74n/a # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
75n/a # \\machine\mountpoint\directories...
76n/a # directory ^^^^^^^^^^^^^^^
77n/a normp = normcase(p)
78n/a index = normp.find('/', 2)
79n/a if index == -1:
80n/a ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
81n/a return ("", p)
82n/a index = normp.find('/', index + 1)
83n/a if index == -1:
84n/a index = len(p)
85n/a return p[:index], p[index:]
86n/a return '', p
87n/a
88n/a
89n/a# Return the tail (basename) part of a path.
90n/a
91n/adef basename(p):
92n/a """Returns the final component of a pathname"""
93n/a return split(p)[1]
94n/a
95n/a
96n/a# Return the head (dirname) part of a path.
97n/a
98n/adef dirname(p):
99n/a """Returns the directory component of a pathname"""
100n/a return split(p)[0]
101n/a
102n/a
103n/a# alias exists to lexists
104n/alexists = exists
105n/a
106n/a
107n/a# Is a path a directory?
108n/a
109n/a# Is a path a mount point? Either a root (with or without drive letter)
110n/a# or an UNC path with at most a / or \ after the mount point.
111n/a
112n/adef ismount(path):
113n/a """Test whether a path is a mount point (defined as root of drive)"""
114n/a unc, rest = splitunc(path)
115n/a if unc:
116n/a return rest in ("", "/", "\\")
117n/a p = splitdrive(path)[1]
118n/a return len(p) == 1 and p[0] in '/\\'
119n/a
120n/a
121n/a# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
122n/a
123n/adef normpath(path):
124n/a """Normalize path, eliminating double slashes, etc."""
125n/a path = path.replace('\\', '/')
126n/a prefix, path = splitdrive(path)
127n/a while path[:1] == '/':
128n/a prefix = prefix + '/'
129n/a path = path[1:]
130n/a comps = path.split('/')
131n/a i = 0
132n/a while i < len(comps):
133n/a if comps[i] == '.':
134n/a del comps[i]
135n/a elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
136n/a del comps[i-1:i+1]
137n/a i = i - 1
138n/a elif comps[i] == '' and i > 0 and comps[i-1] != '':
139n/a del comps[i]
140n/a else:
141n/a i = i + 1
142n/a # If the path is now empty, substitute '.'
143n/a if not prefix and not comps:
144n/a comps.append('.')
145n/a return prefix + '/'.join(comps)
146n/a
147n/a
148n/a# Return an absolute path.
149n/adef abspath(path):
150n/a """Return the absolute version of a path"""
151n/a if not isabs(path):
152n/a path = join(os.getcwd(), path)
153n/a return normpath(path)
154n/a
155n/a# realpath is a no-op on systems without islink support
156n/arealpath = abspath
157n/a
158n/asupports_unicode_filenames = False