Start anew
[msysgit.git] / lib / perl5 / 5.6.1 / ExtUtils / Packlist.pm
blob88ea206196ecbe25cc3318e6b25f505c8b78757c
1 package ExtUtils::Packlist;
3 use 5.005_64;
4 use strict;
5 use Carp qw();
6 our $VERSION = '0.03';
8 # Used for generating filehandle globs. IO::File might not be available!
9 my $fhname = "FH1";
11 sub mkfh()
13 no strict;
14 my $fh = \*{$fhname++};
15 use strict;
16 return($fh);
19 sub new($$)
21 my ($class, $packfile) = @_;
22 $class = ref($class) || $class;
23 my %self;
24 tie(%self, $class, $packfile);
25 return(bless(\%self, $class));
28 sub TIEHASH
30 my ($class, $packfile) = @_;
31 my $self = { packfile => $packfile };
32 bless($self, $class);
33 $self->read($packfile) if (defined($packfile) && -f $packfile);
34 return($self);
37 sub STORE
39 $_[0]->{data}->{$_[1]} = $_[2];
42 sub FETCH
44 return($_[0]->{data}->{$_[1]});
47 sub FIRSTKEY
49 my $reset = scalar(keys(%{$_[0]->{data}}));
50 return(each(%{$_[0]->{data}}));
53 sub NEXTKEY
55 return(each(%{$_[0]->{data}}));
58 sub EXISTS
60 return(exists($_[0]->{data}->{$_[1]}));
63 sub DELETE
65 return(delete($_[0]->{data}->{$_[1]}));
68 sub CLEAR
70 %{$_[0]->{data}} = ();
73 sub DESTROY
77 sub read($;$)
79 my ($self, $packfile) = @_;
80 $self = tied(%$self) || $self;
82 if (defined($packfile)) { $self->{packfile} = $packfile; }
83 else { $packfile = $self->{packfile}; }
84 Carp::croak("No packlist filename specified") if (! defined($packfile));
85 my $fh = mkfh();
86 open($fh, "<$packfile") || Carp::croak("Can't open file $packfile: $!");
87 $self->{data} = {};
88 my ($line);
89 while (defined($line = <$fh>))
91 chomp $line;
92 my ($key, @kvs) = split(' ', $line);
93 $key =~ s!/\./!/!g; # Some .packlists have spurious '/./' bits in the paths
94 if (! @kvs)
96 $self->{data}->{$key} = undef;
98 else
100 my ($data) = {};
101 foreach my $kv (@kvs)
103 my ($k, $v) = split('=', $kv);
104 $data->{$k} = $v;
106 $self->{data}->{$key} = $data;
109 close($fh);
112 sub write($;$)
114 my ($self, $packfile) = @_;
115 $self = tied(%$self) || $self;
116 if (defined($packfile)) { $self->{packfile} = $packfile; }
117 else { $packfile = $self->{packfile}; }
118 Carp::croak("No packlist filename specified") if (! defined($packfile));
119 my $fh = mkfh();
120 open($fh, ">$packfile") || Carp::croak("Can't open file $packfile: $!");
121 foreach my $key (sort(keys(%{$self->{data}})))
123 print $fh ("$key");
124 if (ref($self->{data}->{$key}))
126 my $data = $self->{data}->{$key};
127 foreach my $k (sort(keys(%$data)))
129 print $fh (" $k=$data->{$k}");
132 print $fh ("\n");
134 close($fh);
137 sub validate($;$)
139 my ($self, $remove) = @_;
140 $self = tied(%$self) || $self;
141 my @missing;
142 foreach my $key (sort(keys(%{$self->{data}})))
144 if (! -e $key)
146 push(@missing, $key);
147 delete($self->{data}{$key}) if ($remove);
150 return(@missing);
153 sub packlist_file($)
155 my ($self) = @_;
156 $self = tied(%$self) || $self;
157 return($self->{packfile});
162 __END__
164 =head1 NAME
166 ExtUtils::Packlist - manage .packlist files
168 =head1 SYNOPSIS
170 use ExtUtils::Packlist;
171 my ($pl) = ExtUtils::Packlist->new('.packlist');
172 $pl->read('/an/old/.packlist');
173 my @missing_files = $pl->validate();
174 $pl->write('/a/new/.packlist');
176 $pl->{'/some/file/name'}++;
178 $pl->{'/some/other/file/name'} = { type => 'file',
179 from => '/some/file' };
181 =head1 DESCRIPTION
183 ExtUtils::Packlist provides a standard way to manage .packlist files.
184 Functions are provided to read and write .packlist files. The original
185 .packlist format is a simple list of absolute pathnames, one per line. In
186 addition, this package supports an extended format, where as well as a filename
187 each line may contain a list of attributes in the form of a space separated
188 list of key=value pairs. This is used by the installperl script to
189 differentiate between files and links, for example.
191 =head1 USAGE
193 The hash reference returned by the new() function can be used to examine and
194 modify the contents of the .packlist. Items may be added/deleted from the
195 .packlist by modifying the hash. If the value associated with a hash key is a
196 scalar, the entry written to the .packlist by any subsequent write() will be a
197 simple filename. If the value is a hash, the entry written will be the
198 filename followed by the key=value pairs from the hash. Reading back the
199 .packlist will recreate the original entries.
201 =head1 FUNCTIONS
203 =over
205 =item new()
207 This takes an optional parameter, the name of a .packlist. If the file exists,
208 it will be opened and the contents of the file will be read. The new() method
209 returns a reference to a hash. This hash holds an entry for each line in the
210 .packlist. In the case of old-style .packlists, the value associated with each
211 key is undef. In the case of new-style .packlists, the value associated with
212 each key is a hash containing the key=value pairs following the filename in the
213 .packlist.
215 =item read()
217 This takes an optional parameter, the name of the .packlist to be read. If
218 no file is specified, the .packlist specified to new() will be read. If the
219 .packlist does not exist, Carp::croak will be called.
221 =item write()
223 This takes an optional parameter, the name of the .packlist to be written. If
224 no file is specified, the .packlist specified to new() will be overwritten.
226 =item validate()
228 This checks that every file listed in the .packlist actually exists. If an
229 argument which evaluates to true is given, any missing files will be removed
230 from the internal hash. The return value is a list of the missing files, which
231 will be empty if they all exist.
233 =item packlist_file()
235 This returns the name of the associated .packlist file
237 =back
239 =head1 EXAMPLE
241 Here's C<modrm>, a little utility to cleanly remove an installed module.
243 #!/usr/local/bin/perl -w
245 use strict;
246 use IO::Dir;
247 use ExtUtils::Packlist;
248 use ExtUtils::Installed;
250 sub emptydir($) {
251 my ($dir) = @_;
252 my $dh = IO::Dir->new($dir) || return(0);
253 my @count = $dh->read();
254 $dh->close();
255 return(@count == 2 ? 1 : 0);
258 # Find all the installed packages
259 print("Finding all installed modules...\n");
260 my $installed = ExtUtils::Installed->new();
262 foreach my $module (grep(!/^Perl$/, $installed->modules())) {
263 my $version = $installed->version($module) || "???";
264 print("Found module $module Version $version\n");
265 print("Do you want to delete $module? [n] ");
266 my $r = <STDIN>; chomp($r);
267 if ($r && $r =~ /^y/i) {
268 # Remove all the files
269 foreach my $file (sort($installed->files($module))) {
270 print("rm $file\n");
271 unlink($file);
273 my $pf = $installed->packlist($module)->packlist_file();
274 print("rm $pf\n");
275 unlink($pf);
276 foreach my $dir (sort($installed->directory_tree($module))) {
277 if (emptydir($dir)) {
278 print("rmdir $dir\n");
279 rmdir($dir);
285 =head1 AUTHOR
287 Alan Burlison <Alan.Burlison@uk.sun.com>
289 =cut