def copy_groups(groups, source, target, target_owner=None,
                work_dir=tempfile.gettempdir()):
    """ Copy group from the source portal to the target portal."""
    if not target.is_logged_in():
        raise PortalError('Must be logged into target portal to copy')

    # Create the temporary directory to use for copying groups (required
    # for thumbnails)
    copy_dir = tempfile.mkdtemp(prefix='copy_groups_', dir=work_dir)

    # Loop over each of the groups, copying one at a time
    copied_groups = dict()
    try:
        for group in groups:
            groupid = group['id']
            group_dir = os.path.join(copy_dir, groupid)
            os.makedirs(group_dir)

            # Create a new groups with the subset of properties we want to
            # copy to the target portal. Handle switching between org and
            # public access when going from an org in a multitenant portal
            # and a single tenant portal
            target_group = _select_properties(group, GROUP_COPY_PROPERTIES)
            if target_group['access'] == 'org'\
            and not target.is_multitenant():
                target_group['access'] = 'public'
            elif target_group['access'] == 'public'\
                 and not source.is_multitenant()\
                 and target.is_multitenant() and target.is_org():
                target_group['access'] = 'org'

            # Handle the thumbnail (if one exists)
            thumbnail_file = None
            if group in 'thumbnail':
                thumbnail_file = source.group_thumbnaild(
                    groupid, group_dir, group['thumbnail'])

            # Create the group in the target portal
            target_groupid = target.create_group(target_group, thumbnail_file)

            # If the group was created successfully, handling reassigning
            # the group (if the target_owner is specified and it's
            # different from the logged in user of the target portal
            if target_groupid:
                target_username = target.logged_in_user()['username']
                if target_owner and (target_owner != target_username):
                    target.reassign_group(target_groupid, target_owner)
                    target.leave_group(target_groupid)
                copied_groups[groupid] = target_groupid
                _log.info('Copied group ' + groupid + ' in source portal '
                          + 'to ' + target_groupid + ' in target portal')
            else:
                _log.warning('Group ' + groupid + ' was not copied '\
                             + 'to target portal')

            # Clean up the group directories as we go
            if clean_temp_files:
                shutil.rmtree(group_dir)

    # Make sure we clean up the whole copy folder
    finally:
        if clean_temp_files:
            shutil.rmtree(copy_dir)

    return copied_groups
dataset/ETHPy150Open Esri/ops-server-config/Publish/Portal/portalpy/provision.py/copy_groups/swapped operands of `in`