python 远程操作svn
SVN操作脚本
安装模块
pip install pywinrm
脚本如下
#!/usr/bin/env python3
# coding=utf-8
# author:LJX
# describe:仓库授权
# createdate:2021.5.27
import winrm
import json
class SVN(object):
'''
# 1、查看winrm服务状态。默认没有启动 winrm enumerate winrm/config/listener
# 2、启动服务器(网络类型改为专有网络) winrm quickconfig
# 3、开启防火墙 netsh advfirewall firewall set rule group="Windows 远程管理" new enable=yes
# 4、启动 winrm quickconfig
# 5、查看 winrm enumerate winrm/config/listener
# 5、为winrm service 配置auth winrm set winrm/config/service/auth @{Basic="true"}
# 7、为winrm service 配置加密方式为允许非加密 winrm set winrm/config/service @{AllowUnencrypted="true"}
'''
def __init__(self, host, port, user, passwd):
self.host = host
self.port = port
self.user = user
self.passwd = passwd
self.winConn = winrm.Session('http://{0}:{1}/wsman'.format(self.host, self.port), auth=(self.user, self.passwd))
def exec_shell(self, command, msg):
ret = self.winConn.run_ps(command)
if ret.status_code == 0:
print(msg + "成功")
return ret.std_out.decode()
elif ret.status_code == 1:
raise Exception(msg + "失败:" + str(ret.std_err, "utf-8"))
def parse_data(self, data, isAll=True):
info_list = []
if type(data) is list or isAll:
if isAll:
if type(data) is list:
for info in data:
info_list.append(info["Name"])
return info_list
else:
return [data["Name"]]
else:
return [data["Name"]]
elif type(data) is dict:
return True
def get_SvnUser(self, name=''):
'''
获取所有用户
:return:
'''
try:
data = json.loads(self.exec_shell("Get-SvnLocalUser {0} | ConvertTo-Json".format(name), "查询用户"))
result = self.parse_data(data, 1 - bool(name))
return result
except Exception as e:
print("用户不存在")
return []
def get_SvnGroup(self, name=''):
'''
查询用户组
:return: 用户组列表
'''
try:
data = json.loads(self.exec_shell("""Get-SvnLocalGroup {0}| ConvertTo-Json""".format(name), "查询用户组"))
result = self.parse_data(data, 1 - bool(name))
return result
except Exception as e:
print("用户组不存在")
return []
def get_SvnGroupUser(self, name):
try:
data = json.loads(self.exec_shell("Get-SvnLocalGroupMember {0} | ConvertTo-Json".format(name), "用户组中用户查询"))
result = self.parse_data(data, isAll=True)
return result
except Exception as e:
print("用户组不存在")
return []
def get_Repository(self, name=''):
'''
查询仓库
:return:返回仓库列表
'''
try:
data = json.loads(self.exec_shell("""Get-SvnRepository {0}| ConvertTo-Json""".format(name), "查询仓库"))
result = self.parse_data(data, 1 - bool(name))
return result
except Exception as e:
print("仓库不存在")
return []
def add_SvnUser(self, username, password):
'''
添加用户
:param username: 用户名
:param password: 密码
:return: 返回true 创建成功 or False 创建失败
'''
try:
if self.get_SvnUser(name=username) == []:
data = self.exec_shell(
'''New-SvnLocalUser -Name {0} -Password (ConvertTo-SecureString -String "{1}" -AsPlainText -Force)'''.format(
username, password), "创建用户")
else:
if input("用户已存在是否重置密码?Y/N").lower().split(' ') == "Y":
data = self.exec_shell(
'''Set-SvnLocalUser -Name {0} -Password (ConvertTo-SecureString -String "{1}" -AsPlainText -Force)'''.format(
username, password), "重置密码")
return True
except Exception as e:
print(e)
def add_SvnGroup(self, GroupName):
'''
:param GroupName:
:return: True is successful or False is Fail
'''
try:
if self.get_SvnGroup(GroupName) == []:
data = self.exec_shell("""New-SvnLocalGroup {0}""".format(GroupName), "用户组添加")
else:
print("用户组已存在")
return True
except Exception as e:
print(e)
def add_UserGroup(self, GroupName, Username):
try:
if self.get_SvnGroup(GroupName) != []:
if username in self.get_SvnGroupUser(GroupName):
print("用户已存在组中")
else:
self.exec_shell("Add-SvnLocalGroupMember {0} (Get-SvnLocalUser {1})".format(GroupName, Username),
"用户组添加用户")
else:
print("用户组不存在")
return True
except Exception as e:
print("添加失败")
def add_Repository(self, RepoName):
'''
添加仓库
:param RepoName:
:return:
'''
try:
if self.get_Repository(RepoName) == []:
data = self.exec_shell("""New-SvnRepository {0}""".format(RepoName), "仓库添加")
else:
print("仓库已存在")
return True
except Exception as e:
print(e)
def add_GrantUser(self, Repository, username, access):
'''
仓库授权用户
:param Repository
:param username
:param access: ReadOnly NoAccess ReadWrite
:return: True is successful or False is fail
'''
command = """Add-SvnAccessRule -AuthorizationProfile SubversionLocal -Repository {0} -Path / -AccountName {1} -Access {2} -Force""".format(
Repository, username, access)
if self.get_user(username):
print("用户已存在")
self.exec_shell(command, "仓库授权")
else:
print("用户不存在,创建用户")
data = self.Add_SvnUser(username)
if data:
print("用户创建成功")
self.exec_shell(command, "仓库授权")
else:
print("用户创建失败")
def add_GrantGroup(self, Repository, GroupName, access):
'''
仓库授权用户组
:param Repository:
:param GroupName:
:param access:
:return: True is successful or False is fail
'''
command = '''Add-SvnAccessRule {0} -Path / -AccountId "@{1}" -Access {2}'''.format(Repository, GroupName,
access)
try:
if self.get_SvnGroup(GroupName):
self.exec_shell(command, "仓库授权用户组")
else:
print("用户不存在,创建用户")
data = self.add_SvnGroup(GroupName)
if data:
print("用户创建成功")
self.exec_shell(command, "仓库授权")
else:
print("用户创建失败")
except Exception as e:
print("用户组授权失败")
def createObj():
svn = SVN(host="ip", port="port", user="user", passwd="password")
return svn
if __name__ == "__main__":
svn = createObj()
params = {
"Repository": "xxxx",
"username": "xxxx",
# 权限分别是access: ReadOnly NoAccess ReadWrite
"access": "ReadOnly",
}
# 授权仓库用户权限
data = svn.add_GrantUser(Repository=params["Repository"],username=params["username"],access=params["access"])
# 授权用户用户组权限
# data = svn.add_GrantGroup(Repository=params["Repository"],GroupName=params["username"],access=params["access"])
print(data)
python 远程操作svn的更多相关文章
- 如何使用python远程操作linux
在云服务测试中,往往需要我们进入云服务内容进行相关内容的测试.这测试可以使用平台自身的noVNC.外部辅助xshell等工具连接到云服务内部进行测试.但是在如此反复的测试操作中,就需要用到自动化测试方 ...
- python远程操作服务器
python远程控制 标签(空格分隔): 远程Linux python远程控制:方案: Paramiko Pexpect(主要Linux机器) 安装Paramiko pip install param ...
- c#实现远程操作svn
/// <summary> /// 本地svn服务器地址 /// </summary> private static string localSVN = Configurati ...
- Python 使用Python远程连接并操作InfluxDB数据库
使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...
- python paramiko通过远程操作linux
python-paramiko通过远程操作linux 1. python-paramiko通过远程操作linux python3 远程操作linux 使用第三方paramiko库,对于实现运维自动部署 ...
- python 远程执行命令、上传、下载举例
使用python中的 paramiko 实现远程操作,需要安装 paramiko 模块. # vi pssh.py #!/usr/bin/python #coding=utf-8 ''' Create ...
- 在ubunut下使用pycharm和eclipse进行python远程调试
我比较喜欢Pycharm,因为这个是JetBrains公司出的python IDE工具,该公司下的java IDE工具--IDEA,无论从界面还是操作上都甩eclipse几条街,但项目组里有些人使用e ...
- python基础===利用PyCharm进行Python远程调试(转)
原文链接:利用PyCharm进行Python远程调试 背景描述 有时候Python应用的代码在本地开发环境运行十分正常,但是放到线上以后却出现了莫名其妙的异常,经过再三排查以后还是找不到问题原因,于是 ...
- vscode连接云服务,搭建Python远程开发
配置Python远程开发环境前提 配置步骤 1.windows 10 开发机配置 win10 1809后支持ssh ssh-keygen -t rsa -b 4096 #会显示生成到的目录C:\Use ...
- Python API 操作Hadoop hdfs详解
1:安装 由于是windows环境(linux其实也一样),只要有pip或者setup_install安装起来都是很方便的 >pip install hdfs 2:Client——创建集群连接 ...
随机推荐
- P3574 [POI2014] FAR-FarmCraft 吐槽 + 题解
洛谷上面的题解写的真的不太好,有很多错误,我来谈谈自己的理解. 设 \(f[i]\) 表示以 \(i\) 为根节点的子树中(包括节点 \(i\))的所有人安装好游戏所需要的时间(与下面的 \(g[i] ...
- 报错 no currentsessioncontext configured!
no currentsessioncontext configured! 使用hibernate框架报错 配置了session工厂类,使用getCurrentSession();时候引起的,原因是cu ...
- [Spring+SpringMVC+Mybatis]框架学习笔记(二):Spring-IOC-DI
上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(一):SpringIOC概述 下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(三):Spring实现 ...
- 【译】Visual Studio 2022 中的 Web API 开发
在 Visual Studio 2022 中,Web 开发人员的主要场景之一是使用 ASP.NET Core 创建 Web API.在 Visual Studio 2022 17.6 的最新预览版中, ...
- 跟运维学 Linux - 03
权限机制和性能指标 前面我们学完了操作文件和用户相关知识,本篇学习权限和性能相关知识. 文件的属性看起 看 linux 的权限,先从文件的属性看起 ls -l 加 -d 是只看这个文件夹: pjl@p ...
- el-date-picker 在表单中宽度(width)问题
在使用element-plus的日期选择组件 el-date-picker的时候,发现form表单内的日期选择框并不能跟el-input 一样把宽度撑满.而是要小一圈. 这样在排版中显得不太整齐,但是 ...
- 使用 Rancher 安装 K8s 集群
舞台环境 Ubuntu 22.04.2 LTS Docker 24.0.2 2GB RAM或者更多 CPU 2核心或者更多 Rancher 2.6.9 测试环境中,我准备了两台 Ubuntu 服务器, ...
- uni微信小程序隐私协议
最近小程序又新增了个 隐私协议弹窗.需要用户去授权,官网的一些API才能使用.官网地址 功能展示 项目地址:https://ext.dcloud.net.cn/plugin?id=14358 1.ma ...
- JAVA语言基础day01
笔记: Java开发环境: java编译运行过程: 编译期:.java源文件,经过编译,生成.class字节码文件 运行期:JVM加载.class并运行.class(0和1) 特点:跨平台,一次编译到 ...
- 聊一聊使用Spring事物时不生效的场景
前言 今天介绍一下Spring事物不生效的场景,事物是我们在项目中经常使用的,如果是Java的话,基本上都使用Spring的事物,不过Spring的事物如果使用不当,那么就会导致事物失效或者不回滚,最 ...