在 Python 中运行 Git clone 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15079442/
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
Running Git clone command in Python
提问by user1429246
I am new to both Python and Git. I am in the process of writing a Python script which needs to do the same action as done by running the below git command from my workspace on a Linux server. (i.e. /local/mnt/workspace/)
我是 Python 和 Git 的新手。我正在编写一个 Python 脚本,该脚本需要执行与通过在 Linux 服务器上的工作区运行以下 git 命令来完成的操作相同的操作。(即/local/mnt/workspace/)
git clone git://git.xyz.com/platform/manifest.git -b jb_2.5
I tried using Fab library however module fabric.api isn't installed so I couldn't proceed. Also,
我尝试使用 Fab 库,但是没有安装模块 fabric.api 所以我无法继续。还,
import git
git.Git().clone("git://git.xyz.com/platform/manifest.git")
didn't work.
没有用。
Any other solutions to do this ? Would appreciate the help. Thanks.
任何其他解决方案来做到这一点?将不胜感激的帮助。谢谢。
回答by Brigand
You can define a git function that allows you to make calls to git. Limiting the user to git commands is important for security purposes; otherwise asking for a git url and using other techniques could result in loss of data or other malicious attacks.
您可以定义一个 git 函数来调用 git。出于安全目的,限制用户使用 git 命令很重要;否则要求 git url 并使用其他技术可能会导致数据丢失或其他恶意攻击。
import subprocess
def git(*args):
return subprocess.check_call(['git'] + list(args))
# examples
git("status")
git("clone", "git://git.xyz.com/platform/manifest.git", "-b", "jb_2.5")
Changing it to subprocess.check_output
allows you to see the output git prints, instead of determining success (e.g. git("status")
raises an exception if you're not in a git repo).
将其更改为subprocess.check_output
允许您查看 git 打印的输出,而不是确定成功(例如git("status")
,如果您不在 git 存储库中,则会引发异常)。
Side note: take a look at PIPwhich is designed to help install common packages.
旁注:看看旨在帮助安装通用软件包的PIP。
回答by Mr Fooz
回答by abarnert
Since you didn't show what the error was beyond "didn't work", it's hard to guess what exactly your problem was.
由于您没有显示“无效”之外的错误是什么,因此很难猜测您的问题究竟是什么。
But I'm guessing the problem is that import git
raised an ImportError
, because you never installed the git
module that you're trying to use.
但我猜问题是import git
引发了ImportError
,因为您从未安装过git
您尝试使用的模块。
If so, the exact same readme document that told you how to do git.Git().clone("git://git.xyz.com/platform/manifest.git")
will also tell you how to install it.
如果是这样,告诉您如何操作的完全相同的自述文件也git.Git().clone("git://git.xyz.com/platform/manifest.git")
将告诉您如何安装它。
But most likely, all you need is something like pip install pygit2
or pip install GitPython
or something like that. (You may need sudo
, and you may need to install pip
before you can use it, and so on, but since we know nothing about your platform or your level of knowledge, there's no way to guess exactly what you need.)
但最有可能的是,您所需要的只是类似pip install pygit2
或pip install GitPython
或类似的东西。(您可能需要sudo
,并且可能需要先安装pip
才能使用它,等等,但由于我们对您的平台或知识水平一无所知,因此无法准确猜测您需要什么。)