-
-
Save teddokano/f4b91ad0423603b41ea7 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
#!/usr/bin/env python | |
#encoding=utf-8 | |
# This script creates copy from mbed public user repository | |
# You must install Mercurial package (pip install mercurial) | |
# | |
# Usage: python clone_mbed_user_repo.py <mbed_user_name> <password> <sessionid_developer of coockei> | |
# e.g. $ python clone_mbed_user_repo.py MACRUM password kjdt3hu1loppvyyxq60nhokssa1k48ul | |
# | |
# <sessionid_developer> can be found from developer tool of the web brawser | |
# Chrome: [Other tool] - [Developer tool] - [Resource] - [Cookies] - [developer.mbed.org] - [sessionid_developer] | |
import sys | |
import subprocess | |
import StringIO | |
import re | |
def get_html(cmd): | |
print "%s" % cmd | |
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
print "waiting..." | |
stdout_data, stderr_data = p.communicate() | |
# print "finish: %d %d" % (len(stdout_data), len(stderr_data)) | |
return p.returncode, stdout_data, stderr_data | |
def get_page_num(src): | |
ptn = r"repositories \((.*)\)" | |
n = re.search(ptn, src, re.IGNORECASE) | |
pages = int(n.group(1)) / 40 + 1 | |
return pages | |
def correct_code_repo(stream, username, password): | |
print "correcting..." | |
head = "<a href=\"" | |
repo = "/users/" + username + "/code/[^\"]*/" | |
ptn = re.compile(head + repo) | |
url = re.compile(repo) | |
buf = StringIO.StringIO(stream) | |
line = buf.readline() | |
while line: | |
m = ptn.search(line, 0) | |
if m != None: | |
n = url.search(m.group(0), 0) | |
if n != None: | |
cmd = "hg clone https://" + username + ":" + password + "@developer.mbed.org" + n.group(0) | |
print n.group(0) | |
# print cmd | |
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout_data, stderr_data = p.communicate() | |
print stdout_data | |
if (p.returncode != 0): | |
print stderr_data | |
line = buf.readline() | |
buf.close | |
if __name__ == "__main__": | |
argvs = sys.argv | |
argc = len(argvs) | |
if argc < 3: | |
sys.exit("Usage: %s username password sessionid_developer\n Example: %s test" % sys.argv[0]) | |
username = argvs[1] | |
password = argvs[2] | |
if argc == 4: | |
cmd = "curl -v --cookie sessionid_developer=" + argvs[3] + " https://developer.mbed.org/users/" + username + "/code/" | |
else: | |
cmd = "curl https://developer.mbed.org/users/" + username + "/code/" | |
# cmd = "curl -v --cookie sessionid_developer=" + cookie + " https://developer.mbed.org/users/" + username + "/code/" | |
result, stdout_data, stderr_data = get_html(cmd) | |
n_pages = get_page_num(stdout_data) | |
for page in range(1,n_pages): | |
result, stdout_data, stderr_data = get_html(cmd + "?page=" + str(page)) | |
correct_code_repo(stdout_data, username, password) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment