updated spdx-license handling to include metadata
issue https://github.com/composer/composer/issues/3744 summary of changes - replaced bin script "fetch-spdx-identifiers" by "update-spdx-licenses" - "update-spdx-licenses" uses "Composer/Util/SpdxLicensesUpdater" to update "spdx-licenses.json" file with license identifier, fullname and osi-approved-status - dropped "spdx-identifiers.json" (identifiers only) - dropped "SpdxLicenseIdentifier", it's just "SpdxLicense" now - modified "ShowCommand" to output the license with metadata and removed some unused method arguments (cleanup)pull/3885/head
parent
f10c714751
commit
74a077dd3d
|
@ -1,85 +0,0 @@
|
||||||
#!/usr/bin/env php
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$identifiers = new SPDXLicenseIdentifiersOnline;
|
|
||||||
$printer = new JsonPrinter;
|
|
||||||
$printer->printStringArray($identifiers->getStrings());
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SPDX Identifier List from the registry.
|
|
||||||
*/
|
|
||||||
class SPDXLicenseIdentifiersOnline
|
|
||||||
{
|
|
||||||
const REGISTRY = 'http://www.spdx.org/licenses/';
|
|
||||||
const EXPRESSION = '//*[@typeof="spdx:License"]/code[@property="spdx:licenseId"]/text()';
|
|
||||||
|
|
||||||
private $identifiers;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getStrings()
|
|
||||||
{
|
|
||||||
if ($this->identifiers) {
|
|
||||||
return $this->identifiers;
|
|
||||||
}
|
|
||||||
$this->identifiers = $this->importNodesFromURL(
|
|
||||||
self::REGISTRY,
|
|
||||||
self::EXPRESSION
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->identifiers;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function importNodesFromURL($url, $expressionTextNodes)
|
|
||||||
{
|
|
||||||
$doc = new DOMDocument();
|
|
||||||
$doc->loadHTMLFile($url);
|
|
||||||
$xp = new DOMXPath($doc);
|
|
||||||
$codes = $xp->query($expressionTextNodes);
|
|
||||||
if (!$codes) {
|
|
||||||
throw new \Exception(sprintf('XPath query failed: %s', $expressionTextNodes));
|
|
||||||
}
|
|
||||||
if ($codes->length < 20) {
|
|
||||||
throw new \Exception('Obtaining the license table failed, there can not be less than 20 identifiers.');
|
|
||||||
}
|
|
||||||
$identifiers = array();
|
|
||||||
foreach ($codes as $code) {
|
|
||||||
$identifiers[] = $code->nodeValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $identifiers;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print an array the way this script needs it.
|
|
||||||
*/
|
|
||||||
class JsonPrinter
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param array $array
|
|
||||||
*/
|
|
||||||
public function printStringArray(array $array)
|
|
||||||
{
|
|
||||||
$lines = array('');
|
|
||||||
$line = &$lines[0];
|
|
||||||
$last = count($array) - 1;
|
|
||||||
foreach ($array as $item => $code) {
|
|
||||||
$code = sprintf('"%s"%s', trim($code), $item === $last ? '' : ', ');
|
|
||||||
$length = strlen($line) + strlen($code) - 1;
|
|
||||||
if ($length > 76) {
|
|
||||||
$line = rtrim($line);
|
|
||||||
unset($line);
|
|
||||||
$lines[] = $code;
|
|
||||||
$line = &$lines[count($lines) - 1];
|
|
||||||
} else {
|
|
||||||
$line .= $code;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$json = sprintf("[%s]", implode("\n ", $lines));
|
|
||||||
$json = str_replace(array("[\"", "\"]"), array("[\n \"", "\"\n]"), $json);
|
|
||||||
echo $json;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require __DIR__ . '/../src/bootstrap.php';
|
||||||
|
|
||||||
|
use Composer\Util\SpdxLicensesUpdater;
|
||||||
|
|
||||||
|
$licenses = new SpdxLicensesUpdater;
|
||||||
|
$licenses->update();
|
|
@ -1,59 +0,0 @@
|
||||||
[
|
|
||||||
"Glide", "Abstyles", "AFL-1.1", "AFL-1.2", "AFL-2.0", "AFL-2.1", "AFL-3.0",
|
|
||||||
"AMPAS", "APL-1.0", "Adobe-Glyph", "APAFML", "Adobe-2006", "AGPL-1.0",
|
|
||||||
"Afmparse", "Aladdin", "ADSL", "AMDPLPA", "ANTLR-PD", "Apache-1.0",
|
|
||||||
"Apache-1.1", "Apache-2.0", "AML", "APSL-1.0", "APSL-1.1", "APSL-1.2",
|
|
||||||
"APSL-2.0", "Artistic-1.0", "Artistic-1.0-Perl", "Artistic-1.0-cl8",
|
|
||||||
"Artistic-2.0", "AAL", "Bahyph", "Barr", "Beerware", "BitTorrent-1.0",
|
|
||||||
"BitTorrent-1.1", "BSL-1.0", "Borceux", "BSD-2-Clause",
|
|
||||||
"BSD-2-Clause-FreeBSD", "BSD-2-Clause-NetBSD", "BSD-3-Clause",
|
|
||||||
"BSD-3-Clause-Clear", "BSD-4-Clause", "BSD-Protection",
|
|
||||||
"BSD-3-Clause-Attribution", "BSD-4-Clause-UC", "bzip2-1.0.5", "bzip2-1.0.6",
|
|
||||||
"Caldera", "CECILL-1.0", "CECILL-1.1", "CECILL-2.0", "CECILL-B", "CECILL-C",
|
|
||||||
"ClArtistic", "MIT-CMU", "CNRI-Python", "CNRI-Python-GPL-Compatible",
|
|
||||||
"CPOL-1.02", "CDDL-1.0", "CDDL-1.1", "CPAL-1.0", "CPL-1.0", "CATOSL-1.1",
|
|
||||||
"Condor-1.1", "CC-BY-1.0", "CC-BY-2.0", "CC-BY-2.5", "CC-BY-3.0",
|
|
||||||
"CC-BY-4.0", "CC-BY-ND-1.0", "CC-BY-ND-2.0", "CC-BY-ND-2.5", "CC-BY-ND-3.0",
|
|
||||||
"CC-BY-ND-4.0", "CC-BY-NC-1.0", "CC-BY-NC-2.0", "CC-BY-NC-2.5",
|
|
||||||
"CC-BY-NC-3.0", "CC-BY-NC-4.0", "CC-BY-NC-ND-1.0", "CC-BY-NC-ND-2.0",
|
|
||||||
"CC-BY-NC-ND-2.5", "CC-BY-NC-ND-3.0", "CC-BY-NC-ND-4.0", "CC-BY-NC-SA-1.0",
|
|
||||||
"CC-BY-NC-SA-2.0", "CC-BY-NC-SA-2.5", "CC-BY-NC-SA-3.0", "CC-BY-NC-SA-4.0",
|
|
||||||
"CC-BY-SA-1.0", "CC-BY-SA-2.0", "CC-BY-SA-2.5", "CC-BY-SA-3.0",
|
|
||||||
"CC-BY-SA-4.0", "CC0-1.0", "Crossword", "CUA-OPL-1.0", "Cube", "D-FSL-1.0",
|
|
||||||
"diffmark", "WTFPL", "DOC", "Dotseqn", "DSDP", "dvipdfm", "EPL-1.0",
|
|
||||||
"eCos-2.0", "ECL-1.0", "ECL-2.0", "eGenix", "EFL-1.0", "EFL-2.0",
|
|
||||||
"MIT-advertising", "MIT-enna", "Entessa", "ErlPL-1.1", "EUDatagrid",
|
|
||||||
"EUPL-1.0", "EUPL-1.1", "Eurosym", "Fair", "MIT-feh", "Frameworx-1.0",
|
|
||||||
"FTL", "FSFUL", "FSFULLR", "Giftware", "GL2PS", "Glulxe", "AGPL-3.0",
|
|
||||||
"GFDL-1.1", "GFDL-1.2", "GFDL-1.3", "GPL-1.0", "GPL-1.0+", "GPL-2.0",
|
|
||||||
"GPL-2.0+", "GPL-2.0-with-autoconf-exception",
|
|
||||||
"GPL-2.0-with-bison-exception", "GPL-2.0-with-classpath-exception",
|
|
||||||
"GPL-2.0-with-font-exception", "GPL-2.0-with-GCC-exception", "GPL-3.0",
|
|
||||||
"GPL-3.0+", "GPL-3.0-with-autoconf-exception", "GPL-3.0-with-GCC-exception",
|
|
||||||
"LGPL-2.1", "LGPL-2.1+", "LGPL-3.0", "LGPL-3.0+", "LGPL-2.0", "LGPL-2.0+",
|
|
||||||
"gnuplot", "gSOAP-1.3b", "HaskellReport", "HPND", "IBM-pibs", "IPL-1.0",
|
|
||||||
"ImageMagick", "iMatix", "Imlib2", "IJG", "Intel-ACPI", "Intel", "IPA",
|
|
||||||
"ISC", "JasPer-2.0", "JSON", "LPPL-1.3a", "LPPL-1.0", "LPPL-1.1",
|
|
||||||
"LPPL-1.2", "LPPL-1.3c", "Latex2e", "BSD-3-Clause-LBNL", "Leptonica",
|
|
||||||
"Libpng", "libtiff", "LPL-1.02", "LPL-1.0", "MakeIndex", "MTLL", "MS-PL",
|
|
||||||
"MS-RL", "MirOS", "MITNFA", "MIT", "Motosoto", "MPL-1.0", "MPL-1.1",
|
|
||||||
"MPL-2.0", "MPL-2.0-no-copyleft-exception", "mpich2", "Multics", "Mup",
|
|
||||||
"NASA-1.3", "Naumen", "NBPL-1.0", "NetCDF", "NGPL", "NOSL", "NPL-1.0",
|
|
||||||
"NPL-1.1", "Newsletr", "NLPL", "Nokia", "NPOSL-3.0", "Noweb", "NRL", "NTP",
|
|
||||||
"Nunit", "OCLC-2.0", "ODbL-1.0", "PDDL-1.0", "OGTSL", "OLDAP-2.2.2",
|
|
||||||
"OLDAP-1.1", "OLDAP-1.2", "OLDAP-1.3", "OLDAP-1.4", "OLDAP-2.0",
|
|
||||||
"OLDAP-2.0.1", "OLDAP-2.1", "OLDAP-2.2", "OLDAP-2.2.1", "OLDAP-2.3",
|
|
||||||
"OLDAP-2.4", "OLDAP-2.5", "OLDAP-2.6", "OLDAP-2.7", "OML", "OPL-1.0",
|
|
||||||
"OSL-1.0", "OSL-1.1", "OSL-2.0", "OSL-2.1", "OSL-3.0", "OLDAP-2.8",
|
|
||||||
"OpenSSL", "PHP-3.0", "PHP-3.01", "Plexus", "PostgreSQL", "psfrag",
|
|
||||||
"psutils", "Python-2.0", "QPL-1.0", "Qhull", "Rdisc", "RPSL-1.0", "RPL-1.1",
|
|
||||||
"RPL-1.5", "RHeCos-1.1", "RSCPL", "Ruby", "SAX-PD", "Saxpath", "SCEA",
|
|
||||||
"SWL", "SGI-B-1.0", "SGI-B-1.1", "SGI-B-2.0", "OFL-1.0", "OFL-1.1",
|
|
||||||
"SimPL-2.0", "Sleepycat", "SNIA", "SMLNJ", "StandardML-NJ",
|
|
||||||
"SugarCRM-1.1.3", "SISSL", "SISSL-1.2", "SPL-1.0", "Watcom-1.0", "TCL",
|
|
||||||
"Unlicense", "TMate", "TORQUE-1.1", "TOSL", "Unicode-TOU", "NCSA", "Vim",
|
|
||||||
"VOSTROM", "VSL-1.0", "W3C", "Wsuipa", "WXwindows", "Xnet", "X11", "Xerox",
|
|
||||||
"XFree86-1.1", "xinetd", "xpp", "XSkat", "YPL-1.0", "YPL-1.1", "Zed",
|
|
||||||
"Zend-2.0", "Zimbra-1.3", "Zlib", "zlib-acknowledgement", "ZPL-1.1",
|
|
||||||
"ZPL-2.0", "ZPL-2.1"
|
|
||||||
]
|
|
File diff suppressed because it is too large
Load Diff
|
@ -28,6 +28,7 @@ use Composer\Repository\CompositeRepository;
|
||||||
use Composer\Repository\ComposerRepository;
|
use Composer\Repository\ComposerRepository;
|
||||||
use Composer\Repository\PlatformRepository;
|
use Composer\Repository\PlatformRepository;
|
||||||
use Composer\Repository\RepositoryInterface;
|
use Composer\Repository\RepositoryInterface;
|
||||||
|
use Composer\Util\SpdxLicense;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert Schönthal <seroscho@googlemail.com>
|
* @author Robert Schönthal <seroscho@googlemail.com>
|
||||||
|
@ -115,18 +116,18 @@ EOT
|
||||||
$versions = array($package->getPrettyVersion() => $package->getVersion());
|
$versions = array($package->getPrettyVersion() => $package->getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->printMeta($input, $output, $package, $versions, $installedRepo, $repos);
|
$this->printMeta($package, $versions, $installedRepo);
|
||||||
$this->printLinks($input, $output, $package, 'requires');
|
$this->printLinks($package, 'requires');
|
||||||
$this->printLinks($input, $output, $package, 'devRequires', 'requires (dev)');
|
$this->printLinks($package, 'devRequires', 'requires (dev)');
|
||||||
if ($package->getSuggests()) {
|
if ($package->getSuggests()) {
|
||||||
$this->getIO()->write("\n<info>suggests</info>");
|
$this->getIO()->write("\n<info>suggests</info>");
|
||||||
foreach ($package->getSuggests() as $suggested => $reason) {
|
foreach ($package->getSuggests() as $suggested => $reason) {
|
||||||
$this->getIO()->write($suggested . ' <comment>' . $reason . '</comment>');
|
$this->getIO()->write($suggested . ' <comment>' . $reason . '</comment>');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->printLinks($input, $output, $package, 'provides');
|
$this->printLinks($package, 'provides');
|
||||||
$this->printLinks($input, $output, $package, 'conflicts');
|
$this->printLinks($package, 'conflicts');
|
||||||
$this->printLinks($input, $output, $package, 'replaces');
|
$this->printLinks($package, 'replaces');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -283,14 +284,14 @@ EOT
|
||||||
/**
|
/**
|
||||||
* prints package meta data
|
* prints package meta data
|
||||||
*/
|
*/
|
||||||
protected function printMeta(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, RepositoryInterface $repos)
|
protected function printMeta(CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo)
|
||||||
{
|
{
|
||||||
$this->getIO()->write('<info>name</info> : ' . $package->getPrettyName());
|
$this->getIO()->write('<info>name</info> : ' . $package->getPrettyName());
|
||||||
$this->getIO()->write('<info>descrip.</info> : ' . $package->getDescription());
|
$this->getIO()->write('<info>descrip.</info> : ' . $package->getDescription());
|
||||||
$this->getIO()->write('<info>keywords</info> : ' . join(', ', $package->getKeywords() ?: array()));
|
$this->getIO()->write('<info>keywords</info> : ' . join(', ', $package->getKeywords() ?: array()));
|
||||||
$this->printVersions($input, $output, $package, $versions, $installedRepo, $repos);
|
$this->printVersions($package, $versions, $installedRepo);
|
||||||
$this->getIO()->write('<info>type</info> : ' . $package->getType());
|
$this->getIO()->write('<info>type</info> : ' . $package->getType());
|
||||||
$this->getIO()->write('<info>license</info> : ' . implode(', ', $package->getLicense()));
|
$this->printLicenses($package);
|
||||||
$this->getIO()->write('<info>source</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference()));
|
$this->getIO()->write('<info>source</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference()));
|
||||||
$this->getIO()->write('<info>dist</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference()));
|
$this->getIO()->write('<info>dist</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference()));
|
||||||
$this->getIO()->write('<info>names</info> : ' . implode(', ', $package->getNames()));
|
$this->getIO()->write('<info>names</info> : ' . implode(', ', $package->getNames()));
|
||||||
|
@ -339,7 +340,7 @@ EOT
|
||||||
/**
|
/**
|
||||||
* prints all available versions of this package and highlights the installed one if any
|
* prints all available versions of this package and highlights the installed one if any
|
||||||
*/
|
*/
|
||||||
protected function printVersions(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, RepositoryInterface $repos)
|
protected function printVersions(CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo)
|
||||||
{
|
{
|
||||||
uasort($versions, 'version_compare');
|
uasort($versions, 'version_compare');
|
||||||
$versions = array_keys(array_reverse($versions));
|
$versions = array_keys(array_reverse($versions));
|
||||||
|
@ -361,13 +362,11 @@ EOT
|
||||||
/**
|
/**
|
||||||
* print link objects
|
* print link objects
|
||||||
*
|
*
|
||||||
* @param InputInterface $input
|
|
||||||
* @param OutputInterface $output
|
|
||||||
* @param CompletePackageInterface $package
|
* @param CompletePackageInterface $package
|
||||||
* @param string $linkType
|
* @param string $linkType
|
||||||
* @param string $title
|
* @param string $title
|
||||||
*/
|
*/
|
||||||
protected function printLinks(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, $linkType, $title = null)
|
protected function printLinks(CompletePackageInterface $package, $linkType, $title = null)
|
||||||
{
|
{
|
||||||
$title = $title ?: $linkType;
|
$title = $title ?: $linkType;
|
||||||
if ($links = $package->{'get'.ucfirst($linkType)}()) {
|
if ($links = $package->{'get'.ucfirst($linkType)}()) {
|
||||||
|
@ -378,4 +377,29 @@ EOT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints the licenses of a package with metadata
|
||||||
|
*
|
||||||
|
* @param CompletePackageInterface $package
|
||||||
|
*/
|
||||||
|
protected function printLicenses(CompletePackageInterface $package)
|
||||||
|
{
|
||||||
|
$spdxLicense = new SpdxLicense;
|
||||||
|
|
||||||
|
$licenses = $package->getLicense();
|
||||||
|
|
||||||
|
foreach($licenses as $licenseId) {
|
||||||
|
$license = $spdxLicense->getLicenseByIdentifier($licenseId); // keys: 0 fullname, 1 osi, 2 url
|
||||||
|
|
||||||
|
// is license OSI approved?
|
||||||
|
if($license[1] === true) {
|
||||||
|
$out = sprintf('%s (%s) (OSI approved) %s', $license[0], $licenseId, $license[2]);
|
||||||
|
} else {
|
||||||
|
$out = sprintf('%s (%s) %s', $license[0], $licenseId, $license[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getIO()->write('<info>license</info> : ' . $out);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,7 @@ class ConfigValidator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$licenseValidator = new SpdxLicenseIdentifier();
|
$licenseValidator = new SpdxLicense();
|
||||||
if ('proprietary' !== $manifest['license'] && array() !== $manifest['license'] && !$licenseValidator->validate($manifest['license'])) {
|
if ('proprietary' !== $manifest['license'] && array() !== $manifest['license'] && !$licenseValidator->validate($manifest['license'])) {
|
||||||
$warnings[] = sprintf(
|
$warnings[] = sprintf(
|
||||||
'License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license.'
|
'License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license.'
|
||||||
|
|
|
@ -20,16 +20,85 @@ use Composer\Json\JsonFile;
|
||||||
*
|
*
|
||||||
* @author Tom Klingenberg <tklingenberg@lastflood.net>
|
* @author Tom Klingenberg <tklingenberg@lastflood.net>
|
||||||
*/
|
*/
|
||||||
class SpdxLicenseIdentifier
|
class SpdxLicense
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $identifiers;
|
private $licenses;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->initIdentifiers();
|
$this->loadLicenses();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function loadLicenses()
|
||||||
|
{
|
||||||
|
if(is_array($this->licenses)) {
|
||||||
|
return $this->licenses;
|
||||||
|
}
|
||||||
|
|
||||||
|
$jsonFile = new JsonFile(__DIR__ . '/../../../res/spdx-licenses.json');
|
||||||
|
$this->licenses = $jsonFile->read();
|
||||||
|
|
||||||
|
return $this->licenses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns license metadata by license identifier.
|
||||||
|
*
|
||||||
|
* @param string $identifier
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getLicenseByIdentifier($identifier)
|
||||||
|
{
|
||||||
|
$license = $this->licenses[$identifier];
|
||||||
|
|
||||||
|
// add URL for the license text (it's not included in the json)
|
||||||
|
$license[2] = 'http://spdx.org/licenses/' . $identifier . '#licenseText';
|
||||||
|
|
||||||
|
return $license;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the short identifier of a license by full name.
|
||||||
|
*
|
||||||
|
* @param string $identifier
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getIdentifierByName($name)
|
||||||
|
{
|
||||||
|
foreach ($this->licenses as $identifier => $licenseData) {
|
||||||
|
if($licenseData[0] === $name) { // key 0 = fullname
|
||||||
|
return $identifier;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the OSI Approved status for a license by identifier.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isOsiApprovedByIdentifier($identifier)
|
||||||
|
{
|
||||||
|
return $this->licenses[$identifier][1]; // key 1 = osi approved
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check, if the identifier for a license is valid.
|
||||||
|
*
|
||||||
|
* @param string $identifier
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isValidLicenseIdentifier($identifier)
|
||||||
|
{
|
||||||
|
$identifiers = array_keys($this->licenses);
|
||||||
|
|
||||||
|
return in_array($identifier, $identifiers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,6 +116,7 @@ class SpdxLicenseIdentifier
|
||||||
}
|
}
|
||||||
$license = $count > 1 ? '('.implode(' or ', $license).')' : (string) reset($license);
|
$license = $count > 1 ? '('.implode(' or ', $license).')' : (string) reset($license);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_string($license)) {
|
if (!is_string($license)) {
|
||||||
throw new \InvalidArgumentException(sprintf(
|
throw new \InvalidArgumentException(sprintf(
|
||||||
'Array or String expected, %s given.', gettype($license)
|
'Array or String expected, %s given.', gettype($license)
|
||||||
|
@ -56,25 +126,6 @@ class SpdxLicenseIdentifier
|
||||||
return $this->isValidLicenseString($license);
|
return $this->isValidLicenseString($license);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads SPDX identifiers
|
|
||||||
*/
|
|
||||||
private function initIdentifiers()
|
|
||||||
{
|
|
||||||
$jsonFile = new JsonFile(__DIR__ . '/../../../res/spdx-identifier.json');
|
|
||||||
$this->identifiers = $jsonFile->read();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $identifier
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function isValidLicenseIdentifier($identifier)
|
|
||||||
{
|
|
||||||
return in_array($identifier, $this->identifiers);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $license
|
* @param string $license
|
||||||
*
|
*
|
|
@ -0,0 +1,67 @@
|
||||||
|
<?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\Util;
|
||||||
|
|
||||||
|
use Composer\Json\JsonFormatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The SPDX Licenses Updater scrapes licenses from the spdx website
|
||||||
|
* and updates the "res/spdx-licenses.json" file accordingly.
|
||||||
|
*
|
||||||
|
* The class is used by the update script "bin/update-spdx-licenses".
|
||||||
|
*/
|
||||||
|
class SpdxLicensesUpdater
|
||||||
|
{
|
||||||
|
private $licensesUrl = 'http://www.spdx.org/licenses/';
|
||||||
|
|
||||||
|
public function update()
|
||||||
|
{
|
||||||
|
$json = json_encode($this->getLicenses(), true);
|
||||||
|
$prettyJson = JsonFormatter::format($json, true, true);
|
||||||
|
file_put_contents(__DIR__ . '/../../../res/spdx-licenses.json', $prettyJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getLicenses()
|
||||||
|
{
|
||||||
|
$licenses = array();
|
||||||
|
|
||||||
|
$dom = new \DOMDocument;
|
||||||
|
$dom->loadHTMLFile($this->licensesUrl);
|
||||||
|
|
||||||
|
$xPath = new \DOMXPath($dom);
|
||||||
|
$trs = $xPath->query('//table//tbody//tr');
|
||||||
|
|
||||||
|
// iterate over each row in the table
|
||||||
|
foreach($trs as $tr) {
|
||||||
|
$tds = $tr->getElementsByTagName('td'); // get the columns in this row
|
||||||
|
|
||||||
|
if($tds->length < 4) {
|
||||||
|
throw new \Exception('Obtaining the license table failed. Wrong table format. Found less than 4 cells in a row.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if(trim($tds->item(3)->nodeValue) == 'License Text') {
|
||||||
|
$fullname = trim($tds->item(0)->nodeValue);
|
||||||
|
$identifier = trim($tds->item(1)->nodeValue);
|
||||||
|
$osiApproved = ((isset($tds->item(2)->nodeValue) && $tds->item(2)->nodeValue === 'Y')) ? true : false;
|
||||||
|
|
||||||
|
// The license URL is not scraped intentionally to keep json file size low.
|
||||||
|
// It's build when requested, see SpdxLicense->getLicenseByIdentifier().
|
||||||
|
//$licenseURL = = $tds->item(3)->getAttribute('href');
|
||||||
|
|
||||||
|
$licenses += array($identifier => array($fullname, $osiApproved));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $licenses;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,12 +2,28 @@
|
||||||
namespace Composer\Test\Util;
|
namespace Composer\Test\Util;
|
||||||
|
|
||||||
use Composer\TestCase;
|
use Composer\TestCase;
|
||||||
use Composer\Util\SpdxLicenseIdentifier;
|
use Composer\Util\SpdxLicense;
|
||||||
|
|
||||||
class SpdxLicenseIdentifierTest extends TestCase
|
class SpdxLicenseTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var object
|
||||||
|
*/
|
||||||
|
private $license;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->license = new SpdxLicense;
|
||||||
|
}
|
||||||
|
|
||||||
public static function provideValidLicenses()
|
public static function provideValidLicenses()
|
||||||
{
|
{
|
||||||
|
$json = file_get_contents(__DIR__ . '/../../../../res/spdx-licenses.json');
|
||||||
|
|
||||||
|
$licenses = json_decode($json, true);
|
||||||
|
|
||||||
|
$identifiers = array_keys($licenses);
|
||||||
|
|
||||||
$valid = array_merge(
|
$valid = array_merge(
|
||||||
array(
|
array(
|
||||||
"MIT",
|
"MIT",
|
||||||
|
@ -18,7 +34,7 @@ class SpdxLicenseIdentifierTest extends TestCase
|
||||||
"(LGPL-2.0 or GPL-3.0+)",
|
"(LGPL-2.0 or GPL-3.0+)",
|
||||||
"(EUDatagrid and GPL-3.0+)",
|
"(EUDatagrid and GPL-3.0+)",
|
||||||
),
|
),
|
||||||
json_decode(file_get_contents(__DIR__ . '/../../../../res/spdx-identifier.json'))
|
$identifiers
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($valid as &$r) {
|
foreach ($valid as &$r) {
|
||||||
|
@ -68,8 +84,7 @@ class SpdxLicenseIdentifierTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testValidate($license)
|
public function testValidate($license)
|
||||||
{
|
{
|
||||||
$validator = new SpdxLicenseIdentifier();
|
$this->assertTrue($this->license->validate($license));
|
||||||
$this->assertTrue($validator->validate($license));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,8 +93,7 @@ class SpdxLicenseIdentifierTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testInvalidLicenses($invalidLicense)
|
public function testInvalidLicenses($invalidLicense)
|
||||||
{
|
{
|
||||||
$validator = new SpdxLicenseIdentifier();
|
$this->assertFalse($this->license->validate($invalidLicense));
|
||||||
$this->assertFalse($validator->validate($invalidLicense));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,7 +102,31 @@ class SpdxLicenseIdentifierTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testInvalidArgument($invalidArgument)
|
public function testInvalidArgument($invalidArgument)
|
||||||
{
|
{
|
||||||
$validator = new SpdxLicenseIdentifier();
|
$this->license->validate($invalidArgument);
|
||||||
$validator->validate($invalidArgument);
|
}
|
||||||
|
|
||||||
|
public function testGetLicenseByIdentifier()
|
||||||
|
{
|
||||||
|
$license = $this->license->getLicenseByIdentifier('AGPL-1.0');
|
||||||
|
$this->assertEquals($license[0], 'Affero General Public License v1.0'); // fullname
|
||||||
|
$this->assertFalse($license[1]); // osi approved
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetIdentifierByName()
|
||||||
|
{
|
||||||
|
$identifier = $this->license->getIdentifierByName('Affero General Public License v1.0');
|
||||||
|
$this->assertEquals($identifier, 'AGPL-1.0');
|
||||||
|
|
||||||
|
$identifier = $this->license->getIdentifierByName('BSD 2-clause "Simplified" License');
|
||||||
|
$this->assertEquals($identifier, 'BSD-2-Clause');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsOsiApprovedByIdentifier()
|
||||||
|
{
|
||||||
|
$osiApproved = $this->license->isOsiApprovedByIdentifier('MIT');
|
||||||
|
$this->assertTrue($osiApproved);
|
||||||
|
|
||||||
|
$osiApproved = $this->license->isOsiApprovedByIdentifier('AGPL-1.0');
|
||||||
|
$this->assertFalse($osiApproved);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue