Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. """
  2. Downloads folders from github repo
  3. Requires PyGithub
  4. pip install PyGithub
  5. """
  6.  
  7. import os
  8. import sys
  9. import base64
  10. import shutil
  11. import getopt
  12. from github import Github
  13. from github import GithubException
  14.  
  15.  
  16. def get_sha_for_tag(repository, tag):
  17. """
  18. Returns a commit PyGithub object for the specified repository and tag.
  19. """
  20. branches = repository.get_branches()
  21. matched_branches = [match for match in branches if match.name == tag]
  22. if matched_branches:
  23. return matched_branches[0].commit.sha
  24.  
  25. tags = repository.get_tags()
  26. matched_tags = [match for match in tags if match.name == tag]
  27. if not matched_tags:
  28. raise ValueError('No Tag or Branch exists with that name')
  29. return matched_tags[0].commit.sha
  30.  
  31.  
  32. def download_directory(repository, sha, server_path):
  33. """
  34. Download all contents at server_path with commit tag sha in
  35. the repository.
  36. """
  37. if os.path.exists(server_path):
  38. shutil.rmtree(server_path)
  39.  
  40. os.makedirs(server_path)
  41. contents = repository.get_dir_contents(server_path, ref=sha)
  42.  
  43. for content in contents:
  44. print "Processing %s" % content.path
  45. if content.type == 'dir':
  46. os.makedirs(content.path)
  47. download_directory(repository, sha, content.path)
  48. else:
  49. try:
  50. path = content.path
  51. file_content = repository.get_contents(path, ref=sha)
  52. file_data = base64.b64decode(file_content.content)
  53. file_out = open(content.path, "w+")
  54. file_out.write(file_data)
  55. file_out.close()
  56. except (GithubException, IOError) as exc:
  57. print('Error processing %s: %s', content.path, exc)
  58.  
  59. def usage():
  60. """
  61. Prints the usage command lines
  62. """
  63. print "usage: gh-download --token=token --org=org --repo=repo --branch=branch --folder=folder"
  64.  
  65. def main(argv):
  66. """
  67. Main function block
  68. """
  69. try:
  70. opts, args = getopt.getopt(argv, "t:o:r:b:f:", ["token=", "org=", "repo=", "branch=", "folder="])
  71. except getopt.GetoptError as err:
  72. print str(err)
  73. usage()
  74. sys.exit(2)
  75. for opt, arg in opts:
  76. if opt in ("-t", "--token"):
  77. token = arg
  78. elif opt in ("-o", "--org"):
  79. org = arg
  80. elif opt in ("-r", "--repo"):
  81. repo = arg
  82. elif opt in ("-b", "--branch"):
  83. branch = arg
  84. elif opt in ("-f", "--folder"):
  85. folder = arg
  86.  
  87. github = Github(token)
  88. organization = github.get_organization(org)
  89. repository = organization.get_repo(repo)
  90. sha = get_sha_for_tag(repository, branch)
  91. download_directory(repository, sha, folder)
  92.  
  93. if __name__ == "__main__":
  94. """
  95. Entry point
  96. """
  97. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement