Last active
August 29, 2015 14:06
-
-
Save gesquive/ac0bfde6c59a5a4e77f5 to your computer and use it in GitHub Desktop.
Method to dl git repos or individual files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sh import ErrorReturnCode, git | |
from cStringIO import StringIO | |
import tarfile | |
def git_get(git_repo, git_branch, git_path, destination_path=".", use_compression=True, verbose=False): | |
git_dir = os.path.dirname(git_path) | |
git_file = os.path.basename(git_path) | |
if git_dir.replace("/", "") == git_file: | |
git_file = '' | |
if git_dir.replace("/","").strip() == "": | |
git_loc = git_branch | |
else: | |
git_loc = "%s:%s" % (git_branch, git_dir) | |
arch_format = "tar" | |
arch_mode = "r|" | |
if use_compression: | |
arch_format = "tar.gz" | |
arch_mode = "r|gz" | |
archive_str = StringIO() | |
try: | |
cmd = git.archive(git_loc, git_file, format=arch_format, remote=git_repo) | |
logger.debug("Ran cmd=%s" % cmd.ran) | |
archive_str = StringIO(cmd.stdout) | |
except ErrorReturnCode, e: | |
logger.error("Error downloading from git archive.") | |
logger.error("cmd=%s" % e.full_cmd) | |
logger.error("errmsg=%s" % e.stderr.replace('\n',' ')) | |
return False | |
tar = tarfile.open(fileobj=archive_str, mode=arch_mode) | |
for tarinfo in tar: | |
desc = "extracting name=" + tarinfo.name + " size=" + str(tarinfo.size) | |
if tarinfo.isreg(): | |
desc = desc + " type=file" | |
elif tarinfo.isdir(): | |
desc = desc + " type=dir" | |
else: | |
desc = desc + " type=unk" | |
logger.debug(desc) | |
tar.extract(tarinfo, destination_path) | |
# USAGE: | |
git_get("[email protected]:user/repo.git", "HEAD", "/path/to/utility.py", ".") | |
git_get("[email protected]:user/repo.git", "HEAD", "", "/tmp") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment