1
0
Fork 0

Merge pull request #775 from palex-fpt/PR-746

PEAR Packages extraction based on package.xml format
pull/808/merge
Jordi Boggiano 2012-06-22 06:15:38 -07:00
commit ccc6fa3714
9 changed files with 425 additions and 7 deletions

View File

@ -12,25 +12,41 @@
namespace Composer\Downloader; namespace Composer\Downloader;
use Composer\Package\PackageInterface;
/** /**
* Downloader for pear packages * Downloader for pear packages
* *
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
* @author Kirill chEbba Chebunin <iam@chebba.org> * @author Kirill chEbba Chebunin <iam@chebba.org>
*/ */
class PearDownloader extends TarDownloader class PearDownloader extends FileDownloader
{ {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function extract($file, $path) public function download(PackageInterface $package, $path)
{ {
parent::extract($file, $path); parent::download($package, $path);
if (file_exists($path . '/package.sig')) {
unlink($path . '/package.sig'); $fileName = $this->getFileName($package, $path);
if ($this->io->isVerbose()) {
$this->io->write(' Installing PEAR package');
} }
if (file_exists($path . '/package.xml')) { try {
unlink($path . '/package.xml'); $pearExtractor = new PearPackageExtractor($fileName);
$pearExtractor->extractTo($path);
if ($this->io->isVerbose()) {
$this->io->write(' Cleaning up');
}
unlink($fileName);
} catch (\Exception $e) {
// clean up
$this->filesystem->removeDirectory($path);
throw $e;
} }
$this->io->write('');
} }
} }

View File

@ -0,0 +1,192 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Downloader;
use Composer\Util\Filesystem;
/**
* Extractor for pear packages.
*
* Composer cannot rely on tar files structure when place it inside package target dir. Correct source files
* disposition must be read from package.xml
* This extract pear package source files to target dir.
*
* @author Alexey Prilipko <palex@farpost.com>
*/
class PearPackageExtractor
{
/** @var Filesystem */
private $filesystem;
private $file;
public function __construct($file)
{
if (!is_file($file)) {
throw new \UnexpectedValueException('PEAR package file is not found at '.$file);
}
$this->file = $file;
}
/**
* Installs PEAR source files according to package.xml definitions and removes extracted files
*
* @param $file string path to downloaded PEAR archive file
* @param $target string target install location. all source installation would be performed relative to target path.
* @param $role string type of files to install. default role for PEAR source files are 'php'.
*
* @throws \RuntimeException
*/
public function extractTo($target, $role = 'php')
{
$this->filesystem = new Filesystem();
$extractionPath = $target.'/tarball';
try {
$archive = new \PharData($this->file);
$archive->extractTo($extractionPath, null, true);
if (!is_file($this->combine($extractionPath, '/package.xml'))) {
throw new \RuntimeException('Invalid PEAR package. It must contain package.xml file.');
}
$fileCopyActions = $this->buildCopyActions($extractionPath, $role);
$this->copyFiles($fileCopyActions, $extractionPath, $target);
$this->filesystem->removeDirectory($extractionPath);
} catch (\Exception $exception) {
throw new \UnexpectedValueException(sprintf('Failed to extract PEAR package %s to %s. Reason: %s', $this->file, $target, $exception->getMessage()), 0, $exception);
}
}
/**
* Perform copy actions on files
*
* @param $files array array('from', 'to') with relative paths
* @param $source string path to source dir.
* @param $target string path to destination dir
*/
private function copyFiles($files, $source, $target)
{
foreach ($files as $file) {
$from = $this->combine($source, $file['from']);
$to = $this->combine($target, $file['to']);
$this->copyFile($from, $to);
}
}
private function copyFile($from, $to)
{
if (!is_file($from)) {
throw new \RuntimeException('Invalid PEAR package. package.xml defines file that is not located inside tarball.');
}
$this->filesystem->ensureDirectoryExists(dirname($to));
if (!copy($from, $to)) {
throw new \RuntimeException(sprintf('Failed to copy %s to %s', $from, $to));
}
}
/**
* Builds list of copy and list of remove actions that would transform extracted PEAR tarball into installed package.
*
* @param $source string path to extracted files.
* @param $role string package file types to extract.
* @return array array of 'source' => 'target', where source is location of file in the tarball (relative to source
* path, and target is destination of file (also relative to $source path)
* @throws \RuntimeException
*/
private function buildCopyActions($source, $role)
{
/** @var $package \SimpleXmlElement */
$package = simplexml_load_file($this->combine($source, 'package.xml'));
if(false === $package)
throw new \RuntimeException('Package definition file is not valid.');
$packageSchemaVersion = $package['version'];
if ('1.0' == $packageSchemaVersion) {
$children = $package->release->filelist->children();
$packageName = (string) $package->name;
$packageVersion = (string) $package->release->version;
$sourceDir = $packageName . '-' . $packageVersion;
$result = $this->buildSourceList10($children, $role, $sourceDir);
} elseif ('2.0' == $packageSchemaVersion || '2.1' == $packageSchemaVersion) {
$children = $package->contents->children();
$packageName = (string) $package->name;
$packageVersion = (string) $package->version->release;
$sourceDir = $packageName . '-' . $packageVersion;
$result = $this->buildSourceList20($children, $role, $sourceDir);
} else {
throw new \RuntimeException('Unsupported schema version of package definition file.');
}
return $result;
}
private function buildSourceList10($children, $targetRole, $source = '', $target = '', $role = null)
{
$result = array();
// enumerating files
foreach ($children as $child) {
/** @var $child \SimpleXMLElement */
if ($child->getName() == 'dir') {
$dirSource = $this->combine($source, (string) $child['name']);
$dirTarget = $child['baseinstalldir'] ? : $target;
$dirRole = $child['role'] ? : $role;
$dirFiles = $this->buildSourceList10($child->children(), $targetRole, $dirSource, $dirTarget, $dirRole);
$result = array_merge($result, $dirFiles);
} elseif ($child->getName() == 'file') {
if (($child['role'] ? : $role) == $targetRole) {
$fileName = (string) ($child['name'] ? : $child[0]); // $child[0] means text content
$fileSource = $this->combine($source, $fileName);
$fileTarget = $this->combine((string) $child['baseinstalldir'] ? : $target, $fileName);
$result[] = array('from' => $fileSource, 'to' => $fileTarget);
}
}
}
return $result;
}
private function buildSourceList20($children, $targetRole, $source = '', $target = '', $role = null)
{
$result = array();
// enumerating files
foreach ($children as $child) {
/** @var $child \SimpleXMLElement */
if ($child->getName() == 'dir') {
$dirSource = $this->combine($source, $child['name']);
$dirTarget = $child['baseinstalldir'] ? : $target;
$dirRole = $child['role'] ? : $role;
$dirFiles = $this->buildSourceList20($child->children(), $targetRole, $dirSource, $dirTarget, $dirRole);
$result = array_merge($result, $dirFiles);
} elseif ($child->getName() == 'file') {
if (($child['role'] ? : $role) == $targetRole) {
$fileSource = $this->combine($source, (string) $child['name']);
$fileTarget = $this->combine((string) ($child['baseinstalldir'] ? : $target), (string) $child['name']);
$result[] = array('from' => $fileSource, 'to' => $fileTarget);
}
}
}
return $result;
}
private function combine($left, $right)
{
return rtrim($left, '/') . '/' . ltrim($right, '/');
}
}

