Updated to Git v1.8.4
[msysgit.git] / lib / perl5 / 5.8.8 / ExtUtils / Packlist.pm
blob11ab637150bc549c67b4c56c1665e2377e9bcffb
1 package ExtUtils::Packlist;
3 use 5.00503;
4 use strict;
5 use Carp qw();
6 use vars qw($VERSION);
7 $VERSION = '0.04';
9 # Used for generating filehandle globs. IO::File might not be available!
10 my $fhname = "FH1";
12 sub mkfh()
14 no strict;
15 my $fh = \*{$fhname++};
16 use strict;
17 return($fh);
20 sub new($$)
22 my ($class, $packfile) = @_;
23 $class = ref($class) || $class;
24 my %self;
25 tie(%self, $class, $packfile);
26 return(bless(\%self, $class));
29 sub TIEHASH
31 my ($class, $packfile) = @_;
32 my $self = { packfile => $packfile };
33 bless($self, $class);
34 $self->read($packfile) if (defined($packfile) && -f $packfile);
35 return($self);
38 sub STORE
40 $_[0]->{data}->{$_[1]} = $_[2];
43 sub FETCH
45 return($_[0]->{data}->{$_[1]});
48 sub FIRSTKEY
50 my $reset = scalar(keys(%{$_[0]->{data}}));
51 return(each(%{$_[0]->{data}}));
54 sub NEXTKEY
56 return(each(%{$_[0]->{data}}));
59 sub EXISTS
61 return(exists($_[0]->{data}->{$_[1]}));
64 sub DELETE
66 return(delete($_[0]->{data}->{$_[1]}));
69 sub CLEAR
71 %{$_[0]->{data}} = ();
74 sub DESTROY
78 sub read($;$)
80 my ($self, $packfile) = @_;
81 $self = tied(%$self) || $self;
83 if (defined($packfile)) { $self->{packfile} = $packfile; }
84 else { $packfile = $self->{packfile}; }
85 Carp::croak("No packlist filename specified") if (! defined($packfile));
86 my $fh = mkfh();
87 open($fh, "<$packfile") || Carp::croak("Can't open file $packfile: $!");
88 $self->{data} = {};
89 my ($line);
90 while (defined($line = <$fh>))
92 chomp $line;
93 my ($key, @kvs) = $line;
94 if ($key =~ /^(.*?)( \w+=.*)$/)
96 $key = $1;
97 @kvs = split(' ', $2);
99 $key =~ s!/\./!/!g; # Some .packlists have spurious '/./' bits in the paths
100 if (! @kvs)
102 $self->{data}->{$key} = undef;
104 else
106 my ($data) = {};
107 foreach my $kv (@kvs)
109 my ($k, $v) = split('=', $kv);
110 $data->{$k} = $v;
112 $self->{data}->{$key} = $data;
115 close($fh);
118 sub write($;$)
120 my ($self, $packfile) = @_;
121 $self = tied(%$self) || $self;
122 if (defined($packfile)) { $self->{packfile} = $packfile; }
123 else { $packfile = $self->{packfile}; }
124 Carp::croak("No packlist filename specified") if (! defined($packfile));
125 my $fh = mkfh();
126 open($fh, ">$packfile") || Carp::croak("Can't open file $packfile: $!");
127 foreach my $key (sort(keys(%{$self->{data}})))
129 print $fh ("$key");
130 if (ref($self->{data}->{$key}))
132 my $data = $self->{data}->{$key};
133 foreach my $k (sort(keys(%$data)))
135 print $fh (" $k=$data->{$k}");
138 print $fh ("\n");
140 close($fh);
143 sub validate($;$)
145 my ($self, $remove) = @_;
146 $self = tied(%$self) || $self;
147 my @missing;
148 foreach my $key (sort(keys(%{$self->{data}})))
150 if (! -e $key)
152 push(@missing, $key);
153 delete($self->{data}{$key}) if ($remove);
156 return(@missing);
159 sub packlist_file($)
161 my ($self) = @_;
162 $self = tied(%$self) || $self;
163 return($self->{packfile});
168 __END__
170 =head1 NAME
172 ExtUtils::Packlist - manage .packlist files
174 =head1 SYNOPSIS
176 use ExtUtils::Packlist;
177 my ($pl) = ExtUtils::Packlist->new('.packlist');
178 $pl->read('/an/old/.packlist');
179 my @missing_files = $pl->validate();
180 $pl->write('/a/new/.packlist');
182 $pl->{'/some/file/name'}++;
184 $pl->{'/some/other/file/name'} = { type => 'file',
185 from => '/some/file' };
187 =head1 DESCRIPTION
189 ExtUtils::Packlist provides a standard way to manage .packlist files.
190 Functions are provided to read and write .packlist files. The original
191 .packlist format is a simple list of absolute pathnames, one per line. In
192 addition, this package supports an extended format, where as well as a filename
193 each line may contain a list of attributes in the form of a space separated
194 list of key=value pairs. This is used by the installperl script to
195 differentiate between files and links, for example.
197 =head1 USAGE
199 The hash reference returned by the new() function can be used to examine and
200 modify the contents of the .packlist. Items may be added/deleted from the
201 .packlist by modifying the hash. If the value associated with a hash key is a
202 scalar, the entry written to the .packlist by any subsequent write() will be a
203 simple filename. If the value is a hash, the entry written will be the
204 filename followed by the key=value pairs from the hash. Reading back the
205 .packlist will recreate the original entries.
207 =head1 FUNCTIONS
209 =over 4
211 =item new()
213 This takes an optional parameter, the name of a .packlist. If the file exists,
214 it will be opened and the contents of the file will be read. The new() method
215 returns a reference to a hash. This hash holds an entry for each line in the
216 .packlist. In the case of old-style .packlists, the value associated with each
217 key is undef. In the case of new-style .packlists, the value associated with
218 each key is a hash containing the key=value pairs following the filename in the
219 .packlist.
221 =item read()
223 This takes an optional parameter, the name of the .packlist to be read. If
224 no file is specified, the .packlist specified to new() will be read. If the
225 .packlist does not exist, Carp::croak will be called.
227 =item write()
229 This takes an optional parameter, the name of the .packlist to be written. If
230 no file is specified, the .packlist specified to new() will be overwritten.
232 =item validate()
234 This checks that every file listed in the .packlist actually exists. If an
235 argument which evaluates to true is given, any missing files will be removed
236 from the internal hash. The return value is a list of the missing files, which
237 will be empty if they all exist.
239 =item packlist_file()
241 This returns the name of the associated .packlist file
243 =back
245 =head1 EXAMPLE
247 Here's C<modrm>, a little utility to cleanly remove an installed module.
249 #!/usr/local/bin/perl -w
251 use strict;
252 use IO::Dir;
253 use ExtUtils::Packlist;
254 use ExtUtils::Installed;
256 sub emptydir($) {
257 my ($dir) = @_;
258 my $dh = IO::Dir->new($dir) || return(0);
259 my @count = $dh->read();
260 $dh->close();
261 return(@count == 2 ? 1 : 0);
264 # Find all the installed packages
265 print("Finding all installed modules...\n");
266 my $installed = ExtUtils::Installed->new();
268 foreach my $module (grep(!/^Perl$/, $installed->modules())) {
269 my $version = $installed->version($module) || "???";
270 print("Found module $module Version $version\n");
271 print("Do you want to delete $module? [n] ");
272 my $r = <STDIN>; chomp($r);
273 if ($r && $r =~ /^y/i) {
274 # Remove all the files
275 foreach my $file (sort($installed->files($module))) {
276 print("rm $file\n");
277 unlink($file);
279 my $pf = $installed->packlist($module)->packlist_file();
280 print("rm $pf\n");
281 unlink($pf);
282 foreach my $dir (sort($installed->directory_tree($module))) {
283 if (emptydir($dir)) {
284 print("rmdir $dir\n");
285 rmdir($dir);
291 =head1 AUTHOR
293 Alan Burlison <Alan.Burlison@uk.sun.com>
295 =cut