Skip to content

Instantly share code, notes, and snippets.

@righettod
Last active October 13, 2018 15:47
Show Gist options
  • Save righettod/7fb927254f61c415f1544c83ec24ac2a to your computer and use it in GitHub Desktop.
Save righettod/7fb927254f61c415f1544c83ec24ac2a to your computer and use it in GitHub Desktop.
Clone or update all local clones of GitHub public repositories and gists for the target specified user.
import requests
import colorama
import os
import git
import argparse
import shutil
import json
from git import Repo
from termcolor import colored
"""
Clone or update all local clones of GitHub public repositories ang gists for the target specified user.
Dependencies:
pip install colorama termcolor requests GitPython
"""
colorama.init()
# Define parser for command line arguments
parser = argparse.ArgumentParser(description="Clone or update all local clones of GitHub public repositories for the target specified user.")
parser.add_argument('-u', action="store", dest="username", help="GitHub username", required=True)
parser.add_argument('-f', action="store_true", dest="force_cloning", help="Force cloning for existing local respositories clones", default=False, required=False)
args = parser.parse_args()
# Retrieve the list of the user gists using Github API
# See https://developer.github.com/v3/gists/#list-a-users-gists
print(colored("[*] Get the list of gists for the user '%s'..." % args.username, "yellow", attrs=[]))
gists_infos = requests.get("https://api.github.com/users/%s/gists" % args.username).json()
# Save the gists metadata JSON file
with open("metadata_gists.json", "w") as f:
f.write(json.dumps(gists_infos))
# Parse the list
print(colored("[*] Managing %s gists..." % len(gists_infos), "yellow", attrs=[]))
for gist in gists_infos:
is_private = (not gist["public"])
if not is_private:
for k in gist["files"].keys():
gist_filename = k
break
print(" Downloading gist '%s'..." % gist_filename)
gist_content = requests.get(gist["files"][gist_filename]["raw_url"]).text
with open("gist_" + gist_filename, "w") as f:
f.write(gist_content)
# Final notification for gists
print(colored("[i] %s gists managed." % len(gists_infos), "green", attrs=[]))
# Retrieve the list of the user respositories using Github API
# See https://developer.github.com/v3/repos/#list-user-repositories
print(colored("[*] Get the list of repositories for the user '%s'..." % args.username, "yellow", attrs=[]))
repos_infos = requests.get("https://api.github.com/users/%s/repos" % args.username).json()
# Save the repos metadata JSON file
with open("metadata_repositories.json", "w") as f:
f.write(json.dumps(repos_infos))
# Parse the list
print(colored("[*] Managing %s repositories..." % len(repos_infos), "yellow", attrs=[]))
for repo in repos_infos:
is_private = repo["private"]
if not is_private:
repo_name = repo["name"]
clone_url = repo["clone_url"]
if args.force_cloning and os.path.isdir(repo_name):
print(" Remove existing repository '%s'..." % repo_name)
shutil.rmtree(repo_name)
if os.path.isdir(repo_name):
print(" Updating repository '%s'..." % repo_name)
g = git.cmd.Git(repo_name)
g.pull()
else:
print(" Cloning repository '%s'..." % repo_name)
Repo.clone_from(clone_url, repo_name)
# Final notification for repos
print(colored("[i] %s repositories managed." % len(repos_infos), "green", attrs=[]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment