在Python中运行Git clone命令 [英] Running Git clone command in Python

查看:1885
本文介绍了在Python中运行Git clone命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python和Git都是新手。我正在编写一个Python脚本,它需要执行与从Linux服务器上的工作区运行下面的git命令所做的相同的操作。 (即/ local / mnt / workspace /)

  git clone git://git.xyz.com/platform/manifest。 git -b jb_2.5 

我尝试使用Fab库,但是没有安装module fabric.api我无法继续。
另外,

  import git 
git.Git()。clone(git:// git .xyz.com / platform / manifest.git)

无效。



任何其他解决方案来做到这一点?将不胜感激的帮助。感谢。

解决方案

你可以定义一个允许你调用git的git函数。将用户限制为git命令对于安全目的很重要;否则要求使用其他技术可能会导致数据丢失或其他恶意攻击。

 导入子流程

def git(* args):
返回subprocess.check_call(['git'] + list(args))

#例子
git(status )
git(clone,git://git.xyz.com/platform/manifest.git,-b,jb_2.5)

更改为 subprocess.check_output 可以让您看到输出git打印,而不是确定是否成功(例如,如果您不在git仓库中, git(status)会引发异常)。

< hr>

附注:查看 PIP 它旨在帮助安装常用软件包。


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/)

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,

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.

解决方案

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.

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).


Side note: take a look at PIP which is designed to help install common packages.

这篇关于在Python中运行Git clone命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