Some more npm fetcher fixes
cover Some more npm fetcher fixes
The following changes since commit 12f1fb8c9b70fea0c9145f881bcceb8af32df6af:
toasterui: fix warning 'Unknown event' (2016-03-07 17:21:07 +0000)
are available in the git repository at:
git://git.yoctoproject.org/poky-contrib paule/bb-npm-fixes2
http://git.yoctoproject.org/cgit.cgi/poky-contrib/log/?h=paule/bb-npm-fixes2
Paul Eggleton (3):
fetch2/npm: fix errors with some version specifications fetch2/npm: properly handle npm dependencies fetch2/npm: add missing URL argument to ParameterError
lib/bb/fetch2/npm.py | 70 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 22 deletions(-)
Signature · 1 line
2.5.0
PATCH 1/3 fetch2/npm: fix errors with some version specifications
"2 || 3" is a valid version specification for a dependency in an npm package.json file, but of course that looks like something else when sent to a shell. Quote the version value to avoid this.
--- lib/bb/fetch2/npm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bb/fetch2/npm.py +1 -1
@@ -145,7 +145,7 @@ class Npm(FetchMethod): def _getdependencies(self, pkg, data, version, d, ud): pkgfullname = pkg if version != '*' and not '/' in version: - pkgfullname += "@%s" % version + pkgfullname += "@'%s'" % version logger.debug(2, "Calling getdeps on %s" % pkg) fetchcmd = "npm view %s dist.tarball --registry %s" % (pkgfullname, ud.registry) output = runfetchcmd(fetchcmd, d, True)
Signature · 1 line
2.5.0
PATCH 2/3 fetch2/npm: properly handle npm dependencies
In reply to Paul EggletonThe output of "npm view dependencies" isn't entirely JSON if there are multiple results, but the code here was just discarding the output if the entire thing didn't parse as JSON. Split the output into lines and iterate over it, parsing JSON fragments as we find them; this way we end up with the last package's dependencies since it'll be last in the output.
Digging further, it seems that the dependencies field reported by "npm view" also includes optional dependencies. That wouldn't be a problem except some of these optional dependencies may be OS-specific; for example the "chokidar" module has "fsevents" in its optional dependencies, but fsevents only works on MacOS X (and is only needed there). If we erroneously pull in fsevents, not only is it unnecessary but it causes "npm shrinkwrap" to throw a tantrum. In the absence of a better approach, look at the os field and discard the module (along with any of its dependencies) if it isn't for Linux.
As part of this, we can reduce the calls to npm view to one per package since we get the entire json output rather than querying twice for two separate fields. Overall the time taken has probably increased since we are being more thorough about dependencies, but it's not quite as bad as it could have been.
--- lib/bb/fetch2/npm.py | 64 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 19 deletions(-)
lib/bb/fetch2/npm.py +45 -19
@@ -142,36 +142,62 @@ class Npm(FetchMethod): self._unpackdep(ud, ud.pkgname, workobj, "%s/npmpkg" % destdir, dldir, d) - def _getdependencies(self, pkg, data, version, d, ud): + def _parse_view(self, output): + ''' + Parse the output of npm view --json; the last JSON result + is assumed to be the one that we're interested in. + ''' + pdata = None + outdeps = {} + datalines = [] + bracelevel = 0 + for line in output.splitlines(): + if bracelevel: + datalines.append(line) + elif '{' in line: + datalines = [] + datalines.append(line) + bracelevel = bracelevel + line.count('{') - line.count('}') + if datalines: + pdata = json.loads('\n'.join(datalines)) + return pdata + + def _getdependencies(self, pkg, data, version, d, ud, optional=False): pkgfullname = pkg if version != '*' and not '/' in version: pkgfullname += "@'%s'" % version logger.debug(2, "Calling getdeps on %s" % pkg) - fetchcmd = "npm view %s dist.tarball --registry %s" % (pkgfullname, ud.registry) + fetchcmd = "npm view %s --json --registry %s" % (pkgfullname, ud.registry) output = runfetchcmd(fetchcmd, d, True) - # npm may resolve multiple versions - outputarray = output.strip().splitlines() - if not outputarray: + pdata = self._parse_view(output) + if not pdata: raise FetchError("The command '%s' returned no output" % fetchcmd) - # we just take the latest version npm resolved + if optional: + pkg_os = pdata.get('os', None) + if pkg_os: + if not isinstance(pkg_os, list): + pkg_os = [pkg_os] + if 'linux' not in pkg_os or '!linux' in pkg_os: + logger.debug(2, "Skipping %s since it's incompatible with Linux" % pkg) + return #logger.debug(2, "Output URL is %s - %s - %s" % (ud.basepath, ud.basename, ud.localfile)) - outputurl = outputarray[len(outputarray)-1].rstrip() - if (len(outputarray) > 1): - # remove the preceding version/name from npm output and then the - # first and last quotes - outputurl = outputurl.split(" ")[1][1:-1] + outputurl = pdata['dist']['tarball'] data[pkg] = {} data[pkg]['tgz'] = os.path.basename(outputurl) self._runwget(ud, d, "%s %s" % (self.basecmd, outputurl), False) - #fetchcmd = "npm view %s@%s dependencies --json" % (pkg, version) - fetchcmd = "npm view %s dependencies --json --registry %s" % (pkgfullname, ud.registry) - output = runfetchcmd(fetchcmd, d, True) - try: - depsfound = json.loads(output) - except: - # just assume there is no deps to be loaded here - return + + dependencies = pdata.get('dependencies', {}) + optionalDependencies = pdata.get('optionalDependencies', {}) + depsfound = {} + optdepsfound = {} data[pkg]['deps'] = {} + for dep in dependencies: + if dep in optionalDependencies: + optdepsfound[dep] = dependencies[dep] + else: + depsfound[dep] = dependencies[dep] + for dep, version in optdepsfound.iteritems(): + self._getdependencies(dep, data[pkg]['deps'], version, d, ud, optional=True) for dep, version in depsfound.iteritems(): self._getdependencies(dep, data[pkg]['deps'], version, d, ud)
Signature · 1 line
2.5.0
PATCH 3/3 fetch2/npm: add missing URL argument to ParameterError
In reply to Paul EggletonWithout this you get a rather odd traceback instead of the proper exception message.
--- lib/bb/fetch2/npm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/bb/fetch2/npm.py +2 -2
@@ -75,10 +75,10 @@ class Npm(FetchMethod): # TODO: find a way to get an sha1/sha256 manifest of pkg & all deps ud.pkgname = ud.parm.get("name", None) if not ud.pkgname: - raise ParameterError("NPM fetcher requires a name parameter") + raise ParameterError("NPM fetcher requires a name parameter", ud.url) ud.version = ud.parm.get("version", None) if not ud.version: - raise ParameterError("NPM fetcher requires a version parameter") + raise ParameterError("NPM fetcher requires a version parameter", ud.url) ud.bbnpmmanifest = "%s-%s.deps.json" % (ud.pkgname, ud.version) ud.registry = "http://%s" % ud.basename prefixdir = "npm/%s" % ud.pkgname
Signature · 1 line
2.5.0