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

Python code coverage for Lib/stat.py

#countcontent
1n/a"""Constants/functions for interpreting results of os.stat() and os.lstat().
2n/a
3n/aSuggested usage: from stat import *
4n/a"""
5n/a
6n/a# Indices for stat struct members in the tuple returned by os.stat()
7n/a
8n/aST_MODE = 0
9n/aST_INO = 1
10n/aST_DEV = 2
11n/aST_NLINK = 3
12n/aST_UID = 4
13n/aST_GID = 5
14n/aST_SIZE = 6
15n/aST_ATIME = 7
16n/aST_MTIME = 8
17n/aST_CTIME = 9
18n/a
19n/a# Extract bits from the mode
20n/a
21n/adef S_IMODE(mode):
22n/a """Return the portion of the file's mode that can be set by
23n/a os.chmod().
24n/a """
25n/a return mode & 0o7777
26n/a
27n/adef S_IFMT(mode):
28n/a """Return the portion of the file's mode that describes the
29n/a file type.
30n/a """
31n/a return mode & 0o170000
32n/a
33n/a# Constants used as S_IFMT() for various file types
34n/a# (not all are implemented on all systems)
35n/a
36n/aS_IFDIR = 0o040000 # directory
37n/aS_IFCHR = 0o020000 # character device
38n/aS_IFBLK = 0o060000 # block device
39n/aS_IFREG = 0o100000 # regular file
40n/aS_IFIFO = 0o010000 # fifo (named pipe)
41n/aS_IFLNK = 0o120000 # symbolic link
42n/aS_IFSOCK = 0o140000 # socket file
43n/a
44n/a# Functions to test for each file type
45n/a
46n/adef S_ISDIR(mode):
47n/a """Return True if mode is from a directory."""
48n/a return S_IFMT(mode) == S_IFDIR
49n/a
50n/adef S_ISCHR(mode):
51n/a """Return True if mode is from a character special device file."""
52n/a return S_IFMT(mode) == S_IFCHR
53n/a
54n/adef S_ISBLK(mode):
55n/a """Return True if mode is from a block special device file."""
56n/a return S_IFMT(mode) == S_IFBLK
57n/a
58n/adef S_ISREG(mode):
59n/a """Return True if mode is from a regular file."""
60n/a return S_IFMT(mode) == S_IFREG
61n/a
62n/adef S_ISFIFO(mode):
63n/a """Return True if mode is from a FIFO (named pipe)."""
64n/a return S_IFMT(mode) == S_IFIFO
65n/a
66n/adef S_ISLNK(mode):
67n/a """Return True if mode is from a symbolic link."""
68n/a return S_IFMT(mode) == S_IFLNK
69n/a
70n/adef S_ISSOCK(mode):
71n/a """Return True if mode is from a socket."""
72n/a return S_IFMT(mode) == S_IFSOCK
73n/a
74n/a# Names for permission bits
75n/a
76n/aS_ISUID = 0o4000 # set UID bit
77n/aS_ISGID = 0o2000 # set GID bit
78n/aS_ENFMT = S_ISGID # file locking enforcement
79n/aS_ISVTX = 0o1000 # sticky bit
80n/aS_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR
81n/aS_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR
82n/aS_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR
83n/aS_IRWXU = 0o0700 # mask for owner permissions
84n/aS_IRUSR = 0o0400 # read by owner
85n/aS_IWUSR = 0o0200 # write by owner
86n/aS_IXUSR = 0o0100 # execute by owner
87n/aS_IRWXG = 0o0070 # mask for group permissions
88n/aS_IRGRP = 0o0040 # read by group
89n/aS_IWGRP = 0o0020 # write by group
90n/aS_IXGRP = 0o0010 # execute by group
91n/aS_IRWXO = 0o0007 # mask for others (not in group) permissions
92n/aS_IROTH = 0o0004 # read by others
93n/aS_IWOTH = 0o0002 # write by others
94n/aS_IXOTH = 0o0001 # execute by others
95n/a
96n/a# Names for file flags
97n/a
98n/aUF_NODUMP = 0x00000001 # do not dump file
99n/aUF_IMMUTABLE = 0x00000002 # file may not be changed
100n/aUF_APPEND = 0x00000004 # file may only be appended to
101n/aUF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack
102n/aUF_NOUNLINK = 0x00000010 # file may not be renamed or deleted
103n/aUF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed
104n/aUF_HIDDEN = 0x00008000 # OS X: file should not be displayed
105n/aSF_ARCHIVED = 0x00010000 # file may be archived
106n/aSF_IMMUTABLE = 0x00020000 # file may not be changed
107n/aSF_APPEND = 0x00040000 # file may only be appended to
108n/aSF_NOUNLINK = 0x00100000 # file may not be renamed or deleted
109n/aSF_SNAPSHOT = 0x00200000 # file is a snapshot file
110n/a
111n/a
112n/a_filemode_table = (
113n/a ((S_IFLNK, "l"),
114n/a (S_IFREG, "-"),
115n/a (S_IFBLK, "b"),
116n/a (S_IFDIR, "d"),
117n/a (S_IFCHR, "c"),
118n/a (S_IFIFO, "p")),
119n/a
120n/a ((S_IRUSR, "r"),),
121n/a ((S_IWUSR, "w"),),
122n/a ((S_IXUSR|S_ISUID, "s"),
123n/a (S_ISUID, "S"),
124n/a (S_IXUSR, "x")),
125n/a
126n/a ((S_IRGRP, "r"),),
127n/a ((S_IWGRP, "w"),),
128n/a ((S_IXGRP|S_ISGID, "s"),
129n/a (S_ISGID, "S"),
130n/a (S_IXGRP, "x")),
131n/a
132n/a ((S_IROTH, "r"),),
133n/a ((S_IWOTH, "w"),),
134n/a ((S_IXOTH|S_ISVTX, "t"),
135n/a (S_ISVTX, "T"),
136n/a (S_IXOTH, "x"))
137n/a)
138n/a
139n/adef filemode(mode):
140n/a """Convert a file's mode to a string of the form '-rwxrwxrwx'."""
141n/a perm = []
142n/a for table in _filemode_table:
143n/a for bit, char in table:
144n/a if mode & bit == bit:
145n/a perm.append(char)
146n/a break
147n/a else:
148n/a perm.append("-")
149n/a return "".join(perm)
150n/a
151n/a
152n/a# Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s
153n/a# "st_file_attributes" member
154n/a
155n/aFILE_ATTRIBUTE_ARCHIVE = 32
156n/aFILE_ATTRIBUTE_COMPRESSED = 2048
157n/aFILE_ATTRIBUTE_DEVICE = 64
158n/aFILE_ATTRIBUTE_DIRECTORY = 16
159n/aFILE_ATTRIBUTE_ENCRYPTED = 16384
160n/aFILE_ATTRIBUTE_HIDDEN = 2
161n/aFILE_ATTRIBUTE_INTEGRITY_STREAM = 32768
162n/aFILE_ATTRIBUTE_NORMAL = 128
163n/aFILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
164n/aFILE_ATTRIBUTE_NO_SCRUB_DATA = 131072
165n/aFILE_ATTRIBUTE_OFFLINE = 4096
166n/aFILE_ATTRIBUTE_READONLY = 1
167n/aFILE_ATTRIBUTE_REPARSE_POINT = 1024
168n/aFILE_ATTRIBUTE_SPARSE_FILE = 512
169n/aFILE_ATTRIBUTE_SYSTEM = 4
170n/aFILE_ATTRIBUTE_TEMPORARY = 256
171n/aFILE_ATTRIBUTE_VIRTUAL = 65536
172n/a
173n/a
174n/a# If available, use C implementation
175n/atry:
176n/a from _stat import *
177n/aexcept ImportError:
178n/a pass