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