1 package ExtUtils::Packlist;
7 use vars qw($VERSION $Relocations);
9 $VERSION = eval $VERSION;
11 # Used for generating filehandle globs. IO::File might not be available!
18 Make a filehandle. Same kind of idea as Symbol::gensym().
25 my $fh = \*{$fhname++};
30 =item __find_relocations
32 Works out what absolute paths in the configuration have been located at run
33 time relative to $^X, and generates a regexp that matches them
39 sub __find_relocations
42 while (my ($raw_key, $raw_val) = each %Config) {
43 my $exp_key = $raw_key . "exp";
44 next unless exists $Config{$exp_key};
45 next unless $raw_val =~ m!\.\.\./!;
46 $paths{$Config{$exp_key}}++;
48 # Longest prefixes go first in the alternatives
49 my $alternations = join "|", map {quotemeta $_}
50 sort {length $b <=> length $a} keys %paths;
51 qr/^($alternations)/o;
56 my ($class, $packfile) = @_;
57 $class = ref($class) || $class;
59 tie(%self, $class, $packfile);
60 return(bless(\%self, $class));
65 my ($class, $packfile) = @_;
66 my $self = { packfile => $packfile };
68 $self->read($packfile) if (defined($packfile) && -f $packfile);
74 $_[0]->{data}->{$_[1]} = $_[2];
79 return($_[0]->{data}->{$_[1]});
84 my $reset = scalar(keys(%{$_[0]->{data}}));
85 return(each(%{$_[0]->{data}}));
90 return(each(%{$_[0]->{data}}));
95 return(exists($_[0]->{data}->{$_[1]}));
100 return(delete($_[0]->{data}->{$_[1]}));
105 %{$_[0]->{data}} = ();
114 my ($self, $packfile) = @_;
115 $self = tied(%$self) || $self;
117 if (defined($packfile)) { $self->{packfile} = $packfile; }
118 else { $packfile = $self->{packfile}; }
119 Carp::croak("No packlist filename specified") if (! defined($packfile));
121 open($fh, "<$packfile") || Carp::croak("Can't open file $packfile: $!");
124 while (defined($line = <$fh>))
127 my ($key, $data) = $line;
128 if ($key =~ /^(.*?)( \w+=.*)$/)
131 $data = { map { split('=', $_) } split(' ', $2)};
133 if ($Config{userelocatableinc} && $data->{relocate_as})
137 my ($vol, $dir) = File::Spec->splitpath($packfile);
138 my $newpath = File::Spec->catpath($vol, $dir, $data->{relocate_as});
139 $key = Cwd::realpath($newpath);
142 $key =~ s!/\./!/!g; # Some .packlists have spurious '/./' bits in the paths
143 $self->{data}->{$key} = $data;
150 my ($self, $packfile) = @_;
151 $self = tied(%$self) || $self;
152 if (defined($packfile)) { $self->{packfile} = $packfile; }
153 else { $packfile = $self->{packfile}; }
154 Carp::croak("No packlist filename specified") if (! defined($packfile));
156 open($fh, ">$packfile") || Carp::croak("Can't open file $packfile: $!");
157 foreach my $key (sort(keys(%{$self->{data}})))
159 my $data = $self->{data}->{$key};
160 if ($Config{userelocatableinc}) {
161 $Relocations ||= __find_relocations();
162 if ($packfile =~ $Relocations) {
163 # We are writing into a subdirectory of a run-time relocated
164 # path. Figure out if the this file is also within a subdir.
166 if (File::Spec->no_upwards(File::Spec->abs2rel($key, $prefix)))
168 # The relocated path is within the found prefix
170 (undef, $packfile_prefix)
171 = File::Spec->splitpath($packfile);
174 = File::Spec->abs2rel($key, $packfile_prefix);
179 $data->{relocate_as} = $relocate_as;
186 foreach my $k (sort(keys(%$data)))
188 print $fh (" $k=$data->{$k}");
198 my ($self, $remove) = @_;
199 $self = tied(%$self) || $self;
201 foreach my $key (sort(keys(%{$self->{data}})))
205 push(@missing, $key);
206 delete($self->{data}{$key}) if ($remove);
215 $self = tied(%$self) || $self;
216 return($self->{packfile});
225 ExtUtils::Packlist - manage .packlist files
229 use ExtUtils::Packlist;
230 my ($pl) = ExtUtils::Packlist->new('.packlist');
231 $pl->read('/an/old/.packlist');
232 my @missing_files = $pl->validate();
233 $pl->write('/a/new/.packlist');
235 $pl->{'/some/file/name'}++;
237 $pl->{'/some/other/file/name'} = { type => 'file',
238 from => '/some/file' };
242 ExtUtils::Packlist provides a standard way to manage .packlist files.
243 Functions are provided to read and write .packlist files. The original
244 .packlist format is a simple list of absolute pathnames, one per line. In
245 addition, this package supports an extended format, where as well as a filename
246 each line may contain a list of attributes in the form of a space separated
247 list of key=value pairs. This is used by the installperl script to
248 differentiate between files and links, for example.
252 The hash reference returned by the new() function can be used to examine and
253 modify the contents of the .packlist. Items may be added/deleted from the
254 .packlist by modifying the hash. If the value associated with a hash key is a
255 scalar, the entry written to the .packlist by any subsequent write() will be a
256 simple filename. If the value is a hash, the entry written will be the
257 filename followed by the key=value pairs from the hash. Reading back the
258 .packlist will recreate the original entries.
266 This takes an optional parameter, the name of a .packlist. If the file exists,
267 it will be opened and the contents of the file will be read. The new() method
268 returns a reference to a hash. This hash holds an entry for each line in the
269 .packlist. In the case of old-style .packlists, the value associated with each
270 key is undef. In the case of new-style .packlists, the value associated with
271 each key is a hash containing the key=value pairs following the filename in the
276 This takes an optional parameter, the name of the .packlist to be read. If
277 no file is specified, the .packlist specified to new() will be read. If the
278 .packlist does not exist, Carp::croak will be called.
282 This takes an optional parameter, the name of the .packlist to be written. If
283 no file is specified, the .packlist specified to new() will be overwritten.
287 This checks that every file listed in the .packlist actually exists. If an
288 argument which evaluates to true is given, any missing files will be removed
289 from the internal hash. The return value is a list of the missing files, which
290 will be empty if they all exist.
292 =item packlist_file()
294 This returns the name of the associated .packlist file
300 Here's C<modrm>, a little utility to cleanly remove an installed module.
302 #!/usr/local/bin/perl -w
306 use ExtUtils::Packlist;
307 use ExtUtils::Installed;
311 my $dh = IO::Dir->new($dir) || return(0);
312 my @count = $dh->read();
314 return(@count == 2 ? 1 : 0);
317 # Find all the installed packages
318 print("Finding all installed modules...\n");
319 my $installed = ExtUtils::Installed->new();
321 foreach my $module (grep(!/^Perl$/, $installed->modules())) {
322 my $version = $installed->version($module) || "???";
323 print("Found module $module Version $version\n");
324 print("Do you want to delete $module? [n] ");
325 my $r = <STDIN>; chomp($r);
326 if ($r && $r =~ /^y/i) {
327 # Remove all the files
328 foreach my $file (sort($installed->files($module))) {
332 my $pf = $installed->packlist($module)->packlist_file();
335 foreach my $dir (sort($installed->directory_tree($module))) {
336 if (emptydir($dir)) {
337 print("rmdir $dir\n");
346 Alan Burlison <Alan.Burlison@uk.sun.com>