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.

2282 lines
78 KiB

  1. package PPM;
  2. require 5.004;
  3. require Exporter;
  4. @ISA = qw(Exporter);
  5. @EXPORT = qw(PPMdat PPMERR InstalledPackageProperties ListOfRepositories RemoveRepository AddRepository GetPPMOptions SetPPMOptions InstallPackage RemovePackage VerifyPackage UpgradePackage RepositoryPackages RepositoryPackageProperties QueryInstalledPackages QueryPPD RepositorySummary);
  6. use LWP::UserAgent;
  7. use LWP::Simple;
  8. use File::Basename;
  9. use File::Copy;
  10. use File::Path;
  11. use ExtUtils::Install;
  12. use Cwd;
  13. use Config;
  14. if ($Config{'osname'} eq 'MSWin32') {
  15. require HtmlHelp;
  16. HtmlHelp->import();
  17. }
  18. use XML::PPD;
  19. use XML::PPMConfig;
  20. use XML::Parser;
  21. use Archive::Tar;
  22. use strict;
  23. #set Debug to 1 to debug PPMdat file reading
  24. # 2 to debug parsing PPDs
  25. #
  26. # values may be or'ed together.
  27. #
  28. my $Debug = 0;
  29. my $PPMERR;
  30. my ($PPM_ver, $CPU, $OS_VALUE, $OS_VERSION, $LANGUAGE);
  31. # options from data file.
  32. my ($build_dir, $Ignorecase, $Clean, $Force_install, $Confirm, $Root, $More, $Trace, $TraceFile);
  33. my $TraceStarted = 0;
  34. my %repositories;
  35. my ($current_root, $orig_root);
  36. # Keys for this hash are package names. It is filled in by a successful
  37. # call to read_config(). Each package is a hash with the following keys:
  38. # LOCATION, INST_DATE, INST_ROOT, INST_PACKLIST and INST_PPD.
  39. my %installed_packages = ();
  40. # Keys for this hash are CODEBASE, INSTALL_HREF, INSTALL_EXEC,
  41. # INSTALL_SCRIPT, NAME, VERSION, TITLE, ABSTRACT, LICENSE, AUTHOR,
  42. # UNINSTALL_HREF, UNINSTALL_EXEC, UNINSTALL_SCRIPT, PERLCORE_VER and DEPEND.
  43. # It is filled in after a successful call to parsePPD().
  44. my %current_package = ();
  45. my @current_package_stack;
  46. # this may get overridden by the config file.
  47. my @required_packages = ('PPM', 'libnet', 'Archive-Tar', 'Compress-Zlib',
  48. 'libwww-perl', 'XML-Parser', 'XML-Element');
  49. # Packages that can't be upgraded on Win9x
  50. my @Win9x_denied = qw(xml-parser compress-zlib);
  51. my %Win9x_denied;
  52. @Win9x_denied{@Win9x_denied} = ();
  53. # ppm.xml location is in the environment variable 'PPM_DAT', else it is in
  54. # the same place as this script.
  55. my ($basename, $path) = fileparse($0);
  56. if (defined $ENV{'PPM_DAT'} && -f $ENV{'PPM_DAT'})
  57. {
  58. $PPM::PPMdat = $ENV{'PPM_DAT'};
  59. }
  60. elsif (-f "$Config{'installsitelib'}/ppm.xml")
  61. {
  62. $PPM::PPMdat = "$Config{'installsitelib'}/ppm.xml";
  63. }
  64. elsif (-f "$Config{'installprivlib'}/ppm.xml")
  65. {
  66. $PPM::PPMdat = "$Config{'installprivlib'}/ppm.xml";
  67. }
  68. elsif (-f $path . "/ppm.xml")
  69. {
  70. $PPM::PPMdat = $path . $PPM::PPMdat;
  71. }
  72. else
  73. {
  74. &Trace("Failed to load PPM_DAT file") if $Trace;
  75. print "Failed to load PPM_DAT file\n";
  76. return -1;
  77. }
  78. &Trace("Using config file: $PPM::PPMdat") if $Trace;
  79. my $init = 0;
  80. #
  81. # Exported subs
  82. #
  83. sub InstalledPackageProperties
  84. {
  85. my (%ret_hash, $dep);
  86. read_config();
  87. foreach $_ (keys %installed_packages) {
  88. parsePPD(%{ $installed_packages{$_}{'INST_PPD'} } );
  89. $ret_hash{$_}{'NAME'} = $_;
  90. $ret_hash{$_}{'DATE'} = $installed_packages{$_}{'INST_DATE'};
  91. $ret_hash{$_}{'AUTHOR'} = $current_package{'AUTHOR'};
  92. $ret_hash{$_}{'VERSION'} = $current_package{'VERSION'};
  93. $ret_hash{$_}{'ABSTRACT'} = $current_package{'ABSTRACT'};
  94. $ret_hash{$_}{'PERLCORE_VER'} = $current_package{'PERLCORE_VER'};
  95. foreach $dep (keys %{$current_package{'DEPEND'}}) {
  96. push @{$ret_hash{$_}{'DEPEND'}}, $dep;
  97. }
  98. }
  99. return %ret_hash;
  100. }
  101. sub ListOfRepositories
  102. {
  103. my %reps;
  104. read_config();
  105. foreach (keys %repositories) {
  106. $reps{$_} = $repositories{$_}{'LOCATION'};
  107. }
  108. return %reps;
  109. # return %repositories;
  110. }
  111. sub RemoveRepository
  112. {
  113. my (%argv) = @_;
  114. my ($arg, $repository, $save, $loc);
  115. foreach $arg (keys %argv) {
  116. if ($arg eq 'repository') { $repository = $argv{$arg}; }
  117. if ($arg eq 'save') { $save = $argv{$arg}; }
  118. }
  119. read_config();
  120. foreach $_ (keys %repositories) {
  121. if ($_ =~ /^\Q$repository\E$/) {
  122. &Trace("Removed repository $repositories{$repository}") if $Trace;
  123. delete $repositories{$repository};
  124. last;
  125. }
  126. }
  127. if (defined $save && $save != 0) { save_options(); }
  128. }
  129. sub AddRepository
  130. {
  131. my (%argv) = @_;
  132. my ($arg, $repository, $location, $username, $password, $save);
  133. foreach $arg (keys %argv) {
  134. if ($arg eq 'repository') { $repository = $argv{$arg}; }
  135. if ($arg eq 'location') { $location = $argv{$arg}; }
  136. if ($arg eq 'username') { $username = $argv{$arg}; }
  137. if ($arg eq 'password') { $password = $argv{$arg}; }
  138. if ($arg eq 'save') { $save = $argv{$arg}; }
  139. }
  140. read_config();
  141. $repositories{$repository}{'LOCATION'} = $location;
  142. $repositories{$repository}{'USERNAME'} = $username if defined $username;
  143. $repositories{$repository}{'PASSWORD'} = $password if defined $password;
  144. # $repositories{$repository} = $location;
  145. &Trace("Added repository $repositories{$repository}") if $Trace;
  146. if (defined $save && $save != 0) { save_options(); }
  147. }
  148. sub GetPPMOptions
  149. {
  150. my %ret_hash;
  151. read_config();
  152. $ret_hash{'IGNORECASE'} = $Ignorecase;
  153. $ret_hash{'CLEAN'} = $Clean;
  154. $ret_hash{'FORCE_INSTALL'} = $Force_install;
  155. $ret_hash{'CONFIRM'} = $Confirm;
  156. $ret_hash{'ROOT'} = $Root;
  157. $ret_hash{'BUILDDIR'} = $build_dir;
  158. $ret_hash{'MORE'} = $More;
  159. $ret_hash{'TRACE'} = $Trace;
  160. $ret_hash{'TRACEFILE'} = $TraceFile;
  161. return %ret_hash;
  162. }
  163. sub SetPPMOptions
  164. {
  165. my (%argv) = @_;
  166. my ($arg, %opts, $save);
  167. foreach $arg (keys %argv) {
  168. if ($arg eq 'options') { %opts = %{$argv{$arg}}; }
  169. if ($arg eq 'save') { $save = $argv{$arg}; }
  170. }
  171. $Ignorecase = $opts{'IGNORECASE'};
  172. $Clean = $opts{'CLEAN'};
  173. $Force_install = $opts{'FORCE_INSTALL'};
  174. $Confirm = $opts{'CONFIRM'};
  175. $Root = $opts{'ROOT'};
  176. $build_dir = $opts{'BUILDDIR'};
  177. $More = $opts{'MORE'};
  178. $Trace = $opts{'TRACE'};
  179. $TraceFile = $opts{'TRACEFILE'};
  180. if (defined $save && $save != 0) { save_options(); }
  181. }
  182. sub UpgradePackage
  183. {
  184. my (%argv) = @_;
  185. my ($arg, $package, $location);
  186. foreach $arg (keys %argv) {
  187. if ($arg eq 'package') { $package = $argv{$arg}; }
  188. if ($arg eq 'location') { $location = $argv{$arg}; }
  189. }
  190. return VerifyPackage("package" => $package, "location" => $location, "upgrade" => 1);
  191. }
  192. # Returns 1 on success, 0 and sets $PPMERR on failure.
  193. sub InstallPackage
  194. {
  195. my (%argv) = @_;
  196. my ($arg, $package, $location, $root);
  197. foreach $arg (keys %argv) {
  198. if ($arg eq 'package') { $package = $argv{$arg}; }
  199. if ($arg eq 'location') { $location = $argv{$arg}; }
  200. if ($arg eq 'root') { $root = $argv{$arg}; }
  201. }
  202. my ($PPDfile,%PPD);
  203. read_config();
  204. if (!defined($package) && -d "blib" && -f "Makefile") {
  205. unless (open MAKEFILE, "< Makefile") {
  206. $PPM::PPMERR = "Couldn't open Makefile for reading: $!";
  207. return 0;
  208. }
  209. while (<MAKEFILE>) {
  210. if (/^DISTNAME\s*=\s*(\S+)/) {
  211. $package = $1;
  212. $PPDfile = "$1.ppd";
  213. last;
  214. }
  215. }
  216. close MAKEFILE;
  217. unless (defined $PPDfile) {
  218. $PPM::PPMERR = "Couldn't determine local package name";
  219. return 0;
  220. }
  221. system("$Config{make} ppd");
  222. %PPD = readPPDfile($PPDfile, 'XML::PPD');
  223. if (!defined %PPD) { return 0; }
  224. parsePPD(%PPD);
  225. $Clean = 0;
  226. goto InstallBlib;
  227. }
  228. my $packagefile = $package;
  229. if (!defined($PPDfile = locatePPDfile($packagefile, $location))) {
  230. &Trace("Could not locate a PPD file for package $package") if $Trace;
  231. $PPM::PPMERR = "Could not locate a PPD file for package $package";
  232. return 0;
  233. }
  234. if ($Config{'osname'} eq 'MSWin32' &&
  235. !&Win32::IsWinNT &&
  236. exists $Win9x_denied{lc($package)}) {
  237. $PPM::PPMERR = "Package '$package' cannot be installed with PPM on Win9x--see http://www.ActiveState.com/ppm for details";
  238. return undef;
  239. }
  240. %PPD = readPPDfile($PPDfile, 'XML::PPD');
  241. if (!defined %PPD) { return 0; }
  242. parsePPD(%PPD);
  243. if (defined $current_package{'DEPEND'}) {
  244. my ($dep);
  245. push(@current_package_stack, [%current_package]);
  246. foreach $dep (keys %{$current_package{'DEPEND'}}) {
  247. # Has PPM already installed it?
  248. if(!defined $installed_packages{$dep}) {
  249. # Has *anybody* installed it, or is it part of core Perl?
  250. my $p = $dep;
  251. $p =~ s@-@/@g;
  252. my $found = grep -f, map "$_/$p.pm", @INC;
  253. if (!$found) {
  254. &Trace("Installing dependency '$dep'...") if $Trace;
  255. # print "Installing dependency '$dep'...\n";
  256. if(!InstallPackage("package" => $dep, "location" => $location)) {
  257. &Trace("Error installing dependency: $PPM::PPMERR") if $Trace;
  258. $PPM::PPMERR = "Error installing dependency: $PPM::PPMERR\n";
  259. if ($Force_install eq "No") {
  260. return 0;
  261. }
  262. }
  263. }
  264. }
  265. # make sure minimum version is installed, if necessary
  266. elsif (defined $current_package{'DEPEND'}{$dep}) {
  267. my (@comp) = split (',', $current_package{'DEPEND'}{$dep});
  268. # parsePPD fills in %current_package
  269. push(@current_package_stack, [%current_package]);
  270. parsePPD(%{$installed_packages{$dep}{'INST_PPD'}});
  271. my (@inst) = split (',', $current_package{'VERSION'});
  272. foreach(0..3) {
  273. if ($comp[$_] > $inst[$_]) {
  274. VerifyPackage("package" => $dep, "upgrade" => 1);
  275. last;
  276. }
  277. last if ($comp[$_] < $inst[$_]);
  278. }
  279. %current_package = @{pop @current_package_stack};
  280. }
  281. }
  282. %current_package = @{pop @current_package_stack};
  283. }
  284. my ($basename, $path) = fileparse($PPDfile);
  285. # strip the trailing path separator
  286. my $chr = substr($path, -1, 1);
  287. if ($chr eq '/' || $chr eq '\\') {
  288. chop $path;
  289. }
  290. if ($path =~ /^file:\/\/.*\|/i) {
  291. # $path is a local directory, let's avoid LWP by changing
  292. # it to a pathname.
  293. $path =~ s@^file://@@i;
  294. $path =~ s@^localhost/@@i;
  295. $path =~ s@\|@:@;
  296. }
  297. # get the code and put it in $build_dir
  298. my $install_dir = $build_dir . "/" . $current_package{'NAME'};
  299. File::Path::rmtree($install_dir,0,0);
  300. if(!-d $install_dir && !File::Path::mkpath($install_dir, 0, 0755)) {
  301. &Trace("Could not create $install_dir: $!") if $Trace;
  302. $PPM::PPMERR = "Could not create $install_dir: $!";
  303. return 0;
  304. }
  305. ($basename) = fileparse($current_package{'CODEBASE'});
  306. # CODEBASE is a URL
  307. if ($current_package{'CODEBASE'} =~ m@^...*://@i) {
  308. if (PPM_getstore("source" => "$current_package{'CODEBASE'}",
  309. "target" => "$install_dir/$basename") != 0) {
  310. return 0;
  311. }
  312. }
  313. # CODEBASE is a full pathname
  314. elsif (-f $current_package{'CODEBASE'}) {
  315. &Trace("Copying $current_package{'CODEBASE'} to $install_dir/$basename") if $Trace > 1;
  316. copy($current_package{'CODEBASE'}, "$install_dir/$basename");
  317. }
  318. # CODEBASE is relative to the directory location of the PPD
  319. elsif (-f "$path/$current_package{'CODEBASE'}") {
  320. &Trace("Copying $path/$current_package{'CODEBASE'} to $install_dir/$basename") if $Trace > 1;
  321. copy("$path/$current_package{'CODEBASE'}", "$install_dir/$basename");
  322. }
  323. # CODEBASE is relative to the URL location of the PPD
  324. else {
  325. if (PPM_getstore("source" => "$path/$current_package{'CODEBASE'}",
  326. "target" => "$install_dir/$basename") != 0) {
  327. return 0;
  328. }
  329. }
  330. my $cwd = getcwd();
  331. chdir($install_dir);
  332. my $tar;
  333. if ($basename =~ /\.gz$/i) {
  334. $tar = Archive::Tar->new($basename,1);
  335. }
  336. else {
  337. $tar = Archive::Tar->new($basename,0);
  338. }
  339. $tar->extract($tar->list_files);
  340. $basename =~ /(.*).tar/i;
  341. chdir($1);
  342. InstallBlib:
  343. my $inst_archlib = $Config{installsitearch};
  344. my $inst_root = $Config{prefix};
  345. my $packlist = MM->catdir("$Config{installsitearch}/auto", split(/-/, $current_package{'NAME'}), ".packlist");
  346. # copied from ExtUtils::Install
  347. my $INST_LIB = MM->catdir(MM->curdir,"blib","lib");
  348. my $INST_ARCHLIB = MM->catdir(MM->curdir,"blib","arch");
  349. my $INST_BIN = MM->catdir(MM->curdir,'blib','bin');
  350. my $INST_SCRIPT = MM->catdir(MM->curdir,'blib','script');
  351. my $INST_MAN1DIR = MM->catdir(MM->curdir,'blib','man1');
  352. my $INST_MAN3DIR = MM->catdir(MM->curdir,'blib','man3');
  353. my $INST_HTMLDIR = MM->catdir(MM->curdir,'blib','html');
  354. my $INST_HTMLHELPDIR = MM->catdir(MM->curdir,'blib','htmlhelp');
  355. my ($inst_lib, $inst_bin, $inst_script, $inst_man1dir, $inst_man3dir,
  356. $inst_htmldir, $inst_htmlhelpdir);
  357. $inst_script = $Config{installscript};
  358. $inst_man1dir = $Config{installman1dir};
  359. $inst_man3dir = $Config{installman3dir};
  360. $inst_bin = $Config{installbin};
  361. $inst_htmldir = $Config{installhtmldir};
  362. $inst_htmlhelpdir = $Config{installhtmlhelpdir};
  363. # PPM upgrade has to be done differently; needs to go into 'privlib'
  364. if ($current_package{'NAME'} =~ /^ppm$/i) {
  365. $packlist = MM->catdir("$Config{archlibexp}/auto", split(/-/, $current_package{'NAME'}), ".packlist");
  366. $inst_archlib = $Config{archlibexp};
  367. $inst_lib = $Config{installprivlib};
  368. }
  369. else {
  370. $inst_lib = $Config{installsitelib};
  371. if (defined $root || defined $current_root) {
  372. $root = (defined $root ? $root : $current_root);
  373. if ($root ne $inst_root) {
  374. if ($packlist =~ /\\lib\\(.*)/) {
  375. $packlist = "$root\\lib\\$1";
  376. }
  377. if ($inst_lib =~ /\\lib\\*(.*)/) {
  378. $inst_lib = "$root\\lib\\$1";
  379. }
  380. if ($inst_archlib =~ /\\site\\*(.*)/) {
  381. $inst_archlib = "$root\\site\\$1";
  382. }
  383. $inst_bin =~ s/\Q$inst_root/$root\E/i;
  384. $inst_script =~ s/\Q$inst_root/$root\E/i;
  385. $inst_man1dir =~ s/\Q$inst_root/$root\E/i;
  386. $inst_man3dir =~ s/\Q$inst_root/$root\E/i;
  387. $inst_root = $root;
  388. }
  389. }
  390. }
  391. while (1) {
  392. my $cwd = getcwd();
  393. &Trace("Calling ExtUtils::Install::install") if $Trace > 1;
  394. eval {
  395. ExtUtils::Install::install({
  396. "read" => $packlist, "write" => $packlist,
  397. $INST_LIB => $inst_lib, $INST_ARCHLIB => $inst_archlib,
  398. $INST_BIN => $inst_bin, $INST_SCRIPT => $inst_script,
  399. $INST_MAN1DIR => $inst_man1dir, $INST_MAN3DIR => $inst_man3dir,
  400. $INST_HTMLDIR => $inst_htmldir, $INST_HTMLHELPDIR => $inst_htmlhelpdir},0,0,0);
  401. };
  402. # install might have croaked in another directory
  403. chdir($cwd);
  404. # Can't remove some DLLs, but we can rename them and try again.
  405. if ($@ && $@ =~ /Cannot forceunlink (\S+)/) {
  406. &Trace("[email protected] rename") if $Trace;
  407. my $oldname = $1;
  408. $oldname =~ s/:$//;
  409. my $newname = $oldname . "." . time();
  410. if(!rename($oldname, $newname)) {
  411. &Trace("$!") if $Trace;
  412. $PPM::PPMERR = $@;
  413. return 0;
  414. }
  415. }
  416. # Some other error
  417. elsif($@) {
  418. &Trace("$@") if $Trace;
  419. $PPM::PPMERR = $@;
  420. return 0;
  421. }
  422. else { last; }
  423. }
  424. &Trace("Calling MakePerlHtmlIndexCaller") if $Trace > 1;
  425. HtmlHelp::MakePerlHtmlIndexCaller() if ($Config{'osname'} eq 'MSWin32');
  426. if (defined $current_package{'INSTALL_SCRIPT'}) {
  427. run_script("script" => $current_package{'INSTALL_SCRIPT'},
  428. "scriptHREF" => $current_package{'INSTALL_HREF'},
  429. "exec" => $current_package{'INSTALL_EXEC'},
  430. "inst_root" => $inst_root, "inst_archlib" => $inst_archlib);
  431. }
  432. chdir($cwd);
  433. # ask to store this location as default for this package?
  434. PPMdat_add_package($path, $packlist, $inst_root);
  435. # if 'install.ppm' exists, don't remove; system()
  436. # has probably not finished with it yet.
  437. if ($Clean eq "Yes" && !-f "$install_dir/install.ppm") {
  438. File::Path::rmtree($install_dir,0,0);
  439. }
  440. &Trace("Package $package successfully installed") if $Trace;
  441. reread_config();
  442. return 1;
  443. }
  444. # Returns a hash with key $location, and elements of arrays of package names.
  445. # Uses '%repositories' if $location is not specified.
  446. sub RepositoryPackages
  447. {
  448. my (%argv) = @_;
  449. my ($arg, $location, %ppds);
  450. foreach $arg (keys %argv) {
  451. if ($arg eq 'location') { $location = $argv{$arg}; }
  452. }
  453. if (defined $location) {
  454. @{$ppds{$location}} = list_available("location" => $location);
  455. }
  456. else {
  457. read_config(); # need repositories
  458. foreach $_ (keys %repositories) {
  459. $location = $repositories{$_}{'LOCATION'};
  460. @{$ppds{$location}} = list_available("location" => $location);
  461. }
  462. }
  463. return %ppds;
  464. }
  465. sub RepositoryPackageProperties
  466. {
  467. my (%argv) = @_;
  468. my ($arg, $package, $location, $PPDfile);
  469. foreach $arg (keys %argv) {
  470. if ($arg eq 'package') { $package = $argv{$arg}; }
  471. if ($arg eq 'location') { $location = $argv{$arg}; }
  472. }
  473. read_config();
  474. if (!defined($PPDfile = locatePPDfile($package, $location))) {
  475. &Trace("RepositoryPackageProperties: Could not locate a PPD file for package $package") if $Trace;
  476. $PPM::PPMERR = "Could not locate a PPD file for package $package";
  477. return undef;
  478. }
  479. my %PPD = readPPDfile($PPDfile, 'XML::PPD');
  480. if (!defined %PPD) { return undef; }
  481. parsePPD(%PPD);
  482. my (%ret_hash, $dep);
  483. $ret_hash{'NAME'} = $current_package{'NAME'};
  484. $ret_hash{'TITLE'} = $current_package{'TITLE'};
  485. $ret_hash{'AUTHOR'} = $current_package{'AUTHOR'};
  486. $ret_hash{'VERSION'} = $current_package{'VERSION'};
  487. $ret_hash{'ABSTRACT'} = $current_package{'ABSTRACT'};
  488. $ret_hash{'PERLCORE_VER'} = $current_package{'PERLCORE_VER'};
  489. foreach $dep (keys %{$current_package{'DEPEND'}}) {
  490. push @{$ret_hash{'DEPEND'}}, $dep;
  491. }
  492. return %ret_hash;
  493. }
  494. # Returns 1 on success, 0 and sets $PPMERR on failure.
  495. sub RemovePackage
  496. {
  497. my (%argv) = @_;
  498. my ($arg, $package, $force);
  499. foreach $arg (keys %argv) {
  500. if ($arg eq 'package') { $package = $argv{$arg}; }
  501. if ($arg eq 'force') { $force = $argv{$arg}; }
  502. }
  503. my %PPD;
  504. read_config();
  505. if (!defined $installed_packages{$package}) {
  506. my $pattern = $package;
  507. undef $package;
  508. # Do another lookup, ignoring case
  509. foreach $_ (keys %installed_packages) {
  510. if (/^$pattern$/i) {
  511. $package = $_;
  512. last;
  513. }
  514. }
  515. if (!defined $package) {
  516. &Trace("Package '$pattern' has not been installed by PPM") if $Trace;
  517. $PPM::PPMERR = "Package '$pattern' has not been installed by PPM";
  518. return 0;
  519. }
  520. }
  521. # Don't let them remove PPM itself, libnet, Archive-Tar, etc.
  522. # but we can force removal if we're upgrading
  523. unless (defined $force) {
  524. foreach (@required_packages) {
  525. if ($_ eq $package) {
  526. &Trace("Package '$package' is required by PPM and cannot be removed") if $Trace;
  527. $PPM::PPMERR = "Package '$package' is required by PPM and cannot be removed";
  528. return 0;
  529. }
  530. }
  531. }
  532. my $install_dir = $build_dir . "/" . $package;
  533. %PPD = %{ $installed_packages{$package}{'INST_PPD'} };
  534. parsePPD(%PPD);
  535. my $cwd = getcwd();
  536. if (defined $current_package{'UNINSTALL_SCRIPT'}) {
  537. if (!chdir($install_dir)) {
  538. &Trace("Could not chdir() to $install_dir: $!") if $Trace;
  539. $PPM::PPMERR = "Could not chdir() to $install_dir: $!";
  540. return 0;
  541. }
  542. run_script("script" => $current_package{'UNINSTALL_SCRIPT'},
  543. "scriptHREF" => $current_package{'UNINSTALL_HREF'},
  544. "exec" => $current_package{'UNINSTALL_EXEC'});
  545. chdir($cwd);
  546. }
  547. else {
  548. if (-f $installed_packages{$package}{'INST_PACKLIST'}) {
  549. &Trace("Calling ExtUtils::Install::uninstall") if $Trace > 1;
  550. eval {
  551. ExtUtils::Install::uninstall("$installed_packages{$package}{'INST_PACKLIST'}", 0, 0);
  552. };
  553. warn $@ if $@;
  554. }
  555. }
  556. File::Path::rmtree($install_dir,0,0);
  557. PPMdat_remove_package($package);
  558. # Rebuild the HTML Index
  559. &Trace("Calling MakePerlHtmlIndexCaller") if $Trace > 1;
  560. HtmlHelp::MakePerlHtmlIndexCaller() if ($Config{'osname'} eq 'MSWin32');
  561. &Trace("Package $package removed") if $Trace;
  562. reread_config();
  563. return 1;
  564. }
  565. # returns "0" if package is up-to-date; "1" if an upgrade is available;
  566. # undef and sets $PPMERR on error; and the new VERSION string if a package
  567. # was upgraded.
  568. sub VerifyPackage
  569. {
  570. my (%argv) = @_;
  571. my ($arg, $package, $location, $upgrade);
  572. foreach $arg (keys %argv) {
  573. if ($arg eq 'package') { $package = $argv{$arg}; }
  574. if ($arg eq 'location') { $location = $argv{$arg}; }
  575. if ($arg eq 'upgrade') { $upgrade = $argv{$arg}; }
  576. }
  577. my ($installedPPDfile, $comparePPDfile, %installedPPD, %comparePPD);
  578. read_config();
  579. if (!defined $installed_packages{$package}) {
  580. my $pattern = $package;
  581. undef $package;
  582. # Do another lookup, ignoring case
  583. foreach $_ (keys %installed_packages) {
  584. if (/^$pattern$/i) {
  585. $package = $_;
  586. last;
  587. }
  588. }
  589. if (!defined $package) {
  590. &Trace("Package '$pattern' has not been installed by PPM") if $Trace;
  591. $PPM::PPMERR = "Package '$pattern' has not been installed by PPM";
  592. return undef;
  593. }
  594. }
  595. %installedPPD = %{ $installed_packages{$package}{'INST_PPD'} };
  596. if (!defined($comparePPDfile = locatePPDfile($package, $location))) {
  597. &Trace("VerifyPackage: Could not locate a PPD file for $package") if $Trace;
  598. $PPM::PPMERR = "Could not locate a PPD file for $package";
  599. return undef;
  600. }
  601. %comparePPD = readPPDfile($comparePPDfile, 'XML::PPD');
  602. if (!defined %comparePPD) { return undef; }
  603. parsePPD(%installedPPD);
  604. my ($inst_version) = $current_package{'VERSION'};
  605. my ($inst_major, $inst_minor,
  606. $inst_patch1, $inst_patch2) = split (',', $inst_version);
  607. my ($inst_root) = $installed_packages{$package}{'INST_ROOT'};
  608. parsePPD(%comparePPD);
  609. my ($comp_version) = $current_package{'VERSION'};
  610. my ($comp_major, $comp_minor,
  611. $comp_patch1, $comp_patch2) = split (',', $comp_version);
  612. if ($comp_major > $inst_major ||
  613. ($comp_major == $inst_major && $comp_minor > $inst_minor) ||
  614. ($comp_major == $inst_major && $comp_minor == $inst_minor &&
  615. $comp_patch1 > $inst_patch1) ||
  616. ($comp_major == $inst_major && $comp_minor == $inst_minor &&
  617. $comp_patch1 == $inst_patch1 && $comp_patch2 > $inst_patch2)) {
  618. &Trace("Upgrade to $package is available") if $Trace > 1;
  619. if ($upgrade) {
  620. if ($Config{'osname'} eq 'MSWin32' &&
  621. !&Win32::IsWinNT &&
  622. exists $Win9x_denied{lc($package)}) {
  623. $PPM::PPMERR = "Package '$package' cannot be upgraded with PPM on Win9x--see http://www.ActiveState.com/ppm for details";
  624. return undef;
  625. }
  626. if (!defined $location) {
  627. # need to remember the $location, because once we remove the
  628. # package, it's unavailable.
  629. $location = $installed_packages{$package}{'LOCATION'};
  630. }
  631. RemovePackage("package" => $package, "force" => 1);
  632. InstallPackage("package" => $package, "location" => $location,
  633. "root" => $inst_root) or return undef;
  634. return $comp_version;
  635. }
  636. return '1';
  637. }
  638. else {
  639. # package is up to date
  640. return '0';
  641. }
  642. }
  643. # Changes where the packages are installed.
  644. # Returns previous root on success, undef and sets $PPMERR on failure.
  645. sub chroot
  646. {
  647. my (%argv) = @_;
  648. my ($arg, $location, $previous_root);
  649. foreach $arg (keys %argv) {
  650. if ($arg eq 'location') { $location = $argv{$arg}; }
  651. }
  652. if (!-d $location) {
  653. &Trace("'$location' does not exist.") if $Trace;
  654. $PPM::PPMERR = "'$location' does not exist.\n";
  655. return undef;
  656. }
  657. if (!defined $orig_root) {
  658. $orig_root = $Config{installbin};
  659. $orig_root =~ s/bin.*$//i;
  660. chop $orig_root;
  661. $current_root = $orig_root;
  662. }
  663. # mjn: move this to front-end?
  664. $previous_root = $current_root;
  665. $current_root = $location;
  666. return $previous_root;
  667. }
  668. sub QueryInstalledPackages
  669. {
  670. my (%argv) = @_;
  671. my ($searchRE, $searchtag, $ignorecase, $package, %ret_hash);
  672. $PPM::PPMERR = undef;
  673. my ($arg);
  674. foreach $arg (keys %argv) {
  675. if ($arg eq 'searchRE' && defined $argv{$arg}) {
  676. $searchRE = $argv{$arg};
  677. eval { $searchRE =~ /$searchRE/ };
  678. if ($@) {
  679. &Trace("'$searchRE': invalid regular expression.") if $Trace;
  680. $PPM::PPMERR = "'$searchRE': invalid regular expression.";
  681. return ();
  682. }
  683. }
  684. if ($arg eq 'searchtag' && (defined $argv{$arg})) { $searchtag = uc($argv{$arg}); }
  685. if ($arg eq 'ignorecase') { $ignorecase = $argv{$arg}; }
  686. }
  687. if (!defined $ignorecase) {
  688. $ignorecase = $Ignorecase;
  689. }
  690. read_config();
  691. foreach $package (keys %installed_packages) {
  692. my $results;
  693. if (defined $searchtag) {
  694. my %Package = %{ $installed_packages{$package} };
  695. parsePPD( %{ $Package{'INST_PPD'} } );
  696. $results = $current_package{$searchtag};
  697. }
  698. else {
  699. $results = $package;
  700. }
  701. if (!defined $searchRE) {
  702. $ret_hash{$package} = $results;
  703. }
  704. elsif ($results =~ /$searchRE/) {
  705. $ret_hash{$package} = $results;
  706. }
  707. elsif ($ignorecase eq "Yes" && ($results =~ /$searchRE/i)) {
  708. $ret_hash{$package} = $results;
  709. }
  710. }
  711. return %ret_hash;
  712. }
  713. # Returns the matched string on success, "" on no match, and undef
  714. # on error.
  715. sub QueryPPD
  716. {
  717. my (%argv) = @_;
  718. my ($location, $searchRE, $searchtag, $ignorecase, $package);
  719. my ($arg, $PPDfile, $string);
  720. foreach $arg (keys %argv) {
  721. if ($arg eq 'location') { $location = $argv{$arg}; }
  722. if ($arg eq 'searchRE' && defined $argv{$arg}) {
  723. $searchRE = $argv{$arg};
  724. eval { $searchRE =~ /$searchRE/ };
  725. if ($@) {
  726. &Trace("'$searchRE': invalid regular expression") if $Trace;
  727. $PPM::PPMERR = "'$searchRE': invalid regular expression.";
  728. return undef;
  729. }
  730. }
  731. if ($arg eq 'searchtag') { $searchtag = $argv{$arg}; }
  732. if ($arg eq 'ignorecase') { $ignorecase = $argv{$arg}; }
  733. if ($arg eq 'package') { $package = $argv{$arg}; }
  734. }
  735. if (!defined $ignorecase) {
  736. $ignorecase = $Ignorecase;
  737. }
  738. if (!$location) {
  739. read_config();
  740. }
  741. if (!defined($PPDfile = locatePPDfile($package, $location))) {
  742. &Trace("QueryPPD: Could not locate a PPD file for package $package") if $Trace;
  743. $PPM::PPMERR = "Could not locate a PPD file for package $package";
  744. return undef;
  745. }
  746. my %PPD = readPPDfile($PPDfile, 'XML::PPD');
  747. if (!defined %PPD) { return undef; }
  748. parsePPD(%PPD);
  749. my $retval = "";
  750. if ($searchtag eq 'abstract') {
  751. $string = $current_package{'ABSTRACT'}
  752. }
  753. elsif ($searchtag eq 'author') {
  754. $string = $current_package{'AUTHOR'}
  755. }
  756. elsif ($searchtag eq 'title') {
  757. $string = $current_package{'TITLE'}
  758. }
  759. if (!$searchRE) {
  760. $retval = $string;
  761. }
  762. elsif ($ignorecase eq "Yes") {
  763. if ($string =~ /$searchRE/i) {
  764. $retval = $string;
  765. }
  766. }
  767. elsif ($string =~ /$searchRE/) {
  768. $retval = $string;
  769. }
  770. return $retval;
  771. }
  772. # Returns a summary of available packages for all repositories.
  773. # Returned hash has the following structure:
  774. #
  775. # $hash{repository}{package_name}{NAME}
  776. # $hash{repository}{package_name}{VERSION}
  777. # etc.
  778. #
  779. sub RepositorySummary {
  780. my (%argv) = @_;
  781. my ($arg, $location, %summary, %locations);
  782. foreach $arg (keys %argv) {
  783. if ($arg eq 'location') { $location = $argv{$arg}; }
  784. }
  785. if (!defined $location) {
  786. read_config(); # need repositories
  787. foreach (keys %repositories) {
  788. $locations{$repositories{$_}{'LOCATION'}} = $repositories{$_}{'SUMMARYFILE'};
  789. }
  790. }
  791. else {
  792. foreach (keys %repositories) {
  793. if ($location =~ /\Q$repositories{$_}{'LOCATION'}\E/i) {
  794. $locations{$repositories{$_}{'LOCATION'}} = $repositories{$_}{'SUMMARYFILE'};
  795. last;
  796. }
  797. }
  798. }
  799. foreach $location (keys %locations) {
  800. my $summaryfile = $locations{$location};
  801. if (!$summaryfile) {
  802. &Trace("RepositorySummary: No summary available from $location.") if $Trace;
  803. $PPM::PPMERR = "No summary available from $location.\n";
  804. next;
  805. }
  806. # Todo: The following code should really be a call to
  807. # readPPDfile( "$location/$summaryfile", 'XML::RepositorySummary' );
  808. # for validation.
  809. if (!valid_URL_or_file("$location/$summaryfile")) {
  810. &Trace("RepositorySummary: No summary available from $location.") if $Trace;
  811. $PPM::PPMERR = "No summary available from $location.\n";
  812. }
  813. else {
  814. my ($data);
  815. if ($location =~ m@^...*://@i) {
  816. next if (!defined ($data = read_href("href" => "$location/$summaryfile", "request" => 'GET')));
  817. } else {
  818. local $/;
  819. next if (!open (DATAFILE, "$location/$summaryfile"));
  820. $data = <DATAFILE>;
  821. close(DATAFILE);
  822. }
  823. # take care of '&'
  824. $data =~ s/&(?!\w+;)/&amp;/go;
  825. my $parser = new XML::Parser( Style => 'Objects', Pkg => 'XML::RepositorySummary' );
  826. my @parsed = @{ $parser->parse( $data ) };
  827. my $packages = ${$parsed[0]}{Kids};
  828. foreach my $package (@{$packages}) {
  829. my $elem_type = ref $package;
  830. $elem_type =~ s/.*:://;
  831. next if ($elem_type eq 'Characters');
  832. if ($elem_type eq 'SOFTPKG') {
  833. my %ret_hash;
  834. parsePPD(%{$package});
  835. $ret_hash{'NAME'} = $current_package{'NAME'};
  836. $ret_hash{'TITLE'} = $current_package{'TITLE'};
  837. $ret_hash{'AUTHOR'} = $current_package{'AUTHOR'};
  838. $ret_hash{'VERSION'} = $current_package{'VERSION'};
  839. $ret_hash{'ABSTRACT'} = $current_package{'ABSTRACT'};
  840. $ret_hash{'PERLCORE_VER'} = $current_package{'PERLCORE_VER'};
  841. foreach my $dep (keys %{$current_package{'DEPEND'}}) {
  842. push @{$ret_hash{'DEPEND'}}, $dep;
  843. }
  844. $summary{$location}{$current_package{'NAME'}} = \%ret_hash;
  845. }
  846. }
  847. }
  848. }
  849. return %summary;
  850. }
  851. #
  852. # Internal subs
  853. #
  854. sub PPM_getstore {
  855. my (%argv) = @_;
  856. my ($arg, $source, $target, $username, $password);
  857. foreach $arg (keys %argv) {
  858. if ($arg eq 'source') { $source = $argv{$arg}; }
  859. if ($arg eq 'target') { $target = $argv{$arg}; }
  860. }
  861. if (!defined $source || !defined $target) {
  862. &Trace("PPM_getstore: No source or no target") if $Trace;
  863. $PPM::PPMERR = "No source or no target\n";
  864. return 1;
  865. }
  866. # Do we need to do authorization?
  867. # This is a hack, but will have to do for now.
  868. foreach (keys %repositories) {
  869. if ($source =~ /^$repositories{$_}{'LOCATION'}/i) {
  870. $username = $repositories{$_}{'USERNAME'};
  871. $password = $repositories{$_}{'PASSWORD'};
  872. last;
  873. }
  874. }
  875. if (defined $ENV{HTTP_proxy} || defined $username) {
  876. my $ua = new LWP::UserAgent;
  877. $ua->agent("$0/0.1 " . $ua->agent);
  878. my $proxy_user = $ENV{HTTP_proxy_user};
  879. my $proxy_pass = $ENV{HTTP_proxy_pass};
  880. $ua->env_proxy;
  881. my $req = new HTTP::Request('GET' => $source);
  882. if (defined $proxy_user && defined $proxy_pass) {
  883. &Trace("PPM_getstore: calling proxy_authorization_basic($proxy_user, $proxy_pass)") if $Trace > 1;
  884. $req->proxy_authorization_basic("$proxy_user", "$proxy_pass");
  885. }
  886. if (defined $username && defined $password) {
  887. &Trace("PPM_getstore: calling proxy_authorization_basic($username, $password)") if $Trace > 1;
  888. $req->authorization_basic($username, $password);
  889. }
  890. my $res = $ua->request($req);
  891. if ($res->is_success) {
  892. if (!open(OUT, ">$target")) {
  893. &Trace("PPM_getstore: Couldn't open $target for writing") if $Trace;
  894. $PPM::PPMERR = "Couldn't open $target for writing\n";
  895. return 1;
  896. }
  897. binmode(OUT);
  898. print OUT $res->content;
  899. close(OUT);
  900. } else {
  901. &Trace("PPM_getstore: Error reading $source: " . $res->code . " " . $res->message) if $Trace;
  902. $PPM::PPMERR = "Error reading $source: " . $res->code . " " . $res->message . "\n";
  903. return 1;
  904. }
  905. }
  906. else {
  907. my $status = LWP::Simple::getstore($source, $target);
  908. if ($status < 200 || $status > 299) {
  909. &Trace("PPM_getstore: Read of $source failed") if $Trace;
  910. $PPM::PPMERR = "Read of $source failed";
  911. return 1;
  912. }
  913. }
  914. return 0;
  915. }
  916. sub save_options
  917. {
  918. read_config();
  919. # Read in the existing PPM configuration file
  920. my %PPMConfig = readPPDfile( $PPM::PPMdat, 'XML::PPMConfig' );
  921. if (!defined %PPMConfig) { return 0; }
  922. # Remove all of the declarations for REPOSITORY and PPMPRECIOUS;
  923. # we'll output these from the lists we've got in memory instead.
  924. my $idx;
  925. foreach $idx (0 .. @{$PPMConfig{Kids}})
  926. {
  927. my $elem = $PPMConfig{Kids}[$idx];
  928. my $elem_type = ref $elem;
  929. if ($elem_type =~ /::REPOSITORY$|::PPMPRECIOUS$/o)
  930. {
  931. splice( @{$PPMConfig{Kids}}, $idx, 1 );
  932. redo; # Restart again so we don't miss any
  933. }
  934. }
  935. # Traverse the info we read in and replace the values in it with the new
  936. # config options that we've got.
  937. my $elem;
  938. foreach $elem (@{ $PPMConfig{Kids} })
  939. {
  940. my $elem_type = ref $elem;
  941. $elem_type =~ s/.*:://;
  942. next if ($elem_type ne 'OPTIONS');
  943. $elem->{IGNORECASE} = $Ignorecase;
  944. $elem->{CLEAN} = $Clean;
  945. $elem->{CONFIRM} = $Confirm;
  946. $elem->{FORCEINSTALL} = $Force_install;
  947. $elem->{ROOT} = $Root;
  948. $elem->{BUILDDIR} = $build_dir;
  949. $elem->{MORE} = $More;
  950. $elem->{TRACE} = $Trace;
  951. $elem->{TRACEFILE} = $TraceFile;
  952. }
  953. # Find out where the package listings start and insert our PPMPRECIOUS and
  954. # updated list of REPOSITORYs.
  955. foreach $idx (0 .. @{$PPMConfig{Kids}})
  956. {
  957. my $elem = $PPMConfig{Kids}[$idx];
  958. my $elem_type = ref $elem;
  959. $elem_type =~ s/.*:://;
  960. next unless (($elem_type eq 'PACKAGE') or
  961. ($idx == $#{$PPMConfig{Kids}}));
  962. # Insert our PPMPRECIOUS
  963. my $chardata = new XML::PPMConfig::Characters;
  964. $chardata->{Text} = join( ';', @required_packages );
  965. my $precious = new XML::PPMConfig::PPMPRECIOUS;
  966. push( @{$precious->{Kids}}, $chardata );
  967. splice( @{$PPMConfig{Kids}}, $idx, 0, $precious );
  968. # Insert the list of repositories we've got
  969. my $rep_name;
  970. foreach $rep_name (keys %repositories)
  971. {
  972. my $repository = new XML::PPMConfig::REPOSITORY;
  973. $repository->{NAME} = $rep_name;
  974. $repository->{LOCATION} = $repositories{ $rep_name }{'LOCATION'};
  975. $repository->{USERNAME} = $repositories{ $rep_name }{'USERNAME'} if defined $repositories{ $rep_name }{'USERNAME'};
  976. $repository->{PASSWORD} = $repositories{ $rep_name }{'PASSWORD'} if defined $repositories{ $rep_name }{'PASSWORD'};
  977. $repository->{SUMMARYFILE} = $repositories{ $rep_name }{'SUMMARYFILE'} if defined $repositories{ $rep_name }{'SUMMARYFILE'};
  978. splice( @{$PPMConfig{Kids}}, $idx, 0, $repository );
  979. }
  980. last;
  981. }
  982. # Take the data structure we've got and bless it into a PPMCONFIG object so
  983. # that we can output it.
  984. my $cfg = bless \%PPMConfig, 'XML::PPMConfig::PPMCONFIG';
  985. # Open the output file and output the PPM config file
  986. if (!open( DAT, ">$PPM::PPMdat" )) {
  987. &Trace("open of $PPM::PPMdat failed: $!") if $Trace;
  988. $PPM::PPMERR = "open of $PPM::PPMdat failed: $!\n";
  989. return 1;
  990. }
  991. my $oldout = select DAT;
  992. $cfg->output();
  993. select $oldout;
  994. close( DAT );
  995. &Trace("Wrote config file") if $Trace > 1;
  996. }
  997. # Returns a sorted array of packages with PPDs available from $location.
  998. # Sets PPM::PPMERR and returns undef on error.
  999. sub list_available
  1000. {
  1001. my (%argv) = @_;
  1002. my ($arg, $location, @ppds);
  1003. foreach $arg (keys %argv) {
  1004. if ($arg eq 'location') { $location = $argv{$arg}; }
  1005. }
  1006. if ($location =~ /^file:\/\/.*\|/i) {
  1007. # $location is a local directory, let's avoid LWP by changing
  1008. # it to a pathname.
  1009. $location =~ s@^file://@@i;
  1010. $location =~ s@^localhost/@@i;
  1011. $location =~ s@\|@:@;
  1012. }
  1013. # URL in UNC notation
  1014. if ($location =~ /^file:\/\/\/\//i) {
  1015. $location =~ s@^file://@@i;
  1016. }
  1017. # directory or UNC
  1018. if (-d $location || $location =~ /^\\\\/ || $location =~ /^\/\//) {
  1019. opendir(PPDDIR, $location) or return undef;
  1020. my ($file);
  1021. @ppds = grep { /\.ppd$/i && -f "$location/$_" } readdir(PPDDIR);
  1022. foreach $file (@ppds) {
  1023. $file =~ s/\.ppd//i;
  1024. }
  1025. }
  1026. elsif ($location =~ m@^...*://@i) {
  1027. my $doc = read_href("href" => $location, "request" => 'GET');
  1028. if (!defined $doc) {
  1029. return undef;
  1030. }
  1031. if ($doc =~ /^<head><title>/) {
  1032. # read an IIS format directory listing
  1033. @ppds = grep { /\.ppd/i } split('<br>', $doc);
  1034. my $file;
  1035. foreach $file (@ppds) {
  1036. $file =~ s/\.ppd<.*$//is;
  1037. $file =~ s@.*>@@is;
  1038. }
  1039. }
  1040. elsif ($doc =~ /<BODY BGCOLOR=FFFFFF>\n\n<form name=VPMform/s) {
  1041. # read output of default.prk over an HTTP connection
  1042. @ppds = grep { /^<!--Key:.*-->$/ } split('\n', $doc);
  1043. my $file;
  1044. foreach $file (@ppds) {
  1045. if ($file =~ /^<!--Key:(.*)-->$/) {
  1046. $file = $1;
  1047. }
  1048. }
  1049. }
  1050. else {
  1051. # read an Apache (?) format directory listing
  1052. @ppds = grep { /\.ppd/i } split('\n', $doc);
  1053. my $file;
  1054. foreach $file (@ppds) {
  1055. $file =~ s/^.*>(.*?)\.ppd<.*$/$1/i;
  1056. }
  1057. }
  1058. }
  1059. return sort @ppds;
  1060. }
  1061. sub read_href
  1062. {
  1063. my (%argv) = @_;
  1064. my ($arg, $href, $request, $proxy_user, $proxy_pass);
  1065. foreach $arg (keys %argv) {
  1066. if ($arg eq 'href') { $href = $argv{$arg}; }
  1067. if ($arg eq 'request') { $request = $argv{$arg}; }
  1068. }
  1069. my $ua = new LWP::UserAgent;
  1070. $ua->agent("$0/0.1 " . $ua->agent);
  1071. if (defined $ENV{HTTP_proxy}) {
  1072. $proxy_user = $ENV{HTTP_proxy_user};
  1073. $proxy_pass = $ENV{HTTP_proxy_pass};
  1074. &Trace("read_href: calling env_proxy: $ENV{'HTTP_proxy'}") if $Trace > 1;
  1075. $ua->env_proxy;
  1076. }
  1077. my $req = new HTTP::Request $request => $href;
  1078. if (defined $proxy_user && defined $proxy_pass) {
  1079. &Trace("read_href: calling proxy_authorization_basic($proxy_user, $proxy_pass)") if $Trace > 1;
  1080. $req->proxy_authorization_basic("$proxy_user", "$proxy_pass");
  1081. }
  1082. # Do we need to do authorization?
  1083. # This is a hack, but will have to do for now.
  1084. foreach (keys %repositories) {
  1085. if ($href =~ /^$repositories{$_}{'LOCATION'}/i) {
  1086. my $username = $repositories{$_}{'USERNAME'};
  1087. my $password = $repositories{$_}{'PASSWORD'};
  1088. if (defined $username && defined $password) {
  1089. &Trace("read_href: calling proxy_authorization_basic($username, $password)") if $Trace > 1;
  1090. $req->authorization_basic($username, $password);
  1091. last;
  1092. }
  1093. }
  1094. }
  1095. # send request
  1096. my $res = $ua->request($req);
  1097. # check the outcome
  1098. if ($res->is_success) {
  1099. return $res->content;
  1100. } else {
  1101. &Trace("read_href: Error reading $href: " . $res->code . " " . $res->message) if $Trace;
  1102. $PPM::PPMERR = "Error reading $href: " . $res->code . " " . $res->message . "\n";
  1103. return undef;
  1104. }
  1105. }
  1106. sub reread_config
  1107. {
  1108. %current_package = ();
  1109. %installed_packages = ();
  1110. $init = 0;
  1111. read_config();
  1112. }
  1113. # returns 0 on success, 1 and sets $PPMERR on error.
  1114. sub PPMdat_add_package
  1115. {
  1116. my ($location, $packlist, $inst_root) = @_;
  1117. my $package = $current_package{'NAME'};
  1118. my $time_str = localtime;
  1119. # If we already have this package installed, remove it from the PPM
  1120. # Configuration file so we can put the new one in.
  1121. if (defined $installed_packages{$package} ) {
  1122. # remove the existing entry for this package.
  1123. PPMdat_remove_package($package);
  1124. }
  1125. # Build the new SOFTPKG data structure for this package we're adding.
  1126. my $softpkg =
  1127. new XML::PPMConfig::SOFTPKG( NAME => $package,
  1128. VERSION => $current_package{VERSION}
  1129. );
  1130. if (defined $current_package{TITLE})
  1131. {
  1132. my $chardata =
  1133. new XML::PPMConfig::Characters( Text => $current_package{TITLE} );
  1134. my $newelem = new XML::PPMConfig::TITLE;
  1135. push( @{$newelem->{Kids}}, $chardata );
  1136. push( @{$softpkg->{Kids}}, $newelem );
  1137. }
  1138. if (defined $current_package{ABSTRACT})
  1139. {
  1140. my $chardata =
  1141. new XML::PPMConfig::Characters( Text => $current_package{ABSTRACT});
  1142. my $newelem = new XML::PPMConfig::ABSTRACT;
  1143. push( @{$newelem->{Kids}}, $chardata );
  1144. push( @{$softpkg->{Kids}}, $newelem );
  1145. }
  1146. if (defined $current_package{AUTHOR})
  1147. {
  1148. my $chardata =
  1149. new XML::PPMConfig::Characters( Text => $current_package{AUTHOR} );
  1150. my $newelem = new XML::PPMConfig::AUTHOR;
  1151. push( @{$newelem->{Kids}}, $chardata );
  1152. push( @{$softpkg->{Kids}}, $newelem );
  1153. }
  1154. if (defined $current_package{LICENSE})
  1155. {
  1156. my $chardata =
  1157. new XML::PPMConfig::Characters( Text => $current_package{LICENSE});
  1158. my $newelem = new XML::PPMConfig::LICENSE;
  1159. push( @{$newelem->{Kids}}, $chardata );
  1160. push( @{$softpkg->{Kids}}, $newelem );
  1161. }
  1162. my $impl = new XML::PPMConfig::IMPLEMENTATION;
  1163. push( @{$softpkg->{Kids}}, $impl );
  1164. if (defined $current_package{PERLCORE_VER})
  1165. {
  1166. my $newelem = new XML::PPMConfig::PERLCORE( VERSION => $current_package{PERLCORE_VER} );
  1167. push( @{$impl->{Kids}}, $newelem );
  1168. }
  1169. foreach $_ (keys %{$current_package{DEPEND}})
  1170. {
  1171. my $newelem = new XML::PPMConfig::DEPENDENCY( NAME => $_, VERSION => $current_package{DEPEND}{$_} );
  1172. push( @{$impl->{Kids}}, $newelem );
  1173. }
  1174. my $codebase = new XML::PPMConfig::CODEBASE( HREF => $current_package{CODEBASE} );
  1175. push( @{$impl->{Kids}}, $codebase );
  1176. my $inst = new XML::PPMConfig::INSTALL;
  1177. push( @{$impl->{Kids}}, $inst );
  1178. if (defined $current_package{INSTALL_EXEC})
  1179. { $inst->{EXEC} = $current_package{INSTALL_EXEC}; }
  1180. if (defined $current_package{INSTALL_HREF})
  1181. { $inst->{HREF} = $current_package{INSTALL_HREF}; }
  1182. if (defined $current_package{INSTALL_SCRIPT})
  1183. {
  1184. my $chardata =
  1185. new XML::PPMConfig::Characters( Text => $current_package{INSTALL_SCRIPT} );
  1186. push( @{$inst->{Kids}}, $chardata );
  1187. }
  1188. my $uninst = new XML::PPMConfig::UNINSTALL;
  1189. push( @{$impl->{Kids}}, $uninst );
  1190. if (defined $current_package{UNINSTALL_EXEC})
  1191. { $uninst->{EXEC} = $current_package{UNINSTALL_EXEC}; }
  1192. if (defined $current_package{UNINSTALL_HREF})
  1193. { $uninst->{HREF} = $current_package{UNINSTALL_HREF}; }
  1194. if (defined $current_package{UNINSTALL_SCRIPT})
  1195. {
  1196. my $chardata =
  1197. new XML::PPMConfig::Characters( Text => $current_package{UNINSTALL_SCRIPT} );
  1198. push( @{$uninst->{Kids}}, $chardata );
  1199. }
  1200. # Then, build the PACKAGE object and stick the SOFTPKG inside of it.
  1201. my $pkg = new XML::PPMConfig::PACKAGE( NAME => $package );
  1202. if (defined $location)
  1203. {
  1204. my $chardata = new XML::PPMConfig::Characters( Text => $location );
  1205. my $newelem = new XML::PPMConfig::LOCATION;
  1206. push( @{$newelem->{Kids}}, $chardata );
  1207. push( @{$pkg->{Kids}}, $newelem );
  1208. }
  1209. if (defined $packlist)
  1210. {
  1211. my $chardata = new XML::PPMConfig::Characters( Text => $packlist );
  1212. my $newelem = new XML::PPMConfig::INSTPACKLIST;
  1213. push( @{$newelem->{Kids}}, $chardata );
  1214. push( @{$pkg->{Kids}}, $newelem );
  1215. }
  1216. if (defined $inst_root)
  1217. {
  1218. my $chardata = new XML::PPMConfig::Characters( Text => $inst_root );
  1219. my $newelem = new XML::PPMConfig::INSTROOT;
  1220. push( @{$newelem->{Kids}}, $chardata );
  1221. push( @{$pkg->{Kids}}, $newelem );
  1222. }
  1223. if (defined $time_str)
  1224. {
  1225. my $chardata = new XML::PPMConfig::Characters( Text => $time_str);
  1226. my $newelem = new XML::PPMConfig::INSTDATE;
  1227. push( @{$newelem->{Kids}}, $chardata );
  1228. push( @{$pkg->{Kids}}, $newelem );
  1229. }
  1230. my $instppd = new XML::PPMConfig::INSTPPD;
  1231. push( @{$instppd->{Kids}}, $softpkg );
  1232. push( @{$pkg->{Kids}}, $instppd );
  1233. # Now that we've got the structure built, read in the existing PPM
  1234. # Configuration file, add this to it, and spit it back out.
  1235. my %PPMConfig = readPPDfile( $PPM::PPMdat, 'XML::PPMConfig' );
  1236. if (!defined %PPMConfig) { return 1; }
  1237. push( @{$PPMConfig{Kids}}, $pkg );
  1238. my $cfg = bless \%PPMConfig, 'XML::PPMConfig::PPMCONFIG';
  1239. if (!open( DAT, ">$PPM::PPMdat" )) {
  1240. &Trace("open of $PPM::PPMdat failed: $!") if $Trace;
  1241. $PPM::PPMERR = "open of $PPM::PPMdat failed: $!\n";
  1242. return 1;
  1243. }
  1244. my $oldout = select DAT;
  1245. $cfg->output();
  1246. select $oldout;
  1247. close( DAT );
  1248. &Trace("PPMdat_add_package: wrote $PPM::PPMdat") if $Trace > 1;
  1249. return 0;
  1250. }
  1251. # returns 0 on success, 1 and sets $PPMERR on error.
  1252. sub PPMdat_remove_package
  1253. {
  1254. my ($package) = @_;
  1255. # Read in the existing PPM configuration file
  1256. my %PPMConfig = readPPDfile( $PPM::PPMdat, 'XML::PPMConfig' );
  1257. if (!defined %PPMConfig) { return 1; }
  1258. # Try to find the package that we're supposed to be removing, and yank it
  1259. # out of the list of installed packages.
  1260. my $idx;
  1261. foreach $idx (0 .. @{$PPMConfig{Kids}})
  1262. {
  1263. my $elem = $PPMConfig{Kids}[$idx];
  1264. my $elem_type = ref $elem;
  1265. next if ($elem_type !~ /::PACKAGE$/o);
  1266. next if ($elem->{NAME} ne $package);
  1267. splice( @{$PPMConfig{Kids}}, $idx, 1 );
  1268. }
  1269. # Take the data structure we've got and bless it into a PPMCONFIG object so
  1270. # that we can output it again.
  1271. my $cfg = bless \%PPMConfig, 'XML::PPMConfig::PPMCONFIG';
  1272. # Now that we've removed the package, save the configuration file back out.
  1273. if (!open( DAT, ">$PPM::PPMdat" )) {
  1274. $PPM::PPMERR = "open of $PPM::PPMdat failed: $!\n";
  1275. return 1;
  1276. }
  1277. my $oldout = select DAT;
  1278. $cfg->output();
  1279. select $oldout;
  1280. close( DAT );
  1281. &Trace("PPMdat_remove_package: wrote $PPM::PPMdat") if $Trace > 1;
  1282. return 0;
  1283. }
  1284. # Run $script using system(). If $scriptHREF is specified, its contents are
  1285. # used as the script. If $exec is specified, the script is saved to a
  1286. # temporary file and executed by $exec.
  1287. sub run_script
  1288. {
  1289. my (%argv) = @_;
  1290. my ($arg, $script, $scriptHREF, $exec, $inst_root, $inst_archlib);
  1291. foreach $arg (keys %argv) {
  1292. if ($arg eq 'script') { $script = $argv{$arg}; }
  1293. if ($arg eq 'scriptHREF') { $scriptHREF = $argv{$arg}; }
  1294. if ($arg eq 'exec') { $exec = $argv{$arg}; }
  1295. if ($arg eq 'inst_root') { $inst_root = $argv{$arg}; }
  1296. if ($arg eq 'inst_archlib') { $inst_archlib = $argv{$arg}; }
  1297. }
  1298. my (@commands, $tmpname);
  1299. if ($scriptHREF) {
  1300. if ($exec) {
  1301. # store in a temp file.
  1302. $tmpname = $build_dir . "/PPM-" . time();
  1303. LWP::Simple::getstore($scriptHREF, $tmpname);
  1304. }
  1305. else {
  1306. my $doc = LWP::Simple::get $scriptHREF;
  1307. if (!defined $doc) {
  1308. print $PPM::PPMERR;
  1309. }
  1310. @commands = split("\n", $doc);
  1311. if (!defined $commands[0]) {
  1312. print $PPM::PPMERR;
  1313. return 0;
  1314. }
  1315. }
  1316. }
  1317. else {
  1318. if (-f $script) {
  1319. $tmpname = $script;
  1320. }
  1321. else {
  1322. # change any escaped chars
  1323. $script =~ s/&lt;/</gi;
  1324. $script =~ s/&gt;/>/gi;
  1325. @commands = split(';;', $script);
  1326. if ($exec) {
  1327. my $command;
  1328. # store in a temp file.
  1329. $tmpname = $build_dir . "/PPM-" . time();
  1330. open(TMP, ">$tmpname");
  1331. foreach $command (@commands) {
  1332. print TMP "$command\n";
  1333. }
  1334. close(TMP);
  1335. }
  1336. }
  1337. }
  1338. $ENV{'PPM_INSTROOT'} = $inst_root;
  1339. $ENV{'PPM_INSTARCHLIB'} = $inst_archlib;
  1340. if ($exec) {
  1341. if ($exec =~ /^PPM_PERL$/i) {
  1342. $exec = $^X;
  1343. }
  1344. system("start $exec $tmpname");
  1345. }
  1346. else {
  1347. my $command;
  1348. for $command (@commands) {
  1349. system($command);
  1350. }
  1351. }
  1352. }
  1353. sub parsePPD
  1354. {
  1355. my %PPD = @_;
  1356. my $pkg;
  1357. %current_package = ();
  1358. # Get the package name and version from the attributes and stick it
  1359. # into the 'current package' global var
  1360. $current_package{NAME} = $PPD{NAME};
  1361. $current_package{VERSION} = $PPD{VERSION};
  1362. # Get all the information for this package and put it into the 'current
  1363. # package' global var.
  1364. my $got_implementation = 0;
  1365. my $elem;
  1366. foreach $elem (@{$PPD{Kids}})
  1367. {
  1368. my $elem_type = ref $elem;
  1369. $elem_type =~ s/.*:://;
  1370. next if ($elem_type eq 'Characters');
  1371. if ($elem_type eq 'TITLE')
  1372. {
  1373. # Get the package title out of our _only_ char data child
  1374. $current_package{TITLE} = $elem->{Kids}[0]{Text};
  1375. }
  1376. elsif ($elem_type eq 'LICENSE')
  1377. {
  1378. # Get the HREF for the license out of our attribute
  1379. $current_package{LICENSE} = $elem->{HREF};
  1380. }
  1381. elsif ($elem_type eq 'ABSTRACT')
  1382. {
  1383. # Get the package abstract out of our _only_ char data child
  1384. $current_package{ABSTRACT} = $elem->{Kids}[0]{Text};
  1385. }
  1386. elsif ($elem_type eq 'AUTHOR')
  1387. {
  1388. # Get the authors name out of our _only_ char data child
  1389. $current_package{AUTHOR} = $elem->{Kids}[0]{Text};
  1390. }
  1391. elsif ($elem_type eq 'IMPLEMENTATION')
  1392. {
  1393. # If we don't have a valid implementation yet, check if this is
  1394. # it.
  1395. next if ($got_implementation);
  1396. $got_implementation = implementation( @{ $elem->{Kids} } );
  1397. }
  1398. else
  1399. {
  1400. &Trace("Unknown element '$elem_type' found inside SOFTPKG") if $Trace;
  1401. die "Unknown element '$elem_type' found inside SOFTPKG.";
  1402. }
  1403. } # End of "for each child element inside the PPD"
  1404. if ($Trace > 3 and (%current_package) ) {
  1405. &Trace("Read a PPD:");
  1406. my $elem;
  1407. foreach $elem (keys %current_package) {
  1408. &Trace("\t$elem:\t$current_package{$elem}");
  1409. }
  1410. }
  1411. if ( ($Debug & 2) and (%current_package) )
  1412. {
  1413. print "Read a PPD...\n";
  1414. my $elem;
  1415. foreach $elem (keys %current_package)
  1416. { print "\t$elem:\t$current_package{$elem}\n"; }
  1417. }
  1418. }
  1419. # Tests the passed IMPLEMENTATION for suitability on the current platform.
  1420. # Fills in the CODEBASE, INSTALL_HREF, INSTALL_EXEC, INSTALL_SCRIPT,
  1421. # UNINSTALL_HREF, UNINSTALL_EXEC, UNINSTALL_SCRIPT and DEPEND keys of
  1422. # %current_package. Returns 1 on success, 0 otherwise.
  1423. sub implementation
  1424. {
  1425. my @impl = @_;
  1426. # Declare the tmp vars we're going to use to hold onto things.
  1427. my ($ImplProcessor, $ImplOS, $ImplLanguage, $ImplCodebase);
  1428. my ($ImplInstallHREF, $ImplInstallEXEC, $ImplInstallScript);
  1429. my ($ImplUninstallHREF, $ImplUninstallEXEC, $ImplUninstallScript);
  1430. my ($ImplArch, $ImplPerlCoreVer, %ImplDepend);
  1431. my $elem;
  1432. foreach $elem (@impl)
  1433. {
  1434. my $elem_type = ref $elem;
  1435. $elem_type =~ s/.*:://;
  1436. next if ($elem_type eq 'Characters');
  1437. if ($elem_type eq 'CODEBASE')
  1438. {
  1439. # Get the reference to the codebase out of our attributes.
  1440. $ImplCodebase = $elem->{HREF};
  1441. }
  1442. elsif ($elem_type eq 'DEPENDENCY')
  1443. {
  1444. # Get the name of any dependencies we have out of our attributes.
  1445. # Dependencies in old PPDs might not have version info.
  1446. $ImplDepend{$elem->{NAME}} = (defined $elem->{VERSION} && $elem->{VERSION} ne "") ? $elem->{VERSION} : "0,0,0,0";
  1447. }
  1448. elsif ($elem_type eq 'LANGUAGE')
  1449. {
  1450. # Get the language out of our attributes (if we don't already have
  1451. # the right one).
  1452. if ($ImplLanguage && ($ImplLanguage ne $LANGUAGE))
  1453. { $ImplLanguage = $elem->{VALUE}; }
  1454. }
  1455. elsif ($elem_type eq 'ARCHITECTURE') {
  1456. $ImplArch = $elem->{VALUE};
  1457. }
  1458. elsif ($elem_type eq 'OS')
  1459. {
  1460. # Get the OS value out of our attribute.
  1461. $ImplOS = $elem->{VALUE};
  1462. }
  1463. elsif ($elem_type eq 'OSVERSION')
  1464. {
  1465. # UNFINISHED -> OSVERSION is an ignored element when parsing a PPD
  1466. }
  1467. elsif ($elem_type eq 'PERLCORE')
  1468. {
  1469. # Get the compiled Perl core value out of our attributes
  1470. $ImplPerlCoreVer = $elem->{VERSION};
  1471. }
  1472. elsif ($elem_type eq 'PROCESSOR')
  1473. {
  1474. # Get the processor value out of our attribute
  1475. $ImplProcessor = $elem->{VALUE};
  1476. }
  1477. elsif ($elem_type eq 'INSTALL')
  1478. {
  1479. # Get anything which might have been an attribute
  1480. $ImplInstallHREF = $elem->{HREF};
  1481. $ImplInstallEXEC = $elem->{EXEC};
  1482. # Get any raw Perl script out of here (if we've got any)
  1483. if ( (exists $elem->{Kids}) and (exists $elem->{Kids}[0]{Text}) )
  1484. { $ImplInstallScript = $elem->{Kids}[0]{Text}; }
  1485. }
  1486. elsif ($elem_type eq 'UNINSTALL')
  1487. {
  1488. # Get anything which might have been an attribute
  1489. $ImplUninstallHREF = $elem->{HREF};
  1490. $ImplUninstallEXEC = $elem->{EXEC};
  1491. # Get any raw Perl script out of here (if we've got any)
  1492. if ( (exists $elem->{Kids}) and (exists $elem->{Kids}[0]{Text}) )
  1493. { $ImplUninstallScript = $elem->{Kids}[0]{Text}; }
  1494. }
  1495. else
  1496. {
  1497. die "Unknown element '$elem_type' found inside of IMPLEMENTATION.";
  1498. }
  1499. } # end of 'for every element inside IMPLEMENTATION'
  1500. # Check to see if we've found a valid IMPLEMENTATION for the target
  1501. # machine.
  1502. if ( (defined $ImplArch) and ($ImplArch ne $Config{'archname'}) )
  1503. { return 0; }
  1504. if ( (defined $ImplProcessor) and ($ImplProcessor ne $CPU) )
  1505. { return 0; }
  1506. if ( (defined $ImplLanguage) and ($ImplLanguage ne $LANGUAGE) )
  1507. { return 0; }
  1508. if ( (defined $ImplOS) and ($ImplOS ne $OS_VALUE) )
  1509. { return 0; }
  1510. # UNFINISHED -> No check was done in previous PPM against OS_VERSION
  1511. # Got a valid IMPLEMENTATION, stuff all the values we just read in into the
  1512. # 'current package' global var.
  1513. $current_package{PERLCORE_VER} = $ImplPerlCoreVer
  1514. if (defined $ImplPerlCoreVer);
  1515. $current_package{CODEBASE} = $ImplCodebase
  1516. if (defined $ImplCodebase);
  1517. $current_package{INSTALL_HREF} = $ImplInstallHREF
  1518. if (defined $ImplInstallHREF);
  1519. $current_package{INSTALL_EXEC} = $ImplInstallEXEC
  1520. if (defined $ImplInstallEXEC);
  1521. $current_package{INSTALL_SCRIPT} = $ImplInstallScript
  1522. if (defined $ImplInstallScript);
  1523. $current_package{UNINSTALL_HREF} = $ImplUninstallHREF
  1524. if (defined $ImplUninstallHREF);
  1525. $current_package{UNINSTALL_EXEC} = $ImplUninstallEXEC
  1526. if (defined $ImplUninstallEXEC);
  1527. $current_package{UNINSTALL_SCRIPT} = $ImplUninstallScript
  1528. if (defined $ImplUninstallScript);
  1529. %{$current_package{DEPEND}} = %ImplDepend
  1530. if (defined %ImplDepend);
  1531. return 1;
  1532. }
  1533. sub valid_URL_or_file
  1534. {
  1535. my ($File) = @_;
  1536. if ($File =~ /^file:\/\/.*\|/i) {
  1537. # $File is a local directory, let's avoid LWP by changing
  1538. # it to a pathname.
  1539. $File =~ s@^file://@@i;
  1540. $File =~ s@^localhost/@@i;
  1541. $File =~ s@\|@:@;
  1542. }
  1543. return 1 if (-f $File);
  1544. return 1 if ($File =~ m@^...*://@i && defined read_href("href" => $File, "request" => 'HEAD'));
  1545. &Trace("valid_URL_or_file: $File is not valid") if $Trace;
  1546. return 0;
  1547. }
  1548. # Builds a fully qualified pathname or URL to a PPD for $Package.
  1549. # If '$location' argument is given, that is used. Otherwise, the
  1550. # '<LOCATION>' tag for a previously installed version is used, and
  1551. # if that fails, the default locations are looked at.
  1552. #
  1553. # returns undef if it can't find a valid file or URL.
  1554. #
  1555. sub locatePPDfile
  1556. {
  1557. my ($Package, $location) = @_;
  1558. my $PPDfile;
  1559. if (defined($location)) {
  1560. if ($location =~ /[^\/]$/) { $location .= "/"; }
  1561. $PPDfile = $location . $Package . ".ppd";
  1562. if (!valid_URL_or_file($PPDfile)) {
  1563. # not good.
  1564. undef $PPDfile;
  1565. }
  1566. }
  1567. else {
  1568. # Is $Package a filename or URL?
  1569. if (valid_URL_or_file($Package)) {
  1570. $PPDfile = $Package;
  1571. }
  1572. # does the package have a <LOCATION> in $PPM::PPMdat?
  1573. elsif (defined( $installed_packages{$Package} )) {
  1574. $location = $installed_packages{$Package}{'LOCATION'};
  1575. if ($location =~ /[^\/]$/) { $location .= "/"; }
  1576. $PPDfile = $location . $Package . ".ppd";
  1577. if (!valid_URL_or_file($PPDfile)) {
  1578. # not good.
  1579. undef $PPDfile;
  1580. }
  1581. }
  1582. if (!defined $PPDfile) {
  1583. # No, try the defaults.
  1584. my $location;
  1585. foreach $_ (keys %repositories) {
  1586. $location = $repositories{$_}{'LOCATION'};
  1587. if ($location =~ /[^\/]$/) { $location .= "/"; }
  1588. $PPDfile = $location . $Package . ".ppd";
  1589. if (valid_URL_or_file($PPDfile)) {
  1590. last;
  1591. }
  1592. undef $PPDfile;
  1593. }
  1594. }
  1595. }
  1596. # return the fully qualified pathname or HREF
  1597. return $PPDfile;
  1598. }
  1599. # reads and parses $PPDfile, which can be a file or HREF
  1600. sub readPPDfile
  1601. {
  1602. my ($PPDfile, $Pkg) = @_;
  1603. my (@DATA, $CONTENTS);
  1604. $Pkg = 'XML::PPD' unless (defined $Pkg);
  1605. if ($PPDfile =~ /^file:\/\/.*\|/i) {
  1606. # $PPDfile is a local directory, let's avoid LWP by changing
  1607. # it to a pathname.
  1608. $PPDfile =~ s@^file://@@i;
  1609. $PPDfile =~ s@^localhost/@@i;
  1610. $PPDfile =~ s@\|@:@;
  1611. }
  1612. # if $PPDfile is an HREF
  1613. if ($PPDfile =~ m@^...*://@i) {
  1614. if (!defined (@DATA = read_href("href" => $PPDfile, "request" => 'GET'))) {
  1615. &Trace("readPPDfile: read_href of $PPDfile failed") if $Trace;
  1616. return undef;
  1617. }
  1618. $CONTENTS = join('', @DATA);
  1619. } else {
  1620. # else $PPDfile is a regular file
  1621. if (!open (DATAFILE, $PPDfile)) {
  1622. &Trace("readPPDfile: open of $PPDfile failed") if $Trace;
  1623. $PPM::PPMERR = "open of $PPDfile failed: $!\n";
  1624. return undef;
  1625. }
  1626. @DATA = <DATAFILE>;
  1627. close(DATAFILE);
  1628. $CONTENTS = join('', @DATA);
  1629. }
  1630. # take care of '&'
  1631. $CONTENTS =~ s/&(?!\w+;)/&amp;/go;
  1632. # Got everything we need, no just parse the PPD file
  1633. my $parser = new XML::Parser( Style => 'Objects', Pkg => $Pkg );
  1634. my @parsed = @{ $parser->parse( $CONTENTS ) };
  1635. # Validate what we've read in and output an error if something wasn't
  1636. # parsed properly. As well, if we found any errors, return undef.
  1637. if (!$parsed[0]->rvalidate( \&PPM::parse_err ))
  1638. { return undef; }
  1639. # Done, and things did seem to get parsed properly, return the structure.
  1640. return %{$parsed[0]};
  1641. }
  1642. # Spits out the error from parsing, and sets our global error message
  1643. # accordingly.
  1644. sub parse_err
  1645. {
  1646. &Trace("parse_err: @_") if $Trace;
  1647. warn @_;
  1648. $PPM::PPMERR = 'Errors found while parsing document.';
  1649. }
  1650. # reads and parses the PPM data file $PPM::PPMdat. Stores config information in
  1651. # $PPM_ver, $build_dir, %repositories, $CPU, $OS_VALUE, and $OS_VERSION.
  1652. # Stores information about individual packages in the hash %installed_packages.
  1653. sub read_config
  1654. {
  1655. if ($init) { return; }
  1656. else { $init++; }
  1657. my %PPMConfig = readPPDfile($PPM::PPMdat, 'XML::PPMConfig');
  1658. if (!defined %PPMConfig) { return 0; }
  1659. my $elem;
  1660. foreach $elem (@{$PPMConfig{Kids}})
  1661. {
  1662. my $subelem = ref $elem;
  1663. $subelem =~ s/.*:://;
  1664. next if ($subelem eq 'Characters');
  1665. if ($subelem eq 'PPMVER')
  1666. {
  1667. # Get the value out of our _only_ character data element.
  1668. $PPM_ver = $elem->{Kids}[0]{Text};
  1669. }
  1670. elsif ($subelem eq 'PPMPRECIOUS')
  1671. {
  1672. # Get the value out of our _only_ character data element.
  1673. @required_packages = split( ';', $elem->{Kids}[0]{Text} );
  1674. }
  1675. elsif ($subelem eq 'PLATFORM')
  1676. {
  1677. # Get values out of our attributes
  1678. $CPU = $elem->{CPU};
  1679. $OS_VALUE = $elem->{OSVALUE};
  1680. $OS_VERSION = $elem->{OSVERSION};
  1681. $LANGUAGE = $elem->{LANGUAGE};
  1682. }
  1683. elsif ($subelem eq 'REPOSITORY')
  1684. {
  1685. # Get a repository out of the element attributes
  1686. my ($name);
  1687. $name = $elem->{NAME};
  1688. $repositories{ $name }{'LOCATION'} = $elem->{LOCATION};
  1689. $repositories{ $name }{'USERNAME'} = $elem->{USERNAME};
  1690. $repositories{ $name }{'PASSWORD'} = $elem->{PASSWORD};
  1691. $repositories{ $name }{'SUMMARYFILE'} = $elem->{SUMMARYFILE};
  1692. }
  1693. elsif ($subelem eq 'OPTIONS')
  1694. {
  1695. # Get our options out of the element attributes
  1696. $Ignorecase = $elem->{IGNORECASE};
  1697. $Clean = $elem->{CLEAN};
  1698. $Confirm = $elem->{CONFIRM};
  1699. $Force_install = $elem->{FORCEINSTALL};
  1700. $Root = $elem->{ROOT};
  1701. $More = $elem->{MORE};
  1702. $Trace = defined $elem->{TRACE} ? $elem->{TRACE} : "0";
  1703. $TraceFile = defined $elem->{TRACEFILE} ? $elem->{TRACEFILE} : "PPM.LOG";
  1704. if (defined $Root)
  1705. { PPM::chroot( 'location' => $Root ); }
  1706. $build_dir = $elem->{BUILDDIR};
  1707. # Strip trailing separator
  1708. my $chr = substr( $build_dir, -1, 1 );
  1709. if ($chr eq '/' || $chr eq '\\')
  1710. { chop $build_dir; }
  1711. if ($Trace && !$TraceStarted) {
  1712. $TraceFile = "PPM.log" if (!defined $TraceFile);
  1713. open(PPMTRACE, ">>$TraceFile");
  1714. my $oldfh = select(PPMTRACE);
  1715. $| = 1;
  1716. select($oldfh);
  1717. &Trace("starting up...");
  1718. $TraceStarted = 1;
  1719. }
  1720. }
  1721. elsif ($subelem eq 'PACKAGE')
  1722. {
  1723. # Get our package name out of our attributes
  1724. my $pkg = $elem->{NAME};
  1725. # Gather the information on this package from the child elements.
  1726. my ($loc, $instdate, $root, $packlist, $ppd);
  1727. my $child;
  1728. foreach $child (@{$elem->{Kids}})
  1729. {
  1730. my $child_type = ref $child;
  1731. $child_type =~ s/.*:://;
  1732. next if ($child_type eq 'Characters');
  1733. if ($child_type eq 'LOCATION')
  1734. { $loc = $child->{Kids}[0]{Text}; }
  1735. elsif ($child_type eq 'INSTDATE')
  1736. { $instdate = $child->{Kids}[0]{Text}; }
  1737. elsif ($child_type eq 'INSTROOT')
  1738. { $root = $child->{Kids}[0]{Text}; }
  1739. elsif ($child_type eq 'INSTPACKLIST')
  1740. { $packlist = $child->{Kids}[0]{Text}; }
  1741. elsif ($child_type eq 'INSTPPD')
  1742. {
  1743. # Find the SOFTPKG inside here and hang onto it
  1744. my $tmp;
  1745. foreach $tmp (@{$child->{Kids}})
  1746. {
  1747. if ((ref $tmp) =~ /::SOFTPKG$/o)
  1748. { $ppd = $tmp; }
  1749. }
  1750. }
  1751. else
  1752. {
  1753. die "Unknown element inside of $pkg PACKAGE; $child";
  1754. }
  1755. } # end 'foreach $child...'
  1756. my %package_details = (
  1757. LOCATION => $loc,
  1758. INST_DATE => $instdate,
  1759. INST_ROOT => $root,
  1760. INST_PACKLIST => $packlist,
  1761. INST_PPD => $ppd
  1762. );
  1763. $installed_packages{ $pkg } = \%package_details;
  1764. }
  1765. else
  1766. {
  1767. die "Unknown element found in PPD_DAT file; $subelem";
  1768. }
  1769. }
  1770. if ($Debug & 1) {
  1771. print "This is ppm, version $PPM_ver.\nRepository locations:\n";
  1772. foreach (keys %repositories) {
  1773. print "\t$_: $repositories{$_}{'LOCATION'}\n"
  1774. }
  1775. print "Platform is $OS_VALUE version $OS_VERSION on a $CPU CPU.\n";
  1776. print "Packages will be built in $build_dir\n";
  1777. print "Commands will " . ($Confirm eq "Yes" ? "" : "not ") . "be confirmed.\n";
  1778. print "Temporary files will " . ($Clean eq "Yes" ? "" : "not ") . "be deleted.\n";
  1779. print "Installations will " . ($Force_install eq "Yes" ? "" : "not ") . "continue if a dependency cannot be installed.\n";
  1780. print "Screens will " . ($More > 0 ? "pause after each $More lines.\n" : "not pause after the screen is full.\n");
  1781. print "Tracing info will " . ($Trace > 0 ? "be written to $TraceFile.\n" : "not be written.\n");
  1782. print "Case-" . ($Ignorecase eq "Yes" ? "in" : "") . "sensitive searches will be performed.\n";
  1783. my $pkg;
  1784. foreach $pkg (keys %installed_packages)
  1785. {
  1786. print "\nFound installed package $pkg, " .
  1787. "installed on $installed_packages{$pkg}{INST_DATE}\n" .
  1788. "in directory root $installed_packages{$pkg}{INST_ROOT} " .
  1789. "from $installed_packages{$pkg}{'LOCATION'}.\n\n";
  1790. }
  1791. }
  1792. }
  1793. sub Trace
  1794. {
  1795. print PPMTRACE "$0: @_ at ", scalar localtime(), "\n";
  1796. }
  1797. 1;
  1798. __END__
  1799. =head1 NAME
  1800. ppm - PPM (Perl Package Management)
  1801. =head1 SYNOPSIS
  1802. use PPM;
  1803. PPM::InstallPackage("package" => $package, "location" => $location, "root" => $root);
  1804. PPM::RemovePackage("package" => $package, "force" => $force);
  1805. PPM::VerifyPackage("package" => $package, "location" => $location, "upgrade" => $upgrade);
  1806. PPM::QueryInstalledPackages("searchRE" => $searchRE, "searchtag" => $searchtag, "ignorecase" => $ignorecase);
  1807. PPM::InstalledPackageProperties();
  1808. PPM::QueryPPD("location" => $location, "searchRE" => $searchRE, "searchtag" => $searchtag, "ignorecase" => $ignorecase, "package" => $package);
  1809. PPM::ListOfRepositories();
  1810. PPM::RemoveRepository("repository" => $repository, "save" => $save);
  1811. PPM::AddRepository("repository" => $repository, "location" => $location, "save" => $save);
  1812. PPM::RepositoryPackages("location" => $location);
  1813. PPM::RepositoryPackageProperties("package" => $package, "location" => $location);
  1814. PPM::RepositorySummary("location" => $location);
  1815. PPM::GetPPMOptions();
  1816. PPM::SetPPMOptions("options" => %options, "save" => $save);
  1817. =head1 DESCRIPTION
  1818. PPM is a group of functions intended to simplify the tasks of locating,
  1819. installing, upgrading and removing software 'packages'. It can determine
  1820. if the most recent version of a software package is installed on a system,
  1821. and can install or upgrade that package from a local or remote host.
  1822. PPM uses files containing a modified form of the Open Software Distribution
  1823. (OSD) specification for information about software packages.
  1824. These description files, which are written in Extensible Markup
  1825. Language (XML) code, are referred to as 'PPD' files. Information about
  1826. OSD can be found at the W3C web site (at the time of this writing,
  1827. http://www.w3.org/TR/NOTE-OSD.html). The modifications to OSD used by PPM
  1828. are documented in PPM::ppd.
  1829. PPD files for packages are generated from POD files using the pod2ppd
  1830. command.
  1831. =head1 USAGE
  1832. =over 4
  1833. =item PPM::InstallPackage("package" => $package, "location" => $location, "root" => $root);
  1834. Installs the specified package onto the local system. 'package' may
  1835. be a simple package name ('foo'), a pathname (P:\PACKAGES\FOO.PPD) or
  1836. a URL (HTTP://www.ActiveState.com/packages/foo.ppd). In the case of a
  1837. simple package name, the function will look for the package's PPD file
  1838. at 'location', if provided; otherwise, it will use information stored
  1839. in the PPM data file (see 'Files' section below) to locate the PPD file
  1840. for the requested package. The package's files will be installed under
  1841. the directory specified in 'root'; if not specified the default value
  1842. of 'root' will be used.
  1843. The function uses the values stored in the PPM data file to determine the
  1844. local operating system, operating system version and CPU type. If the PPD
  1845. for this package contains implementations for different platforms, these
  1846. values will be used to determine which one is installed.
  1847. InstallPackage() updates the PPM data file with information about the package
  1848. installation. It stores a copy of the PPD used for installation, as well
  1849. as the location from which this PPD was obtained. This location will
  1850. become the default PPD location for this package.
  1851. During an installation, the following actions are performed:
  1852. - the PPD file for the package is read
  1853. - a directory for this package is created in the directory specified in
  1854. <BUILDDIR> in the PPM data file.
  1855. - the file specified with the <CODEBASE> tag in the PPD file is
  1856. retrieved/copied into the directory created above.
  1857. - the package is unarchived in the directory created for this package
  1858. - individual files from the archive are installed in the appropriate
  1859. directories of the local Perl installation.
  1860. - perllocal.pod is updated with the install information.
  1861. - if provided, the <INSTALL> script from the PPD is executed in the
  1862. directory created above.
  1863. - information about the installation is stored in the PPM data file.
  1864. =item PPM::RemovePackage("package" => $package, "force" => $force)
  1865. Removes the specified package from the system. Reads the package's PPD
  1866. (stored during installation) for removal details. If 'force' is
  1867. specified, even a package required by PPM will be removed (useful
  1868. when installing an upgrade).
  1869. =item PPM::VerifyPackage("package" => $package, "location" => $location, "upgrade" => $upgrade)
  1870. Reads a PPD file for 'package', and compares the currently installed
  1871. version of 'package' to the version available according to the PPD.
  1872. The PPD file is expected to be on a local directory or remote site
  1873. specified either in the PPM data file or in the 'location' argument.
  1874. The 'location' argument may be a directory location or a URL.
  1875. The 'upgrade' argument forces an upgrade if the installed package is
  1876. not up-to-date.
  1877. The PPD file for each package will initially be searched for at
  1878. 'location', and if not found will then be searched for using the
  1879. locations specified in the PPM data file.
  1880. =item PPM::QueryInstalledPackages("searchRE" => $searchRE, "searchtag" => $searchtag, "ignorecase" => $ignorecase);
  1881. Returns a hash containing information about all installed packages.
  1882. By default, a list of all installed packages is returned. If a regular
  1883. expression 'searchRE' is specified, only packages matching it are
  1884. returned. If 'searchtag' is specified, the pattern match is applied
  1885. to the appropriate tag (e.g., ABSTRACT).
  1886. The data comes from the PPM data file, which contains installation
  1887. information about each installed package.
  1888. =item PPM::InstalledPackageProperties();
  1889. Returns a hash with package names as keys, and package properties as
  1890. attributes.
  1891. =item PPM::QueryPPD("location" => $location, "searchRE" => $searchRE, "searchtag" => $searchtag, "ignorecase" => $ignorecase, "package" => $package);
  1892. Searches for 'searchRE' (a regular expression) in the <ABSTRACT>,
  1893. <AUTHOR> or <TITLE> tags of the PPD file for 'package' at 'location'.
  1894. 'location' may be either a remote address or a directory path, and if
  1895. it is not provided, the default location as specified in the PPM data
  1896. file will be used. If the 'ignorecase' option is specified, it overrides
  1897. the current global case-sensitivity setting. On success, the matching
  1898. string is returned.
  1899. =item PPM::RepositoryPackages("location" => $location);
  1900. Returns a hash, with 'location' being the key, and arrays of all packages
  1901. with package description (PPD) files available at 'location' as its
  1902. elements. 'location' may be either a remote address or a directory path.
  1903. If 'location' is not specified, the default location as specified in
  1904. the PPM data file will be used.
  1905. =item PPM::ListOfRepositories();
  1906. Returns a hash containing the name of the repository and its location.
  1907. These repositories will be searched if an explicit location is not
  1908. provided in any function needing to locate a PPD.
  1909. =item PPM::RemoveRepository("repository" => $repository, "save" => $save);
  1910. Removes the repository named 'repository' from the list of available
  1911. repositories. If 'save' is not specified, the change is for the current
  1912. session only.
  1913. =item PPM::AddRepository("repository" => $repository, "location" => $location, "save" => $save);
  1914. Adds the repository named 'repository' to the list of available repositories.
  1915. If 'save' is not specified, the change is for the current session only.
  1916. =item PPM::RepositoryPackageProperties("package" => $package, "location" => $location);
  1917. Reads the PPD file for 'package', from 'location' or the default repository,
  1918. and returns a hash with keys being the various tags from the PPD (e.g.
  1919. 'ABSTRACT', 'AUTHOR', etc.).
  1920. =item PPM::RepositorySummary("location" => $location);
  1921. Attempts to retrieve the summary file associated with the specified repository,
  1922. or from all repositories if 'location' is not specified. The return value
  1923. is a hash with the key being the repository, and the data being another
  1924. hash of package name keys, and package detail data.
  1925. =item PPM::GetPPMOptions();
  1926. Returns a hash containing values for all PPM internal options ('IGNORECASE',
  1927. 'CLEAN', 'CONFIRM', 'ROOT', 'BUILDDIR').
  1928. =item PPM::SetPPMOptions("options" => %options, "save" => $save);
  1929. Sets internal PPM options as specified in the 'options' hash, which is
  1930. expected to be the hash previously returned by a call to GetPPMOptions().
  1931. =back
  1932. =head1 EXAMPLES
  1933. =over 4
  1934. =item PPM::AddRepository("repository" => 'ActiveState', "location" => "http://www.ActiveState.com/packages", "save" => 1);
  1935. Adds a repository to the list of available repositories, and saves it in
  1936. the PPM options file.
  1937. =item PPM::InstallPackage("package" => 'http://www.ActiveState.com/packages/foo.ppd');
  1938. Installs the software package 'foo' based on the information in the PPD
  1939. obtained from the specified URL.
  1940. =item PPM::VerifyPackage("package" => 'foo', "upgrade" => true)
  1941. Compares the currently installed version of the software package 'foo' to
  1942. the one available according to the PPD obtained from the package-specific
  1943. location provided in the PPM data file, and upgrades to a newer
  1944. version if available. If a location for this specific package is not
  1945. given in PPM data file, a default location is searched.
  1946. =item PPM::VerifyPackage("package" => 'foo', "location" => 'P:\PACKAGES', "upgrade" => true);
  1947. Compares the currently installed version of the software package 'foo'
  1948. to the one available according to the PPD obtained from the specified
  1949. directory, and upgrades to a newer version if available.
  1950. =item PPM::VerifyPackage("package" => 'PerlDB');
  1951. Verifies that package 'PerlDB' is up to date, using package locations specified
  1952. in the PPM data file.
  1953. =item PPM::RepositoryPackages("location" => http://www.ActiveState.com/packages);
  1954. Returns a hash keyed on 'location', with its elements being an array of
  1955. packages with PPD files available at the specified location.
  1956. =item PPM::QueryPPD("location" => 'P:\PACKAGES', "searchRE" => 'ActiveState', "searchtag" => 'author');
  1957. Searches the specified location for any package with an <AUTHOR> tag
  1958. containing the string 'ActiveState'. On a successful search, the matching
  1959. string is returned.
  1960. =item %opts = PPM::GetPPMOptions();
  1961. =item $opts{'CONFIRM'} = 'No';
  1962. =item PPM::SetPPMOptions("options" => \%opts, "save" => 1);
  1963. Sets and saves the value of the option 'CONFIRM' to 'No'.
  1964. =back
  1965. =head1 ENVIRONMENT VARIABLES
  1966. =over 4
  1967. =item HTTP_proxy
  1968. If the environment variable 'HTTP_proxy' is set, then it will
  1969. be used as the address of a proxy for accessing the Internet.
  1970. If the environment variables 'HTTP_proxy_user' and 'HTTP_proxy_pass'
  1971. are set, they will be used as the login and password for the
  1972. proxy server.
  1973. =back
  1974. =head1 FILES
  1975. =over 4
  1976. =item package.ppd
  1977. A description of a software package, in Perl Package Distribution (PPD)
  1978. format. More information on this file format can be found in L<XML::PPD>.
  1979. PPM stores a copy of the PPD it uses to install or upgrade any software
  1980. package.
  1981. =item ppm.xml - PPM data file.
  1982. The XML format file in which PPM stores configuration and package
  1983. installation information. This file is created when PPM is installed,
  1984. and under normal circumstances should never require modification other
  1985. than by PPM itself. For more information on this file, refer to
  1986. L<XML::PPMConfig>.
  1987. =back
  1988. =head1 AUTHOR
  1989. Murray Nesbitt, E<lt>F<[email protected]>E<gt>
  1990. =head1 SEE ALSO
  1991. L<XML::PPMConfig>
  1992. .
  1993. =cut