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