Package.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | [email protected] so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <[email protected]> |
  17. // | Martin Jansen <[email protected]> |
  18. // | Greg Beaver <[email protected]> |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Package.php,v 1.61.2.7 2005/02/17 17:47:55 cellog Exp $
  22. require_once 'PEAR/Common.php';
  23. require_once 'PEAR/Command/Common.php';
  24. class PEAR_Command_Package extends PEAR_Command_Common
  25. {
  26. // {{{ properties
  27. var $commands = array(
  28. 'package' => array(
  29. 'summary' => 'Build Package',
  30. 'function' => 'doPackage',
  31. 'shortcut' => 'p',
  32. 'options' => array(
  33. 'nocompress' => array(
  34. 'shortopt' => 'Z',
  35. 'doc' => 'Do not gzip the package file'
  36. ),
  37. 'showname' => array(
  38. 'shortopt' => 'n',
  39. 'doc' => 'Print the name of the packaged file.',
  40. ),
  41. ),
  42. 'doc' => '[descfile]
  43. Creates a PEAR package from its description file (usually called
  44. package.xml).
  45. '
  46. ),
  47. 'package-validate' => array(
  48. 'summary' => 'Validate Package Consistency',
  49. 'function' => 'doPackageValidate',
  50. 'shortcut' => 'pv',
  51. 'options' => array(),
  52. 'doc' => '
  53. ',
  54. ),
  55. 'cvsdiff' => array(
  56. 'summary' => 'Run a "cvs diff" for all files in a package',
  57. 'function' => 'doCvsDiff',
  58. 'shortcut' => 'cd',
  59. 'options' => array(
  60. 'quiet' => array(
  61. 'shortopt' => 'q',
  62. 'doc' => 'Be quiet',
  63. ),
  64. 'reallyquiet' => array(
  65. 'shortopt' => 'Q',
  66. 'doc' => 'Be really quiet',
  67. ),
  68. 'date' => array(
  69. 'shortopt' => 'D',
  70. 'doc' => 'Diff against revision of DATE',
  71. 'arg' => 'DATE',
  72. ),
  73. 'release' => array(
  74. 'shortopt' => 'R',
  75. 'doc' => 'Diff against tag for package release REL',
  76. 'arg' => 'REL',
  77. ),
  78. 'revision' => array(
  79. 'shortopt' => 'r',
  80. 'doc' => 'Diff against revision REV',
  81. 'arg' => 'REV',
  82. ),
  83. 'context' => array(
  84. 'shortopt' => 'c',
  85. 'doc' => 'Generate context diff',
  86. ),
  87. 'unified' => array(
  88. 'shortopt' => 'u',
  89. 'doc' => 'Generate unified diff',
  90. ),
  91. 'ignore-case' => array(
  92. 'shortopt' => 'i',
  93. 'doc' => 'Ignore case, consider upper- and lower-case letters equivalent',
  94. ),
  95. 'ignore-whitespace' => array(
  96. 'shortopt' => 'b',
  97. 'doc' => 'Ignore changes in amount of white space',
  98. ),
  99. 'ignore-blank-lines' => array(
  100. 'shortopt' => 'B',
  101. 'doc' => 'Ignore changes that insert or delete blank lines',
  102. ),
  103. 'brief' => array(
  104. 'doc' => 'Report only whether the files differ, no details',
  105. ),
  106. 'dry-run' => array(
  107. 'shortopt' => 'n',
  108. 'doc' => 'Don\'t do anything, just pretend',
  109. ),
  110. ),
  111. 'doc' => '<package.xml>
  112. Compares all the files in a package. Without any options, this
  113. command will compare the current code with the last checked-in code.
  114. Using the -r or -R option you may compare the current code with that
  115. of a specific release.
  116. ',
  117. ),
  118. 'cvstag' => array(
  119. 'summary' => 'Set CVS Release Tag',
  120. 'function' => 'doCvsTag',
  121. 'shortcut' => 'ct',
  122. 'options' => array(
  123. 'quiet' => array(
  124. 'shortopt' => 'q',
  125. 'doc' => 'Be quiet',
  126. ),
  127. 'reallyquiet' => array(
  128. 'shortopt' => 'Q',
  129. 'doc' => 'Be really quiet',
  130. ),
  131. 'slide' => array(
  132. 'shortopt' => 'F',
  133. 'doc' => 'Move (slide) tag if it exists',
  134. ),
  135. 'delete' => array(
  136. 'shortopt' => 'd',
  137. 'doc' => 'Remove tag',
  138. ),
  139. 'dry-run' => array(
  140. 'shortopt' => 'n',
  141. 'doc' => 'Don\'t do anything, just pretend',
  142. ),
  143. ),
  144. 'doc' => '<package.xml>
  145. Sets a CVS tag on all files in a package. Use this command after you have
  146. packaged a distribution tarball with the "package" command to tag what
  147. revisions of what files were in that release. If need to fix something
  148. after running cvstag once, but before the tarball is released to the public,
  149. use the "slide" option to move the release tag.
  150. ',
  151. ),
  152. 'run-tests' => array(
  153. 'summary' => 'Run Regression Tests',
  154. 'function' => 'doRunTests',
  155. 'shortcut' => 'rt',
  156. 'options' => array(
  157. 'recur' => array(
  158. 'shortopt' => 'r',
  159. 'doc' => 'Run tests in child directories, recursively. 4 dirs deep maximum',
  160. ),
  161. 'ini' => array(
  162. 'shortopt' => 'i',
  163. 'doc' => 'actual string of settings to pass to php in format " -d setting=blah"',
  164. 'arg' => 'SETTINGS'
  165. ),
  166. 'realtimelog' => array(
  167. 'shortopt' => 'l',
  168. 'doc' => 'Log test runs/results as they are run',
  169. ),
  170. ),
  171. 'doc' => '[testfile|dir ...]
  172. Run regression tests with PHP\'s regression testing script (run-tests.php).',
  173. ),
  174. 'package-dependencies' => array(
  175. 'summary' => 'Show package dependencies',
  176. 'function' => 'doPackageDependencies',
  177. 'shortcut' => 'pd',
  178. 'options' => array(),
  179. 'doc' => '
  180. List all depencies the package has.'
  181. ),
  182. 'sign' => array(
  183. 'summary' => 'Sign a package distribution file',
  184. 'function' => 'doSign',
  185. 'shortcut' => 'si',
  186. 'options' => array(),
  187. 'doc' => '<package-file>
  188. Signs a package distribution (.tar or .tgz) file with GnuPG.',
  189. ),
  190. 'makerpm' => array(
  191. 'summary' => 'Builds an RPM spec file from a PEAR package',
  192. 'function' => 'doMakeRPM',
  193. 'shortcut' => 'rpm',
  194. 'options' => array(
  195. 'spec-template' => array(
  196. 'shortopt' => 't',
  197. 'arg' => 'FILE',
  198. 'doc' => 'Use FILE as RPM spec file template'
  199. ),
  200. 'rpm-pkgname' => array(
  201. 'shortopt' => 'p',
  202. 'arg' => 'FORMAT',
  203. 'doc' => 'Use FORMAT as format string for RPM package name, %s is replaced
  204. by the PEAR package name, defaults to "PEAR::%s".',
  205. ),
  206. ),
  207. 'doc' => '<package-file>
  208. Creates an RPM .spec file for wrapping a PEAR package inside an RPM
  209. package. Intended to be used from the SPECS directory, with the PEAR
  210. package tarball in the SOURCES directory:
  211. $ pear makerpm ../SOURCES/Net_Socket-1.0.tgz
  212. Wrote RPM spec file PEAR::Net_Geo-1.0.spec
  213. $ rpm -bb PEAR::Net_Socket-1.0.spec
  214. ...
  215. Wrote: /usr/src/redhat/RPMS/i386/PEAR::Net_Socket-1.0-1.i386.rpm
  216. ',
  217. ),
  218. );
  219. var $output;
  220. // }}}
  221. // {{{ constructor
  222. /**
  223. * PEAR_Command_Package constructor.
  224. *
  225. * @access public
  226. */
  227. function PEAR_Command_Package(&$ui, &$config)
  228. {
  229. parent::PEAR_Command_Common($ui, $config);
  230. }
  231. // }}}
  232. // {{{ _displayValidationResults()
  233. function _displayValidationResults($err, $warn, $strict = false)
  234. {
  235. foreach ($err as $e) {
  236. $this->output .= "Error: $e\n";
  237. }
  238. foreach ($warn as $w) {
  239. $this->output .= "Warning: $w\n";
  240. }
  241. $this->output .= sprintf('Validation: %d error(s), %d warning(s)'."\n",
  242. sizeof($err), sizeof($warn));
  243. if ($strict && sizeof($err) > 0) {
  244. $this->output .= "Fix these errors and try again.";
  245. return false;
  246. }
  247. return true;
  248. }
  249. // }}}
  250. // {{{ doPackage()
  251. function doPackage($command, $options, $params)
  252. {
  253. $this->output = '';
  254. include_once 'PEAR/Packager.php';
  255. if (sizeof($params) < 1) {
  256. $params[0] = "package.xml";
  257. }
  258. $pkginfofile = isset($params[0]) ? $params[0] : 'package.xml';
  259. $packager =& new PEAR_Packager();
  260. $err = $warn = array();
  261. $dir = dirname($pkginfofile);
  262. $compress = empty($options['nocompress']) ? true : false;
  263. $result = $packager->package($pkginfofile, $compress);
  264. if (PEAR::isError($result)) {
  265. $this->ui->outputData($this->output, $command);
  266. return $this->raiseError($result);
  267. }
  268. // Don't want output, only the package file name just created
  269. if (isset($options['showname'])) {
  270. $this->output = $result;
  271. }
  272. if (PEAR::isError($result)) {
  273. $this->output .= "Package failed: ".$result->getMessage();
  274. }
  275. $this->ui->outputData($this->output, $command);
  276. return true;
  277. }
  278. // }}}
  279. // {{{ doPackageValidate()
  280. function doPackageValidate($command, $options, $params)
  281. {
  282. $this->output = '';
  283. if (sizeof($params) < 1) {
  284. $params[0] = "package.xml";
  285. }
  286. $obj = new PEAR_Common;
  287. $info = null;
  288. if ($fp = @fopen($params[0], "r")) {
  289. $test = fread($fp, 5);
  290. fclose($fp);
  291. if ($test == "<?xml") {
  292. $info = $obj->infoFromDescriptionFile($params[0]);
  293. }
  294. }
  295. if (empty($info)) {
  296. $info = $obj->infoFromTgzFile($params[0]);
  297. }
  298. if (PEAR::isError($info)) {
  299. return $this->raiseError($info);
  300. }
  301. $obj->validatePackageInfo($info, $err, $warn);
  302. $this->_displayValidationResults($err, $warn);
  303. $this->ui->outputData($this->output, $command);
  304. return true;
  305. }
  306. // }}}
  307. // {{{ doCvsTag()
  308. function doCvsTag($command, $options, $params)
  309. {
  310. $this->output = '';
  311. $_cmd = $command;
  312. if (sizeof($params) < 1) {
  313. $help = $this->getHelp($command);
  314. return $this->raiseError("$command: missing parameter: $help[0]");
  315. }
  316. $obj = new PEAR_Common;
  317. $info = $obj->infoFromDescriptionFile($params[0]);
  318. if (PEAR::isError($info)) {
  319. return $this->raiseError($info);
  320. }
  321. $err = $warn = array();
  322. $obj->validatePackageInfo($info, $err, $warn);
  323. if (!$this->_displayValidationResults($err, $warn, true)) {
  324. $this->ui->outputData($this->output, $command);
  325. break;
  326. }
  327. $version = $info['version'];
  328. $cvsversion = preg_replace('/[^a-z0-9]/i', '_', $version);
  329. $cvstag = "RELEASE_$cvsversion";
  330. $files = array_keys($info['filelist']);
  331. $command = "cvs";
  332. if (isset($options['quiet'])) {
  333. $command .= ' -q';
  334. }
  335. if (isset($options['reallyquiet'])) {
  336. $command .= ' -Q';
  337. }
  338. $command .= ' tag';
  339. if (isset($options['slide'])) {
  340. $command .= ' -F';
  341. }
  342. if (isset($options['delete'])) {
  343. $command .= ' -d';
  344. }
  345. $command .= ' ' . $cvstag . ' ' . escapeshellarg($params[0]);
  346. foreach ($files as $file) {
  347. $command .= ' ' . escapeshellarg($file);
  348. }
  349. if ($this->config->get('verbose') > 1) {
  350. $this->output .= "+ $command\n";
  351. }
  352. $this->output .= "+ $command\n";
  353. if (empty($options['dry-run'])) {
  354. $fp = popen($command, "r");
  355. while ($line = fgets($fp, 1024)) {
  356. $this->output .= rtrim($line)."\n";
  357. }
  358. pclose($fp);
  359. }
  360. $this->ui->outputData($this->output, $_cmd);
  361. return true;
  362. }
  363. // }}}
  364. // {{{ doCvsDiff()
  365. function doCvsDiff($command, $options, $params)
  366. {
  367. $this->output = '';
  368. if (sizeof($params) < 1) {
  369. $help = $this->getHelp($command);
  370. return $this->raiseError("$command: missing parameter: $help[0]");
  371. }
  372. $obj = new PEAR_Common;
  373. $info = $obj->infoFromDescriptionFile($params[0]);
  374. if (PEAR::isError($info)) {
  375. return $this->raiseError($info);
  376. }
  377. $files = array_keys($info['filelist']);
  378. $cmd = "cvs";
  379. if (isset($options['quiet'])) {
  380. $cmd .= ' -q';
  381. unset($options['quiet']);
  382. }
  383. if (isset($options['reallyquiet'])) {
  384. $cmd .= ' -Q';
  385. unset($options['reallyquiet']);
  386. }
  387. if (isset($options['release'])) {
  388. $cvsversion = preg_replace('/[^a-z0-9]/i', '_', $options['release']);
  389. $cvstag = "RELEASE_$cvsversion";
  390. $options['revision'] = $cvstag;
  391. unset($options['release']);
  392. }
  393. $execute = true;
  394. if (isset($options['dry-run'])) {
  395. $execute = false;
  396. unset($options['dry-run']);
  397. }
  398. $cmd .= ' diff';
  399. // the rest of the options are passed right on to "cvs diff"
  400. foreach ($options as $option => $optarg) {
  401. $arg = @$this->commands[$command]['options'][$option]['arg'];
  402. $short = @$this->commands[$command]['options'][$option]['shortopt'];
  403. $cmd .= $short ? " -$short" : " --$option";
  404. if ($arg && $optarg) {
  405. $cmd .= ($short ? '' : '=') . escapeshellarg($optarg);
  406. }
  407. }
  408. foreach ($files as $file) {
  409. $cmd .= ' ' . escapeshellarg($file);
  410. }
  411. if ($this->config->get('verbose') > 1) {
  412. $this->output .= "+ $cmd\n";
  413. }
  414. if ($execute) {
  415. $fp = popen($cmd, "r");
  416. while ($line = fgets($fp, 1024)) {
  417. $this->output .= rtrim($line)."\n";
  418. }
  419. pclose($fp);
  420. }
  421. $this->ui->outputData($this->output, $command);
  422. return true;
  423. }
  424. // }}}
  425. // {{{ doRunTests()
  426. function doRunTests($command, $options, $params)
  427. {
  428. include_once 'PEAR/RunTest.php';
  429. $log = new PEAR_Common;
  430. $log->ui = &$this->ui; // slightly hacky, but it will work
  431. $run = new PEAR_RunTest($log);
  432. $tests = array();
  433. if (isset($options['recur'])) {
  434. $depth = 4;
  435. } else {
  436. $depth = 1;
  437. }
  438. if (!count($params)) {
  439. $params[] = '.';
  440. }
  441. foreach ($params as $p) {
  442. if (is_dir($p)) {
  443. $dir = System::find(array($p, '-type', 'f',
  444. '-maxdepth', $depth,
  445. '-name', '*.phpt'));
  446. $tests = array_merge($tests, $dir);
  447. } else {
  448. if (!@file_exists($p)) {
  449. if (!preg_match('/\.phpt$/', $p)) {
  450. $p .= '.phpt';
  451. }
  452. $dir = System::find(array(dirname($p), '-type', 'f',
  453. '-maxdepth', $depth,
  454. '-name', $p));
  455. $tests = array_merge($tests, $dir);
  456. } else {
  457. $tests[] = $p;
  458. }
  459. }
  460. }
  461. $ini_settings = '';
  462. if (isset($options['ini'])) {
  463. $ini_settings .= $options['ini'];
  464. }
  465. if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) {
  466. $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}";
  467. }
  468. if ($ini_settings) {
  469. $this->ui->outputData('Using INI settings: "' . $ini_settings . '"');
  470. }
  471. $skipped = $passed = $failed = array();
  472. $this->ui->outputData('Running ' . count($tests) . ' tests', $command);
  473. $start = time();
  474. if (isset($options['realtimelog'])) {
  475. @unlink('run-tests.log');
  476. }
  477. foreach ($tests as $t) {
  478. if (isset($options['realtimelog'])) {
  479. $fp = @fopen('run-tests.log', 'a');
  480. if ($fp) {
  481. fwrite($fp, "Running test $t...");
  482. fclose($fp);
  483. }
  484. }
  485. $result = $run->run($t, $ini_settings);
  486. if (OS_WINDOWS) {
  487. for($i=0;$i<2000;$i++) {
  488. $i = $i; // delay - race conditions on windows
  489. }
  490. }
  491. if (isset($options['realtimelog'])) {
  492. $fp = @fopen('run-tests.log', 'a');
  493. if ($fp) {
  494. fwrite($fp, "$result\n");
  495. fclose($fp);
  496. }
  497. }
  498. if ($result == 'FAILED') {
  499. $failed[] = $t;
  500. }
  501. if ($result == 'PASSED') {
  502. $passed[] = $t;
  503. }
  504. if ($result == 'SKIPPED') {
  505. $skipped[] = $t;
  506. }
  507. }
  508. $total = date('i:s', time() - $start);
  509. if (count($failed)) {
  510. $output = "TOTAL TIME: $total\n";
  511. $output .= count($passed) . " PASSED TESTS\n";
  512. $output .= count($skipped) . " SKIPPED TESTS\n";
  513. $output .= count($failed) . " FAILED TESTS:\n";
  514. foreach ($failed as $failure) {
  515. $output .= $failure . "\n";
  516. }
  517. if (isset($options['realtimelog'])) {
  518. $fp = @fopen('run-tests.log', 'a');
  519. } else {
  520. $fp = @fopen('run-tests.log', 'w');
  521. }
  522. if ($fp) {
  523. fwrite($fp, $output, strlen($output));
  524. fclose($fp);
  525. $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command);
  526. }
  527. } elseif (@file_exists('run-tests.log') && !@is_dir('run-tests.log')) {
  528. @unlink('run-tests.log');
  529. }
  530. $this->ui->outputData('TOTAL TIME: ' . $total);
  531. $this->ui->outputData(count($passed) . ' PASSED TESTS', $command);
  532. $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command);
  533. if (count($failed)) {
  534. $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command);
  535. foreach ($failed as $failure) {
  536. $this->ui->outputData($failure, $command);
  537. }
  538. }
  539. return true;
  540. }
  541. // }}}
  542. // {{{ doPackageDependencies()
  543. function doPackageDependencies($command, $options, $params)
  544. {
  545. // $params[0] -> the PEAR package to list its information
  546. if (sizeof($params) != 1) {
  547. return $this->raiseError("bad parameter(s), try \"help $command\"");
  548. }
  549. $obj = new PEAR_Common();
  550. if (PEAR::isError($info = $obj->infoFromAny($params[0]))) {
  551. return $this->raiseError($info);
  552. }
  553. if (is_array($info['release_deps'])) {
  554. $data = array(
  555. 'caption' => 'Dependencies for ' . $info['package'],
  556. 'border' => true,
  557. 'headline' => array("Type", "Name", "Relation", "Version"),
  558. );
  559. foreach ($info['release_deps'] as $d) {
  560. if (isset($this->_deps_rel_trans[$d['rel']])) {
  561. $rel = $this->_deps_rel_trans[$d['rel']];
  562. } else {
  563. $rel = $d['rel'];
  564. }
  565. if (isset($this->_deps_type_trans[$d['type']])) {
  566. $type = ucfirst($this->_deps_type_trans[$d['type']]);
  567. } else {
  568. $type = $d['type'];
  569. }
  570. if (isset($d['name'])) {
  571. $name = $d['name'];
  572. } else {
  573. $name = '';
  574. }
  575. if (isset($d['version'])) {
  576. $version = $d['version'];
  577. } else {
  578. $version = '';
  579. }
  580. $data['data'][] = array($type, $name, $rel, $version);
  581. }
  582. $this->ui->outputData($data, $command);
  583. return true;
  584. }
  585. // Fallback
  586. $this->ui->outputData("This package does not have any dependencies.", $command);
  587. }
  588. // }}}
  589. // {{{ doSign()
  590. function doSign($command, $options, $params)
  591. {
  592. // should move most of this code into PEAR_Packager
  593. // so it'll be easy to implement "pear package --sign"
  594. if (sizeof($params) != 1) {
  595. return $this->raiseError("bad parameter(s), try \"help $command\"");
  596. }
  597. if (!file_exists($params[0])) {
  598. return $this->raiseError("file does not exist: $params[0]");
  599. }
  600. $obj = new PEAR_Common;
  601. $info = $obj->infoFromTgzFile($params[0]);
  602. if (PEAR::isError($info)) {
  603. return $this->raiseError($info);
  604. }
  605. include_once "Archive/Tar.php";
  606. include_once "System.php";
  607. $tar = new Archive_Tar($params[0]);
  608. $tmpdir = System::mktemp('-d pearsign');
  609. if (!$tar->extractList('package.xml package.sig', $tmpdir)) {
  610. return $this->raiseError("failed to extract tar file");
  611. }
  612. if (file_exists("$tmpdir/package.sig")) {
  613. return $this->raiseError("package already signed");
  614. }
  615. @unlink("$tmpdir/package.sig");
  616. $input = $this->ui->userDialog($command,
  617. array('GnuPG Passphrase'),
  618. array('password'));
  619. $gpg = popen("gpg --batch --passphrase-fd 0 --armor --detach-sign --output $tmpdir/package.sig $tmpdir/package.xml 2>/dev/null", "w");
  620. if (!$gpg) {
  621. return $this->raiseError("gpg command failed");
  622. }
  623. fwrite($gpg, "$input[0]\r");
  624. if (pclose($gpg) || !file_exists("$tmpdir/package.sig")) {
  625. return $this->raiseError("gpg sign failed");
  626. }
  627. $tar->addModify("$tmpdir/package.sig", '', $tmpdir);
  628. return true;
  629. }
  630. // }}}
  631. // {{{ doMakeRPM()
  632. /*
  633. (cox)
  634. TODO:
  635. - Fill the rpm dependencies in the template file.
  636. IDEAS:
  637. - Instead of mapping the role to rpm vars, perhaps it's better
  638. to use directly the pear cmd to install the files by itself
  639. in %postrun so:
  640. pear -d php_dir=%{_libdir}/php/pear -d test_dir=.. <package>
  641. */
  642. function doMakeRPM($command, $options, $params)
  643. {
  644. if (sizeof($params) != 1) {
  645. return $this->raiseError("bad parameter(s), try \"help $command\"");
  646. }
  647. if (!file_exists($params[0])) {
  648. return $this->raiseError("file does not exist: $params[0]");
  649. }
  650. include_once "Archive/Tar.php";
  651. include_once "PEAR/Installer.php";
  652. include_once "System.php";
  653. $tar = new Archive_Tar($params[0]);
  654. $tmpdir = System::mktemp('-d pear2rpm');
  655. $instroot = System::mktemp('-d pear2rpm');
  656. $tmp = $this->config->get('verbose');
  657. $this->config->set('verbose', 0);
  658. $installer = new PEAR_Installer($this->ui);
  659. $info = $installer->install($params[0],
  660. array('installroot' => $instroot,
  661. 'nodeps' => true));
  662. $pkgdir = "$info[package]-$info[version]";
  663. $info['rpm_xml_dir'] = '/var/lib/pear';
  664. $this->config->set('verbose', $tmp);
  665. if (!$tar->extractList("package.xml", $tmpdir, $pkgdir)) {
  666. return $this->raiseError("failed to extract $params[0]");
  667. }
  668. if (!file_exists("$tmpdir/package.xml")) {
  669. return $this->raiseError("no package.xml found in $params[0]");
  670. }
  671. if (isset($options['spec-template'])) {
  672. $spec_template = $options['spec-template'];
  673. } else {
  674. $spec_template = $this->config->get('data_dir') .
  675. '/PEAR/template.spec';
  676. }
  677. if (isset($options['rpm-pkgname'])) {
  678. $rpm_pkgname_format = $options['rpm-pkgname'];
  679. } else {
  680. $rpm_pkgname_format = "PEAR::%s";
  681. }
  682. $info['extra_headers'] = '';
  683. $info['doc_files'] = '';
  684. $info['files'] = '';
  685. $info['rpm_package'] = sprintf($rpm_pkgname_format, $info['package']);
  686. $srcfiles = 0;
  687. foreach ($info['filelist'] as $name => $attr) {
  688. if (!isset($attr['role'])) {
  689. continue;
  690. }
  691. $name = preg_replace('![/:\\\\]!', '/', $name);
  692. if ($attr['role'] == 'doc') {
  693. $info['doc_files'] .= " $name";
  694. // Map role to the rpm vars
  695. } else {
  696. $c_prefix = '%{_libdir}/php/pear';
  697. switch ($attr['role']) {
  698. case 'php':
  699. $prefix = $c_prefix; break;
  700. case 'ext':
  701. $prefix = '%{_libdir}/php'; break; // XXX good place?
  702. case 'src':
  703. $srcfiles++;
  704. $prefix = '%{_includedir}/php'; break; // XXX good place?
  705. case 'test':
  706. $prefix = "$c_prefix/tests/" . $info['package']; break;
  707. case 'data':
  708. $prefix = "$c_prefix/data/" . $info['package']; break;
  709. case 'script':
  710. $prefix = '%{_bindir}'; break;
  711. }
  712. $name = str_replace('\\', '/', $name);
  713. $info['files'] .= "$prefix/$name\n";
  714. }
  715. }
  716. if ($srcfiles > 0) {
  717. include_once "OS/Guess.php";
  718. $os = new OS_Guess;
  719. $arch = $os->getCpu();
  720. } else {
  721. $arch = 'noarch';
  722. }
  723. $cfg = array('master_server', 'php_dir', 'ext_dir', 'doc_dir',
  724. 'bin_dir', 'data_dir', 'test_dir');
  725. foreach ($cfg as $k) {
  726. $info[$k] = $this->config->get($k);
  727. }
  728. $info['arch'] = $arch;
  729. $fp = @fopen($spec_template, "r");
  730. if (!$fp) {
  731. return $this->raiseError("could not open RPM spec file template $spec_template: $php_errormsg");
  732. }
  733. $spec_contents = preg_replace('/@([a-z0-9_-]+)@/e', '$info["\1"]', fread($fp, filesize($spec_template)));
  734. fclose($fp);
  735. $spec_file = "$info[rpm_package]-$info[version].spec";
  736. $wp = fopen($spec_file, "wb");
  737. if (!$wp) {
  738. return $this->raiseError("could not write RPM spec file $spec_file: $php_errormsg");
  739. }
  740. fwrite($wp, $spec_contents);
  741. fclose($wp);
  742. $this->ui->outputData("Wrote RPM spec file $spec_file", $command);
  743. return true;
  744. }
  745. // }}}
  746. }
  747. ?>