tools.py 文件源码

python
阅读 15 收藏 0 点赞 0 评论 0

项目:watcher 作者: nosmokingbandit 项目源码 文件源码
def humanUnixAttributes(mode):
    """
    Convert a Unix file attributes (or "file mode") to an unicode string.

    Original source code:
    http://cvs.savannah.gnu.org/viewcvs/coreutils/lib/filemode.c?root=coreutils

    >>> humanUnixAttributes(0644)
    u'-rw-r--r-- (644)'
    >>> humanUnixAttributes(02755)
    u'-rwxr-sr-x (2755)'
    """

    def ftypelet(mode):
        if stat.S_ISREG (mode) or not stat.S_IFMT(mode):
            return '-'
        if stat.S_ISBLK (mode): return 'b'
        if stat.S_ISCHR (mode): return 'c'
        if stat.S_ISDIR (mode): return 'd'
        if stat.S_ISFIFO(mode): return 'p'
        if stat.S_ISLNK (mode): return 'l'
        if stat.S_ISSOCK(mode): return 's'
        return '?'

    chars = [ ftypelet(mode), 'r', 'w', 'x', 'r', 'w', 'x', 'r', 'w', 'x' ]
    for i in xrange(1, 10):
        if not mode & 1 << 9 - i:
            chars[i] = '-'
    if mode & stat.S_ISUID:
        if chars[3] != 'x':
            chars[3] = 'S'
        else:
            chars[3] = 's'
    if mode & stat.S_ISGID:
        if chars[6] != 'x':
            chars[6] = 'S'
        else:
            chars[6] = 's'
    if mode & stat.S_ISVTX:
        if chars[9] != 'x':
            chars[9] = 'T'
        else:
            chars[9] = 't'
    return u"%s (%o)" % (''.join(chars), mode)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号