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