Skip to content

Instantly share code, notes, and snippets.

@dannyhw
Created December 29, 2016 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannyhw/f749d5b60e8568929f709926d3e0f5bc to your computer and use it in GitHub Desktop.
Save dannyhw/f749d5b60e8568929f709926d3e0f5bc to your computer and use it in GitHub Desktop.
import base64
import os
from github import Github, GithubException
def get_sha_for_tag(repository, tag):
"""
Returns a commit PyGithub object for the specified repository and tag.
"""
branches = repository.get_branches()
matched_branches = [match for match in branches if match.name == tag]
if matched_branches:
return matched_branches[0].commit.sha
tags = repository.get_tags()
matched_tags = [match for match in tags if match.name == tag]
if not matched_tags:
raise ValueError('No Tag or Branch exists with that name')
return matched_tags[0].commit.sha
def download_directory(repository, sha, server_path):
"""
Download all contents at server_path with commmit tag sha in the repository
"""
contents = repository.get_dir_contents(server_path, ref=sha)
for content in contents:
print "Processing %s" % content.path
if content.type == 'dir':
os.makedirs(content.path)
download_directory(repository, sha, content.path)
else:
try:
path = content.path
file_content = repository.get_contents(path, ref=sha)
file_data = base64.b64decode(file_content.content)
file_out = open(content.path, "w")
file_out.write(file_data)
file_out.close()
except (GithubException, IOError) as exc:
print 'error processing %s: %s' % (content.path, exc)
if __name__ == "__main__":
#put user and password here Github('USER', 'PASSWORD')
GITHUB = Github('', '')
USER = GITHUB.get_user()
#put repo name here
REPO = USER.get_repo('REPO_NAME')
#enter branch name here
SHA = get_sha_for_tag(REPO, "BRANCH_NAME")
#put directory name here
DIRECTORY_TO_DOWNLOAD = "SOME_DIRECTORY"
os.makedirs(DIRECTORY_TO_DOWNLOAD)
download_directory(REPO, SHA, DIRECTORY_TO_DOWNLOAD)
@dannyhw
Copy link
Author

dannyhw commented Dec 29, 2016

This is something I was testing for an aws lambda function I was going to try. I don't think I'll actually use it now.
Maybe it will be useful to someone.
Putting the credentials and other variables in like this isn't ideal so I'd re-work that a bit if you want to use it.

The changes I made to the code from the blog are to maintain the directory structure. I also didn't want to have user input since I wanted to use it for a lambda function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment