Created
October 14, 2015 07:03
-
-
Save vst/6d0ec44b3176b74842aa to your computer and use it in GitHub Desktop.
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
from collections import OrderedDict | |
import base64 | |
import json | |
import urllib2 | |
import sys | |
import datetime | |
## Define Bitbucket API endpoints: | |
ENDPOINT_REPOSITORIES = "https://api.bitbucket.org/2.0/repositories/{handle}?page={page}" | |
def get_base64_authtoken(username, password): | |
""" | |
Consumes a username and password and produces a base64 encoded | |
token for basic HTTP authentication. | |
""" | |
return base64.encodestring("%s:%s" % (username, password)).replace("\n", "") | |
def add_auth_header(request, authtoken): | |
""" | |
Adds a basic HTTP authentication header to the request. | |
""" | |
request.add_header("Authorization", "Basic " + authtoken) | |
def get_repositories(handle, username=None, password=None, page=1, url=None): | |
""" | |
Returns the list of repositories for the handle. | |
""" | |
def _get_remote_data(url): | |
## Initialize the request instance: | |
request = urllib2.Request(url) | |
## Add authentication header if required: | |
if username is not None and password is not None: | |
add_auth_header(request, get_base64_authtoken(username, password)) | |
## Get the response from the endpoint: | |
response = urllib2.urlopen(request) | |
## Read the contents: | |
contents = response.read() | |
## Parse and return: | |
return json.loads(contents) | |
## Get remote data: | |
data = _get_remote_data(ENDPOINT_REPOSITORIES.format(handle=handle, page=page)) | |
## Define and populate the return value: | |
retval = data["values"] | |
## Do we have more results? | |
while "next" in data and data["next"] is not None: | |
## Get the next page: | |
data = _get_remote_data(data["next"]) | |
## Define and populate the return value: | |
retval += data["values"] | |
## Done, return retval: | |
return retval | |
if __name__ == "__main__": | |
## Get arguments: | |
handle = sys.argv[1] if len(sys.argv) >= 2 else None | |
username = sys.argv[2] if len(sys.argv) >= 3 else None | |
password = sys.argv[3] if len(sys.argv) >= 4 else None | |
## Can we proceed? | |
if handle is None: | |
sys.stderr.write("Error: handle is missing.\n") | |
sys.exit(1) | |
## Get all repositories: | |
repos = get_repositories(handle, username, password) | |
## Declare and initialized the distilled data: | |
distilled = [] | |
## Iterate over repositories and print: | |
for repo in repos: | |
## Transform the ssh link: | |
for clone in repo["links"]["clone"]: | |
repo["clone_" + clone["name"]] = clone["href"] | |
## Add to distilled: | |
distilled.append(OrderedDict([ | |
("name", repo["name"]), | |
("full", repo["full_name"]), | |
("ssh", repo["clone_ssh"]), | |
("http", repo["clone_https"])])) | |
## Now spit out the bash script: | |
print "#!/bin/bash" | |
print "" | |
print "## This script is automatically generated and might change next time." | |
print "## Generated %s" % (datetime.datetime.now()) | |
print "" | |
print "echo 'Starting' $(date)" | |
print "echo" | |
for repo in distilled: | |
print """ | |
echo "Going for {full}" | |
test -d {full} && (echo "Directory exists. Updating..."; cd {full}; git remote update) || (echo "First time. Cloning..."; mkdir -p {full}; cd {full}; git clone --mirror {http} .) | |
echo | |
""".format(**repo) | |
print "echo" | |
print "echo 'Finishing' $(date)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment