Leaked source code of windows server 2003
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.

301 lines
7.6 KiB

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