为了同步CyanogenMod源代码,你也学会了翻墙,对吗?
通常 .repo/manifest.xml 文件有Google AOSP的Project,也有Github的Project;
访问Github不需要翻墙,而同步Google源代码则避免不了了;
想分开同步,想智能一点吗?
1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 import os,sys,subprocess 5 import xml.dom.minidom 6 from xml.dom.minidom import Node 7 8 # 代理环境 9 proxy_user='' 10 proxy_pass='' 11 proxy_host='123.123.123.123' # 请改 12 proxy_port=12345 # 请改 13 if len(proxy_user) == 0: 14 http_proxy="http://%s:%d" %(proxy_host, proxy_port) 15 https_proxy="http://%s:%d" %(proxy_host, proxy_port) 16 else: 17 http_proxy="http://%s:%s@%s:%d" %(proxy_user, proxy_pass, proxy_host, proxy_port) 18 https_proxy="http://%s:%s@%s:%d" %(proxy_user, proxy_pass, proxy_host, proxy_port) 19 proxyenv={"http_proxy":http_proxy, "https_proxy":https_proxy} 20 21 # 解析.repo/manifest.xml文件,取出project节点 22 doc = xml.dom.minidom.parse('.repo/manifest.xml') 23 projects = doc.getElementsByTagName('project') 24 25 github_projects=[] 26 google_projects=[] 27 28 errorsync_projects=[] 29 30 # 执行同步函数 31 def sync_project(project, env=None): 32 """ 33 执行同步命令 34 project: 期望同步的Project name 35 """ 36 cmd="repo sync %s" %project 37 print '>>>', cmd 38 # 执行同步 39 process = subprocess.Popen(cmd, shell=True, env=env, 40 stdout=subprocess.PIPE, stderr=subprocess.PIPE) 41 # 实时读取STDOUT 42 out = process.stdout.readline() 43 while out: 44 print out, 45 out = process.stdout.readline() 46 err = process.stderr.readline() 47 # 实时读取STDERR 48 while err: 49 print err, 50 err = process.stderr.readline() 51 # 等待进程结束获得返回值 52 process.poll() 53 errcode = process.returncode 54 # 若有错误追加到错误数组 55 if errcode != 0: 56 errorsync_projects.append(project) 57 58 # 对project进行分类 59 # AOSP的Project保存在 google_projects 数组 60 # CyanogenMod的Project保存在 github_project 数组 61 for node in projects: 62 remote=node.getAttribute('remote') 63 project=node.getAttribute('name') 64 if remote == 'aosp': 65 google_projects.append(project) 66 else: 67 github_projects.append(project) 68 69 cur_env=os.environ.copy() 70 cur_env["http_proxy"]=http_proxy 71 cur_env["https_proxy"]=https_proxy 72 # 同步来自Google的Project 73 for project in google_projects: 74 sync_project(project, cur_env) 75 sys.exit() 76 77 # 同步来自Github的Project 78 for project in github_projects: 79 sync_project(project) 80 81 # 输出同步出错的Project 82 if len(errorsync_projects) != 0: 83 print '>>> 同步出现了错误的Project: ' 84 for project in errorsync_projects: 85 print project 86 87 print '>>> 请执行命令以重新同步Project: ' 88 for project in errorsync_projects: 89 if project in google_projects: 90 sync_cmd="env http_proxy=%s https_proxy=%s repo sync %s" %(http_proxy, https_proxy, project)91 else: 92 sync_cmd="repo sync %s" %project 93 print sync_cmd
保存脚本到 sync_split.py,同步的时候执行:
cd /path/to/src
python sync_split.py
Enjoy!