Excise inexact blather.
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / Installed.pm
CommitLineData
354f3b56 1package ExtUtils::Installed;
17f410f9 2
3b825e41 3use 5.006_001;
354f3b56 4use strict;
5use Carp qw();
6use ExtUtils::Packlist;
7use ExtUtils::MakeMaker;
8use Config;
9use File::Find;
10use File::Basename;
5de3f0da 11use File::Spec;
88d01e8d 12our $VERSION = '0.04';
007a26ab 13
14my $DOSISH = ($^O =~ /^(MSWin\d\d|os2|dos|mint)$/);
15
16sub _is_prefix
17{
18my ($self, $path, $prefix) = @_;
19if (substr($path, 0, length($prefix)) eq $prefix)
20 {
21 return(1);
22 }
23if ($DOSISH)
24 {
25 $path =~ s|\\|/|g;
26 $prefix =~ s|\\|/|g;
27 if ($path =~ m{^\Q$prefix\E}i)
28 {
29 return(1);
30 }
31 }
32return(0);
33}
354f3b56 34
35sub _is_type($$$)
36{
37my ($self, $path, $type) = @_;
38return(1) if ($type eq "all");
39if ($type eq "doc")
40 {
007a26ab 41 return($self->_is_prefix($path, $Config{installman1dir})
354f3b56 42 ||
007a26ab 43 $self->_is_prefix($path, $Config{installman3dir})
354f3b56 44 ? 1 : 0)
45 }
46if ($type eq "prog")
47 {
007a26ab 48 return($self->_is_prefix($path, $Config{prefix})
354f3b56 49 &&
29a2fc3d 50 !($Config{installman1dir} &&
51 $self->_is_prefix($path, $Config{installman1dir}))
354f3b56 52 &&
29a2fc3d 53 !($Config{installman3dir} &&
54 $self->_is_prefix($path, $Config{installman3dir}))
354f3b56 55 ? 1 : 0);
56 }
57return(0);
58}
59
60sub _is_under($$;)
61{
62my ($self, $path, @under) = @_;
63$under[0] = "" if (! @under);
64foreach my $dir (@under)
65 {
007a26ab 66 return(1) if ($self->_is_prefix($path, $dir));
354f3b56 67 }
68return(0);
69}
70
71sub new($)
72{
73my ($class) = @_;
74$class = ref($class) || $class;
75my $self = {};
76
007a26ab 77my $installarchlib = $Config{installarchlib};
78my $archlib = $Config{archlib};
79my $sitearch = $Config{sitearch};
80
81if ($DOSISH)
82 {
83 $installarchlib =~ s|\\|/|g;
84 $archlib =~ s|\\|/|g;
85 $sitearch =~ s|\\|/|g;
86 }
87
354f3b56 88# Read the core packlist
89$self->{Perl}{packlist} =
19e86e2f 90 ExtUtils::Packlist->new( File::Spec->catfile($installarchlib, '.packlist') );
0ff3fa1a 91$self->{Perl}{version} = $Config{version};
354f3b56 92
93# Read the module packlists
94my $sub = sub
95 {
96 # Only process module .packlists
007a26ab 97 return if ($_) ne ".packlist" || $File::Find::dir eq $installarchlib;
354f3b56 98
99 # Hack of the leading bits of the paths & convert to a module name
100 my $module = $File::Find::name;
007a26ab 101 $module =~ s!\Q$archlib\E/auto/(.*)/.packlist!$1!s;
102 $module =~ s!\Q$sitearch\E/auto/(.*)/.packlist!$1!s;
354f3b56 103 my $modfile = "$module.pm";
104 $module =~ s!/!::!g;
105
106 # Find the top-level module file in @INC
107 $self->{$module}{version} = '';
108 foreach my $dir (@INC)
109 {
5de3f0da 110 my $p = File::Spec->catfile($dir, $modfile);
354f3b56 111 if (-f $p)
112 {
113 $self->{$module}{version} = MM->parse_version($p);
114 last;
115 }
116 }
117
118 # Read the .packlist
119 $self->{$module}{packlist} = ExtUtils::Packlist->new($File::Find::name);
120 };
007a26ab 121find($sub, $archlib, $sitearch);
354f3b56 122
123return(bless($self, $class));
124}
125
126sub modules($)
127{
128my ($self) = @_;
129return(sort(keys(%$self)));
130}
131
132sub files($$;$)
133{
134my ($self, $module, $type, @under) = @_;
135
136# Validate arguments
137Carp::croak("$module is not installed") if (! exists($self->{$module}));
138$type = "all" if (! defined($type));
139Carp::croak('type must be "all", "prog" or "doc"')
140 if ($type ne "all" && $type ne "prog" && $type ne "doc");
141
142my (@files);
143foreach my $file (keys(%{$self->{$module}{packlist}}))
144 {
145 push(@files, $file)
146 if ($self->_is_type($file, $type) && $self->_is_under($file, @under));
147 }
148return(@files);
149}
150
151sub directories($$;$)
152{
153my ($self, $module, $type, @under) = @_;
154my (%dirs);
155foreach my $file ($self->files($module, $type, @under))
156 {
157 $dirs{dirname($file)}++;
158 }
159return(sort(keys(%dirs)));
160}
161
162sub directory_tree($$;$)
163{
164my ($self, $module, $type, @under) = @_;
165my (%dirs);
166foreach my $dir ($self->directories($module, $type, @under))
167 {
168 $dirs{$dir}++;
9b604809 169 my ($last) = ("");
354f3b56 170 while ($last ne $dir)
171 {
172 $last = $dir;
173 $dir = dirname($dir);
174 last if (! $self->_is_under($dir, @under));
175 $dirs{$dir}++;
176 }
177 }
178return(sort(keys(%dirs)));
179}
180
181sub validate($;$)
182{
183my ($self, $module, $remove) = @_;
184Carp::croak("$module is not installed") if (! exists($self->{$module}));
185return($self->{$module}{packlist}->validate($remove));
186}
187
188sub packlist($$)
189{
190my ($self, $module) = @_;
191Carp::croak("$module is not installed") if (! exists($self->{$module}));
192return($self->{$module}{packlist});
193}
194
195sub version($$)
196{
197my ($self, $module) = @_;
198Carp::croak("$module is not installed") if (! exists($self->{$module}));
199return($self->{$module}{version});
200}
201
202sub DESTROY
203{
204}
205
2061;
207
208__END__
209
210=head1 NAME
211
212ExtUtils::Installed - Inventory management of installed modules
213
214=head1 SYNOPSIS
215
216 use ExtUtils::Installed;
217 my ($inst) = ExtUtils::Installed->new();
218 my (@modules) = $inst->modules();
219 my (@missing) = $inst->validate("DBI");
220 my $all_files = $inst->files("DBI");
221 my $files_below_usr_local = $inst->files("DBI", "all", "/usr/local");
222 my $all_dirs = $inst->directories("DBI");
223 my $dirs_below_usr_local = $inst->directory_tree("DBI", "prog");
224 my $packlist = $inst->packlist("DBI");
225
226=head1 DESCRIPTION
227
228ExtUtils::Installed provides a standard way to find out what core and module
229files have been installed. It uses the information stored in .packlist files
230created during installation to provide this information. In addition it
231provides facilities to classify the installed files and to extract directory
232information from the .packlist files.
233
234=head1 USAGE
235
236The new() function searches for all the installed .packlists on the system, and
237stores their contents. The .packlists can be queried with the functions
238described below.
239
240=head1 FUNCTIONS
241
bbc7dcd2 242=over 4
354f3b56 243
244=item new()
245
246This takes no parameters, and searches for all the installed .packlists on the
247system. The packlists are read using the ExtUtils::packlist module.
248
249=item modules()
250
251This returns a list of the names of all the installed modules. The perl 'core'
252is given the special name 'Perl'.
253
254=item files()
255
256This takes one mandatory parameter, the name of a module. It returns a list of
257all the filenames from the package. To obtain a list of core perl files, use
258the module name 'Perl'. Additional parameters are allowed. The first is one
259of the strings "prog", "man" or "all", to select either just program files,
260just manual files or all files. The remaining parameters are a list of
261directories. The filenames returned will be restricted to those under the
262specified directories.
263
264=item directories()
265
266This takes one mandatory parameter, the name of a module. It returns a list of
267all the directories from the package. Additional parameters are allowed. The
268first is one of the strings "prog", "man" or "all", to select either just
269program directories, just manual directories or all directories. The remaining
270parameters are a list of directories. The directories returned will be
271restricted to those under the specified directories. This method returns only
272the leaf directories that contain files from the specified module.
273
274=item directory_tree()
275
276This is identical in operation to directory(), except that it includes all the
277intermediate directories back up to the specified directories.
278
279=item validate()
280
281This takes one mandatory parameter, the name of a module. It checks that all
282the files listed in the modules .packlist actually exist, and returns a list of
283any missing files. If an optional second argument which evaluates to true is
284given any missing files will be removed from the .packlist
285
286=item packlist()
287
288This returns the ExtUtils::Packlist object for the specified module.
289
290=item version()
291
292This returns the version number for the specified module.
293
294=back
295
ddf41153 296=head1 EXAMPLE
297
298See the example in L<ExtUtils::Packlist>.
299
354f3b56 300=head1 AUTHOR
301
302Alan Burlison <Alan.Burlison@uk.sun.com>
303
304=cut