import ntsecuritycon import win32security DACL_PRESENT = 1 DACL_DEFAULT = 0 def WinChmod(filename, acl_list, user="SYSTEM"): """Provide chmod-like functionality for windows. Doco links: goo.gl/n7YR1 goo.gl/rDv81 goo.gl/hDobb Args: filename: target filename for acl acl_list: list of ntsecuritycon acl strings to be applied with bitwise OR. e.g. ["FILE_GENERIC_READ", "FILE_GENERIC_WRITE"] user: username string Raises: AttributeError: if a bad permission is passed RuntimeError: if filename doesn't exist """ if not os.path.exists(filename): raise RuntimeError("filename %s does not exist" % filename) acl_bitmask = int() for acl in acl_list: acl_bitmask |= getattr(ntsecuritycon, acl) dacl = win32security.ACL() win_user, _, _ = win32security.LookupAccountName("", user) dacl.AddAccessAllowedAce(win32security.ACL_REVISION, acl_bitmask, win_user) security_descriptor = win32security.GetFileSecurity( filename, win32security.DACL_SECURITY_INFORMATION) # Tell windows to set the acl and mark it as explicitly set security_descriptor.SetSecurityDescriptorDacl(DACL_PRESENT, dacl, DACL_DEFAULT) win32security.SetFileSecurity(filename, win32security.DACL_SECURITY_INFORMATION, security_descriptor)
Monday, May 13, 2013
Basic python chmod for windows
A basic python version of chmod for Windows using pywin32:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment