

def chgroups(name, groups, append=False):
    "\n    Change the groups to which the user belongs. Note that the user's primary\n    group does not have to be one of the groups passed, membership in the\n    user's primary group is automatically assumed.\n\n    groups\n        Groups to which the user should belong, can be passed either as a\n        python list or a comma-separated string\n\n    append\n        Instead of removing user from groups not included in the ``groups``\n        parameter, just add user to any groups for which they are not members\n\n    CLI Example:\n\n    .. code-block:: bash\n\n        salt '*' user.chgroups foo wheel,root\n    "
    uinfo = info(name)
    if (not uinfo):
        raise CommandExecutionError("User '{0}' does not exist".format(name))
    if isinstance(groups, string_types):
        groups = groups.split(',')
    bad_groups = [x for x in groups if salt.utils.contains_whitespace(x)]
    if bad_groups:
        raise SaltInvocationError('Invalid group name(s): {0}'.format(', '.join(bad_groups)))
    ugrps = set(list_groups(name))
    desired = set((str(x) for x in groups if bool(str(group))))
    primary_group = __salt__['file.gid_to_group'](uinfo['gid'])
    if primary_group:
        desired.add(primary_group)
    if (ugrps == desired):
        return True
    for group in (desired - ugrps):
        _dscl(['/Groups/{0}'.format(group), 'GroupMembership', name], ctype='append')
    if (not append):
        for group in (ugrps - desired):
            _dscl(['/Groups/{0}'.format(group), 'GroupMembership', name], ctype='delete')
    time.sleep(1)
    return (set(list_groups(name)) == desired)

dataset/ETHPy150Open saltstack/salt/salt/modules/mac_user.py chgroups/VarMisuse@134/420 `x`->`group`