Script to git clone all repositories of a user from bitbucket

Script to git clone all repositories of a user from bitbucket

There you go!

1) Ensure python3 is available.

prompt> python3 -V

prompt> Python 3.7.2

(Should work with python2 also, but not tested)

2) Make sure you have “requests” module imported

prompt>pip3 install requests

3) create a .netrc file in home directory.

prompt> cat ~/.netrc

machine bitbucket.org

login <your login name>

password <your password>

4) The clone.py file

#!/usr/bin/env python3

import sys
import requests
import os
import netrc
from os.path import expanduser
from urllib import parse as urlparse
home = expanduser("~")
path = home
netrc = netrc.netrc(home+"/.netrc")
user, account, password = netrc.authenticators("bitbucket.org")
s = requests.Session()
s.auth = (user,password)
r = s.get("https://api.bitbucket.org/1.0/user/repositories")
x = r.json()
userenc = urlparse.quote_plus(user)
curr = os.getcwd()
os.chdir(path)

for repo in x:
    reponame = repo['name']
    print("Cloning "+reponame);
    reponameenc = urlparse.quote_plus(reponame)
    clone = "git clone https://bitbucket.org/"+userenc+"/"+reponameenc+".git"
    os.system(clone)

os.chdir(curr)
print("\n CLONED SUCCESSFULLY.!")

5) Run the python script to clone all your repositories

prompt>python3 clone.py

Advertisements
READ  Code to tell if a binary tree is balanced

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use | Privacy Policy 
2018-2019 © eXitDiscount