Show
Ignore:
Timestamp:
12/08/08 04:13:18 (4 years ago)
Author:
ktat
Message:

delete duplicated code; fix bug in extracting files

Location:
lang/perl/Helper-Simple/trunk/lib/Helper
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Helper-Simple/trunk/lib/Helper/Simple.pm

    r25994 r26080  
    1010use Archive::Zip qw/:ERROR_CODES/; 
    1111use Data::Dumper; 
    12 use IO::File (); 
    1312use Template (); 
    1413use Tie::Hash::Indexed (); 
     
    284283  my @path_exchange = @{$class->path_exchange}; 
    285284  my $path_exchanger = @path_exchange ? sub { 
    286     my ($dir, $omit) = @_; 
     285    my ($dir) = @_; 
    287286    $$dir =~ s/^\Q$root_dir\E//; 
    288287    foreach my $ex (@path_exchange) { 
     
    309308} 
    310309 
    311 sub setup_helper { 
     310sub setup_script { 
    312311  my ($class, $helper_place, $generator) = @_; 
    313312  my $helper; 
     
    363362      _ok(exists  => $dir . $file); 
    364363    } elsif (ref $c and my $compress = $c->{extract}) { 
    365       my $dir, $pkg->_extract($compress, $file, decode_base64($c->{content}), $path_exchanger, $c); 
     364      $pkg->_extract($compress, $file, decode_base64($c->{content}), $path_exchanger, $c); 
    366365      if (defined $c->{render_file} and $c->{render_file}) { 
    367366        $pkg->_render_file_recursive($c->{extract_to}, $path_exchanger); 
     
    551550    my $res = $pkg->_get_from_url($target, $option); 
    552551    if ($res->is_success and $content = $res->content) { 
    553       _info('download success', $target); 
     552      _ok('download', $target); 
    554553    } else { 
    555       _ng('download', $target); 
     554      if ($res->status_line eq '200 OK') { 
     555        _ng('download', $target . "(empty content)"); 
     556      } else { 
     557        _ng('download', $target . "(" . $res->status_line . ")"); 
     558      } 
    556559      $success = 0; 
    557560    } 
    558561    if ($success) { 
    559562      if (my $compress = $option->{extract}) { 
    560         $pkg->_extract($compress, $target, $content, undef, $option); 
     563        $pkg->_extract($compress, $target, $content, $path_exchanger, $option); 
    561564      } else { 
    562565        my $name = $option->{filename}; 
     
    571574sub _extract { 
    572575  my ($pkg, $compress, $target, $content, $path_exchanger, $option) = @_; 
     576  if ($compress eq 1) { 
     577    if ($target =~/\.(tar\.gz|tar\.bz2|zip|t[bg]z)$/) { 
     578      $compress = $1; 
     579    } else { 
     580      Carp::croak "cannot detect compress type from extention: $target"; 
     581    } 
     582  } 
    573583  $compress =~ s/\./_/g; 
    574584  Carp::croak "cannot use $compress as compress option." unless $pkg->can("_extract_" . $compress); 
    575585 
    576586  my $extract = '_extract_' . $compress; 
    577   if (my $dir = $pkg->$extract($target, $content, $path_exchanger, $option)) { 
    578     _ok('extracted to', $dir); 
     587  my $fh; 
     588  if ($content) { 
     589    $fh = IO::String->new(\$content); 
     590  } else { 
     591    $fh = IO::String->new(\do{slurp($target)}); 
     592  } 
     593  if ($pkg->$extract($target, $fh, $path_exchanger, $option)) { 
     594    my $dir = $pkg->dir; 
     595    _ok('extracted to', $dir . ($option->{extract_to} || '')); 
    579596    if (defined $path_exchanger) { 
    580597      my $_dir = $dir; 
    581598      $path_exchanger->(\$_dir); 
    582       _info("path is exchanged", "$dir => $_dir"); 
    583     } 
    584     return $dir; 
     599      _info("path is exchanged", "$dir => $_dir") if $dir ne $_dir; 
     600    } 
     601    return 1; 
    585602  } else { 
    586603    _ng('extract', $target); 
     
    616633} 
    617634 
     635sub _modify_filename { 
     636  my ($self, $filename, $option, $path_exchanger) = @_; 
     637  if (defined $option->{omit}) { 
     638    unless ($$filename =~s/^\Q$option->{omit}\E//) { 
     639      return 0; 
     640    } 
     641  } 
     642  $$filename = $self->dir 
     643    . ($option->{extract_to} ? $option->{extract_to} . '/': '') . $$filename; 
     644  if (defined $path_exchanger) { 
     645    $path_exchanger->($filename); 
     646  } 
     647  return 1; 
     648} 
     649 
    618650sub _extract_zip { 
    619   my ($pkg, $target, $content, $path_exchanger, $option) = @_; 
    620   my $dir = $pkg->dir . ($option->{extract_to} ||= ''); 
    621   $dir .= '/' unless $dir =~ m{/$}; 
    622   my $fh; 
    623   if ($content) { 
    624     $fh = IO::String->new($content); 
    625   } else { 
    626     $fh = IO::File->new($target) or die "cannot open $target"; 
    627   } 
     651  my ($pkg, $target, $fh, $path_exchanger, $option) = @_; 
    628652  my $zip = Archive::Zip->new(); 
    629653  if ($zip->readFromFileHandle($fh) == AZ_OK) { 
    630     _info('extract', $target); 
     654    _info('extract', $pkg->dir . $target); 
    631655    foreach my $member ($zip->members) { 
    632656      my $filename = $member->fileName; 
    633       if (defined $option->{omit}) { 
    634         $filename =~s/^\Q$option->{omit}\E//; 
    635       } 
    636       $filename =  $dir . $filename; 
    637       if (defined $path_exchanger) { 
    638         $path_exchanger->(\$filename); 
    639       } 
    640       $zip->extractMember($member, $filename); 
     657      if ($pkg->_modify_filename(\$filename, $option, $path_exchanger)) { 
     658        $zip->extractMember($member, $filename); 
     659      } 
    641660      chmod $member->unixFileAttributes & ~ umask, $filename; 
    642661    } 
    643     return $dir; 
     662    return 1; 
    644663  } else { 
    645664    return 0; 
     
    651670 
    652671sub _extract_tar_gz { 
    653   my ($pkg, $target, $content, $path_exchanger, $option) = @_; 
    654   my $dir = $pkg->dir . ($option->{extract_to} ||= ''); 
    655   $dir .= '/' unless $dir =~ m{/$}; 
    656   my $fh; 
    657   if ($content) { 
    658     $fh = IO::String->new($content); 
    659   } else { 
    660     $fh = IO::File->new($target) or die "cannot open $target"; 
    661   } 
     672  my ($pkg, $target, $fh, $path_exchanger, $option) = @_; 
    662673  $fh = IO::Uncompress::Gunzip->new($fh); 
    663674  my $tar = Archive::Tar->new(); 
     
    666677    foreach my $file ($tar->list_files) { 
    667678      my $filename = $file; 
    668       if (defined $option->{omit}) { 
    669         $filename =~s/^\Q$option->{omit}\E//; 
    670       } 
    671       $filename =  $dir . $filename; 
    672       if (defined $path_exchanger) { 
    673         $path_exchanger->(\$filename); 
    674       } 
    675       $tar->extract_file($file, $filename); 
    676     } 
    677     return $dir; 
     679      if ($pkg->_modify_filename(\$filename, $option, $path_exchanger)) { 
     680        $tar->extract_file($file, $filename); 
     681      } 
     682    } 
     683    return 1; 
    678684  } else { 
    679685    return 0; 
     
    682688 
    683689sub _extract_tar_bz2 { 
    684   my ($pkg, $target, $content, $path_exchanger, $option) = @_; 
    685   my $dir = $pkg->dir . ($option->{extract_to} ||= ''); 
    686   $dir .= '/' unless $dir =~ m{/$}; 
    687   my $fh; 
    688   if ($content) { 
    689     $fh = IO::String->new($content); 
    690   } else { 
    691     $fh = IO::File->new($target) or die "cannot open $target"; 
    692   } 
     690  my ($pkg, $target, $fh, $path_exchanger, $option) = @_; 
    693691  $fh = IO::Uncompress::Bunzip2->new($fh); 
    694692  if (my $tar = Archive::Tar->new($fh)) { 
     
    696694    foreach my $file ($tar->list_files) { 
    697695      my $filename = $file; 
    698       if (defined $option->{omit}) { 
    699         $filename =~s/^\Q$option->{omit}\E//; 
    700       } 
    701       $filename =  $dir . $filename; 
    702       if (defined $path_exchanger) { 
    703         $path_exchanger->(\$filename); 
    704       } 
    705       $tar->extract_file($file, $filename); 
    706     } 
    707     return $dir; 
     696      if ($pkg->_modify_filename(\$filename, $option, $path_exchanger)) { 
     697        $tar->extract_file($file, $filename); 
     698      } 
     699    } 
     700    return 1; 
    708701  } else { 
    709702    return 0; 
     
    787780=head1 NAME 
    788781 
    789 Helper::Simple - simply create your own helper 
     782Helper::Simple - simply create your own setupper/helper 
    790783 
    791784=head1 VERSION 
     
    815808    use Helper::Simple; 
    816809     
    817     YourApp::Helper->setup_helper('/path/to/yourhelper.pl'); 
     810    YourApp::Helper->setup_script('/path/to/yourhelper.pl'); 
    818811     
    819812    1; 
     
    872865This create files under ./path. 
    873866 
    874 =head2 setup_helper 
    875  
    876  YourHelper->setup_helper('./path/to/helper.pl'); 
     867=head2 setup_script 
     868 
     869 YourHelper->setup_script('./path/to/helper.pl'); 
    877870 
    878871This create helper as ./path/to/helper.pl 
    879872If you don't like default helepr script. you can set your own generator. 
    880873 
    881  YourHelper->setup_helper('./path/to/helper.pl', sub { 
     874 YourHelper->setup_script('./path/to/helper.pl', sub { 
    882875   my ($class, $data) = @_; 
    883876   # .... 
     
    941934 
    942935This copy file/directory from local. 
    943 For $option, you can use C<extract>, c<omit> and C<render_file> of C<download>. 
     936 
     937For $option, see the follwoing. 
     938 
     939=head3 render_file => 0/1 
     940 
     941If you want to render extracted files, set this value true. 
     942default values is 0. 
     943 
     944=head3 extract => 'zip'/'tar.gz'/'tar.bz2' ... 
     945 
     946If $path_to_file is compressed file and you want to extract it, 
     947give the extention. 
     948 
     949 1           ... judge from extention 
     950 zip         ... zipped file 
     951 tar.gz/tgz  ... tar gz 
     952 tar.bz2/tbz ... tar bzip2 
    944953 
    945954=head2 download / dl 
     
    950959For $option, see the following. 
    951960 
    952 =head3 render => 0/1 
    953  
    954 If you want to render extracted files, set this value true. 
    955 default values is 0. 
    956  
    957961=head3 extract => zip 
    958962 
     
    960964give the extention. 
    961965 
    962  zip ... zipped file 
    963  
    964 In future, some formats will be added, gz, tar.gz, lzh etc. 
     966 1           ... judge from extention 
     967 zip         ... zipped file 
     968 tar.gz/tgz  ... tar gz 
     969 tar.bz2/tbz ... tar bzip2 
    965970 
    966971=head3 omit => '/path/for/omit' 
     
    970975give the directory name to omit. 
    971976 
     977If you specify omit and path is not matched, 
     978such files are skipped to extract. 
     979 
    972980=head3 filename => 'your_favolite_filename' 
    973981 
     
    990998=head2 var 
    991999 
    992 Set variables for Template module used in C<file>.. 
    993 This is inheritable vars. Namespaces under the namespace which this function is used, 
    994 these 
     1000Set variables for Template module used in C<file> or C<copy> with render_file option is true. 
     1001This is inheritable vars. In the namespaces under the namespace which this function is used, 
     1002defined vars can be used. 
    9951003 
    9961004 var { 
     
    10011009=head2 my_var 
    10021010 
    1003 Set variables for Template module used in C<file>. 
     1011Set variables for Template module used in C<file> or C<copy> with render_file option is true. 
    10041012This is like C<var>, but B<not> inheritable. 
    10051013 
     
    10231031=head2 here 
    10241032 
     1033It is not recommended to use. 
     1034 
    10251035From this function, you can use all method of Helper::Simple. 
    10261036It returns the called namespace object. 
    10271037 
    1028 It is not recommended to use. 
    1029  
    10301038 package YourHelper::Llib::Hoge; 
    10311039  
    10321040 here->dir; # returns "lib/Hoge/" 
    10331041 
    1034 =head1 TODO 
    1035  
    1036 =over 4 
    1037  
    1038 =item need helper program to create template. 
    1039  
    1040 like the following; 
    1041  
    1042  helper-template -template catalyst YourApp 
    1043  helper-setup -template catalyst YourApp ./yourapp.pl 
    1044  
    1045 =back 
     1042=head1 EXAMPLE 
     1043 
     1044It is very simple catalyst setupper. 
     1045 
     1046At first, create catalyst template. 
     1047 
     1048 % catalyst.pl CatalystTemplate 
     1049 % cd ./CatalystTemplate 
     1050 % ./script/catalysttempalte_create.pl View TT 
     1051 % find -type f | xargs perl -p -i -e 's{CatalystTemplate}{\[% appclass %\]}g' 
     1052 % find -type f | xargs perl -p -i -e 's{catalysttemplate}{\[% appname %\]}g' 
     1053 
     1054Next, create your catalyst setupper. 
     1055 
     1056 #!/usr/bin/perl 
     1057  
     1058 my $appclass = $ARGV[0]; 
     1059 my $appname  = lc $ARGV[0]; 
     1060  
     1061 die "class name & helper name" unless $ARGV[1]; 
     1062  
     1063 package MyHelper; 
     1064  
     1065 use strict; 
     1066 use lib qw(../lib); 
     1067 use Helper::Simple; 
     1068  
     1069 var { 
     1070   appclass => $appclass, 
     1071   appname  => $appname, 
     1072 }; 
     1073  
     1074 copy ['CatalystTemplate', {render_file => 1}]; 
     1075  
     1076 package MyHelper::CatalystTemplate::root::static::js; 
     1077  
     1078 use Helper::Simple::Const; 
     1079  
     1080 copy ['jquery.ui-1.5.2.tar.bz2', {omit => 'jquery.ui-1.5.2', extract => 'tar.bz2'}]; 
     1081 dl JQUERY_SUGGEST; 
     1082  
     1083 package main; 
     1084  
     1085 MyHelper->path_exchange('CatalystTemplate/lib/CatalystTemplate', 'CatalystTemplate/lib/' . $appclass); 
     1086 MyHelper->path_exchange('CatalystTemplate/script/catalysttemplate',  'CatalystTemplate/script/' . $appname ); 
     1087 MyHelper->path_exchange('CatalystTemplate/root/static/js/jquery.ui-1.5.2', 'CatalystTemplate/root/static/js/jquery.ui'); 
     1088 MyHelper->path_exchange('CatalystTemplate', $appclass); 
     1089 MyHelper->setup_script($ARGV[1]); 
     1090 
    10461091 
    10471092=head1 AUTHOR 
  • lang/perl/Helper-Simple/trunk/lib/Helper/Simple/Const.pm

    r25992 r26080  
    22 
    33use constant { 
    4     JQUERY_UI                => ['http://jquery-ui.googlecode.com/files/jquery.ui-1.5.2.zip', {extract => 'zip'} ], 
     4    JQUERY_UI                => ['http://jquery-ui.googlecode.com/files/jquery.ui-1.5.2.zip', {extract => '1'} ], 
    55    JQUERY_SUGGEST           => ['http://www.vulgarisoip.com/files/jquery.suggest.js'], 
    66    JQUERY_MULTI_FILE_UPLOAD => ['http://www.fyneworks.com/jquery/multiple-file-upload/multiple-file-upload.zip' ],