home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / scripts / pear.in < prev    next >
Text File  |  2001-11-13  |  10KB  |  359 lines

  1. #!@prefix@/bin/php -Cq
  2. <?php // -*- PHP -*-
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4.0                                                      |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2001 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.02 of the PHP license,      |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  18. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  19. // +----------------------------------------------------------------------+
  20. //
  21.  
  22.  
  23. /*
  24.  
  25. command structure:
  26.  
  27. * remote queries:
  28. ** list available packages
  29. ** list not-installed packages
  30. ** list upgrades
  31.  
  32. ** list packages by author
  33. ** list packages by category
  34. ** list packages by state
  35.  
  36. * operations on package files:
  37.  
  38. * installation commands:
  39.  
  40. * package generation commands:
  41.  
  42.  
  43.  
  44.  */
  45. require_once 'PEAR.php';
  46. require_once 'PEAR/Common.php';
  47. require_once 'PEAR/Config.php';
  48. require_once 'PEAR/Remote.php';
  49. require_once 'PEAR/Registry.php';
  50. require_once 'Console/Getopt.php';
  51.  
  52. error_reporting(E_ALL);
  53.  
  54. PEAR::setErrorHandling(PEAR_ERROR_PRINT, "pear: %s\n");
  55.  
  56. // {{{ config file and option parsing
  57.  
  58. $options = Console_Getopt::getopt($argv, "c:C:d:D:h?sSqu:v");
  59. if (PEAR::isError($options)) {
  60.     usage($options);
  61. }
  62.  
  63. if (OS_WINDOWS) {
  64.     $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pearsys.ini';
  65.     $pear_user_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.ini';
  66. } else {
  67.     $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.conf';
  68.     $pear_user_config = $HTTP_ENV_VARS['HOME'].DIRECTORY_SEPARATOR.'.pearrc';
  69. }
  70.  
  71. $opts = $options[0];
  72.  
  73. foreach ($opts as $opt) {
  74.     switch ($opt[0]) {
  75.         case 'c':
  76.             $pear_user_config = $opt[1];
  77.             break;
  78.         case 'C':
  79.             $pear_default_config = $opt[1];
  80.             break;
  81.     }
  82. }
  83.  
  84. $config = new PEAR_Config($pear_user_config, $pear_default_config);
  85. $store_user_config = false;
  86. $store_default_config = false;
  87. $verbose = 0;
  88.  
  89. foreach ($opts as $opt) {
  90.     $param = $opt[1];
  91.     switch ($opt[0]) {
  92.         case 'd':
  93.             list($key, $value) = explode('=', $param);
  94.             $config->set($key, $value);
  95.             break;
  96.         case 'D':
  97.             list($key, $value) = explode('=', $param);
  98.             $config->set($key, $value, true);
  99.             break;
  100.         case 's':
  101.             $store_user_config = true;
  102.             break;
  103.         case 'S':
  104.             $store_default_config = true;
  105.             break;
  106.         case 'u':
  107.             $config->toDefault($param);
  108.             break;
  109.         case 'v':
  110.             $verbose++;
  111.             break;
  112.         case 'q':
  113.             $verbose--;
  114.             break;
  115.     }
  116. }
  117.  
  118. if ($store_default_config) {
  119.     if (@is_writeable($pear_default_config)) {
  120.         $config->writeConfigFile($pear_default_config, 'default');
  121.     } else {
  122.         die("You don't have write access to $pear_default_config, exiting!\n");
  123.     }
  124. }
  125.  
  126. if ($store_user_config) {
  127.     $config->writeConfigFile($pear_user_config, 'userdefined');
  128. }
  129.  
  130. $fallback_config = array(
  131.     'master_server' => 'pear.php.net',
  132.     'php_dir'       => PEAR_INSTALL_DIR,
  133.     'ext_dir'       => PEAR_EXTENSION_DIR,
  134.     'doc_dir'       => '',
  135.     'verbose'       => true,
  136. );
  137. $fallback_done = array();
  138.  
  139. foreach ($fallback_config as $key => $value) {
  140.     if (!$config->isDefined($key)) {
  141.         $config->set($key, $value);
  142.         $fallback_done[$key] = true;
  143.     }
  144. }
  145.  
  146. //$verbose    = $config->get("verbose");
  147. $script_dir = $config->get("php_dir");
  148. $ext_dir    = $config->get("ext_dir");
  149. $doc_dir    = $config->get("doc_dir");
  150.  
  151. $command = (isset($options[1][1])) ? $options[1][1] : null;
  152. $rest = array_slice($options[1], 2);
  153.  
  154. $command_options = array(
  155.     "list" => "v",
  156. );
  157.  
  158. if (isset($command_options[$command])) {
  159.     $tmp = Console_Getopt::getopt($rest, $command_options[$command]);
  160.     if (PEAR::isError($tmp)) {
  161.         usage($tmp);
  162.     }
  163.     $cmdopt = $tmp[0];
  164.     $cmdargs = $tmp[1];
  165. } else {
  166.     $cmdopt = array();
  167.     $cmdargs = $rest;
  168. }
  169.  
  170.  
  171. // }}}
  172.  
  173. switch ($command) {
  174.     // {{{ install
  175.  
  176.     case 'install': {
  177.         include_once 'PEAR/Installer.php';
  178.         $pkgfile = $options[1][2];
  179.         $installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
  180.         $installer->setErrorHandling(PEAR_ERROR_DIE,
  181.                                      basename($pkgfile) . ": %s\n");
  182.         $installer->debug = $verbose;
  183.         $installer->install($pkgfile);
  184.         print "install ok\n";
  185.         break;
  186.     }
  187.  
  188.     // }}}
  189.     // {{{ package
  190.  
  191.     case 'package': {
  192.         include_once 'PEAR/Packager.php';
  193.         $pkginfofile = isset($options[1][2]) ? $options[1][2] : null;
  194.         $packager =& new PEAR_Packager($script_dir, $ext_dir, $doc_dir);
  195.         $packager->setErrorHandling(PEAR_ERROR_DIE, "pear page: %s\n");
  196.         $packager->debug = $verbose;
  197.         if (PEAR::isError($packager->Package($pkginfofile))) {
  198.             print "\npackage failed\n";
  199.         } else {
  200.             print "package ok\n";
  201.         }
  202.         break;
  203.     }
  204.  
  205.     // }}}
  206.     // {{{ info
  207.  
  208.     case 'info': {
  209.         $parser = new PEAR_Common;
  210.         $parser->setErrorHandling(PEAR_ERROR_DIE, "pear info: %s\n");
  211.         $info = $parser->infoFromTgzFile($options[1][2]);
  212.         unset($info['filelist']);
  213.         present_array($info);
  214.         break;
  215.     }
  216.  
  217.     // }}}
  218.     // {{{ list-installed
  219.  
  220.     case 'list-installed': {
  221.         $reg = new PEAR_Registry;
  222.         $installed = $reg->packageInfo();
  223.         $i = $j = 0;
  224.         print "Installed packages:\n===================\n";
  225.         foreach ($installed as $package) {
  226.             if ($i++ % 20 == 0) {
  227.                 if ($j++ > 0) {
  228.                     print "\n";
  229.                 }
  230.                 printf("%-20s %-10s %s\n",
  231.                        "Package", "Version", "State");
  232.                 print str_repeat("-", 75)."\n";
  233.             }
  234.             printf("%-20s %-10s %s\n", $package['package'],
  235.                    $package['version'], $package['release_state']);
  236.         }
  237.         break;
  238.     }
  239.  
  240.     // }}}
  241.     // {{{ list-available
  242.  
  243.     case 'list-available': {
  244.         $remote = new PEAR_Remote($config);
  245.         $result = $remote->call('package.listAll');
  246.         $i = $j = 0;
  247.         print "Available packages:\n===================\n";
  248.         foreach ($result as $package) {
  249.             if ($i++ % 20 == 0) {
  250.                 if ($j++ > 0) {
  251.                     print "\n";
  252.                 }
  253.                 printf("%-20s %-10s %-15s %s\n",
  254.                        "Package", "Stable", "Lead", "Category");
  255.                 print str_repeat("-", 75)."\n";
  256.             }
  257.             $stable = $package['stable'];
  258.             printf("%-20s %-10s %-15s %s\n", $package['name'],
  259.                    $stable ? $stable : "???",
  260.                    $package['lead'], $package['category']);
  261.         }
  262.         break;
  263.     }
  264.  
  265.     // }}}
  266.     // {{{ show-config
  267.  
  268.     case 'show-config': {
  269.         $keys = $config->getKeys();
  270.         foreach ($keys as $key) {
  271.             $value = $config->get($key);
  272.             $xi = "";
  273.             if ($config->isDefaulted($key)) {
  274.                 $xi .= " (default)";
  275.             }
  276.             if ($fallback_done[$key]) {
  277.                 $xi .= " (fallback)";
  278.             }
  279.             printf("%s = %s%s\n", $key, $value, $xi);
  280.         }
  281.         break;
  282.     }
  283.  
  284.     // }}}
  285.     default: {
  286.         if (!$store_default_config && !$store_user_config) {
  287.             usage();
  288.         }
  289.         break;
  290.     }
  291. }
  292.  
  293. // {{{ usage()
  294.  
  295. function usage($error = null)
  296. {
  297.     $stderr = fopen('php://stderr', 'w');
  298.     if (PEAR::isError($error)) {
  299.         fputs($stderr, $error->getMessage());
  300.     } elseif ($error !== null) {
  301.         fputs($stderr, $error);
  302.     }
  303.     fputs($stderr,
  304.           "Usage: pear [options] command <parameters>\n".
  305.           "Options:\n".
  306.           "     -v         increase verbosity level (default 1)\n".
  307.           "     -q         be quiet, decrease verbosity level\n".
  308.           "     -c file    find user configuration in `file'\n".
  309.           "     -C file    find system configuration in `file'\n".
  310.           "     -d foo=bar set user config variable `foo' to `bar'\n".
  311.           "     -D foo=bar set system config variable `foo' to `bar'\n".
  312.           "     -s         store user configuration\n".
  313.           "     -s         store system configuration\n".
  314.           "     -u foo     unset `foo' in the user configuration\n".
  315.           "     -h, -?     display help/usage (this message)\n".
  316.           "Commands:\n".
  317.           "   install <package file>\n".
  318.           "   package [package info file]\n".
  319.           "   list-installed\n".
  320.           "   list-available\n".
  321.           "   info\n".
  322.           "   show-config\n".
  323.           "\n");
  324.     fclose($stderr);
  325.     exit;
  326. }
  327.  
  328. // }}}
  329. // {{{ present_array()
  330.  
  331. function present_array(&$arr, $keys = null)
  332. {
  333.     if ($keys === null) {
  334.         $keys = array_keys($arr);
  335.     }
  336.     $longest_key = max(array_map("strlen", array_keys($arr)));
  337.     $format_string = "%{$longest_key}s : %s\n";
  338.     foreach ($keys as $k) {
  339.         if (is_array($arr[$k])) {
  340.             $v = implode(", ", $arr[$k]);
  341.         } else {
  342.             $v = $arr[$k];
  343.         }
  344.         printf($format_string, $k, $v);
  345.     }
  346. }
  347.  
  348. // }}}
  349.  
  350.  
  351. /*
  352.  * Local variables:
  353.  * tab-width: 4
  354.  * c-basic-offset: 4
  355.  * indent-tabs-mode: nil
  356.  * End:
  357.  */
  358. ?>
  359.