docker-php-extension-installer/scripts/inspect-pecl-package

413 lines
12 KiB
Plaintext
Raw Normal View History

2020-12-11 17:19:07 +00:00
#!/usr/bin/env php
<?php
set_error_handler(
static function ($errno, $errstr, $errfile, $errline) {
$msg = "Error {$errno}: {$errstr}\n";
if ($errfile) {
$msg .= "File: {$errfile}\n";
if ($errline) {
$msg .= "Line: {$errline}\n";
}
}
throw new RuntimeException($msg);
},
-1
);
try {
if (!isset($argv[1]) || isset($argv[2]) || !preg_match('/^\w+$/', $argv[1]) || in_array('-h', $argv, true) || in_array('--help', $argv, true)) {
echo "Syntax: {$argv[0]} <extension>\n";
exit(in_array('-h', $argv, true) || in_array('--help', $argv, true) ? 0 : 1);
}
2020-12-15 08:30:48 +00:00
define('TMP_DIR', __DIR__ . '/tmp');
if (!is_dir(TMP_DIR)) {
mkdir(TMP_DIR);
}
echo "# FETCHING DATA\n";
2020-12-11 17:19:07 +00:00
echo 'Retrieving package versions... ';
$versions = listPackageVersions($argv[1]);
$numVersions = count($versions);
echo "done.\n";
2020-12-15 08:30:48 +00:00
$versionInfos = [];
2020-12-11 17:19:07 +00:00
$versionCount = 0;
foreach ($versions as $version) {
$versionCount++;
echo "Inspecting version {$version} [{$versionCount}/{$numVersions}]... ";
2020-12-15 08:30:48 +00:00
$versionInfos[] = inspectPackageVersion($argv[1], $version);
2020-12-11 17:19:07 +00:00
echo "done.\n";
}
2020-12-15 08:30:48 +00:00
echo "# PHP VERSION COMPATIBILITY LIST\n";
$instance = null;
foreach ($versionInfos as $versionInfo) {
if ($instance === null || !$instance->isCompatibleWith($versionInfo)) {
if ($instance !== null) {
echo $instance, "\n";
2020-12-11 17:19:07 +00:00
}
2020-12-15 08:30:48 +00:00
$instance = new CompatiblePHPVersions($versionInfo->getMinPHPVersion(), $versionInfo->getMaxPHPVersion());
2020-12-11 17:19:07 +00:00
}
2020-12-15 08:30:48 +00:00
$instance->addVersion($versionInfo->getVersion());
}
if ($instance !== null) {
echo $instance, "\n";
}
echo "# CONFIGURATION OPTIONS\n";
$instance = null;
foreach ($versionInfos as $versionInfo) {
if ($instance === null || !$instance->isCompatibleWith($versionInfo)) {
if ($instance !== null) {
echo $instance, "\n";
}
$instance = new CompatibleConfigurationOptions($versionInfo->getConfigureOptions());
}
$instance->addVersion($versionInfo->getVersion());
}
if ($instance !== null) {
echo $instance, "\n";
2020-12-11 17:19:07 +00:00
}
} catch (RuntimeException $x) {
echo $x->getMessage(), "\n";
exit(1);
}
/**
* @return string[]
*/
function listPackageVersions(string $package): array
{
2020-12-15 08:30:48 +00:00
$cachedDir = TMP_DIR;
$cachedFile = "{$cachedDir}/{$package}.xml";
if (is_file($cachedFile) && filemtime($cachedFile) > (time() - 1800)) {
return listPackageVersionsXml($package, file_get_contents($cachedFile));
}
2020-12-11 17:19:07 +00:00
try {
$xml = file_get_contents("https://pecl.php.net/rest/r/{$package}/allreleases.xml");
} catch (RuntimeException $x) {
if (strpos($x->getMessage(), '404 Not Found') !== false) {
throw new RuntimeException("Invalid PECL package name or no versions available for {$package}");
}
throw $x;
}
2020-12-15 08:30:48 +00:00
$versions = listPackageVersionsXml($package, $xml);
if (!is_dir($cachedDir)) {
mkdir($cachedDir, 0777, true);
}
file_put_contents($cachedFile, $xml);
return $versions;
}
function listPackageVersionsXml(string $package, string $xml): array
{
2020-12-11 17:19:07 +00:00
$dom = new DOMDocument();
if (!$dom->loadXML($xml)) {
throw new RuntimeException('Failed to parse the downloaded XML data.');
}
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('v', 'http://pear.php.net/dtd/rest.allreleases');
$versionNodes = $xpath->query('/v:a/v:r/v:v');
if ($versionNodes->count() === 0) {
throw new RuntimeException("No versions available for {$package}");
}
$versions = [];
foreach ($versionNodes as $versionNode) {
$versions[] = $versionNode->nodeValue;
}
usort($versions, 'version_compare');
return $versions;
}
2020-12-15 08:30:48 +00:00
function inspectPackageVersion(string $package, string $version): PackageVersionInfo
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$cachedDir = TMP_DIR . "/{$package}";
$cachedFile = "{$cachedDir}/{$version}.xml";
if (is_file($cachedFile)) {
return inspectPackageVersionXml($package, $version, $cachedFile);
}
2020-12-11 17:19:07 +00:00
$tgzFile = tempnam(sys_get_temp_dir(), 'dpx');
try {
file_put_contents($tgzFile, file_get_contents("https://pecl.php.net/get/{$package}-{$version}.tgz"));
$archive = new PharData($tgzFile);
$archive->decompress('tar');
$tarFile = preg_replace('/\.\w+$/', '.tar', $tgzFile);
try {
$extractedDir = preg_replace('/\.tar$/', '.decompressed', $tarFile);
mkdir($extractedDir);
try {
try {
$archive->extractTo($extractedDir, 'package2.xml');
rename("{$extractedDir}/package2.xml", "{$extractedDir}/package.xml");
} catch (PharException $x) {
$archive->extractTo($extractedDir, 'package.xml');
}
2020-12-11 17:19:07 +00:00
try {
2020-12-15 08:30:48 +00:00
$info = inspectPackageVersionXml($package, $version, "{$extractedDir}/package.xml");
if (!is_dir($cachedDir)) {
mkdir($cachedDir, 0777, true);
2020-12-11 17:19:07 +00:00
}
2020-12-15 08:30:48 +00:00
copy("{$extractedDir}/package.xml", $cachedFile);
2020-12-11 17:19:07 +00:00
} finally {
unlink("{$extractedDir}/package.xml");
}
} finally {
rmdir($extractedDir);
}
} finally {
unlink($tarFile);
}
} finally {
unlink($tgzFile);
}
return $info;
}
2020-12-15 08:30:48 +00:00
function inspectPackageVersionXml(string $package, string $version, string $xmlFile): PackageVersionInfo
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$dom = new DOMDocument();
if (!$dom->loadXML(file_get_contents($xmlFile))) {
throw new RuntimeException("Failed to parse the downloaded package.xml file located at {$xmlFile}.");
}
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('v2', 'http://pear.php.net/dtd/package-2.0');
if ($xpath->query('/v2:package/v2:dependencies')->count() === 1) {
$ns = 'v2:';
} elseif ($xpath->query('/package/release/version')->count() === 1) {
$ns = '';
} else {
throw new RuntimeException('Unsupported namespace');
}
$minPHPVersionNodes = $xpath->query("/{$ns}package/{$ns}dependencies/{$ns}required/{$ns}php/{$ns}min");
$maxPHPVersionNodes = $xpath->query("/{$ns}package/{$ns}dependencies/{$ns}required/{$ns}php/{$ns}max");
$info = new PackageVersionInfo(
$version,
$minPHPVersionNodes->count() === 0 ? '' : $minPHPVersionNodes[0]->nodeValue,
$maxPHPVersionNodes->count() === 0 ? '' : $maxPHPVersionNodes[0]->nodeValue
);
foreach ($xpath->query("/{$ns}package/{$ns}extsrcrelease/{$ns}configureoption") as $configureOptionNode) {
$info->addConfigureOption(new PackageConfigureOption(
$configureOptionNode->getAttribute('name'),
$configureOptionNode->getAttribute('prompt'),
$configureOptionNode->hasAttribute('default') ? $configureOptionNode->getAttribute('default') : null
));
}
return $info;
}
class PackageVersionInfo
{
private string $version;
2020-12-11 17:19:07 +00:00
private string $minPHPVersion;
private string $maxPHPVersion;
private array $configureOptions = [];
2020-12-15 08:30:48 +00:00
public function __construct(string $version, string $minPHPVersion, string $maxPHPVersion = '')
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$this->version = $version;
2020-12-11 17:19:07 +00:00
$this->minPHPVersion = $minPHPVersion;
$this->maxPHPVersion = $maxPHPVersion;
}
2020-12-15 08:30:48 +00:00
public function getVersion(): string
{
return $this->version;
}
2020-12-11 17:19:07 +00:00
public function getMinPHPVersion(): string
{
return $this->minPHPVersion;
}
public function getMaxPHPVersion(): string
{
return $this->maxPHPVersion;
}
/**
* @return $this
*/
public function addConfigureOption(PackageConfigureOption $value): self
{
$this->configureOptions[] = $value;
return $this;
}
/**
* @return PackageConfigureOption[]
*/
public function getConfigureOptions(): array
{
return $this->configureOptions;
}
}
2020-12-15 08:30:48 +00:00
class PackageConfigureOption
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
private string $name;
private string $prompt;
private ?string $default;
2020-12-11 17:19:07 +00:00
2020-12-15 08:30:48 +00:00
public function __construct(string $name, string $prompt, ?string $default = null)
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$this->name = $name;
$this->prompt = $prompt;
$this->default = $default;
2020-12-11 17:19:07 +00:00
}
2020-12-15 08:30:48 +00:00
public function __toString(): string
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$result = "{$this->getName()} \"{$this->getPrompt()}\"";
if ($this->getDefault() !== null) {
$result .= " [{$this->getDefault()}]";
}
return $result;
}
public function getName(): string
{
return $this->name;
}
public function getPrompt(): string
{
return $this->prompt;
}
public function getDefault(): ?string
{
return $this->default;
2020-12-11 17:19:07 +00:00
}
}
2020-12-15 08:30:48 +00:00
class CompatiblePHPVersions
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
private string $minPHPVersion;
private string $maxPHPVersion;
2020-12-11 17:19:07 +00:00
private array $versions = [];
2020-12-15 08:30:48 +00:00
public function __construct(string $minPHPVersion, string $maxPHPVersion)
{
$this->minPHPVersion = $minPHPVersion;
$this->maxPHPVersion = $maxPHPVersion;
}
public function __toString(): string
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$versions = $this->getVersions();
return implode("\n", [
"- Package versions: {$versions[0]}" . (isset($versions[1]) ? ' -> ' . $versions[count($versions) - 1] : ''),
" - Min PHP version: {$this->getMinPHPVersion()}",
" - Max PHP version: {$this->getMaxPHPVersion()}",
]);
}
public function getMinPHPVersion(): string
{
return $this->minPHPVersion;
}
public function getMaxPHPVersion(): string
{
return $this->maxPHPVersion;
2020-12-11 17:19:07 +00:00
}
/**
* @return $this
*/
public function addVersion(string $value): self
{
$this->versions[] = $value;
return $this;
}
/**
* @return string[]
*/
public function getVersions(): array
{
return $this->versions;
}
2020-12-15 08:30:48 +00:00
public function isCompatibleWith(PackageVersionInfo $info): bool
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
return $this->getMinPHPVersion() === $info->getMinPHPVersion() && $this->getMaxPHPVersion() === $info->getMaxPHPVersion();
2020-12-11 17:19:07 +00:00
}
}
2020-12-15 08:30:48 +00:00
class CompatibleConfigurationOptions
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
/**
* @var PackageConfigureOption[]
*/
private array $configureOptions = [];
private array $versions = [];
2020-12-11 17:19:07 +00:00
2020-12-15 08:30:48 +00:00
public function __construct(array $configureOptions)
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$this->configureOptions = $configureOptions;
2020-12-11 17:19:07 +00:00
}
2020-12-15 08:30:48 +00:00
public function __toString(): string
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$versions = $this->getVersions();
$lines = [
"- Package versions: {$versions[0]}" . (isset($versions[1]) ? ' -> ' . $versions[count($versions) - 1] : ''),
];
$options = $this->getConfigureOptions();
if ($options === []) {
$lines[] = ' <no options>';
} else {
foreach ($options as $index => $option) {
$lines[] = $index + 1 . ') ' . (string) $option;
}
}
return implode("\n", $lines);
2020-12-11 17:19:07 +00:00
}
2020-12-15 08:30:48 +00:00
/**
* @return PackageConfigureOption[]
*/
public function getConfigureOptions(): array
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
return $this->configureOptions;
2020-12-11 17:19:07 +00:00
}
2020-12-15 08:30:48 +00:00
/**
* @return $this
*/
public function addVersion(string $value): self
2020-12-11 17:19:07 +00:00
{
2020-12-15 08:30:48 +00:00
$this->versions[] = $value;
return $this;
}
/**
* @return string[]
*/
public function getVersions(): array
{
return $this->versions;
}
public function isCompatibleWith(PackageVersionInfo $info): bool
{
return $this->getConfigureOptions() == $info->getConfigureOptions();
2020-12-11 17:19:07 +00:00
}
}