`
貌似掉线
  • 浏览: 256634 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

从github上下载android源码(已附脚本)

阅读更多
老早前就想下载android代码学习学习了,无奈android官方文档提供的方法,经尝试后发现完全不适合我,因为一旦中途断开就要重新下载,否则什么代码都看不到。而且不知道提供的那个网址被墙了还是怎样,经常连不上。换了android.googlesource.com还是差不多,代码一样下不下来。
不过经过一番努力还是有结果的,发现github有个项目是做了android.googlesource.com中android源码的镜像的,而从github上下载代码则稳定很多,速度也很快,地址为:
https://github.com/android
我试着从上面下载了其中一部分,发现很快就下载下来了。不过上面有近一百个地址,要是让我一个一个下,然后再进行分类,移到相应的文件夹,我一定会疯的,这不是我的性格。作为一个优秀的程序员,懒惰就是最大的优点。
在这种情况下,当然是写一个脚本来下载代码了,这个脚本的关键代码就是:
git clone --depth=1 $2 $1

其中参数1是下载到本地的路径,参数2是git地址。
接下来就是要解析https://github.com/android 这个页面,得到所有git地址了。
查看源代码,发现每一个git地址都在网页源代码的一个叫“repolist-name”的CSS类中,如下所示:
  <h3 class="repolist-name">
    <a href="/android/platform_frameworks_base">platform_frameworks_base</a>
  </h3>

一共有98个。在这个标签里面,
<a href="/android/platform_frameworks_base">platform_frameworks_base</a>

是我们要提取的内容,href属性的值前面加上“http://github.com”,后面加上“.git”就是我们要的git地址,而platform_frameworks_base中的下划线转换成斜杠“/”,就是我们在本地对应的路径。
接下来建一个java工程,解析这个网页并将这两个地址输出到文本中,以方便我们的脚本编写。
解析html用jsoup库,然后输出到文本,也懒得自己再去操作输出流什么的了,用apache中的common-io包。
这里我一共写了两个类,一个是Main.java用来解析,一个是JavaBean,即GithubUrl类,代码如下:
/*
 * @(#)Main.java                       Project:GetAndroidSource
 * Date:2013-9-1
 *
 * Copyright (c) 2013 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.getandroidsource;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author Geek_Soledad <a target="_blank" href=
 *         "http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=XTAuOSVzPDM5LzI0OR0sLHM_MjA"
 *         style="text-decoration:none;"><img src=
 *         "http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_01.png"
 *         /></a>
 */
public class Main {

        private static final String GITHUB_URL = "https://github.com";
        private static final String GITHUB_ANDROID_URL = "https://github.com/android";

        public static void main(String[] args) {
                List<GithubUrl> urls = getGithubUrl();
                System.out.println("repolists: " + urls.size());
                try {
                        FileUtils.writeLines(new File("E:\\gitclone.txt"), urls);
                        System.out.println("write to E:\\gitclone.txt finish");
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        private static List<GithubUrl> getGithubUrl() {
                Document doc = null;
                try {
                        doc = Jsoup.connect(GITHUB_ANDROID_URL).get();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                if (doc == null) {
                        return null;
                }
                List<GithubUrl> urls = new ArrayList<GithubUrl>();
                Elements repolists = doc.getElementsByClass("repolist-name");
                for (Element repolist : repolists) {
                        Elements hrefs = repolist.select("a[href]");
                        if (hrefs.isEmpty()) {
                                System.out.println("it is empth");
                                continue;
                        }
                        if (hrefs.size() > 1) {
                                System.out.println(hrefs.text() + ":" + hrefs.size());
                        }
                        Element e = hrefs.first();
                        System.out.println(e.html() + ":" + e.text() + "---" + e.attr("href"));
                        urls.add(new GithubUrl(e.text().replaceAll("_", "/"), GITHUB_URL + e.attr("href")));
                }
                return urls;
        }

}

/*
 * @(#)GithubUrl.java                  Project:GetAndroidSource
 * Date:2013-9-1
 *
 * Copyright (c) 2013 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.getandroidsource;

/**
 * @author Geek_Soledad <a target="_blank" href=
 *         "http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=XTAuOSVzPDM5LzI0OR0sLHM_MjA"
 *         style="text-decoration:none;"><img src=
 *         "http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_01.png"
 *         /></a>
 */
public class GithubUrl {

        private String title;
        private String href;

        public GithubUrl(String title, String href) {
                super();
                this.title = title;
                this.href = href;
        }

        public String getTitle() {
                return title;
        }

        public void setTitle(String title) {
                this.title = title;
        }

        public String getHref() {
                return href;
        }

        public void setHref(String href) {
                this.href = href;
        }

        @Override
        public String toString() {
                return String.format("cloneit %s %s.git", title, href);
        }

}

上面的GithubUrl.java中,重写了toString,是因为我在脚本了定义了一个cloneit的函数用来从指定的地址下载代码到指定的目录中,这样输出的话我到时可以直接粘贴到脚本中。不过刚才发现上面的代码要略微修改一下,虽然没什么错误。
今天就先写到这里,因为是在公司电脑里写,而脚本在我家里的电脑,所以脚本在下一篇再贴上来吧。而上面的java工程我托管到了code.google.com中,可以通过svn下载,命令如下:
svn checkout http://msdx-java-code.googlecode.com/svn/trunk/GetAndroidSource



本来想在另一篇日志附上脚本的,但觉得内容不多,就决定改一下这篇文章,把脚本附在下面了。
脚本的主要代码如下:
#!/bin/bash

#sudo dpkg-reconfigure dash

#根本目录
#$1为保存的地址,$2为下载地址
function cloneit()
{
    if [ -d $1 ]; then
        echo -e "\033[0;38;40m $2 has been git clone \033[0m"
        echo "git pull --depth 1 $1"
        cd $1
        git pull --depth 1
        cd -
    else 
        echo -e "\033[0;31;40m git clone $2 $1 \033[0m"
        git clone --depth=1 $2 $1  
    fi  
    echo finish $(date) $1
}

#下载路径
cloneit platform/frameworks/base https://github.com/android/platform_frameworks_
base.git
cloneit platform/build https://github.com/android/platform_build.git

其中在cloneit函数里面,先判断文件夹是否存在,如果存在就使用git pull更新代码,否则就使用git clone下载代码。
完整脚本下载地址:http://www.vdisk.cn/down/index/14777400
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics