python执行git命令_在Python中运行Git clone命令

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值