1N/Apackage ExtUtils::Installed;
1N/A
1N/Ause 5.00503;
1N/Ause strict;
1N/Ause Carp qw();
1N/Ause ExtUtils::Packlist;
1N/Ause ExtUtils::MakeMaker;
1N/Ause Config;
1N/Ause File::Find;
1N/Ause File::Basename;
1N/Ause File::Spec;
1N/A
1N/Amy $Is_VMS = $^O eq 'VMS';
1N/Amy $DOSISH = ($^O =~ /^(MSWin\d\d|os2|dos|mint)$/);
1N/A
1N/Arequire VMS::Filespec if $Is_VMS;
1N/A
1N/Ause vars qw($VERSION);
1N/A$VERSION = '0.08';
1N/A
1N/Asub _is_prefix {
1N/A my ($self, $path, $prefix) = @_;
1N/A return unless defined $prefix && defined $path;
1N/A
1N/A if( $Is_VMS ) {
1N/A $prefix = VMS::Filespec::unixify($prefix);
1N/A $path = VMS::Filespec::unixify($path);
1N/A }
1N/A
1N/A # Sloppy Unix path normalization.
1N/A $prefix =~ s{/+}{/}g;
1N/A $path =~ s{/+}{/}g;
1N/A
1N/A return 1 if substr($path, 0, length($prefix)) eq $prefix;
1N/A
1N/A if ($DOSISH) {
1N/A $path =~ s|\\|/|g;
1N/A $prefix =~ s|\\|/|g;
1N/A return 1 if $path =~ m{^\Q$prefix\E}i;
1N/A }
1N/A return(0);
1N/A}
1N/A
1N/Asub _is_doc {
1N/A my ($self, $path) = @_;
1N/A my $man1dir = $Config{man1direxp};
1N/A my $man3dir = $Config{man3direxp};
1N/A return(($man1dir && $self->_is_prefix($path, $man1dir))
1N/A ||
1N/A ($man3dir && $self->_is_prefix($path, $man3dir))
1N/A ? 1 : 0)
1N/A}
1N/A
1N/Asub _is_type {
1N/A my ($self, $path, $type) = @_;
1N/A return 1 if $type eq "all";
1N/A
1N/A return($self->_is_doc($path)) if $type eq "doc";
1N/A
1N/A if ($type eq "prog") {
1N/A return($self->_is_prefix($path, $Config{prefix} || $Config{prefixexp})
1N/A &&
1N/A !($self->_is_doc($path))
1N/A ? 1 : 0);
1N/A }
1N/A return(0);
1N/A}
1N/A
1N/Asub _is_under {
1N/A my ($self, $path, @under) = @_;
1N/A $under[0] = "" if (! @under);
1N/A foreach my $dir (@under) {
1N/A return(1) if ($self->_is_prefix($path, $dir));
1N/A }
1N/A
1N/A return(0);
1N/A}
1N/A
1N/Asub new {
1N/A my ($class) = @_;
1N/A $class = ref($class) || $class;
1N/A my $self = {};
1N/A
1N/A my $archlib = $Config{archlibexp};
1N/A my $sitearch = $Config{sitearchexp};
1N/A
1N/A # File::Find does not know how to deal with VMS filepaths.
1N/A if( $Is_VMS ) {
1N/A $archlib = VMS::Filespec::unixify($archlib);
1N/A $sitearch = VMS::Filespec::unixify($sitearch);
1N/A }
1N/A
1N/A if ($DOSISH) {
1N/A $archlib =~ s|\\|/|g;
1N/A $sitearch =~ s|\\|/|g;
1N/A }
1N/A
1N/A # Read the core packlist
1N/A $self->{Perl}{packlist} =
1N/A ExtUtils::Packlist->new( File::Spec->catfile($archlib, '.packlist') );
1N/A $self->{Perl}{version} = $Config{version};
1N/A
1N/A # Read the module packlists
1N/A my $sub = sub {
1N/A # Only process module .packlists
1N/A return if $_ ne ".packlist" || $File::Find::dir eq $archlib;
1N/A
1N/A # Hack of the leading bits of the paths & convert to a module name
1N/A my $module = $File::Find::name;
1N/A
1N/A $module =~ s!\Q$archlib\E/?auto/(.*)/.packlist!$1!s or
1N/A $module =~ s!\Q$sitearch\E/?auto/(.*)/.packlist!$1!s;
1N/A my $modfile = "$module.pm";
1N/A $module =~ s!/!::!g;
1N/A
1N/A # Find the top-level module file in @INC
1N/A $self->{$module}{version} = '';
1N/A foreach my $dir (@INC) {
1N/A my $p = File::Spec->catfile($dir, $modfile);
1N/A if (-r $p) {
1N/A $module = _module_name($p, $module) if $Is_VMS;
1N/A
1N/A require ExtUtils::MM;
1N/A $self->{$module}{version} = MM->parse_version($p);
1N/A last;
1N/A }
1N/A }
1N/A
1N/A # Read the .packlist
1N/A $self->{$module}{packlist} =
1N/A ExtUtils::Packlist->new($File::Find::name);
1N/A };
1N/A
1N/A my(@dirs) = grep { -e } ($archlib, $sitearch);
1N/A find($sub, @dirs) if @dirs;
1N/A
1N/A return(bless($self, $class));
1N/A}
1N/A
1N/A# VMS's non-case preserving file-system means the package name can't
1N/A# be reconstructed from the filename.
1N/Asub _module_name {
1N/A my($file, $orig_module) = @_;
1N/A
1N/A my $module = '';
1N/A if (open PACKFH, $file) {
1N/A while (<PACKFH>) {
1N/A if (/package\s+(\S+)\s*;/) {
1N/A my $pack = $1;
1N/A # Make a sanity check, that lower case $module
1N/A # is identical to lowercase $pack before
1N/A # accepting it
1N/A if (lc($pack) eq lc($orig_module)) {
1N/A $module = $pack;
1N/A last;
1N/A }
1N/A }
1N/A }
1N/A close PACKFH;
1N/A }
1N/A
1N/A print STDERR "Couldn't figure out the package name for $file\n"
1N/A unless $module;
1N/A
1N/A return $module;
1N/A}
1N/A
1N/A
1N/A
1N/Asub modules {
1N/A my ($self) = @_;
1N/A
1N/A # Bug/feature of sort in scalar context requires this.
1N/A return wantarray ? sort keys %$self : keys %$self;
1N/A}
1N/A
1N/Asub files {
1N/A my ($self, $module, $type, @under) = @_;
1N/A
1N/A # Validate arguments
1N/A Carp::croak("$module is not installed") if (! exists($self->{$module}));
1N/A $type = "all" if (! defined($type));
1N/A Carp::croak('type must be "all", "prog" or "doc"')
1N/A if ($type ne "all" && $type ne "prog" && $type ne "doc");
1N/A
1N/A my (@files);
1N/A foreach my $file (keys(%{$self->{$module}{packlist}})) {
1N/A push(@files, $file)
1N/A if ($self->_is_type($file, $type) &&
1N/A $self->_is_under($file, @under));
1N/A }
1N/A return(@files);
1N/A}
1N/A
1N/Asub directories {
1N/A my ($self, $module, $type, @under) = @_;
1N/A my (%dirs);
1N/A foreach my $file ($self->files($module, $type, @under)) {
1N/A $dirs{dirname($file)}++;
1N/A }
1N/A return sort keys %dirs;
1N/A}
1N/A
1N/Asub directory_tree {
1N/A my ($self, $module, $type, @under) = @_;
1N/A my (%dirs);
1N/A foreach my $dir ($self->directories($module, $type, @under)) {
1N/A $dirs{$dir}++;
1N/A my ($last) = ("");
1N/A while ($last ne $dir) {
1N/A $last = $dir;
1N/A $dir = dirname($dir);
1N/A last if !$self->_is_under($dir, @under);
1N/A $dirs{$dir}++;
1N/A }
1N/A }
1N/A return(sort(keys(%dirs)));
1N/A}
1N/A
1N/Asub validate {
1N/A my ($self, $module, $remove) = @_;
1N/A Carp::croak("$module is not installed") if (! exists($self->{$module}));
1N/A return($self->{$module}{packlist}->validate($remove));
1N/A}
1N/A
1N/Asub packlist {
1N/A my ($self, $module) = @_;
1N/A Carp::croak("$module is not installed") if (! exists($self->{$module}));
1N/A return($self->{$module}{packlist});
1N/A}
1N/A
1N/Asub version {
1N/A my ($self, $module) = @_;
1N/A Carp::croak("$module is not installed") if (! exists($self->{$module}));
1N/A return($self->{$module}{version});
1N/A}
1N/A
1N/A
1N/A1;
1N/A
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/AExtUtils::Installed - Inventory management of installed modules
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A use ExtUtils::Installed;
1N/A my ($inst) = ExtUtils::Installed->new();
1N/A my (@modules) = $inst->modules();
1N/A my (@missing) = $inst->validate("DBI");
1N/A my $all_files = $inst->files("DBI");
1N/A my $files_below_usr_local = $inst->files("DBI", "all", "/usr/local");
1N/A my $all_dirs = $inst->directories("DBI");
1N/A my $dirs_below_usr_local = $inst->directory_tree("DBI", "prog");
1N/A my $packlist = $inst->packlist("DBI");
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AExtUtils::Installed provides a standard way to find out what core and module
1N/Afiles have been installed. It uses the information stored in .packlist files
1N/Acreated during installation to provide this information. In addition it
1N/Aprovides facilities to classify the installed files and to extract directory
1N/Ainformation from the .packlist files.
1N/A
1N/A=head1 USAGE
1N/A
1N/AThe new() function searches for all the installed .packlists on the system, and
1N/Astores their contents. The .packlists can be queried with the functions
1N/Adescribed below.
1N/A
1N/A=head1 FUNCTIONS
1N/A
1N/A=over 4
1N/A
1N/A=item new()
1N/A
1N/AThis takes no parameters, and searches for all the installed .packlists on the
1N/Asystem. The packlists are read using the ExtUtils::packlist module.
1N/A
1N/A=item modules()
1N/A
1N/AThis returns a list of the names of all the installed modules. The perl 'core'
1N/Ais given the special name 'Perl'.
1N/A
1N/A=item files()
1N/A
1N/AThis takes one mandatory parameter, the name of a module. It returns a list of
1N/Aall the filenames from the package. To obtain a list of core perl files, use
1N/Athe module name 'Perl'. Additional parameters are allowed. The first is one
1N/Aof the strings "prog", "doc" or "all", to select either just program files,
1N/Ajust manual files or all files. The remaining parameters are a list of
1N/Adirectories. The filenames returned will be restricted to those under the
1N/Aspecified directories.
1N/A
1N/A=item directories()
1N/A
1N/AThis takes one mandatory parameter, the name of a module. It returns a list of
1N/Aall the directories from the package. Additional parameters are allowed. The
1N/Afirst is one of the strings "prog", "doc" or "all", to select either just
1N/Aprogram directories, just manual directories or all directories. The remaining
1N/Aparameters are a list of directories. The directories returned will be
1N/Arestricted to those under the specified directories. This method returns only
1N/Athe leaf directories that contain files from the specified module.
1N/A
1N/A=item directory_tree()
1N/A
1N/AThis is identical in operation to directories(), except that it includes all the
1N/Aintermediate directories back up to the specified directories.
1N/A
1N/A=item validate()
1N/A
1N/AThis takes one mandatory parameter, the name of a module. It checks that all
1N/Athe files listed in the modules .packlist actually exist, and returns a list of
1N/Aany missing files. If an optional second argument which evaluates to true is
1N/Agiven any missing files will be removed from the .packlist
1N/A
1N/A=item packlist()
1N/A
1N/AThis returns the ExtUtils::Packlist object for the specified module.
1N/A
1N/A=item version()
1N/A
1N/AThis returns the version number for the specified module.
1N/A
1N/A=back
1N/A
1N/A=head1 EXAMPLE
1N/A
1N/ASee the example in L<ExtUtils::Packlist>.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/AAlan Burlison <Alan.Burlison@uk.sun.com>
1N/A
1N/A=cut