Example #1
0
 /**
  * Package up both a package.xml and package2.xml for the same release
  * @param PEAR_Packager
  * @param PEAR_PackageFile_v1
  * @param bool generate a .tgz or a .tar
  * @param string|null temporary directory to package in
  */
 function toTgz2(&$packager, &$pf1, $compress = true, $where = null)
 {
     require_once 'Archive/Tar.php';
     if (!$this->_packagefile->isEquivalent($pf1)) {
         return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: "' . basename($pf1->getPackageFile()) . '" is not equivalent to "' . basename($this->_packagefile->getPackageFile()) . '"');
     }
     if ($where === null) {
         if (!($where = System::mktemp(array('-d')))) {
             return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: mktemp failed');
         }
     } elseif (!@System::mkDir(array('-p', $where))) {
         return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: "' . $where . '" could' . ' not be created');
     }
     if (file_exists($where . DIRECTORY_SEPARATOR . 'package.xml') && !is_file($where . DIRECTORY_SEPARATOR . 'package.xml')) {
         return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: unable to save package.xml as' . ' "' . $where . DIRECTORY_SEPARATOR . 'package.xml"');
     }
     if (!$this->_packagefile->validate(PEAR_VALIDATE_PACKAGING)) {
         return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: invalid package.xml');
     }
     $ext = $compress ? '.tgz' : '.tar';
     $pkgver = $this->_packagefile->getPackage() . '-' . $this->_packagefile->getVersion();
     $dest_package = getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext;
     if (file_exists(getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext) && !is_file(getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext)) {
         return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: cannot create tgz file "' . getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext . '"');
     }
     if ($pkgfile = $this->_packagefile->getPackageFile()) {
         $pkgdir = dirname(realpath($pkgfile));
         $pkgfile = basename($pkgfile);
     } else {
         return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: package file object must ' . 'be created from a real file');
     }
     // {{{ Create the package file list
     $filelist = array();
     $i = 0;
     $this->_packagefile->flattenFilelist();
     $contents = $this->_packagefile->getContents();
     if (isset($contents['bundledpackage'])) {
         // bundles of packages
         $contents = $contents['bundledpackage'];
         if (!isset($contents[0])) {
             $contents = array($contents);
         }
         $packageDir = $where;
         foreach ($contents as $i => $package) {
             $fname = $package;
             $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
             if (!file_exists($file)) {
                 return $packager->raiseError("File does not exist: {$fname}");
             }
             $tfile = $packageDir . DIRECTORY_SEPARATOR . $fname;
             System::mkdir(array('-p', dirname($tfile)));
             copy($file, $tfile);
             $filelist[$i++] = $tfile;
             $packager->log(2, "Adding package {$fname}");
         }
     } else {
         // normal packages
         $contents = $contents['dir']['file'];
         if (!isset($contents[0])) {
             $contents = array($contents);
         }
         $packageDir = $where;
         foreach ($contents as $i => $file) {
             $fname = $file['attribs']['name'];
             $atts = $file['attribs'];
             $orig = $file;
             $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
             if (!file_exists($file)) {
                 return $packager->raiseError("File does not exist: {$fname}");
             } else {
                 $tfile = $packageDir . DIRECTORY_SEPARATOR . $fname;
                 unset($orig['attribs']);
                 if (count($orig)) {
                     // file with tasks
                     // run any package-time tasks
                     if (function_exists('file_get_contents')) {
                         $contents = file_get_contents($file);
                     } else {
                         $fp = fopen($file, "r");
                         $contents = @fread($fp, filesize($file));
                         fclose($fp);
                     }
                     foreach ($orig as $tag => $raw) {
                         $tag = str_replace($this->_packagefile->getTasksNs() . ':', '', $tag);
                         $task = "PEAR_Task_{$tag}";
                         $task =& new $task($this->_packagefile->_config, $this->_packagefile->_logger, PEAR_TASK_PACKAGE);
                         $task->init($raw, $atts, null);
                         $res = $task->startSession($this->_packagefile, $contents, $tfile);
                         if (!$res) {
                             continue;
                             // skip this task
                         }
                         if (PEAR::isError($res)) {
                             return $res;
                         }
                         $contents = $res;
                         // save changes
                         System::mkdir(array('-p', dirname($tfile)));
                         $wp = fopen($tfile, "wb");
                         fwrite($wp, $contents);
                         fclose($wp);
                     }
                 }
                 if (!file_exists($tfile)) {
                     System::mkdir(array('-p', dirname($tfile)));
                     copy($file, $tfile);
                 }
                 $filelist[$i++] = $tfile;
                 $this->_packagefile->setFileAttribute($fname, 'md5sum', md5_file($tfile), $i - 1);
                 $packager->log(2, "Adding file {$fname}");
             }
         }
     }
     // }}}
     if ($pf1 !== null) {
         $name = 'package2.xml';
     } else {
         $name = 'package.xml';
     }
     $packagexml = $this->toPackageFile($where, PEAR_VALIDATE_PACKAGING, $name);
     if ($packagexml) {
         $tar =& new Archive_Tar($dest_package, $compress);
         $tar->setErrorHandling(PEAR_ERROR_RETURN);
         // XXX Don't print errors
         // ----- Creates with the package.xml file
         $ok = $tar->createModify(array($packagexml), '', $where);
         if (PEAR::isError($ok)) {
             return $packager->raiseError($ok);
         } elseif (!$ok) {
             return $packager->raiseError('PEAR_Packagefile_v2::toTgz(): adding ' . $name . ' failed');
         }
         // ----- Add the content of the package
         if (!$tar->addModify($filelist, $pkgver, $where)) {
             return $packager->raiseError('PEAR_Packagefile_v2::toTgz(): tarball creation failed');
         }
         // add the package.xml version 1.0
         if ($pf1 !== null) {
             $pfgen =& $pf1->getDefaultGenerator();
             $packagexml1 = $pfgen->toPackageFile($where, PEAR_VALIDATE_PACKAGING, 'package.xml', true);
             if (!$tar->addModify(array($packagexml1), '', $where)) {
                 return $packager->raiseError('PEAR_Packagefile_v2::toTgz(): adding package.xml failed');
             }
         }
         return $dest_package;
     }
 }