<s> import grp <EOL> import logging <EOL> import os <EOL> import pwd <EOL> import re <EOL> import stat <EOL> from pipes import quote <EOL> from subprocess import Popen , PIPE <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> GETFACL_PATH = "<STR_LIT>" <EOL> SETFACL_PATH = "<STR_LIT>" <EOL> ACL_FLAGS_NONE = <NUM_LIT> <EOL> ACL_FLAGS_OS_UNIX = <NUM_LIT> <EOL> ACL_FLAGS_OS_WINDOWS = <NUM_LIT> <EOL> ACL_FLAGS_TYPE_POSIX = <NUM_LIT> <EOL> ACL_FLAGS_TYPE_NFSV4 = <NUM_LIT> <EOL> ACL_WINDOWS_FILE = "<STR_LIT>" <EOL> ACL_MAC_FILE = "<STR_LIT>" <EOL> class Base_ACL_Exception ( Exception ) : <EOL> def __init__ ( self , msg = None ) : <EOL> self . value = msg <EOL> log . debug ( "<STR_LIT>" ) <EOL> if msg : <EOL> log . debug ( "<STR_LIT>" , msg ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return self . value <EOL> class Base_ACL_pipe : <EOL> def __init__ ( self , cmd ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , cmd ) <EOL> self . __pipe = Popen ( <EOL> cmd , stdin = PIPE , stdout = PIPE , stderr = PIPE , shell = True , <EOL> close_fds = True <EOL> ) <EOL> self . __stdin = self . __pipe . stdin <EOL> self . __stdout = self . __pipe . stdout <EOL> self . __stderr = self . __pipe . stderr <EOL> self . __out , err = self . __pipe . communicate ( ) <EOL> log . debug ( "<STR_LIT>" , self . __out ) <EOL> if self . __pipe . returncode != <NUM_LIT:0> : <EOL> raise Base_ACL_Exception ( err ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return self . __out <EOL> def __iter__ ( self ) : <EOL> lines = self . __out . splitlines ( ) <EOL> for line in lines : <EOL> yield line <EOL> class Base_ACL_getfacl : <EOL> def __init__ ( self , path , flags = <NUM_LIT:0> ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> path , <EOL> flags <EOL> ) <EOL> self . __getfacl = GETFACL_PATH <EOL> self . __path = path <EOL> args = self . _build_args ( path , flags ) <EOL> cmd = "<STR_LIT>" % self . __getfacl <EOL> if args : <EOL> cmd += "<STR_LIT>" % args <EOL> cmd += quote ( self . __path ) <EOL> self . __out = str ( Base_ACL_pipe ( cmd ) ) <EOL> log . debug ( "<STR_LIT>" , self . __out ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def _build_args ( self , path , flags ) : <EOL> return None <EOL> def __str__ ( self ) : <EOL> return self . __out <EOL> def __iter__ ( self ) : <EOL> lines = self . __out . splitlines ( ) <EOL> for line in lines : <EOL> yield line <EOL> class Base_ACL_setfacl ( object ) : <EOL> _entry = None <EOL> def __init__ ( self , path , entry = None , flags = <NUM_LIT:0> , pos = <NUM_LIT:0> ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> path , <EOL> entry if entry else "<STR_LIT>" , <EOL> flags <EOL> ) <EOL> self . __setfacl = SETFACL_PATH <EOL> self . __path = path <EOL> self . _entry = entry <EOL> args = self . _build_args ( path , entry , flags , pos ) <EOL> cmd = "<STR_LIT>" % self . __setfacl <EOL> if args : <EOL> cmd += "<STR_LIT>" % args <EOL> if self . _entry : <EOL> cmd += "<STR_LIT>" % self . _entry <EOL> cmd += quote ( self . __path ) <EOL> self . __out = str ( Base_ACL_pipe ( cmd ) ) <EOL> log . debug ( "<STR_LIT>" , self . __out ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def _build_args ( self , path , entry , flags , pos ) : <EOL> return None <EOL> class Base_ACL_Entry : <EOL> pass <EOL> class Base_ACL ( object ) : <EOL> @ staticmethod <EOL> def get_acl_type ( path ) : <EOL> if not path : <EOL> return <NUM_LIT:0> <EOL> if not os . access ( path , <NUM_LIT:0> ) : <EOL> return <NUM_LIT:0> <EOL> type = ACL_FLAGS_NONE <EOL> for line in Base_ACL_getfacl ( path ) : <EOL> if not line . startswith ( "<STR_LIT:#>" ) : <EOL> line = line . strip ( ) <EOL> parts = line . split ( '<STR_LIT::>' ) <EOL> type = ( ACL_FLAGS_TYPE_NFSV4 if ( len ( parts ) > <NUM_LIT:3> ) else ACL_FLAGS_TYPE_POSIX ) <EOL> break <EOL> return type <EOL> @ staticmethod <EOL> def get_acl_ostype ( path ) : <EOL> ostype = ACL_FLAGS_OS_UNIX <EOL> if os . access ( os . path . join ( path , ACL_WINDOWS_FILE ) , <NUM_LIT:0> ) : <EOL> ostype = ACL_FLAGS_OS_WINDOWS <EOL> return ostype <EOL> def __init__ ( self , path , acl = None ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> path , <EOL> acl if acl else "<STR_LIT>" <EOL> ) <EOL> self . __entries = [ ] <EOL> self . __file = None <EOL> self . __owner = None <EOL> self . __group = None <EOL> self . __dirty = False <EOL> self . __path = path <EOL> self . __flags = ACL_FLAGS_NONE <EOL> st = os . stat ( self . __path ) <EOL> self . __mode = st . st_mode <EOL> self . __flags |= self . __acl_ostype ( ) <EOL> self . __flags |= self . __acl_type ( ) <EOL> self . _load ( ) <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> self . __owner , <EOL> self . __group , <EOL> self . __flags <EOL> ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __acl_type ( self ) : <EOL> type = ACL_FLAGS_TYPE_POSIX <EOL> for line in Base_ACL_getfacl ( self . path ) : <EOL> if not line . startswith ( "<STR_LIT:#>" ) : <EOL> line = line . strip ( ) <EOL> parts = line . split ( '<STR_LIT::>' ) <EOL> type = ( ACL_FLAGS_TYPE_NFSV4 if ( len ( parts ) > <NUM_LIT:3> ) else ACL_FLAGS_TYPE_POSIX ) <EOL> break <EOL> return type <EOL> def __acl_ostype ( self ) : <EOL> ostype = ACL_FLAGS_OS_UNIX <EOL> if os . access ( os . path . join ( self . __path , ACL_WINDOWS_FILE ) , <NUM_LIT:0> ) : <EOL> ostype = ACL_FLAGS_OS_WINDOWS <EOL> return ostype <EOL> def set_entries ( self , entry ) : <EOL> if entry : <EOL> self . __entries . append ( entry ) <EOL> else : <EOL> self . __entries = entry <EOL> def get_entries ( self ) : <EOL> return self . __entries <EOL> entries = property ( get_entries , set_entries ) <EOL> def set_file ( self , file ) : <EOL> self . __file = file <EOL> def get_file ( self ) : <EOL> return self . __file <EOL> file = property ( get_file , set_file ) <EOL> def set_owner ( self , owner ) : <EOL> self . __owner = owner <EOL> def get_owner ( self ) : <EOL> return self . __owner <EOL> owner = property ( get_owner , set_owner ) <EOL> def set_group ( self , group ) : <EOL> self . __group = group <EOL> def get_group ( self ) : <EOL> return self . __group <EOL> group = property ( get_group , set_group ) <EOL> def set_unix ( self , value ) : <EOL> self . __flags = ( <EOL> ( self . __flags | ACL_FLAGS_OS_UNIX ) <EOL> if value else ( self . flags & ~ ACL_FLAGS_OS_UNIX ) <EOL> ) <EOL> def get_unix ( self ) : <EOL> return ( True if self . __flags & ACL_FLAGS_OS_UNIX else False ) <EOL> unix = property ( get_unix , set_unix ) <EOL> def set_windows ( self , value ) : <EOL> self . __flags = ( <EOL> ( self . __flags | ACL_FLAGS_OS_WINDOWS ) <EOL> if value else ( self . flags & ~ ACL_FLAGS_OS_WINDOWS ) <EOL> ) <EOL> def get_windows ( self ) : <EOL> return ( True if self . __flags & ACL_FLAGS_OS_WINDOWS else False ) <EOL> windows = property ( get_windows , set_windows ) <EOL> def set_posix ( self , value ) : <EOL> self . __flags = ( <EOL> ( self . __flags | ACL_FLAGS_TYPE_POSIX ) <EOL> if value else ( self . flags & ~ ACL_FLAGS_TYPE_POSIX ) <EOL> ) <EOL> def get_posix ( self ) : <EOL> return ( True if self . __flags & ACL_FLAGS_TYPE_POSIX else False ) <EOL> posix = property ( get_posix , set_posix ) <EOL> def set_nfsv4 ( self , value ) : <EOL> self . __flags = ( <EOL> ( self . __flags | ACL_FLAGS_TYPE_NFSV4 ) <EOL> if value else ( self . flags & ~ ACL_FLAGS_TYPE_NFSV4 ) <EOL> ) <EOL> def get_nfsv4 ( self ) : <EOL> return ( True if self . __flags & ACL_FLAGS_TYPE_NFSV4 else False ) <EOL> nfsv4 = property ( get_nfsv4 , set_nfsv4 ) <EOL> def set_flags ( self , value ) : <EOL> pass <EOL> def get_flags ( self ) : <EOL> return self . __flags <EOL> flags = property ( get_flags , set_flags ) <EOL> def set_path ( self , path ) : <EOL> self . __path = path <EOL> def get_path ( self ) : <EOL> return self . __path <EOL> path = property ( get_path , set_path ) <EOL> def set_dirty ( self , dirty ) : <EOL> self . __dirty = dirty <EOL> def get_dirty ( self ) : <EOL> return self . __dirty <EOL> dirty = property ( get_dirty , set_dirty ) <EOL> def set_mode ( self , mode ) : <EOL> pass <EOL> def get_mode ( self ) : <EOL> return self . __mode <EOL> mode = property ( get_mode , set_mode ) <EOL> def _load ( self ) : <EOL> pass <EOL> def _refresh ( self ) : <EOL> self . entries = [ ] <EOL> self . _load ( ) <EOL> def update ( self , * args , ** kwargs ) : <EOL> pass <EOL> def add ( self , * args , ** kwargs ) : <EOL> pass <EOL> def get ( self , * args , ** kwargs ) : <EOL> pass <EOL> def remove ( self , * args , ** kwargs ) : <EOL> pass <EOL> def reset ( self , * args , ** kwargs ) : <EOL> pass <EOL> def clear ( self , * args , ** kwargs ) : <EOL> pass <EOL> def chmod ( self , mode ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , mode ) <EOL> os . chmod ( self . path , int ( mode , <NUM_LIT:8> ) ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def chown ( self , who ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , who ) <EOL> if not who : <EOL> return False <EOL> user = group = None <EOL> uid = gid = - <NUM_LIT:1> <EOL> parts = who . split ( '<STR_LIT::>' ) <EOL> if parts [ <NUM_LIT:0> ] : <EOL> user = parts [ <NUM_LIT:0> ] <EOL> if len ( parts ) > <NUM_LIT:1> and parts [ <NUM_LIT:1> ] : <EOL> group = parts [ <NUM_LIT:1> ] <EOL> if user and re . match ( '<STR_LIT>' , user ) : <EOL> uid = int ( user ) <EOL> elif user : <EOL> entry = pwd . getpwnam ( user ) <EOL> uid = entry . pw_uid <EOL> if group and re . match ( '<STR_LIT>' , group ) : <EOL> gid = int ( group ) <EOL> elif group : <EOL> entry = grp . getgrnam ( group ) <EOL> gid = entry . gr_gid <EOL> os . chown ( self . path , uid , gid ) <EOL> self . _refresh ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> return True <EOL> def save ( self ) : <EOL> if not self . dirty : <EOL> return False <EOL> self . _refresh ( ) <EOL> return True <EOL> class Base_ACL_Hierarchy ( Base_ACL ) : <EOL> def __init__ ( self , path , acl = None ) : <EOL> super ( Base_ACL_Hierarchy , self ) . __init__ ( path , acl ) <EOL> self . __jobs = [ ] <EOL> def _recurse ( self , path , callback , * args , ** kwargs ) : <EOL> callback ( path , * args , ** kwargs ) <EOL> files = os . listdir ( path ) <EOL> decoded = path . decode ( '<STR_LIT:utf-8>' ) <EOL> for f in files : <EOL> file = os . path . join ( decoded , f . decode ( '<STR_LIT:utf-8>' ) ) . encode ( '<STR_LIT:utf-8>' ) <EOL> st = os . lstat ( file ) <EOL> if stat . S_ISLNK ( st . st_mode ) : <EOL> continue <EOL> if stat . S_ISDIR ( st . st_mode ) : <EOL> if self . get_acl_ostype ( path ) == ACL_FLAGS_OS_WINDOWS : <EOL> self . windows = True <EOL> self . _recurse ( file , callback , * args , ** kwargs ) <EOL> else : <EOL> if f != ACL_WINDOWS_FILE : <EOL> callback ( file , * args , ** kwargs ) <EOL> def new_ACL ( self , path ) : <EOL> return Base_ACL ( path ) <EOL> def _set_windows_file_defaults ( self , acl ) : <EOL> pass <EOL> def _set_windows_directory_defaults ( self , acl ) : <EOL> pass <EOL> def _set_unix_file_defaults ( self , acl ) : <EOL> pass <EOL> def _set_unix_directory_defaults ( self , acl ) : <EOL> pass <EOL> def __set_file_defaults ( self , acl ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , acl ) <EOL> if self . windows : <EOL> self . _set_windows_file_defaults ( acl ) <EOL> else : <EOL> self . _set_unix_file_defaults ( acl ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __set_directory_defaults ( self , acl ) : <EOL> if self . windows : <EOL> self . _set_windows_directory_defaults ( acl ) <EOL> else : <EOL> self . _set_unix_directory_defaults ( acl ) <EOL> def __set_defaults ( self , path , * args , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , path ) <EOL> acl = self . new_ACL ( path ) <EOL> if stat . S_ISREG ( acl . mode ) : <EOL> self . __set_file_defaults ( acl ) <EOL> elif stat . S_ISDIR ( acl . mode ) : <EOL> self . __set_directory_defaults ( acl ) <EOL> else : <EOL> self . __set_file_defaults ( acl ) <EOL> acl . save ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def set_defaults ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is True : <EOL> self . _recurse ( self . path , self . __set_defaults , * args , ** kwargs ) <EOL> else : <EOL> self . __set_defaults ( self . path , * args , ** kwargs ) <EOL> def __reset ( self , path , * args , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , path ) <EOL> acl = self . new_ACL ( path ) <EOL> acl . reset ( * args , ** kwargs ) <EOL> acl . save ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def reset ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is True : <EOL> self . _recurse ( self . path , self . __reset , * args , ** kwargs ) <EOL> else : <EOL> self . __reset ( self . path , * args , ** kwargs ) <EOL> def __clear ( self , path , * args , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , path ) <EOL> acl = self . new_ACL ( path ) <EOL> acl . clear ( * args , ** kwargs ) <EOL> acl . save ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def clear ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is True : <EOL> self . _recurse ( self . path , self . __clear , * args , ** kwargs ) <EOL> else : <EOL> self . __clear ( self . path , * args , ** kwargs ) <EOL> def __add ( self , path , * args , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , path ) <EOL> acl = self . new_ACL ( path ) <EOL> acl . add ( * args , ** kwargs ) <EOL> acl . save ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def add ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is True : <EOL> self . _recurse ( self . path , self . __add , * args , ** kwargs ) <EOL> else : <EOL> self . __add ( self . path , * args , ** kwargs ) <EOL> def __update ( self , path , * args , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , path ) <EOL> acl = self . new_ACL ( path ) <EOL> acl . update ( * args , ** kwargs ) <EOL> acl . save ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def update ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is True : <EOL> self . _recurse ( self . path , self . __update , * args , ** kwargs ) <EOL> else : <EOL> self . __update ( self . path , * args , ** kwargs ) <EOL> def __remove ( self , path , * args , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , path ) <EOL> acl = self . new_ACL ( path ) <EOL> acl . remove ( * args , ** kwargs ) <EOL> acl . save ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def remove ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is True : <EOL> self . _recurse ( self . path , self . __remove , * args , ** kwargs ) <EOL> else : <EOL> self . __remove ( self . path , * args , ** kwargs ) <EOL> def __chmod ( self , path , mode ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> path , <EOL> mode <EOL> ) <EOL> acl = self . new_ACL ( path ) <EOL> acl . chmod ( mode ) <EOL> acl . save ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def chmod ( self , mode , recursive = False ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , mode ) <EOL> if recursive : <EOL> self . _recurse ( self . path , self . __chmod , mode ) <EOL> else : <EOL> self . __chmod ( self . path , mode ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __chown ( self , path , who ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> path , <EOL> who <EOL> ) <EOL> acl = self . new_ACL ( path ) <EOL> acl . chown ( who ) <EOL> acl . save ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def chown ( self , who , recursive = False ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , who ) <EOL> if recursive : <EOL> self . _recurse ( self . path , self . __chown , who ) <EOL> else : <EOL> self . __chown ( self . path , who ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def close ( self ) : <EOL> self . path = None <EOL> self . flags = ACL_FLAGS_NONE <EOL> def run ( self ) : <EOL> pass </s>
