Beispiel #1
0
def _ensure_metadata(path,
                     user,
                     group,
                     mode=None,
                     cd_access=None,
                     recursive_ownership=False,
                     recursive_mode_flags=None,
                     recursion_follow_links=False,
                     safemode_folders=[]):
    user_entity = group_entity = None
    _user_entity = _group_entity = None

    if user or group:
        stat = sudo.stat(path)

    if user:
        try:
            _user_entity = pwd.getpwnam(user)
        except KeyError:
            raise Fail("User '{0}' doesn't exist".format(user))

        if stat.st_uid != _user_entity.pw_uid:
            user_entity = _user_entity
            Logger.info("Changing owner for %s from %d to %s" %
                        (path, stat.st_uid, user))

    if group:
        try:
            _group_entity = grp.getgrnam(group)
        except KeyError:
            raise Fail("Group '{0}' doesn't exist".format(group))

        if stat.st_gid != _group_entity.gr_gid:
            group_entity = _group_entity
            Logger.info("Changing group for %s from %d to %s" %
                        (path, stat.st_gid, group))

    if recursive_ownership:
        assert_not_safemode_folder(path, safemode_folders)
        sudo.chown_recursive(path, _user_entity, _group_entity,
                             recursion_follow_links)

    sudo.chown(path, user_entity, group_entity)

    if recursive_mode_flags:
        if not isinstance(recursive_mode_flags, dict):
            raise Fail(
                "'recursion_follow_links' value should be a dictionary with 'f' and(or) 'd' key (for file and directory permission flags)"
            )

        regexp_to_match = "^({0},)*({0})$".format("[ugoa]+[+=-][rwx]+")
        for key, flags in recursive_mode_flags.iteritems():
            if key != 'd' and key != 'f':
                raise Fail(
                    "'recursive_mode_flags' with value '%s' has unknown key '%s', only keys 'f' and 'd' are valid"
                    % (str(recursive_mode_flags), str(key)))

            if not re.match(regexp_to_match, flags):
                raise Fail(
                    "'recursive_mode_flags' found '%s', but should value format have the following format: [ugoa...][[+-=][perms...]...]."
                    % (str(flags)))

        assert_not_safemode_folder(path, safemode_folders)
        sudo.chmod_recursive(path, recursive_mode_flags,
                             recursion_follow_links)

    if mode:
        stat = sudo.stat(path)
        if stat.st_mode != mode:
            Logger.info("Changing permission for %s from %o to %o" %
                        (path, stat.st_mode, mode))
            sudo.chmod(path, mode)

    if cd_access:
        if not re.match("^[ugoa]+$", cd_access):
            raise Fail("'cd_acess' value '%s' is not valid" % (cd_access))

        dir_path = re.sub('/+', '/', path)
        while dir_path != os.sep:
            if sudo.path_isdir(dir_path):
                sudo.chmod_extended(dir_path, cd_access + "+rx")

            dir_path = os.path.split(dir_path)[0]
Beispiel #2
0
def _ensure_metadata(path, user, group, mode=None, cd_access=None, recursive_ownership=False, recursive_mode_flags=None, recursion_follow_links=False, safemode_folders=[]):
  user_entity = group_entity = None
  _user_entity = _group_entity = None

  if user or group:
    stat = sudo.stat(path)

  if user:
    try:
      _user_entity = pwd.getpwnam(user)
    except KeyError:
      raise Fail("User '{0}' doesn't exist".format(user))
    
    if stat.st_uid != _user_entity.pw_uid:
      user_entity = _user_entity
      Logger.info(
        "Changing owner for %s from %d to %s" % (path, stat.st_uid, user))
      
  if group:
    try:
      _group_entity = grp.getgrnam(group)
    except KeyError:
      raise Fail("Group '{0}' doesn't exist".format(group))
    
    if stat.st_gid != _group_entity.gr_gid:
      group_entity = _group_entity
      Logger.info(
        "Changing group for %s from %d to %s" % (path, stat.st_gid, group))
  
  if recursive_ownership:
    assert_not_safemode_folder(path, safemode_folders)
    sudo.chown_recursive(path, _user_entity, _group_entity, recursion_follow_links)
  
  sudo.chown(path, user_entity, group_entity)
  
  if recursive_mode_flags:
    if not isinstance(recursive_mode_flags, dict):
      raise Fail("'recursion_follow_links' value should be a dictionary with 'f' and(or) 'd' key (for file and directory permission flags)")
    
    regexp_to_match = "^({0},)*({0})$".format("[ugoa]+[+=-][rwx]+" )
    for key, flags in recursive_mode_flags.iteritems():
      if key != 'd' and key != 'f':
        raise Fail("'recursive_mode_flags' with value '%s' has unknown key '%s', only keys 'f' and 'd' are valid" % (str(recursive_mode_flags), str(key)))
          
      if not re.match(regexp_to_match, flags):
        raise Fail("'recursive_mode_flags' found '%s', but should value format have the following format: [ugoa...][[+-=][perms...]...]." % (str(flags)))
    
    assert_not_safemode_folder(path, safemode_folders)
    sudo.chmod_recursive(path, recursive_mode_flags, recursion_follow_links)

  if mode:
    stat = sudo.stat(path)
    if stat.st_mode != mode:
      Logger.info("Changing permission for %s from %o to %o" % (
      path, stat.st_mode, mode))
      sudo.chmod(path, mode)
      
  if cd_access:
    if not re.match("^[ugoa]+$", cd_access):
      raise Fail("'cd_acess' value '%s' is not valid" % (cd_access))
    
    dir_path = re.sub('/+', '/', path)
    while dir_path != os.sep:
      if sudo.path_isdir(dir_path):
        sudo.chmod_extended(dir_path, cd_access+"+rx")
        
      dir_path = os.path.split(dir_path)[0]