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