在Python代码中使用Git命令 [英] Use Git commands within Python code
本文介绍了在Python代码中使用Git命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被要求编写一个脚本,从Git获取最新的代码,进行构建并执行一些自动化的单元测试。
我发现有两个内置的Python模块用于与Git进行交互,这些模块很容易获得: GitPython
和 libgit2
。
我应该使用什么方法/模块?
解决方案
更简单的解决方案是使用Python 子流程
模块调用git。在你的情况下,这将拉动最新的代码并构建:
import subprocess
subprocess.call([git ,pull])
subprocess.call([make])
subprocess.call([make,test])
文件:
I have been asked to write a script that pulls the latest code from Git, makes a build, and performs some automated unit tests.
I found that there are two built-in Python modules for interacting with Git that are readily available:
GitPython
andlibgit2
.What approach/module should I use?
解决方案An easier solution would be to use the Python
subprocess
module to call git. In your case, this would pull the latest code and build:import subprocess subprocess.call(["git", "pull"]) subprocess.call(["make"]) subprocess.call(["make", "test"])
Docs:
这篇关于在Python代码中使用Git命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文