npm.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. BitBake 'Fetch' NPM implementation
  5. The NPM fetcher is used to retrieve files from the npmjs repository
  6. Usage in the recipe:
  7. SRC_URI = "npm://registry.npmjs.org/;name=${PN};version=${PV}"
  8. Suported SRC_URI options are:
  9. - name
  10. - version
  11. npm://registry.npmjs.org/${PN}/-/${PN}-${PV}.tgz would become npm://registry.npmjs.org;name=${PN};version=${PV}
  12. The fetcher all triggers off the existence of ud.localpath. If that exists and has the ".done" stamp, its assumed the fetch is good/done
  13. """
  14. import os
  15. import sys
  16. import urllib.request, urllib.parse, urllib.error
  17. import json
  18. import subprocess
  19. import signal
  20. import bb
  21. from bb.fetch2 import FetchMethod
  22. from bb.fetch2 import FetchError
  23. from bb.fetch2 import ChecksumError
  24. from bb.fetch2 import runfetchcmd
  25. from bb.fetch2 import logger
  26. from bb.fetch2 import UnpackError
  27. from bb.fetch2 import ParameterError
  28. from distutils import spawn
  29. def subprocess_setup():
  30. # Python installs a SIGPIPE handler by default. This is usually not what
  31. # non-Python subprocesses expect.
  32. # SIGPIPE errors are known issues with gzip/bash
  33. signal.signal(signal.SIGPIPE, signal.SIG_DFL)
  34. class Npm(FetchMethod):
  35. """Class to fetch urls via 'npm'"""
  36. def init(self, d):
  37. pass
  38. def supports(self, ud, d):
  39. """
  40. Check to see if a given url can be fetched with npm
  41. """
  42. return ud.type in ['npm']
  43. def debug(self, msg):
  44. logger.debug(1, "NpmFetch: %s", msg)
  45. def clean(self, ud, d):
  46. logger.debug(2, "Calling cleanup %s" % ud.pkgname)
  47. bb.utils.remove(ud.localpath, False)
  48. bb.utils.remove(ud.pkgdatadir, True)
  49. bb.utils.remove(ud.fullmirror, False)
  50. def urldata_init(self, ud, d):
  51. """
  52. init NPM specific variable within url data
  53. """
  54. if 'downloadfilename' in ud.parm:
  55. ud.basename = ud.parm['downloadfilename']
  56. else:
  57. ud.basename = os.path.basename(ud.path)
  58. # can't call it ud.name otherwise fetcher base class will start doing sha1stuff
  59. # TODO: find a way to get an sha1/sha256 manifest of pkg & all deps
  60. ud.pkgname = ud.parm.get("name", None)
  61. if not ud.pkgname:
  62. raise ParameterError("NPM fetcher requires a name parameter", ud.url)
  63. ud.version = ud.parm.get("version", None)
  64. if not ud.version:
  65. raise ParameterError("NPM fetcher requires a version parameter", ud.url)
  66. ud.bbnpmmanifest = "%s-%s.deps.json" % (ud.pkgname, ud.version)
  67. ud.bbnpmmanifest = ud.bbnpmmanifest.replace('/', '-')
  68. ud.registry = "http://%s" % (ud.url.replace('npm://', '', 1).split(';'))[0]
  69. prefixdir = "npm/%s" % ud.pkgname
  70. ud.pkgdatadir = d.expand("${DL_DIR}/%s" % prefixdir)
  71. if not os.path.exists(ud.pkgdatadir):
  72. bb.utils.mkdirhier(ud.pkgdatadir)
  73. ud.localpath = d.expand("${DL_DIR}/npm/%s" % ud.bbnpmmanifest)
  74. self.basecmd = d.getVar("FETCHCMD_wget") or "/usr/bin/env wget -O -t 2 -T 30 -nv --passive-ftp --no-check-certificate "
  75. ud.prefixdir = prefixdir
  76. ud.write_tarballs = ((d.getVar("BB_GENERATE_MIRROR_TARBALLS") or "0") != "0")
  77. mirrortarball = 'npm_%s-%s.tar.xz' % (ud.pkgname, ud.version)
  78. mirrortarball = ud.mirrortarball.replace('/', '-')
  79. ud.fullmirror = os.path.join(d.getVar("DL_DIR"), mirrortarball)
  80. ud.mirrortarballs = [mirrortarball]
  81. def need_update(self, ud, d):
  82. if os.path.exists(ud.localpath):
  83. return False
  84. return True
  85. def _runwget(self, ud, d, command, quiet):
  86. logger.debug(2, "Fetching %s using command '%s'" % (ud.url, command))
  87. bb.fetch2.check_network_access(d, command, ud.url)
  88. dldir = d.getVar("DL_DIR")
  89. runfetchcmd(command, d, quiet, workdir=dldir)
  90. def _unpackdep(self, ud, pkg, data, destdir, dldir, d):
  91. file = data[pkg]['tgz']
  92. logger.debug(2, "file to extract is %s" % file)
  93. if file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'):
  94. cmd = 'tar xz --strip 1 --no-same-owner --warning=no-unknown-keyword -f %s/%s' % (dldir, file)
  95. else:
  96. bb.fatal("NPM package %s downloaded not a tarball!" % file)
  97. # Change to subdir before executing command
  98. if not os.path.exists(destdir):
  99. os.makedirs(destdir)
  100. path = d.getVar('PATH')
  101. if path:
  102. cmd = "PATH=\"%s\" %s" % (path, cmd)
  103. bb.note("Unpacking %s to %s/" % (file, destdir))
  104. ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=destdir)
  105. if ret != 0:
  106. raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), ud.url)
  107. if 'deps' not in data[pkg]:
  108. return
  109. for dep in data[pkg]['deps']:
  110. self._unpackdep(ud, dep, data[pkg]['deps'], "%s/node_modules/%s" % (destdir, dep), dldir, d)
  111. def unpack(self, ud, destdir, d):
  112. dldir = d.getVar("DL_DIR")
  113. with open("%s/npm/%s" % (dldir, ud.bbnpmmanifest)) as datafile:
  114. workobj = json.load(datafile)
  115. dldir = "%s/%s" % (os.path.dirname(ud.localpath), ud.pkgname)
  116. if 'subdir' in ud.parm:
  117. unpackdir = '%s/%s' % (destdir, ud.parm.get('subdir'))
  118. else:
  119. unpackdir = '%s/npmpkg' % destdir
  120. self._unpackdep(ud, ud.pkgname, workobj, unpackdir, dldir, d)
  121. def _parse_view(self, output):
  122. '''
  123. Parse the output of npm view --json; the last JSON result
  124. is assumed to be the one that we're interested in.
  125. '''
  126. pdata = None
  127. outdeps = {}
  128. datalines = []
  129. bracelevel = 0
  130. for line in output.splitlines():
  131. if bracelevel:
  132. datalines.append(line)
  133. elif '{' in line:
  134. datalines = []
  135. datalines.append(line)
  136. bracelevel = bracelevel + line.count('{') - line.count('}')
  137. if datalines:
  138. pdata = json.loads('\n'.join(datalines))
  139. return pdata
  140. def _getdependencies(self, pkg, data, version, d, ud, optional=False, fetchedlist=None):
  141. if fetchedlist is None:
  142. fetchedlist = []
  143. pkgfullname = pkg
  144. if version != '*' and not '/' in version:
  145. pkgfullname += "@'%s'" % version
  146. logger.debug(2, "Calling getdeps on %s" % pkg)
  147. fetchcmd = "npm view %s --json --registry %s" % (pkgfullname, ud.registry)
  148. output = runfetchcmd(fetchcmd, d, True)
  149. pdata = self._parse_view(output)
  150. if not pdata:
  151. raise FetchError("The command '%s' returned no output" % fetchcmd)
  152. if optional:
  153. pkg_os = pdata.get('os', None)
  154. if pkg_os:
  155. if not isinstance(pkg_os, list):
  156. pkg_os = [pkg_os]
  157. blacklist = False
  158. for item in pkg_os:
  159. if item.startswith('!'):
  160. blacklist = True
  161. break
  162. if (not blacklist and 'linux' not in pkg_os) or '!linux' in pkg_os:
  163. logger.debug(2, "Skipping %s since it's incompatible with Linux" % pkg)
  164. return
  165. #logger.debug(2, "Output URL is %s - %s - %s" % (ud.basepath, ud.basename, ud.localfile))
  166. outputurl = pdata['dist']['tarball']
  167. data[pkg] = {}
  168. data[pkg]['tgz'] = os.path.basename(outputurl)
  169. if not outputurl in fetchedlist:
  170. self._runwget(ud, d, "%s --directory-prefix=%s %s" % (self.basecmd, ud.prefixdir, outputurl), False)
  171. fetchedlist.append(outputurl)
  172. dependencies = pdata.get('dependencies', {})
  173. optionalDependencies = pdata.get('optionalDependencies', {})
  174. dependencies.update(optionalDependencies)
  175. depsfound = {}
  176. optdepsfound = {}
  177. data[pkg]['deps'] = {}
  178. for dep in dependencies:
  179. if dep in optionalDependencies:
  180. optdepsfound[dep] = dependencies[dep]
  181. else:
  182. depsfound[dep] = dependencies[dep]
  183. for dep, version in optdepsfound.items():
  184. self._getdependencies(dep, data[pkg]['deps'], version, d, ud, optional=True, fetchedlist=fetchedlist)
  185. for dep, version in depsfound.items():
  186. self._getdependencies(dep, data[pkg]['deps'], version, d, ud, fetchedlist=fetchedlist)
  187. def _getshrinkeddependencies(self, pkg, data, version, d, ud, lockdown, manifest, toplevel=True):
  188. logger.debug(2, "NPM shrinkwrap file is %s" % data)
  189. if toplevel:
  190. name = data.get('name', None)
  191. if name and name != pkg:
  192. for obj in data.get('dependencies', []):
  193. if obj == pkg:
  194. self._getshrinkeddependencies(obj, data['dependencies'][obj], data['dependencies'][obj]['version'], d, ud, lockdown, manifest, False)
  195. return
  196. outputurl = "invalid"
  197. if ('resolved' not in data) or (not data['resolved'].startswith('http')):
  198. # will be the case for ${PN}
  199. fetchcmd = "npm view %s@%s dist.tarball --registry %s" % (pkg, version, ud.registry)
  200. logger.debug(2, "Found this matching URL: %s" % str(fetchcmd))
  201. outputurl = runfetchcmd(fetchcmd, d, True)
  202. else:
  203. outputurl = data['resolved']
  204. self._runwget(ud, d, "%s --directory-prefix=%s %s" % (self.basecmd, ud.prefixdir, outputurl), False)
  205. manifest[pkg] = {}
  206. manifest[pkg]['tgz'] = os.path.basename(outputurl).rstrip()
  207. manifest[pkg]['deps'] = {}
  208. if pkg in lockdown:
  209. sha1_expected = lockdown[pkg][version]
  210. sha1_data = bb.utils.sha1_file("npm/%s/%s" % (ud.pkgname, manifest[pkg]['tgz']))
  211. if sha1_expected != sha1_data:
  212. msg = "\nFile: '%s' has %s checksum %s when %s was expected" % (manifest[pkg]['tgz'], 'sha1', sha1_data, sha1_expected)
  213. raise ChecksumError('Checksum mismatch!%s' % msg)
  214. else:
  215. logger.debug(2, "No lockdown data for %s@%s" % (pkg, version))
  216. if 'dependencies' in data:
  217. for obj in data['dependencies']:
  218. logger.debug(2, "Found dep is %s" % str(obj))
  219. self._getshrinkeddependencies(obj, data['dependencies'][obj], data['dependencies'][obj]['version'], d, ud, lockdown, manifest[pkg]['deps'], False)
  220. def download(self, ud, d):
  221. """Fetch url"""
  222. jsondepobj = {}
  223. shrinkobj = {}
  224. lockdown = {}
  225. if not os.listdir(ud.pkgdatadir) and os.path.exists(ud.fullmirror):
  226. dest = d.getVar("DL_DIR")
  227. bb.utils.mkdirhier(dest)
  228. runfetchcmd("tar -xJf %s" % (ud.fullmirror), d, workdir=dest)
  229. return
  230. shwrf = d.getVar('NPM_SHRINKWRAP')
  231. logger.debug(2, "NPM shrinkwrap file is %s" % shwrf)
  232. if shwrf:
  233. try:
  234. with open(shwrf) as datafile:
  235. shrinkobj = json.load(datafile)
  236. except Exception as e:
  237. raise FetchError('Error loading NPM_SHRINKWRAP file "%s" for %s: %s' % (shwrf, ud.pkgname, str(e)))
  238. elif not ud.ignore_checksums:
  239. logger.warning('Missing shrinkwrap file in NPM_SHRINKWRAP for %s, this will lead to unreliable builds!' % ud.pkgname)
  240. lckdf = d.getVar('NPM_LOCKDOWN')
  241. logger.debug(2, "NPM lockdown file is %s" % lckdf)
  242. if lckdf:
  243. try:
  244. with open(lckdf) as datafile:
  245. lockdown = json.load(datafile)
  246. except Exception as e:
  247. raise FetchError('Error loading NPM_LOCKDOWN file "%s" for %s: %s' % (lckdf, ud.pkgname, str(e)))
  248. elif not ud.ignore_checksums:
  249. logger.warning('Missing lockdown file in NPM_LOCKDOWN for %s, this will lead to unreproducible builds!' % ud.pkgname)
  250. if ('name' not in shrinkobj):
  251. self._getdependencies(ud.pkgname, jsondepobj, ud.version, d, ud)
  252. else:
  253. self._getshrinkeddependencies(ud.pkgname, shrinkobj, ud.version, d, ud, lockdown, jsondepobj)
  254. with open(ud.localpath, 'w') as outfile:
  255. json.dump(jsondepobj, outfile)
  256. def build_mirror_data(self, ud, d):
  257. # Generate a mirror tarball if needed
  258. if ud.write_tarballs and not os.path.exists(ud.fullmirror):
  259. # it's possible that this symlink points to read-only filesystem with PREMIRROR
  260. if os.path.islink(ud.fullmirror):
  261. os.unlink(ud.fullmirror)
  262. dldir = d.getVar("DL_DIR")
  263. logger.info("Creating tarball of npm data")
  264. runfetchcmd("tar -cJf %s npm/%s npm/%s" % (ud.fullmirror, ud.bbnpmmanifest, ud.pkgname), d,
  265. workdir=dldir)
  266. runfetchcmd("touch %s.done" % (ud.fullmirror), d, workdir=dldir)