This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Try ExtUtils::Command.t everywhere, not just on Win32
[perl5.git] / lib / ExtUtils / Packlist.pm
CommitLineData
93cd2f30 1package ExtUtils::Packlist;
17f410f9 2
3b825e41 3use 5.006_001;
93cd2f30
MB
4use strict;
5use Carp qw();
d6a466d7 6our $VERSION = '0.04';
93cd2f30
MB
7
8# Used for generating filehandle globs. IO::File might not be available!
9my $fhname = "FH1";
10
11sub mkfh()
12{
13no strict;
14my $fh = \*{$fhname++};
15use strict;
16return($fh);
17}
18
19sub new($$)
20{
21my ($class, $packfile) = @_;
22$class = ref($class) || $class;
23my %self;
24tie(%self, $class, $packfile);
25return(bless(\%self, $class));
26}
27
28sub TIEHASH
29{
30my ($class, $packfile) = @_;
31my $self = { packfile => $packfile };
32bless($self, $class);
33$self->read($packfile) if (defined($packfile) && -f $packfile);
34return($self);
35}
36
37sub STORE
38{
39$_[0]->{data}->{$_[1]} = $_[2];
40}
41
42sub FETCH
43{
44return($_[0]->{data}->{$_[1]});
45}
46
47sub FIRSTKEY
48{
49my $reset = scalar(keys(%{$_[0]->{data}}));
50return(each(%{$_[0]->{data}}));
51}
52
53sub NEXTKEY
54{
55return(each(%{$_[0]->{data}}));
56}
57
58sub EXISTS
59{
60return(exists($_[0]->{data}->{$_[1]}));
61}
62
63sub DELETE
64{
65return(delete($_[0]->{data}->{$_[1]}));
66}
67
68sub CLEAR
69{
70%{$_[0]->{data}} = ();
71}
72
73sub DESTROY
74{
75}
76
77sub read($;$)
78{
79my ($self, $packfile) = @_;
80$self = tied(%$self) || $self;
81
82if (defined($packfile)) { $self->{packfile} = $packfile; }
83else { $packfile = $self->{packfile}; }
84Carp::croak("No packlist filename specified") if (! defined($packfile));
85my $fh = mkfh();
86open($fh, "<$packfile") || Carp::croak("Can't open file $packfile: $!");
87$self->{data} = {};
88my ($line);
89while (defined($line = <$fh>))
90 {
91 chomp $line;
92 my ($key, @kvs) = split(' ', $line);
9b604809 93 $key =~ s!/\./!/!g; # Some .packlists have spurious '/./' bits in the paths
93cd2f30
MB
94 if (! @kvs)
95 {
96 $self->{data}->{$key} = undef;
97 }
98 else
99 {
100 my ($data) = {};
101 foreach my $kv (@kvs)
102 {
103 my ($k, $v) = split('=', $kv);
104 $data->{$k} = $v;
105 }
106 $self->{data}->{$key} = $data;
107 }
108 }
109close($fh);
110}
111
112sub write($;$)
113{
114my ($self, $packfile) = @_;
115$self = tied(%$self) || $self;
116if (defined($packfile)) { $self->{packfile} = $packfile; }
117else { $packfile = $self->{packfile}; }
118Carp::croak("No packlist filename specified") if (! defined($packfile));
119my $fh = mkfh();
120open($fh, ">$packfile") || Carp::croak("Can't open file $packfile: $!");
121foreach my $key (sort(keys(%{$self->{data}})))
122 {
123 print $fh ("$key");
124 if (ref($self->{data}->{$key}))
125 {
126 my $data = $self->{data}->{$key};
127 foreach my $k (sort(keys(%$data)))
128 {
129 print $fh (" $k=$data->{$k}");
130 }
131 }
132 print $fh ("\n");
133 }
134close($fh);
135}
136
137sub validate($;$)
138{
139my ($self, $remove) = @_;
140$self = tied(%$self) || $self;
141my @missing;
142foreach my $key (sort(keys(%{$self->{data}})))
143 {
144 if (! -e $key)
145 {
146 push(@missing, $key);
147 delete($self->{data}{$key}) if ($remove);
148 }
149 }
150return(@missing);
151}
152
9b604809
AB
153sub packlist_file($)
154{
155my ($self) = @_;
156$self = tied(%$self) || $self;
157return($self->{packfile});
158}
159
93cd2f30
MB
1601;
161
162__END__
163
164=head1 NAME
165
166ExtUtils::Packlist - manage .packlist files
167
168=head1 SYNOPSIS
169
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');
175
176 $pl->{'/some/file/name'}++;
177 or
178 $pl->{'/some/other/file/name'} = { type => 'file',
179 from => '/some/file' };
180
181=head1 DESCRIPTION
182
183ExtUtils::Packlist provides a standard way to manage .packlist files.
184Functions are provided to read and write .packlist files. The original
185.packlist format is a simple list of absolute pathnames, one per line. In
186addition, this package supports an extended format, where as well as a filename
187each line may contain a list of attributes in the form of a space separated
188list of key=value pairs. This is used by the installperl script to
189differentiate between files and links, for example.
190
191=head1 USAGE
192
193The hash reference returned by the new() function can be used to examine and
194modify 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
196scalar, the entry written to the .packlist by any subsequent write() will be a
197simple filename. If the value is a hash, the entry written will be the
198filename followed by the key=value pairs from the hash. Reading back the
199.packlist will recreate the original entries.
200
201=head1 FUNCTIONS
202
bbc7dcd2 203=over 4
93cd2f30
MB
204
205=item new()
206
207This takes an optional parameter, the name of a .packlist. If the file exists,
208it will be opened and the contents of the file will be read. The new() method
209returns 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
211key is undef. In the case of new-style .packlists, the value associated with
212each key is a hash containing the key=value pairs following the filename in the
213.packlist.
214
215=item read()
216
217This takes an optional parameter, the name of the .packlist to be read. If
218no file is specified, the .packlist specified to new() will be read. If the
219.packlist does not exist, Carp::croak will be called.
220
221=item write()
222
223This takes an optional parameter, the name of the .packlist to be written. If
224no file is specified, the .packlist specified to new() will be overwritten.
225
226=item validate()
227
228This checks that every file listed in the .packlist actually exists. If an
229argument which evaluates to true is given, any missing files will be removed
230from the internal hash. The return value is a list of the missing files, which
231will be empty if they all exist.
232
9b604809
AB
233=item packlist_file()
234
235This returns the name of the associated .packlist file
236
93cd2f30
MB
237=back
238
ddf41153
AB
239=head1 EXAMPLE
240
241Here's C<modrm>, a little utility to cleanly remove an installed module.
242
243 #!/usr/local/bin/perl -w
244
245 use strict;
246 use IO::Dir;
247 use ExtUtils::Packlist;
248 use ExtUtils::Installed;
249
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);
256 }
257
258 # Find all the installed packages
259 print("Finding all installed modules...\n");
260 my $installed = ExtUtils::Installed->new();
261
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);
272 }
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);
280 }
281 }
282 }
283 }
284
93cd2f30
MB
285=head1 AUTHOR
286
287Alan Burlison <Alan.Burlison@uk.sun.com>
288
289=cut