Created
April 2, 2013 03:10
-
-
Save Penguin2600/5289635 to your computer and use it in GitHub Desktop.
alternate 100% python form -- untested.
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
#!/usr/bin/env python | |
import os | |
import sys | |
import json | |
import errno | |
import urllib2 | |
import datetime | |
from subprocess import call | |
DEBUG=False | |
def do_debug(debugString): | |
if DEBUG: | |
print (debugString) | |
def fetch_repo_list(user): | |
try: | |
endpoint = "https://api.github.com/users/%s/repos" % user | |
apiResults = urllib2.urlopen(endpoint) | |
jsonData = apiResults.read() | |
repoList=get_repo_from_json(jsonData) | |
except Exception as e: | |
do_debug("Error in Repo Fetch: %s" % e) | |
return repoList | |
def get_repo_from_json(jsonData): | |
repoList=[] | |
try: | |
data = json.loads(jsonData) | |
for repo in data: | |
repoDict={'user': repo['owner']['login'], 'name': repo['name'], 'url': repo["git_url"]} | |
repoList.append(repoDict) | |
except Exception as e: | |
do_debug("Error in Json Parse: %s" % e) | |
return repoList | |
def get_bundle_path(sPath,user,now): | |
bundlePath="%s/bundles/%s/%s" (sPath, user, now) | |
return bundlePath | |
def get_temp_path(sPath,user): | |
tempPath="/tmp/repos/%s/%s" (user, name) | |
return tempPath | |
def mkdir_p(path): | |
try: | |
os.makedirs(path) | |
except OSError as exc: | |
if exc.errno == errno.EEXIST and os.path.isdir(path): | |
pass | |
else: raise | |
def rmdir(path): | |
if os.path.exists(path): | |
try: | |
os.remove(path) | |
except: | |
pass | |
def user_validate(user): | |
return user.isalnum() | |
def usage(): | |
print "Usage: %s user1 user2 user3 ..." % os.path.basename(__file__) | |
def main(): | |
sPath=__file__ | |
now=datetime.datetime.now().strftime("%Y%m%d-%H%M%S") | |
if len(sys.argv[1:]): | |
for user in sys.argv[1:]: | |
if user_validate(user): | |
userRepos=fetch_repo_list(user) | |
for repo in userRepos: | |
bundlePath=get_bundle_path(sPath,user,now) | |
tempPath=get_temp_path(user,repo['name']) | |
mkdir_p(bPath) | |
rmdir(tempPath) | |
call(["git clone --mirror %s %s" % (repo['url'], tempPath)]) | |
call(["cd %s && git bundle create %s/%s.bundle --all" % (tempPath, bundlePath, repo['name']) ]) | |
else: | |
usage() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment