python批量创建gitlab的用户

在脚本的同目录下需要创建一个名为“useradd.txt”的文件,用于存放需要创建的用户,useradd的内容格式:账号,邮箱,密码(注意,这3个单元之间要以英文逗号隔开),

成都创新互联公司是一家专业提供柞水企业网站建设,专注与成都网站制作、做网站、外贸营销网站建设H5开发、小程序制作等业务。10年已为柞水众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

举例,我要新建一个账号为test,邮箱为test@qq.com,密码为12345678的用户,useradd.txt的格式如下:

test,test@qq.com,12345678

整个代码包含以下功能

1、创建用户并赋予权限

2、删除用户

3、更新指定组内的用户权限

4、将指定组内的部分用户权限去除,运行界面如下

python批量创建gitlab的用户

代码内容如下:

#C:\Python27
# coding=utf-8
import gitlab,re

def add(gl):
    gid = int(raw_input('Group ID: '))
    group = gl.groups.get(gid)
    print group.name
    mkey = []
    path = 'useradd.txt'
    with open(path,'r') as f:
        for fnun in f.readlines():
            fkey = fnun.strip()
            name = fkey.split(',')[0]
            mail = fkey.split(',')[1]
            username = mail.split('@')[0]
            passwd = fkey.split(',')[2]
            user = gl.users.create({'email':mail,'password':passwd,'username':username ,'name':name})
            print user.username,user.id
            mkey.append(user.id)
    for i in mkey:
        member = group.members.create({'user_id':i,'access_level':30})
        print member.username,member.access_level

######del user#######
def deluser(gl):
    #cancel = [153, 151, 149, 147, 91, 51, 49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 17, 15]
    num = raw_input('Inout the user_id: ')                  #输入需要删除的用户的ID
    cancel = []
    cancel.append(num)
    for m in cancel:
        user = gl.users.get(m)
        uname = user.name
        print 'Useri_id:%s , name:%s  has been erased' % (m,uname)
        gl.users.delete(m)
######add user to group#######
def update(gl):
    gid = int(raw_input('Group ID: '))
    group = gl.groups.get(gid)
    print group.name
    fnum = [139, 137, 127, 125, 123, 121, 117, 115, 113, 109, 105, 101, 97, 93, 87, 79, 63, 61, 59]     #需要更新权限的用户ID
    for i in fnum:
        member = group.members.create({'user_id':i,'access_level':30})
        print member.username,member.access_level
######remove user from group#######
def remove(gl):
    cancel = [51, 49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 17, 15]     #删除指定组内的用户权限的ID

    gid = int(raw_input('Group ID: '))
    group = gl.groups.get(gid)
    print group.name
    #num = raw_input('Inout the user_id: ')
    #cancel = []
    #cancel.append(num)
    for m in cancel:
        user = gl.users.get(m)
        uname = user.name
        print 'Useri_id:%s , name:%s  will be removed' % (m,uname)
        member = group.members.delete(m)
    mem = group.members.list(all=True)
    for me in mem:
        print me.id,me.username
def main():
    print '''%s
1. 增加git用户并赋予权限
2. 删除git用户
3. 更新git用户权限
4. 从指定的组中删除用户权限
%s ''' % ('-'*20,'-'*20)
    gl = gitlab.Gitlab('http://10.50.36.33/', private_token='ffffffffffffffffj')
    info = {1:'add(gl)',2:'deluser(gl)',3:'update(gl)',4:'remove(gl)'}
    num = int(raw_input('Input your choice: '))
    exec info[num]
if __name__ == '__main__':
    main()


文章名称:python批量创建gitlab的用户
网站地址:http://myzitong.com/article/jgpsig.html