create_npm.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Recipe creation tool - node.js NPM module support plugin
  2. #
  3. # Copyright (C) 2016 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. import os
  18. import logging
  19. import subprocess
  20. import tempfile
  21. import shutil
  22. import json
  23. from recipetool.create import RecipeHandler, split_pkg_licenses, handle_license_vars, check_npm
  24. logger = logging.getLogger('recipetool')
  25. tinfoil = None
  26. def tinfoil_init(instance):
  27. global tinfoil
  28. tinfoil = instance
  29. class NpmRecipeHandler(RecipeHandler):
  30. lockdownpath = None
  31. def _handle_license(self, data):
  32. '''
  33. Handle the license value from an npm package.json file
  34. '''
  35. license = None
  36. if 'license' in data:
  37. license = data['license']
  38. if isinstance(license, dict):
  39. license = license.get('type', None)
  40. return license
  41. def _shrinkwrap(self, srctree, localfilesdir, extravalues, lines_before):
  42. try:
  43. runenv = dict(os.environ, PATH=tinfoil.config_data.getVar('PATH'))
  44. bb.process.run('npm shrinkwrap', cwd=srctree, stderr=subprocess.STDOUT, env=runenv, shell=True)
  45. except bb.process.ExecutionError as e:
  46. logger.warn('npm shrinkwrap failed:\n%s' % e.stdout)
  47. return
  48. tmpfile = os.path.join(localfilesdir, 'npm-shrinkwrap.json')
  49. shutil.move(os.path.join(srctree, 'npm-shrinkwrap.json'), tmpfile)
  50. extravalues.setdefault('extrafiles', {})
  51. extravalues['extrafiles']['npm-shrinkwrap.json'] = tmpfile
  52. lines_before.append('NPM_SHRINKWRAP := "${THISDIR}/${PN}/npm-shrinkwrap.json"')
  53. def _lockdown(self, srctree, localfilesdir, extravalues, lines_before):
  54. runenv = dict(os.environ, PATH=tinfoil.config_data.getVar('PATH'))
  55. if not NpmRecipeHandler.lockdownpath:
  56. NpmRecipeHandler.lockdownpath = tempfile.mkdtemp('recipetool-npm-lockdown')
  57. bb.process.run('npm install lockdown --prefix %s' % NpmRecipeHandler.lockdownpath,
  58. cwd=srctree, stderr=subprocess.STDOUT, env=runenv, shell=True)
  59. relockbin = os.path.join(NpmRecipeHandler.lockdownpath, 'node_modules', 'lockdown', 'relock.js')
  60. if not os.path.exists(relockbin):
  61. logger.warn('Could not find relock.js within lockdown directory; skipping lockdown')
  62. return
  63. try:
  64. bb.process.run('node %s' % relockbin, cwd=srctree, stderr=subprocess.STDOUT, env=runenv, shell=True)
  65. except bb.process.ExecutionError as e:
  66. logger.warn('lockdown-relock failed:\n%s' % e.stdout)
  67. return
  68. tmpfile = os.path.join(localfilesdir, 'lockdown.json')
  69. shutil.move(os.path.join(srctree, 'lockdown.json'), tmpfile)
  70. extravalues.setdefault('extrafiles', {})
  71. extravalues['extrafiles']['lockdown.json'] = tmpfile
  72. lines_before.append('NPM_LOCKDOWN := "${THISDIR}/${PN}/lockdown.json"')
  73. def _handle_dependencies(self, d, deps, lines_before, srctree):
  74. import scriptutils
  75. # If this isn't a single module we need to get the dependencies
  76. # and add them to SRC_URI
  77. def varfunc(varname, origvalue, op, newlines):
  78. if varname == 'SRC_URI':
  79. if not origvalue.startswith('npm://'):
  80. src_uri = origvalue.split()
  81. changed = False
  82. for dep, depdata in deps.items():
  83. version = self.get_node_version(dep, depdata, d)
  84. if version:
  85. url = 'npm://registry.npmjs.org;name=%s;version=%s;subdir=node_modules/%s' % (dep, version, dep)
  86. scriptutils.fetch_uri(d, url, srctree)
  87. src_uri.append(url)
  88. changed = True
  89. if changed:
  90. return src_uri, None, -1, True
  91. return origvalue, None, 0, True
  92. updated, newlines = bb.utils.edit_metadata(lines_before, ['SRC_URI'], varfunc)
  93. if updated:
  94. del lines_before[:]
  95. for line in newlines:
  96. # Hack to avoid newlines that edit_metadata inserts
  97. if line.endswith('\n'):
  98. line = line[:-1]
  99. lines_before.append(line)
  100. return updated
  101. def _replace_license_vars(self, srctree, lines_before, handled, extravalues, d):
  102. for item in handled:
  103. if isinstance(item, tuple):
  104. if item[0] == 'license':
  105. del item
  106. break
  107. calledvars = []
  108. def varfunc(varname, origvalue, op, newlines):
  109. if varname in ['LICENSE', 'LIC_FILES_CHKSUM']:
  110. for i, e in enumerate(reversed(newlines)):
  111. if not e.startswith('#'):
  112. stop = i
  113. while stop > 0:
  114. newlines.pop()
  115. stop -= 1
  116. break
  117. calledvars.append(varname)
  118. if len(calledvars) > 1:
  119. # The second time around, put the new license text in
  120. insertpos = len(newlines)
  121. handle_license_vars(srctree, newlines, handled, extravalues, d)
  122. return None, None, 0, True
  123. return origvalue, None, 0, True
  124. updated, newlines = bb.utils.edit_metadata(lines_before, ['LICENSE', 'LIC_FILES_CHKSUM'], varfunc)
  125. if updated:
  126. del lines_before[:]
  127. lines_before.extend(newlines)
  128. else:
  129. raise Exception('Did not find license variables')
  130. def process(self, srctree, classes, lines_before, lines_after, handled, extravalues):
  131. import bb.utils
  132. import oe
  133. from collections import OrderedDict
  134. if 'buildsystem' in handled:
  135. return False
  136. def read_package_json(fn):
  137. with open(fn, 'r', errors='surrogateescape') as f:
  138. return json.loads(f.read())
  139. files = RecipeHandler.checkfiles(srctree, ['package.json'])
  140. if files:
  141. check_npm(tinfoil.config_data)
  142. data = read_package_json(files[0])
  143. if 'name' in data and 'version' in data:
  144. extravalues['PN'] = data['name']
  145. extravalues['PV'] = data['version']
  146. classes.append('npm')
  147. handled.append('buildsystem')
  148. if 'description' in data:
  149. extravalues['SUMMARY'] = data['description']
  150. if 'homepage' in data:
  151. extravalues['HOMEPAGE'] = data['homepage']
  152. deps = data.get('dependencies', {})
  153. updated = self._handle_dependencies(tinfoil.config_data, deps, lines_before, srctree)
  154. if updated:
  155. # We need to redo the license stuff
  156. self._replace_license_vars(srctree, lines_before, handled, extravalues, tinfoil.config_data)
  157. # Shrinkwrap
  158. localfilesdir = tempfile.mkdtemp(prefix='recipetool-npm')
  159. self._shrinkwrap(srctree, localfilesdir, extravalues, lines_before)
  160. # Lockdown
  161. self._lockdown(srctree, localfilesdir, extravalues, lines_before)
  162. # Split each npm module out to is own package
  163. npmpackages = oe.package.npm_split_package_dirs(srctree)
  164. for item in handled:
  165. if isinstance(item, tuple):
  166. if item[0] == 'license':
  167. licvalues = item[1]
  168. break
  169. if licvalues:
  170. # Augment the license list with information we have in the packages
  171. licenses = {}
  172. license = self._handle_license(data)
  173. if license:
  174. licenses['${PN}'] = license
  175. for pkgname, pkgitem in npmpackages.items():
  176. _, pdata = pkgitem
  177. license = self._handle_license(pdata)
  178. if license:
  179. licenses[pkgname] = license
  180. # Now write out the package-specific license values
  181. # We need to strip out the json data dicts for this since split_pkg_licenses
  182. # isn't expecting it
  183. packages = OrderedDict((x,y[0]) for x,y in npmpackages.items())
  184. packages['${PN}'] = ''
  185. pkglicenses = split_pkg_licenses(licvalues, packages, lines_after, licenses)
  186. all_licenses = list(set([item for pkglicense in pkglicenses.values() for item in pkglicense]))
  187. # Go back and update the LICENSE value since we have a bit more
  188. # information than when that was written out (and we know all apply
  189. # vs. there being a choice, so we can join them with &)
  190. for i, line in enumerate(lines_before):
  191. if line.startswith('LICENSE = '):
  192. lines_before[i] = 'LICENSE = "%s"' % ' & '.join(all_licenses)
  193. break
  194. # Need to move S setting after inherit npm
  195. for i, line in enumerate(lines_before):
  196. if line.startswith('S ='):
  197. lines_before.pop(i)
  198. lines_after.insert(0, '# Must be set after inherit npm since that itself sets S')
  199. lines_after.insert(1, line)
  200. break
  201. return True
  202. return False
  203. # FIXME this is duplicated from lib/bb/fetch2/npm.py
  204. def _parse_view(self, output):
  205. '''
  206. Parse the output of npm view --json; the last JSON result
  207. is assumed to be the one that we're interested in.
  208. '''
  209. pdata = None
  210. outdeps = {}
  211. datalines = []
  212. bracelevel = 0
  213. for line in output.splitlines():
  214. if bracelevel:
  215. datalines.append(line)
  216. elif '{' in line:
  217. datalines = []
  218. datalines.append(line)
  219. bracelevel = bracelevel + line.count('{') - line.count('}')
  220. if datalines:
  221. pdata = json.loads('\n'.join(datalines))
  222. return pdata
  223. # FIXME this is effectively duplicated from lib/bb/fetch2/npm.py
  224. # (split out from _getdependencies())
  225. def get_node_version(self, pkg, version, d):
  226. import bb.fetch2
  227. pkgfullname = pkg
  228. if version != '*' and not '/' in version:
  229. pkgfullname += "@'%s'" % version
  230. logger.debug(2, "Calling getdeps on %s" % pkg)
  231. runenv = dict(os.environ, PATH=d.getVar('PATH'))
  232. fetchcmd = "npm view %s --json" % pkgfullname
  233. output, _ = bb.process.run(fetchcmd, stderr=subprocess.STDOUT, env=runenv, shell=True)
  234. data = self._parse_view(output)
  235. return data.get('version', None)
  236. def register_recipe_handlers(handlers):
  237. handlers.append((NpmRecipeHandler(), 60))