在腳本的同目錄下需要?jiǎng)?chuàng)建一個(gè)名為“useradd.txt”的文件,用于存放需要?jiǎng)?chuàng)建的用戶,useradd的內(nèi)容格式:賬號(hào),郵箱,密碼(注意,這3個(gè)單元之間要以英文逗號(hào)隔開),
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供柞水企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為柞水眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
舉例,我要新建一個(gè)賬號(hào)為test,郵箱為test@qq.com,密碼為12345678的用戶,useradd.txt的格式如下:
test,test@qq.com,12345678
整個(gè)代碼包含以下功能
1、創(chuàng)建用戶并賦予權(quán)限
2、刪除用戶
3、更新指定組內(nèi)的用戶權(quán)限
4、將指定組內(nèi)的部分用戶權(quán)限去除,運(yùn)行界面如下
代碼內(nèi)容如下:
#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: ') #輸入需要?jiǎng)h除的用戶的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] #需要更新權(quán)限的用戶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] #刪除指定組內(nèi)的用戶權(quán)限的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用戶并賦予權(quán)限
2. 刪除git用戶
3. 更新git用戶權(quán)限
4. 從指定的組中刪除用戶權(quán)限
%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()
當(dāng)前名稱:python批量創(chuàng)建gitlab的用戶
網(wǎng)頁(yè)URL:http://www.rwnh.cn/article6/jgpsig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、自適應(yīng)網(wǎng)站、小程序開發(fā)、建站公司、網(wǎng)站排名、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)