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.