Last active
December 9, 2019 06:49
-
-
Save toyowata/bd3a4d069f5d67c1f78e to your computer and use it in GitHub Desktop.
Create backup from mbed user code repository
This file contains hidden or 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 | |
# coding: utf-8 | |
# This script creates copy from mbed user code 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 for server response ..." | |
stdout_data, stderr_data = p.communicate() | |
return p.returncode, stdout_data, stderr_data | |
def get_page_num(src): | |
ptn = r"\?page=\d+" | |
n = re.search(ptn, src, re.IGNORECASE) | |
matchList = re.findall(ptn, src) | |
if n != None: | |
pages = max([int(i.lstrip(r'\?page=')) for i in matchList]) | |
else: | |
pages = 0 | |
return pages | |
def collect_code_repo(stream, username, password): | |
print "collecting repository names ..." | |
head = "<a class=\"repo-name\" 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 + "@os.mbed.com" + n.group(0) | |
print n.group(0) | |
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://os.mbed.com/users/" + username + "/code/" | |
else: | |
cmd = "curl https://os.mbed.com/users/" + username + "/code/" | |
result, stdout_data, stderr_data = get_html(cmd) | |
n_pages = get_page_num(stdout_data) | |
if n_pages >= 1: | |
collect_code_repo(stdout_data, username, password) | |
else: | |
print "No code repository page found." | |
for page in range(2, n_pages + 1): | |
result, stdout_data, stderr_data = get_html(cmd + "?page=" + str(page)) | |
collect_code_repo(stdout_data, username, password) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment