12月的地震震斷了幾根光纜,麻煩的事情接踵而至,直連sourceforge上不去了,只好用代理。雖然能夠下載到打包好的代碼,但某些代碼已顯得陳舊,而cvs最新的代碼確要用工具checkout,但很郁悶的事情cvs不支持http代理。有一下一些解決辦法:
1、找sockets代理,然后用eborder等軟件使cvs能夠用。明顯,網絡上提供sockets代理的少之又少。
2、通過工具把http代理變成sockets代理。當然此法能夠行得通,但cvs checkout的速度慢的驚人,沒有可行性。
3、找聯通的網絡,他們出國沒有受到損壞,速度很快。
4、等待網絡修好:)
5、另:感謝A.E告訴我eclipse也可以支持!
……
由于急需一些開源項目的cvs代碼,以上途徑又不太現實,所以還是另想辦法。
但令人高興的是,我可以用http代理通過瀏覽器查看sourceforge的ViewVC工具所提供的cvs代碼,這給我了很大的啟發,準備利用 ViewVC來下載源代碼。隨后就分析ViewVC生成的頁面,我們這里以lib3ds.cvs.sourceforge.net作為例子。
打開頁面以后呈現在面前的是一個目錄結構,點擊進入下一層目錄,可以看到ViewVC為我們輸出了目錄和文件。每一個目錄和文件都有一個超鏈接,如果單擊目錄的話會進入下一層目錄,而點擊文件會進入文件的詳細說明(例http: //lib3ds.cvs.sourceforge.net/lib3ds/lib3ds/3ds-utils.spec.in?view=log),包括CVS Tags等等。
在http://lib3ds.cvs.sourceforge.net/lib3ds/lib3ds/3ds-utils.spec.in?view=log 頁面里,會發現有一個download超鏈接,這個超鏈接可以讓我們下載到這個文件,點擊這個文件以后,地址欄會變為:http: //lib3ds.cvs.sourceforge.net/*checkout*/lib3ds/lib3ds/3ds-utils.spec.in?revision =1.1,文件的詳細內容也在眼前了,這就是我們需要的源代碼。
請注意地址里面的/*checkout*/,這將是我們的入手點,只要找到文件的相對路徑,我們在前面加上/*checkout*/就可以下載這個文件了。而后面的參數可以忽略,默認會得到最新的版本。
很好,下一步就是分析如何得到相對地址。由于ViewVC工具生成的網頁代碼很有規律,一個目錄的超鏈接類似于:
<a name="examples" href="/lib3ds/lib3ds/examples/" title="View directory contents">
而一個文件的超鏈接類似于:
<a href="/lib3ds/lib3ds/lib3ds/viewport.h?revision=1.6&view=markup" title="View file contents">
和<a href="/*checkout*/lib3ds/lib3ds/autogen.sh?revision=1.14" title="Download file contents">
只需要通過正則表達式就可以把地址抓出來,剩下的工作應該知道了吧:)
我做了一個小小的程序來實現最基本的功能,對于更多的功能,比如更多的錯誤恢復、多線程下載等等請自己實現。
VS2005演示工程和下載地址:http://www.hesicong.net/aspx/fileuploader/Upload/Internet_ViewVC_CVS_Checkout.rar
最后不要忘了到我的個人主頁來湊個熱鬧哦http://www.hesicong.net
下面是完整的源代碼,在VS2005下編譯運行成功。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExPRessions;
namespace Internet_ViewVC_CVS_Checkout
{
/// <summary>
/// A simple ViewVC CVS checkout class
/// Author: hesicong
/// Homepage: www.hesicong.net hesicong.VEVb.com
/// </summary>
public class ViewVC_CVS_Checkout
{
public static Regex regFindDir = new Regex(
@"href=""(?<DIRURL>.*)?""/s*title=""View/sdirectory/scontents"">",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
public static Regex regFindFiles = new Regex(
@"href=""(?<FILEURL>.*)?/?(.*)""/s*title=""(View|Download)/sfi"
+ @"le/scontents"">",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
public class DirList
{
public List<DirList> dir;
public List<string> file;
};
static string store;
static WebClient wc = new WebClient();
static WebClient wcFileDown = new WebClient();
public static void Main()
{
Console.ForegroundColor = ConsoleColor.White;
WebProxy proxy = new WebProxy();
Console.WriteLine("======================================================================");
Console.WriteLine(" ViewVC CVS Checkout ");
Console.WriteLine(" Author:hesicong Homepage:www.hesicong.net hesicong.VEVb.com ");
Console.WriteLine("======================================================================");
Console.Write("Enter your Proxy:(IP:PORT): ");
proxy.Address = new Uri("HTTP://"+Console.ReadLine());
Console.Write(@"ViewVC Start URL:(HTTP:///): ");
wc.BaseAddress = Console.ReadLine();
Console.Write(@"Where to store your files? (Driver:/Dir): ");
store = Console.ReadLine();
wcFileDown.Proxy = proxy;
wc.Proxy = proxy;
Console.WriteLine("Start downloading");
DirList dl;
dl = getTree("/");
Console.WriteLine("Done! Press Enter Key to Finish");
Console.ReadLine();
}
/// <summary>
/// Search the newest files and download them from CVS
/// </summary>
/// <param name="address">Relative url where ViewVC page</param>
/// <returns></returns>
public static DirList getTree(string address)
{
DirList dir = new DirList();
dir.dir=new List<DirList>();
dir.file = new List<string>();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("[Getting Content] " + address);
string src;
try
{
src = wc.DownloadString(address);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
MatchCollection dirMc = regFindDir.Matches(src);
//Add dirs
Console.WriteLine("Find " + dirMc.Count + " Dirs");
foreach (Match aDirMatch in dirMc)
{
string aDir = aDirMatch.Groups["DIRURL"].Value;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[Enter DIR] " + aDir);
dir.dir.Add(getTree(aDir));
}
MatchCollection fileMc = regFindFiles.Matches(src);
Console.WriteLine("Find " + fileMc.Count + " Files");
foreach (Match aFileMatch in fileMc)
{
string aFile = aFileMatch.Groups["FILEURL"].Value;
aFile = aFile.Replace("/*checkout*", ""); //Replace download file content url to normal
dir.file.Add(aFile);
string b = wc.BaseAddress.ToString();
string urlToDown = b + "*checkout*" + aFile;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(" Downloading: "+urlToDown);
string fileStore = store + aFile.Replace('/','//');
string fileStoreDir=Path.GetDirectoryName(fileStore);
if(Directory.Exists(fileStoreDir)==false)
{
Directory.CreateDirectory(fileStoreDir);
}
if(File.Exists(fileStore)==true)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" File already exist, rewrite?(Y/N)");
if(Console.ReadKey().Key==ConsoleKey.Y)
{
File.Delete(fileStore);
}
Console.WriteLine();
}
try
{
wcFileDown.DownloadFile(urlToDown, fileStore);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
Console.ResetColor();
return dir;
}
}
}
http://www.survivalescaperooms.com/hesicong/archive/2007/01/11/617734.html
新聞熱點
疑難解答