Created
December 29, 2016 18:12
-
-
Save dannyhw/f749d5b60e8568929f709926d3e0f5bc to your computer and use it in GitHub Desktop.
Based on the post written here: https://sookocheff.com/post/tools/downloading-directories-of-code-from-github-using-the-github-api/
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.