View File

@ -163,6 +163,7 @@ class PearRepository extends ArrayRepository
'autoload' => array( 'autoload' => array(
'classmap' => array(''), 'classmap' => array(''),
), ),
'include-path' => array('/'),
); );
try { try {
@ -305,6 +306,7 @@ class PearRepository extends ArrayRepository
'autoload' => array( 'autoload' => array(
'classmap' => array(''), 'classmap' => array(''),
), ),
'include-path' => array('/'),
); );
$packageKeys = array('l' => 'license', 'd' => 'description'); $packageKeys = array('l' => 'license', 'd' => 'description');
foreach ($packageKeys as $pear => $composer) { foreach ($packageKeys as $pear => $composer) {

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0">
<package version="1.0" packagerversion="1.4.0a4">
<!-- stripped version of package-->
<name>PEAR_Frontend_Gtk</name>
<summary>Gtk (Desktop) PEAR Package Manager</summary>
<release>
<version>0.4.0</version>
<date>2005-03-14</date>
<license>PHP License</license>
<state>beta</state>
<notes>Implement channels, support PEAR 1.4.0 (Greg Beaver)
Tidy up logging a little.
</notes>
<filelist>
<file role="php" baseinstalldir="PEAR/Frontend" md5sum="3edf486653e75f4aac78c1d9d52e72ad" name="Gtk.php"/>
<file role="php" baseinstalldir="PEAR/Frontend" md5sum="d9847b8a0f3e2987f250c5112ef4032a" name="Gtk/Config.php"/>
<file role="php" baseinstalldir="PEAR/Frontend" md5sum="a65ef13fb4c2080404b465af28f67fe3" name="Gtk/xpm/black_close_icon.xpm"/>
</filelist>
</release>
</package>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<package packagerversion="1.6.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<!-- stripped version of package-->
<name>Net_URL</name>
<channel>pear.php.net</channel>
<summary>Easy parsing of Urls</summary>
<description>Provides easy parsing of URLs and their constituent parts.</description>
<version>
<release>1.0.15</release>
<api>1.0.15</api>
</version>
<contents>
<dir name="/">
<file baseinstalldir="Net" md5sum="af793351a5f00e31a2df697b54cfbc02" name="docs/example.php" role="doc" />
<file baseinstalldir="Net" md5sum="0488b5531c31332113100971be7ba2d9" name="docs/6470.php" role="doc" />
<file baseinstalldir="Net" md5sum="c7e690d656b56cc48a12399331a35b27" name="URL.php" role="php" />
</dir>
</contents>
</package>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://pear.php.net/dtd/package-2.1" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.1 http://pear.php.net/dtd/package-2.1.xsd" packagerversion="2.0.0">
<!-- stripped version of package-->
<name>Zend_Authentication</name>
<channel>packages.zendframework.com</channel>
<summary>
Package Zend_Authentication summary.\n\n" . "Package detailed description here (found in README)
</summary>
<description/>
<version>
<release>2.0.0beta4</release>
<api>2.0.0beta4</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<contents>
<dir name="/">
<file role="php" name="php/Zend/Authentication/Storage/StorageInterface.php"/>
<file role="php" name="php/Zend/Authentication/Result.php"/>
</dir>
</contents>
<phprelease/>
</package>

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Downloader;
use Composer\Downloader\PearDownloader;
class PearDownloaderTest extends \PHPUnit_Framework_TestCase
{
public function testErrorMessages()
{
$packageMock = $this->getMock('Composer\Package\PackageInterface');
$packageMock->expects($this->any())
->method('getDistUrl')
->will($this->returnValue('file://'.__FILE__))
;
$io = $this->getMock('Composer\IO\IOInterface');
$downloader = new PearDownloader($io);
try {
$downloader->download($packageMock, sys_get_temp_dir().'/composer-pear-test');
$this->fail('Download of invalid pear packages should throw an exception');
} catch (\UnexpectedValueException $e) {
$this->assertContains('Failed to extract PEAR package', $e->getMessage());
}
}
}

View File

@ -0,0 +1,81 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Downloader;
use Composer\Downloader\PearPackageExtractor;
class PearPackageExtractorTest extends \PHPUnit_Framework_TestCase
{
public function testShouldExtractPackage_1_0()
{
$extractor = $this->getMockForAbstractClass('Composer\Downloader\PearPackageExtractor', array(), '', false);
$method = new \ReflectionMethod($extractor, 'buildCopyActions');
$method->setAccessible(true);
$fileActions = $method->invoke($extractor, __DIR__ . '/Fixtures/Package_v1.0', 'php');
$expectedFileActions = array(
0 => Array(
'from' => 'PEAR_Frontend_Gtk-0.4.0/Gtk.php',
'to' => 'PEAR/Frontend/Gtk.php',
),
1 => Array(
'from' => 'PEAR_Frontend_Gtk-0.4.0/Gtk/Config.php',
'to' => 'PEAR/Frontend/Gtk/Config.php',
),
2 => Array(
'from' => 'PEAR_Frontend_Gtk-0.4.0/Gtk/xpm/black_close_icon.xpm',
'to' => 'PEAR/Frontend/Gtk/xpm/black_close_icon.xpm',
)
);
$this->assertSame($expectedFileActions, $fileActions);
}
public function testShouldExtractPackage_2_0()
{
$extractor = $this->getMockForAbstractClass('Composer\Downloader\PearPackageExtractor', array(), '', false);
$method = new \ReflectionMethod($extractor, 'buildCopyActions');
$method->setAccessible(true);
$fileActions = $method->invoke($extractor, __DIR__ . '/Fixtures/Package_v2.0', 'php');
$expectedFileActions = array(
0 => Array(
'from' => 'Net_URL-1.0.15/URL.php',
'to' => 'Net/URL.php',
)
);
$this->assertSame($expectedFileActions, $fileActions);
}
public function testShouldExtractPackage_2_1()
{
$extractor = $this->getMockForAbstractClass('Composer\Downloader\PearPackageExtractor', array(), '', false);
$method = new \ReflectionMethod($extractor, 'buildCopyActions');
$method->setAccessible(true);
$fileActions = $method->invoke($extractor, __DIR__ . '/Fixtures/Package_v2.1', 'php');
$expectedFileActions = array(
0 => Array(
'from' => 'Zend_Authentication-2.0.0beta4/php/Zend/Authentication/Storage/StorageInterface.php',
'to' => '/php/Zend/Authentication/Storage/StorageInterface.php',
),
1 => Array(
'from' => 'Zend_Authentication-2.0.0beta4/php/Zend/Authentication/Result.php',
'to' => '/php/Zend/Authentication/Result.php',
)
);
$this->assertSame($expectedFileActions, $fileActions);
}
}

View File

@ -29,6 +29,30 @@ class PearRepositoryTest extends TestCase
*/ */
private $remoteFilesystem; private $remoteFilesystem;
public function testComposerNonCompatibleRepositoryShouldSetIncludePath()
{
$url = 'pear.phpmd.org';
$expectedPackages = array(
array('name' => 'pear-phpmd/PHP_PMD', 'version' => '1.3.3'),
);
$repoConfig = array(
'url' => $url
);
$this->createRepository($repoConfig);
foreach ($expectedPackages as $expectedPackage) {
$package = $this->repository->findPackage($expectedPackage['name'], $expectedPackage['version']);
$this->assertInstanceOf('Composer\Package\PackageInterface',
$package,
'Expected package ' . $expectedPackage['name'] . ', version ' . $expectedPackage['version'] .
' not found in pear channel ' . $url
);
$this->assertSame(array('/'), $package->getIncludePaths());
}
}
/** /**
* @dataProvider repositoryDataProvider * @dataProvider repositoryDataProvider
* @param string $url * @param string $url