- 论坛徽章:
- 0
|
本帖最后由 hp_truth 于 2010-09-10 14:26 编辑
CPAN命令好像是用Net::FTP来获取下载地址的,但是我的ftp端口被禁了,所以不能用cpan命令来安装了,不过http端口可以用,以前都是一个一个下载安装,最近觉得有必要自己写一个自动安装的脚本。 到目前为止还比较好使,大家感兴趣的可以试一下 )
- #!/usr/local/bin/perl5
- use warnings;
- use strict;
- use WWW::Mechanize;
- use HTML::TreeBuilder;
- use HTML::Query 'query';
- use Cwd;
- use File::Basename;
- die "Usage: $0 module_name\n" unless @ARGV;
- my ($module) = @ARGV;
- my $prefix = "~/perl5";
- my $tmp_dir = "~/test";
- my $http_proxy = "http://proxy.com:80";
- my $mech = WWW::Mechanize->new(autocheck=>1);
- $mech->stack_depth(0);
- $mech->proxy(['http'], $http_proxy) if (! $ENV{http_proxy});
- print "you want to install $module...\n";
- &install($module);
- ###########################
- sub get_url_from_cpan {
- my $module = shift;
- my $cpan_url = "http://search.cpan.org";
- print "get search_page ...\nurl=$cpan_url\n";
- # search the module in CPAN main page
- $mech->get($cpan_url);
- $mech->set_fields(query => $module);
- $mech->submit();
- my $search_page_content = $mech->content();
- # parse the search page to find the url of this module
- my $tree = HTML::TreeBuilder->new;
- my $success = $tree->parse($search_page_content);
- $success = $tree->eof();
- die "parse search_page_content failed: $!\n" unless $success;
- my $h2s = $tree->query('h2.sr'); #<h2> with sr class
- my $href;
- for my $h2 (@$h2s) {
- my $module_name = $h2->query('a[href]')->as_trimmed_text->[0];
- print "module_name: $module_name, module: $module\n";
- if ($module_name eq $module) {
- $href = $h2->query('a[href]')->attr('href')->[0];
- last;
- }
- }
- # fetch the page of this module from CPAN
- my $module_url = $cpan_url . $href;
- print "get module_page ...\nurl=$module_url\n";
- $mech->get($module_url);
- my $module_page_content = $mech->content();
- undef $tree;
- $tree = HTML::TreeBuilder->new;
- $success = $tree->parse($module_page_content);
- $success = $tree->eof();
- die "parse module_page_content failed: $!\n" unless $success;
- # find the download url for this module
- my $ps = $tree->query("p[style]");
- for my $p (@$ps) {
- if ($p->as_trimmed_text =~ m/Download:/) {
- my $href = $p->query('a[href]')->attr('href')->[0];
- print "Path = " . $cpan_url . $href . "\n";
- return $cpan_url . $href;
- }
- }
- return undef; # fail if reach here
- }
- sub install {
- my $module = shift;
- my @suffixlist = (".tar.gz", ".tgz");
- my $suffix = ".tar.gz";
- my $path;
- my $cur_dir = getcwd;
- chdir($tmp_dir);
- print "cur_dir: $cur_dir, chidr to: " . getcwd . "\n";
- # judge if file already exists.
- # for example, file A-B.tar.gz for module A::B
- my @files = grep {-f $_} <*>;
- my $dir = $module;
- $dir =~ s/::/-/g;
- @files = grep /^$dir-(\d.)+/, @files;
- print "files=@files\n";
- # if module.tar.gz does not exists, we wget it from CPAN
- # else use it directly
- unless (@files) {
- my $url = &get_url_from_cpan($module);
- ($dir, $path, $suffix) = fileparse($url, @suffixlist);
- system("wget $url 1>/dev/null 2>&1");
- }
- else {
- ($dir, $path ,$suffix) = fileparse($files[0], @suffixlist);
- }
- # unpack the module.tar.gz file
- # and chdir to the related dir
- print "dir = $dir\n";
- system("gunzip < $dir$suffix|tar -xf -");
- chdir($dir);
- # start to install this module
- # 1. perl5 Makefile.PL prefix=$prefix
- # 2. make
- # 3. make install
- # If there is dependency, we should install them first
- # until all dependency are installed.
- my $dependency = 1;
- while ($dependency) {
- my $mf_result = `perl5 Makefile.PL prefix=$prefix 2>&1`;
- if ($mf_result =~ m/Warning: prerequisite (\S*) .* not found/) {
- my ($new_module) = $1 ;
- $new_module =~ s/-(\d.)+$//;
- $new_module =~ s/-/::/g;
- print ">" x 30 . "\n";
- print "$new_module shoulde be installed first\n";
- system("~/bin/cpan_install.pl", $new_module);
- }
- else {
- $dependency = 0;
- }
- }
- system("make && make install");
- chdir($cur_dir);
- }
复制代码 |
|