summaryrefslogtreecommitdiff
path: root/Lib/stat.py
diff options
context:
space:
mode:
authorGiampaolo Rodola' <g.rodola@gmail.com>2012-05-15 15:30:25 +0200
committerGiampaolo Rodola' <g.rodola@gmail.com>2012-05-15 15:30:25 +0200
commit27809e10ec7eef99eb1bb29314ba91ddb6300b55 (patch)
treeb1c9854159ef876ddfda7e1a9c722a0dae367850 /Lib/stat.py
parentfc75063925b1cf344e214177a3ddfd7ac3d961ca (diff)
downloadcpython-27809e10ec7eef99eb1bb29314ba91ddb6300b55.tar.gz
#14807: move undocumented tarfile.filemode() to stat.filemode(). Add tarfile.filemode alias with deprecation warning.
Diffstat (limited to 'Lib/stat.py')
-rw-r--r--Lib/stat.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/stat.py b/Lib/stat.py
index 0f29498b48..704adfe2e1 100644
--- a/Lib/stat.py
+++ b/Lib/stat.py
@@ -107,3 +107,43 @@ SF_IMMUTABLE = 0x00020000 # file may not be changed
SF_APPEND = 0x00040000 # file may only be appended to
SF_NOUNLINK = 0x00100000 # file may not be renamed or deleted
SF_SNAPSHOT = 0x00200000 # file is a snapshot file
+
+
+_filemode_table = (
+ ((S_IFLNK, "l"),
+ (S_IFREG, "-"),
+ (S_IFBLK, "b"),
+ (S_IFDIR, "d"),
+ (S_IFCHR, "c"),
+ (S_IFIFO, "p")),
+
+ ((S_IRUSR, "r"),),
+ ((S_IWUSR, "w"),),
+ ((S_IXUSR|S_ISUID, "s"),
+ (S_ISUID, "S"),
+ (S_IXUSR, "x")),
+
+ ((S_IRGRP, "r"),),
+ ((S_IWGRP, "w"),),
+ ((S_IXGRP|S_ISGID, "s"),
+ (S_ISGID, "S"),
+ (S_IXGRP, "x")),
+
+ ((S_IROTH, "r"),),
+ ((S_IWOTH, "w"),),
+ ((S_IXOTH|S_ISVTX, "t"),
+ (S_ISVTX, "T"),
+ (S_IXOTH, "x"))
+)
+
+def filemode(mode):
+ """Convert a file's mode to a string of the form '-rwxrwxrwx'."""
+ perm = []
+ for table in _filemode_table:
+ for bit, char in table:
+ if mode & bit == bit:
+ perm.append(char)
+ break
+ else:
+ perm.append("-")
+ return "".join(perm)