]>
Commit | Line | Data |
---|---|---|
3257aa99 DM |
1 | """\r |
2 | Path operations common to more than one OS\r | |
3 | Do not use directly. The OS specific modules import the appropriate\r | |
4 | functions from this module themselves.\r | |
5 | """\r | |
6 | import os\r | |
7 | import stat\r | |
8 | \r | |
9 | __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',\r | |
10 | 'getsize', 'isdir', 'isfile']\r | |
11 | \r | |
12 | \r | |
13 | try:\r | |
14 | _unicode = unicode\r | |
15 | except NameError:\r | |
16 | # If Python is built without Unicode support, the unicode type\r | |
17 | # will not exist. Fake one.\r | |
18 | class _unicode(object):\r | |
19 | pass\r | |
20 | \r | |
21 | # Does a path exist?\r | |
22 | # This is false for dangling symbolic links on systems that support them.\r | |
23 | def exists(path):\r | |
24 | """Test whether a path exists. Returns False for broken symbolic links"""\r | |
25 | try:\r | |
26 | os.stat(path)\r | |
27 | except os.error:\r | |
28 | return False\r | |
29 | return True\r | |
30 | \r | |
31 | \r | |
32 | # This follows symbolic links, so both islink() and isdir() can be true\r | |
33 | # for the same path on systems that support symlinks\r | |
34 | def isfile(path):\r | |
35 | """Test whether a path is a regular file"""\r | |
36 | try:\r | |
37 | st = os.stat(path)\r | |
38 | except os.error:\r | |
39 | return False\r | |
40 | return stat.S_ISREG(st.st_mode)\r | |
41 | \r | |
42 | \r | |
43 | # Is a path a directory?\r | |
44 | # This follows symbolic links, so both islink() and isdir()\r | |
45 | # can be true for the same path on systems that support symlinks\r | |
46 | def isdir(s):\r | |
47 | """Return true if the pathname refers to an existing directory."""\r | |
48 | try:\r | |
49 | st = os.stat(s)\r | |
50 | except os.error:\r | |
51 | return False\r | |
52 | return stat.S_ISDIR(st.st_mode)\r | |
53 | \r | |
54 | \r | |
55 | def getsize(filename):\r | |
56 | """Return the size of a file, reported by os.stat()."""\r | |
57 | return os.stat(filename).st_size\r | |
58 | \r | |
59 | \r | |
60 | def getmtime(filename):\r | |
61 | """Return the last modification time of a file, reported by os.stat()."""\r | |
62 | return os.stat(filename).st_mtime\r | |
63 | \r | |
64 | \r | |
65 | def getatime(filename):\r | |
66 | """Return the last access time of a file, reported by os.stat()."""\r | |
67 | return os.stat(filename).st_atime\r | |
68 | \r | |
69 | \r | |
70 | def getctime(filename):\r | |
71 | """Return the metadata change time of a file, reported by os.stat()."""\r | |
72 | return os.stat(filename).st_ctime\r | |
73 | \r | |
74 | \r | |
75 | # Return the longest prefix of all list elements.\r | |
76 | def commonprefix(m):\r | |
77 | "Given a list of pathnames, returns the longest common leading component"\r | |
78 | if not m: return ''\r | |
79 | s1 = min(m)\r | |
80 | s2 = max(m)\r | |
81 | for i, c in enumerate(s1):\r | |
82 | if c != s2[i]:\r | |
83 | return s1[:i]\r | |
84 | return s1\r | |
85 | \r | |
86 | # Split a path in root and extension.\r | |
87 | # The extension is everything starting at the last dot in the last\r | |
88 | # pathname component; the root is everything before that.\r | |
89 | # It is always true that root + ext == p.\r | |
90 | \r | |
91 | # Generic implementation of splitext, to be parametrized with\r | |
92 | # the separators\r | |
93 | def _splitext(p, sep, altsep, extsep):\r | |
94 | """Split the extension from a pathname.\r | |
95 | \r | |
96 | Extension is everything from the last dot to the end, ignoring\r | |
97 | leading dots. Returns "(root, ext)"; ext may be empty."""\r | |
98 | \r | |
99 | sepIndex = p.rfind(sep)\r | |
100 | if altsep:\r | |
101 | altsepIndex = p.rfind(altsep)\r | |
102 | sepIndex = max(sepIndex, altsepIndex)\r | |
103 | \r | |
104 | dotIndex = p.rfind(extsep)\r | |
105 | if dotIndex > sepIndex:\r | |
106 | # skip all leading dots\r | |
107 | filenameIndex = sepIndex + 1\r | |
108 | while filenameIndex < dotIndex:\r | |
109 | if p[filenameIndex] != extsep:\r | |
110 | return p[:dotIndex], p[dotIndex:]\r | |
111 | filenameIndex += 1\r | |
112 | \r | |
113 | return p, ''\r |