通过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.com', # '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()
# 修改用户名称 20211203 newUser = p4.run("renameuser", "--from=user001", "--to=tom")