convert a symbolic permission string





9
Date Submitted Fri. Oct. 13th, 2006 8:03 AM
Revision 1 of 1
Scripter bertheymans
Tags chmod | linux | python | system | unix
Comments 0 comments
This python class converts a permission mask in symbolic format (the kind a diretcory listing produces, ex.: drwxr-xr-x) to a string that's usable in a chmod command.

ex.: chmod u=rwx,g=rwx,o=rwtx ~/myscripts/convert_mask.py

This can be very useful when recovering permissions from a backup.

"""
converts a permission mask in symbolic format (ex.: drwxr-xr-x) to a
string that's usable in a chmod command

ex.: chmod u=rwx,g=,o= ~/myscripts/convert_mask.py

Created by Bert Heymans on 2006-10-12. This code is public domain.
"
""
class SymbolicPermission:
       
        def __init__(self, symbolicString):
        """
                        split up into user, group, other
                "
""
                self.user = symbolicString[1:4]
                self.group = symbolicString[4:7]
                self.other = symbolicString[7:10]
                pass
       
        def toUsableString(self):
        """
                        format to a usable string, that can be used in a chmod command
                        ex.: u=rwx,g=,o=
                "
""
                def replaceAll(perm):
                        perm = perm.replace('-','')
                        perm = perm.replace('s','sx')
                        perm = perm.replace('t','tx')
                        # don't reverse order of S and s replacement (obvious)
                        perm = perm.replace('S','s')
                        perm = perm.replace('T','t')
                        return perm
                self.user = replaceAll(self.user)
                self.group = replaceAll(self.group)
                self.other = replaceAll(self.other)
                return "u=%s,g=%s,o=%s" % (self.user, self.group, self.other)

 

Bert Heymans

blog.heymans.org

Comments

There are currently no comments for this snippet.

Voting