如何从 python 脚本运行 SVN 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16573474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:03:30  来源:igfitidea点击:

How to run SVN commands from a python script?

pythonsvn

提问by hopper

Basically I want to write a python script that does several things and one of them will be to run a checkout on a repository using subversion (SVN) and maybe preform a couple more of svn commands. What's the best way to do this ? This will be running as a crond script.

基本上我想编写一个 python 脚本来做几件事,其中之一是使用 subversion (SVN) 在存储库上运行检出,并且可能执行更多的 svn 命令。这样做的最佳方法是什么?这将作为 crond 脚本运行。

采纳答案by ovidoo83

Would this work?

这行得通吗?

p = subprocess.Popen("svn info svn://xx.xx.xx.xx/project/trunk | grep \"Revision\" | awk '{print }'", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print "Revision is", output

回答by Torxed

Try pysvn

试试pysvn

Gives you great access as far as i've tested it. Here's some examples: http://pysvn.tigris.org/docs/pysvn_prog_guide.html

就我测试过的而言,它为您提供了很好的访问权限。以下是一些示例:http: //pysvn.tigris.org/docs/pysvn_prog_guide.html

The reason for why i'm saying as far as i've tested it is because i've moved over to Git.. but if i recall pysvn is (the only and) the best library for svn.

就我测试过的而言,我之所以这么说是因为我已经转移到 Git 了……但是如果我记得 pysvn 是(唯一的)最好的 svn 库。

回答by Andry

Take a look into the python xonsh module: http://xon.sh/tutorial.html

看看python xonsh模块:http://xon.sh/tutorial.html

It can call shell commands plus piping and output redirection with close touch to the python native code (nested) without need to play with python communicate bullshet and escape characters around.

它可以调用 shell 命令以及管道和输出重定向,与 python 本机代码(嵌套)密切接触,而无需使用 python 通信牛舍和转义字符。

Examples:

例子

env | uniq | sort | grep PATH

env | uniq | sort | grep PATH

COMMAND1 e>o < input.txt | COMMAND2 > output.txt e>> errors.txt

COMMAND1 e>o < input.txt | COMMAND2 > output.txt e>> errors.txt

echo "my home is $HOME"

echo "my home is $HOME"

echo @(7+3)

echo @(7+3)