【通过python操作perforce】

from P4 import P4, P4Exception
p4 = P4()

连接P4

def connect_P4(ip, port, username, passwd):
p4.port = ip + “:” + port
p4.user = username
p4.password = passwd
try:
p4.connect()
p4.run_login()
except P4Exception:
for e in p4.errors:
print(e)

用户的增删改查

查询用户

def select_P4_users():
try:
spec = p4.run(“users”)
print(spec)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

新建用户

参数 user为list格式如下

user = {

‘User’: ‘newuser’,

‘Email’: ‘newuser@email’,

‘FullName’: ‘New User’

}

def add_P4_user(user):
try:
p4.save_user(user, “-f”)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

删除用户user001和用户的所有客户端

delete_P4_user(“guest1”)

def delete_P4_user(username):
try:
spec = p4.run(“user”, “-y”, “-D”, username)
print(spec)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

######## 用户组的增删改查

查询用户组

def select_P4_group():
try:
spec = p4.run(“groups”)
print(spec)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

查询用户组详情

P4_group_info(“NormalUser”)

def P4_group_info(group_name):
try:
spec = p4.run(“group”, “-o”, group_name)
print(spec)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

新建用户组(修改则改变对应group的key对应的value)

参数group:

# group = {
#     'Group': 'newGroup',
#     'PasswordTimeout': 'unlimited',
#     'Timeout': '43100',
#     'Users': 'user001'
# }

def add_P4_group(group):
try:
p4.save_group(group)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

用户组用户的修改(增加/删除)

change_P4_group_user(“NormalUser”, “guest1”)

def change_P4_group_user(groupname, username):
try:
group = p4.fetch_group(groupname)
group[‘Users’].append(username) # 增加用户
group[“Users”].remove(username) # 删除用户组的用户
p4.save_group(group)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

删除用户组

delete_group(“NormalUser001”)

def delete_group(group_name):
try:
spec = p4.run(“group”, “-d”, group_name)
print(spec)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

######权限增删改查##########

查看用户权限

def P4_protects():
try:
spec = p4.run(“protects”)
print(spec)
except P4Exception:
for e in p4.errors:
print(e)
finally:
p4.disconnect()

删除或新增用户权限

delete_or_add_protect(“read user * * //…”)

def delete_or_add_protect(command):
try:
protect = p4.fetch_protect()
protect[‘Protections’].append(command) # 增加权限
protect[‘Protections’].remove(command) # 删除权限
p4.save_protect(protect)
except P4Exception:
for e in p4.errors: # Display errors
print(e)
finally:
p4.disconnect()

修改权限组

change_P4_protect(“read user * * //…”, “write user * * //…”)

def change_P4_protect(before_protect, after_protect):
try:
protect = p4.fetch_protect()
for i in protect[‘Protections’]:
if i == before_protect:
protect[‘Protections’][protect[‘Protections’].index(i)] = after_protect
p4.save_protect(protect)
except P4Exception:
for e in p4.errors: # Display errors
print(e)
finally:
p4.disconnect()

if name == ‘main’:
connect_P4(“ip”, “port”, “user”, “passwd”)
# 调用的方法 例如:select_P4_group()
select_P4_group()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值