home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / ExtUtils / Manifest.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  10.8 KB  |  395 lines

  1. package ExtUtils::Manifest;
  2.  
  3. require Exporter;
  4. use Config;
  5. use File::Find;
  6. use File::Copy 'copy';
  7. use Carp;
  8. use strict;
  9.  
  10. use vars qw($VERSION @ISA @EXPORT_OK
  11.         $Is_VMS $Debug $Verbose $Quiet $MANIFEST $found);
  12.  
  13. $VERSION = substr(q$Revision: 1.33 $, 10);
  14. @ISA=('Exporter');
  15. @EXPORT_OK = ('mkmanifest', 'manicheck', 'fullcheck', 'filecheck', 
  16.           'skipcheck', 'maniread', 'manicopy');
  17.  
  18. $Is_VMS = $^O eq 'VMS';
  19. if ($Is_VMS) { require File::Basename }
  20.  
  21. $Debug = 0;
  22. $Verbose = 1;
  23. $Quiet = 0;
  24. $MANIFEST = 'MANIFEST';
  25.  
  26. unless (defined $Config{d_link}) {
  27.     *ln = \&cp;
  28. }
  29.  
  30. sub mkmanifest {
  31.     my $manimiss = 0;
  32.     my $read = maniread() or $manimiss++;
  33.     $read = {} if $manimiss;
  34.     local *M;
  35.     rename $MANIFEST, "$MANIFEST.bak" unless $manimiss;
  36.     open M, ">$MANIFEST" or die "Could not open $MANIFEST: $!";
  37.     my $matches = _maniskip();
  38.     my $found = manifind();
  39.     my($key,$val,$file,%all);
  40.     %all = (%$found, %$read);
  41.     $all{$MANIFEST} = ($Is_VMS ? "$MANIFEST\t\t" : '') . 'This list of files'
  42.         if $manimiss; # add new MANIFEST to known file list
  43.     foreach $file (sort keys %all) {
  44.     next if &$matches($file);
  45.     if ($Verbose){
  46.         warn "Added to $MANIFEST: $file\n" unless exists $read->{$file};
  47.     }
  48.     my $text = $all{$file};
  49.     ($file,$text) = split(/\s+/,$text,2) if $Is_VMS && $text;
  50.     my $tabs = (5 - (length($file)+1)/8);
  51.     $tabs = 1 if $tabs < 1;
  52.     $tabs = 0 unless $text;
  53.     print M $file, "\t" x $tabs, $text, "\n";
  54.     }
  55.     close M;
  56. }
  57.  
  58. sub manifind {
  59.     local $found = {};
  60.     find(sub {return if -d $_;
  61.           (my $name = $File::Find::name) =~ s|./||;
  62.           warn "Debug: diskfile $name\n" if $Debug;
  63.           $name  =~ s#(.*)\.$#\L$1# if $Is_VMS;
  64.           $found->{$name} = "";}, ".");
  65.     $found;
  66. }
  67.  
  68. sub fullcheck {
  69.     _manicheck(3);
  70. }
  71.  
  72. sub manicheck {
  73.     return @{(_manicheck(1))[0]};
  74. }
  75.  
  76. sub filecheck {
  77.     return @{(_manicheck(2))[1]};
  78. }
  79.  
  80. sub skipcheck {
  81.     _manicheck(6);
  82. }
  83.  
  84. sub _manicheck {
  85.     my($arg) = @_;
  86.     my $read = maniread();
  87.     my $found = manifind();
  88.     my $file;
  89.     my(@missfile,@missentry);
  90.     if ($arg & 1){
  91.     foreach $file (sort keys %$read){
  92.         warn "Debug: manicheck checking from $MANIFEST $file\n" if $Debug;
  93.         unless ( exists $found->{$file} ) {
  94.         warn "No such file: $file\n" unless $Quiet;
  95.         push @missfile, $file;
  96.         }
  97.     }
  98.     }
  99.     if ($arg & 2){
  100.     $read ||= {};
  101.     my $matches = _maniskip();
  102.     my $skipwarn = $arg & 4;
  103.     foreach $file (sort keys %$found){
  104.         if (&$matches($file)){
  105.         warn "Skipping $file\n" if $skipwarn;
  106.         next;
  107.         }
  108.         warn "Debug: manicheck checking from disk $file\n" if $Debug;
  109.         unless ( exists $read->{$file} ) {
  110.         warn "Not in $MANIFEST: $file\n" unless $Quiet;
  111.         push @missentry, $file;
  112.         }
  113.     }
  114.     }
  115.     (\@missfile,\@missentry);
  116. }
  117.  
  118. sub maniread {
  119.     my ($mfile) = @_;
  120.     $mfile ||= $MANIFEST;
  121.     my $read = {};
  122.     local *M;
  123.     unless (open M, $mfile){
  124.     warn "$mfile: $!";
  125.     return $read;
  126.     }
  127.     while (<M>){
  128.     chomp;
  129.     next if /^#/;
  130.     if ($Is_VMS) {
  131.         my($file)= /^(\S+)/;
  132.         next unless $file;
  133.         my($base,$dir) = File::Basename::fileparse($file);
  134.         $dir =~ tr/./_/;
  135.         my(@pieces) = split(/\./,$base);
  136.         if (@pieces > 2) { $base = shift(@pieces) . '.' . join('_',@pieces); }
  137.         my $okfile = "$dir$base";
  138.         warn "Debug: Illegal name $file changed to $okfile\n" if $Debug;
  139.         $read->{"\L$okfile"}=$_;
  140.     }
  141.     else { /^(\S+)\s*(.*)/ and $read->{$1}=$2; }
  142.     }
  143.     close M;
  144.     $read;
  145. }
  146.  
  147. sub _maniskip {
  148.     my ($mfile) = @_;
  149.     my $matches = sub {0};
  150.     my @skip ;
  151.     $mfile ||= "$MANIFEST.SKIP";
  152.     local *M;
  153.     return $matches unless -f $mfile;
  154.     open M, $mfile or return $matches;
  155.     while (<M>){
  156.     chomp;
  157.     next if /^#/;
  158.     next if /^\s*$/;
  159.     push @skip, $_;
  160.     }
  161.     close M;
  162.     my $opts = $Is_VMS ? 'oi ' : 'o ';
  163.     my $sub = "\$matches = "
  164.     . "sub { my(\$arg)=\@_; return 1 if "
  165.     . join (" || ",  (map {s!/!\\/!g; "\$arg =~ m/$_/$opts"} @skip), 0)
  166.     . " }";
  167.     eval $sub;
  168.     print "Debug: $sub\n" if $Debug;
  169.     $matches;
  170. }
  171.  
  172. sub manicopy {
  173.     my($read,$target,$how)=@_;
  174.     croak "manicopy() called without target argument" unless defined $target;
  175.     $how ||= 'cp';
  176.     require File::Path;
  177.     require File::Basename;
  178.     my(%dirs,$file);
  179.     $target = VMS::Filespec::unixify($target) if $Is_VMS;
  180.     umask 0 unless $Is_VMS;
  181.     File::Path::mkpath([ $target ],1,$Is_VMS ? undef : 0755);
  182.     foreach $file (keys %$read){
  183.     $file = VMS::Filespec::unixify($file) if $Is_VMS;
  184.     if ($file =~ m!/!) { # Ilya, that hurts, I fear, or maybe not?
  185.         my $dir = File::Basename::dirname($file);
  186.         $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
  187.         File::Path::mkpath(["$target/$dir"],1,$Is_VMS ? undef : 0755);
  188.     }
  189.     cp_if_diff($file, "$target/$file", $how);
  190.     }
  191. }
  192.  
  193. sub cp_if_diff {
  194.     my($from, $to, $how)=@_;
  195.     -f $from or carp "$0: $from not found";
  196.     my($diff) = 0;
  197.     local(*F,*T);
  198.     open(F,$from) or croak "Can't read $from: $!\n";
  199.     if (open(T,$to)) {
  200.     while (<F>) { $diff++,last if $_ ne <T>; }
  201.     $diff++ unless eof(T);
  202.     close T;
  203.     }
  204.     else { $diff++; }
  205.     close F;
  206.     if ($diff) {
  207.     if (-e $to) {
  208.         unlink($to) or confess "unlink $to: $!";
  209.     }
  210.       STRICT_SWITCH: {
  211.         best($from,$to), last STRICT_SWITCH if $how eq 'best';
  212.         cp($from,$to), last STRICT_SWITCH if $how eq 'cp';
  213.         ln($from,$to), last STRICT_SWITCH if $how eq 'ln';
  214.         croak("ExtUtils::Manifest::cp_if_diff " .
  215.           "called with illegal how argument [$how]. " .
  216.           "Legal values are 'best', 'cp', and 'ln'.");
  217.     }
  218.     }
  219. }
  220.  
  221. sub cp {
  222.     my ($srcFile, $dstFile) = @_;
  223.     my ($perm,$access,$mod) = (stat $srcFile)[2,8,9];
  224.     copy($srcFile,$dstFile);
  225.     utime $access, $mod + ($Is_VMS ? 1 : 0), $dstFile;
  226.     chmod(  0444 | ( $perm & 0111 ? 0111 : 0 ),  $dstFile );
  227. }
  228.  
  229. sub ln {
  230.     my ($srcFile, $dstFile) = @_;
  231.     return &cp if $Is_VMS;
  232.     link($srcFile, $dstFile);
  233.     local($_) = $dstFile; # chmod a+r,go-w+X (except "X" only applies to u=x)
  234.     my $mode= 0444 | (stat)[2] & 0700;
  235.     chmod(  $mode | ( $mode & 0100 ? 0111 : 0 ),  $_  );
  236. }
  237.  
  238. sub best {
  239.     my ($srcFile, $dstFile) = @_;
  240.     if (-l $srcFile) {
  241.     cp($srcFile, $dstFile);
  242.     } else {
  243.     ln($srcFile, $dstFile) or cp($srcFile, $dstFile);
  244.     }
  245. }
  246.  
  247. 1;
  248.  
  249. __END__
  250.  
  251. =head1 NAME
  252.  
  253. ExtUtils::Manifest - utilities to write and check a MANIFEST file
  254.  
  255. =head1 SYNOPSIS
  256.  
  257. C<require ExtUtils::Manifest;>
  258.  
  259. C<ExtUtils::Manifest::mkmanifest;>
  260.  
  261. C<ExtUtils::Manifest::manicheck;>
  262.  
  263. C<ExtUtils::Manifest::filecheck;>
  264.  
  265. C<ExtUtils::Manifest::fullcheck;>
  266.  
  267. C<ExtUtils::Manifest::skipcheck;>
  268.  
  269. C<ExtUtild::Manifest::manifind();>
  270.  
  271. C<ExtUtils::Manifest::maniread($file);>
  272.  
  273. C<ExtUtils::Manifest::manicopy($read,$target,$how);>
  274.  
  275. =head1 DESCRIPTION
  276.  
  277. Mkmanifest() writes all files in and below the current directory to a
  278. file named in the global variable $ExtUtils::Manifest::MANIFEST (which
  279. defaults to C<MANIFEST>) in the current directory. It works similar to
  280.  
  281.     find . -print
  282.  
  283. but in doing so checks each line in an existing C<MANIFEST> file and
  284. includes any comments that are found in the existing C<MANIFEST> file
  285. in the new one. Anything between white space and an end of line within
  286. a C<MANIFEST> file is considered to be a comment. Filenames and
  287. comments are seperated by one or more TAB characters in the
  288. output. All files that match any regular expression in a file
  289. C<MANIFEST.SKIP> (if such a file exists) are ignored.
  290.  
  291. Manicheck() checks if all the files within a C<MANIFEST> in the
  292. current directory really do exist. It only reports discrepancies and
  293. exits silently if MANIFEST and the tree below the current directory
  294. are in sync.
  295.  
  296. Filecheck() finds files below the current directory that are not
  297. mentioned in the C<MANIFEST> file. An optional file C<MANIFEST.SKIP>
  298. will be consulted. Any file matching a regular expression in such a
  299. file will not be reported as missing in the C<MANIFEST> file.
  300.  
  301. Fullcheck() does both a manicheck() and a filecheck().
  302.  
  303. Skipcheck() lists all the files that are skipped due to your
  304. C<MANIFEST.SKIP> file.
  305.  
  306. Manifind() retruns a hash reference. The keys of the hash are the
  307. files found below the current directory.
  308.  
  309. Maniread($file) reads a named C<MANIFEST> file (defaults to
  310. C<MANIFEST> in the current directory) and returns a HASH reference
  311. with files being the keys and comments being the values of the HASH.
  312. Blank lines and lines which start with C<#> in the C<MANIFEST> file
  313. are discarded.
  314.  
  315. I<Manicopy($read,$target,$how)> copies the files that are the keys in
  316. the HASH I<%$read> to the named target directory. The HASH reference
  317. I<$read> is typically returned by the maniread() function. This
  318. function is useful for producing a directory tree identical to the
  319. intended distribution tree. The third parameter $how can be used to
  320. specify a different methods of "copying". Valid values are C<cp>,
  321. which actually copies the files, C<ln> which creates hard links, and
  322. C<best> which mostly links the files but copies any symbolic link to
  323. make a tree without any symbolic link. Best is the default.
  324.  
  325. =head1 MANIFEST.SKIP
  326.  
  327. The file MANIFEST.SKIP may contain regular expressions of files that
  328. should be ignored by mkmanifest() and filecheck(). The regular
  329. expressions should appear one on each line. Blank lines and lines
  330. which start with C<#> are skipped.  Use C<\#> if you need a regular
  331. expression to start with a sharp character. A typical example:
  332.  
  333.     \bRCS\b
  334.     ^MANIFEST\.
  335.     ^Makefile$
  336.     ~$
  337.     \.html$
  338.     \.old$
  339.     ^blib/
  340.     ^MakeMaker-\d
  341.  
  342. =head1 EXPORT_OK
  343.  
  344. C<&mkmanifest>, C<&manicheck>, C<&filecheck>, C<&fullcheck>,
  345. C<&maniread>, and C<&manicopy> are exportable.
  346.  
  347. =head1 GLOBAL VARIABLES
  348.  
  349. C<$ExtUtils::Manifest::MANIFEST> defaults to C<MANIFEST>. Changing it
  350. results in both a different C<MANIFEST> and a different
  351. C<MANIFEST.SKIP> file. This is useful if you want to maintain
  352. different distributions for different audiences (say a user version
  353. and a developer version including RCS).
  354.  
  355. C<$ExtUtils::Manifest::Quiet> defaults to 0. If set to a true value,
  356. all functions act silently.
  357.  
  358. =head1 DIAGNOSTICS
  359.  
  360. All diagnostic output is sent to C<STDERR>.
  361.  
  362. =over
  363.  
  364. =item C<Not in MANIFEST:> I<file>
  365.  
  366. is reported if a file is found, that is missing in the C<MANIFEST>
  367. file which is excluded by a regular expression in the file
  368. C<MANIFEST.SKIP>.
  369.  
  370. =item C<No such file:> I<file>
  371.  
  372. is reported if a file mentioned in a C<MANIFEST> file does not
  373. exist.
  374.  
  375. =item C<MANIFEST:> I<$!>
  376.  
  377. is reported if C<MANIFEST> could not be opened.
  378.  
  379. =item C<Added to MANIFEST:> I<file>
  380.  
  381. is reported by mkmanifest() if $Verbose is set and a file is added
  382. to MANIFEST. $Verbose is set to 1 by default.
  383.  
  384. =back
  385.  
  386. =head1 SEE ALSO
  387.  
  388. L<ExtUtils::MakeMaker> which has handy targets for most of the functionality.
  389.  
  390. =head1 AUTHOR
  391.  
  392. Andreas Koenig <F<koenig@franz.ww.TU-Berlin.DE>>
  393.  
  394. =cut
  395.