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