recipetool: create: split npm module dependencies into packages

Rather than rolling all of an npm module's dependencies into the same
package, split them into one module per package, setting the SUMMARY and
PKGV values from the package.json file for each package. Additionally,
mark each package with the appropriate license using the license
scanning we already do, falling back to the license stated in the
package.json file for the module if unknown. All of this is mostly in
aid of ensuring all modules and their licenses now show up in the
manifests for the image.

Additionally we set the main LICENSE value more concretely once we've
calculated the per-package licenses, since we have more information at
that point.

(From OE-Core rev: 8226805f83d21e7c1d2ba21969f3e8ee4b137496)

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Paul Eggleton
2016-03-09 17:48:52 +13:00
committed by Richard Purdie
parent d46827cfd3
commit 91455005b6
4 changed files with 134 additions and 3 deletions

View File

@@ -544,6 +544,7 @@ def create_recipe(args):
# Apply the handlers
handled = []
handled.append(('license', licvalues))
if args.binary:
classes.append('bin_package')
@@ -815,6 +816,33 @@ def guess_license(srctree):
return licenses
def split_pkg_licenses(licvalues, packages, outlines, fallback_licenses=None, pn='${PN}'):
"""
Given a list of (license, path, md5sum) as returned by guess_license(),
a dict of package name to path mappings, write out a set of
package-specific LICENSE values.
"""
pkglicenses = {pn: []}
for license, licpath, _ in licvalues:
for pkgname, pkgpath in packages.iteritems():
if licpath.startswith(pkgpath + '/'):
if pkgname in pkglicenses:
pkglicenses[pkgname].append(license)
else:
pkglicenses[pkgname] = [license]
break
else:
# Accumulate on the main package
pkglicenses[pn].append(license)
outlicenses = {}
for pkgname in packages:
license = ' '.join(list(set(pkglicenses.get(pkgname, ['Unknown']))))
if license == 'Unknown' and pkgname in fallback_licenses:
license = fallback_licenses[pkgname]
outlines.append('LICENSE_%s = "%s"' % (pkgname, license))
outlicenses[pkgname] = license.split()
return outlicenses
def read_pkgconfig_provides(d):
pkgdatadir = d.getVar('PKGDATA_DIR', True)
pkgmap = {}

View File

@@ -17,20 +17,37 @@
import logging
import json
from recipetool.create import RecipeHandler
from recipetool.create import RecipeHandler, split_pkg_licenses
logger = logging.getLogger('recipetool')
class NpmRecipeHandler(RecipeHandler):
def _handle_license(self, data):
'''
Handle the license value from an npm package.json file
'''
license = None
if 'license' in data:
license = data['license']
if isinstance(license, dict):
license = license.get('type', None)
return None
def process(self, srctree, classes, lines_before, lines_after, handled, extravalues):
import oe
from collections import OrderedDict
if 'buildsystem' in handled:
return False
def read_package_json(fn):
with open(fn, 'r') as f:
return json.loads(f.read())
files = RecipeHandler.checkfiles(srctree, ['package.json'])
if files:
with open(files[0], 'r') as f:
data = json.loads(f.read())
data = read_package_json(files[0])
if 'name' in data and 'version' in data:
extravalues['PN'] = data['name']
extravalues['PV'] = data['version']
@@ -40,6 +57,40 @@ class NpmRecipeHandler(RecipeHandler):
lines_before.append('SUMMARY = "%s"' % data['description'])
if 'homepage' in data:
lines_before.append('HOMEPAGE = "%s"' % data['homepage'])
# Split each npm module out to is own package
npmpackages = oe.package.npm_split_package_dirs(srctree)
for item in handled:
if isinstance(item, tuple):
if item[0] == 'license':
licvalues = item[1]
break
if licvalues:
# Augment the license list with information we have in the packages
licenses = {}
license = self._handle_license(data)
if license:
licenses['${PN}'] = license
for pkgname, pkgitem in npmpackages.iteritems():
_, pdata = pkgitem
license = self._handle_license(pdata)
if license:
licenses[pkgname] = license
# Now write out the package-specific license values
# We need to strip out the json data dicts for this since split_pkg_licenses
# isn't expecting it
packages = OrderedDict((x,y[0]) for x,y in npmpackages.iteritems())
packages['${PN}'] = ''
pkglicenses = split_pkg_licenses(licvalues, packages, lines_after, licenses)
all_licenses = list(set([item for pkglicense in pkglicenses.values() for item in pkglicense]))
# Go back and update the LICENSE value since we have a bit more
# information than when that was written out (and we know all apply
# vs. there being a choice, so we can join them with &)
for i, line in enumerate(lines_before):
if line.startswith('LICENSE = '):
lines_before[i] = 'LICENSE = "%s"' % ' & '.join(all_licenses)
break
return True
return False