在Python中运行Git clone命令

use*_*246 4 python git

我是Python和Git的新手.我在写一个Python脚本它需要做同样的动作,通过从我的Linux服务器上的工作区运行以下命令的Git做的过程.(即/ local/mnt/workspace /)

git clone git://git.xyz.com/platform/manifest.git -b jb_2.5
Run Code Online (Sandbox Code Playgroud)

我尝试使用Fab库但是没有安装模块fabric.api所以我无法继续.也,

import git
git.Git().clone("git://git.xyz.com/platform/manifest.git") 
Run Code Online (Sandbox Code Playgroud)

没用.

还有其他解决办法吗?很感激帮助.谢谢.

Bri*_*and 14

你可以定义一个允许你调用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")
Run Code Online (Sandbox Code Playgroud)

将其更改为subprocess.check_output允许您查看输出git打印,而不是确定成功(例如git("status"),如果您不在git repo中,则会引发异常).


附注:看看PIP,它旨在帮助安装常见的软件包.


Mr *_*ooz 3

您可以使用 shell(通过os.systemsubprocess)或使用GitPython包。