Skip to content

Instantly share code, notes, and snippets.

@k0001
Created October 15, 2008 03:26
Show Gist options
  • Save k0001/16849 to your computer and use it in GitHub Desktop.
Save k0001/16849 to your computer and use it in GitHub Desktop.
import stat
def chmodize(st_mode):
"""Return the string representation of some file's permissions
matching the ``st_mode`` integer value
"""
st_mode = int(st_mode)
# first
if stat.S_ISREG(st_mode): x = '-'
elif stat.S_ISDIR(st_mode): x = 'd'
elif stat.S_ISLNK(st_mode): x = 'l'
elif stat.S_ISBLK(st_mode): x = 'b'
elif stat.S_ISCHR(st_mode): x = 'c'
elif stat.S_ISFIFO(st_mode): x = 'p'
elif stat.S_ISSOCK(st_mode): x = 's'
# second & third
x +=(stat.S_IRUSR & st_mode and 'r' or '-') +\
(stat.S_IWUSR & st_mode and 'w' or '-')
# fourth
if stat.S_ISUID & st_mode: x += 's'
elif stat.S_IXUSR & st_mode: x += 'x'
else: x += '-'
# fifth & sixth
x +=(st_mode & stat.S_IRGRP and 'r' or '-') +\
(st_mode & stat.S_IWGRP and 'w' or '-')
# seventh
if stat.S_ISGID & st_mode: x += 's'
elif stat.S_IXGRP & st_mode: x += 'x'
else: x += '-'
# eigth, nineth & tenth
x +=(st_mode & stat.S_IROTH and 'r' or '-') +\
(st_mode & stat.S_IWOTH and 'w' or '-') +\
(st_mode & stat.S_IXOTH and 'x' or '-')
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment