Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
11 KiB

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