<jupyter_start><jupyter_text>ユーザーをグループに追加しますユーザーをグループに追加します。JupyterHub上にユーザーが作成済みである必要があります。所属グループに応じたリソース制限設定に使用します。  グループの定義 グループ名<jupyter_code>group_name = 'group1'
group_name<jupyter_output><empty_output><jupyter_text>このグループに所属させるユーザーのメールアドレス<jupyter_code>user_email_list = '''
<EMAIL>
<EMAIL>
<EMAIL>
'''
import re
user_emails = user_email_list.splitlines()
user_emails = [x.strip() for x in user_emails 
               if re.sub(r'#.*$', '', x).strip()]
user_emails
import hashlib

def get_username_from_mail_address(mail_address):
    # Convert to lower and remove characters except for alphabets and digits
    wk = mail_address.split('@')
    local_part = wk[0].lower()
    result = re.sub(r'[^a-zA-Z0-9]', '', local_part)
    # Add top 6bytes of hash string
    md5 = hashlib.md5()
    md5.update(mail_address.encode('us-ascii'))
    h = md5.hexdigest()[0:6]
    result += 'x'
    result += h;
    return result;

user_names = [get_username_from_mail_address(x) for x in user_emails]
user_names<jupyter_output><empty_output><jupyter_text>グループの設定<jupyter_code>import os
apitoken = os.environ['JUPYTERHUB_API_TOKEN']
api_url = os.environ['JUPYTERHUB_API_URL']<jupyter_output><empty_output><jupyter_text>グループの作成（必要な場合）<jupyter_code>headers = {
    'Authorization': 'token %s' % apitoken,
}
import requests
import json

r = requests.get(api_url + '/groups/{}'.format(group_name),
                 headers=headers)
if r.status_code == 404:
    r = requests.post(api_url + '/groups/{}'.format(group_name),
                      headers=headers)
    group = json.loads(r.text)
else:
    group = json.loads(r.text)

group
import json

r = requests.get(api_url + '/groups/{}'.format(group_name),
                 headers=headers)
group = json.loads(r.text)
current_users = group['users']

user2mail = dict(zip(user_names, user_emails))

new_users =  set(user_names) - set(current_users)

if new_users:
    print("Add the following users to group '{}'".format(group_name))
    for username in new_users:
        print('{}({})'.format(user2mail[username], username))
    h = headers.copy()
    h['Content-Type'] : 'application/json'
    r = requests.post(api_url + '/groups/{}/users'.format(group_name),
                      data = json.dumps({
                          "users" : list(new_users)
                      }),
                      headers=h)
    assert r.status_code == 200
member_users = set(user_names) - set(new_users)
if member_users:
    print("The fowllowing users are already member of group '{}'".format(group_name))
    for username in member_users:
        print('{}({})'.format(user2mail[username], username))<jupyter_output><empty_output><jupyter_text>確認<jupyter_code>r = requests.get(api_url + '/groups/{}'.format(group_name),
                  headers=headers)
group = json.loads(r.text)
group<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13819712381620483, 'fraction_numerical': 0.00701508242721852, 'mean_word_length': 3.9172413793103447, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '389174', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1101, 'n_words': 170, 'path': 'roles/deploy-manage-tools/files/admin-tools/83_AddUsersToGroup.ipynb', 'repo_name': 'NII-cloud-operation/CoursewareHub-LC_manage-tools', 'size': '2899'}