Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
6.9 KiB

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