summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/fetch2
diff options
context:
space:
mode:
authorPaul Eggleton <paul.eggleton@linux.intel.com>2016-03-09 17:22:13 +1300
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-03-09 22:45:16 +0000
commitfbf27c4c5dae4837f399901c6afb4ffb25098e46 (patch)
treee003e218a60075065813f0ade2812a2e94ae6db1 /bitbake/lib/bb/fetch2
parentef6a4518e402c676a1de21990f1decd673c4cace (diff)
downloadpoky-fbf27c4c5dae4837f399901c6afb4ffb25098e46.tar.gz
bitbake: fetch2/npm: properly handle npm dependencies
The 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. (Bitbake rev: 436d67fe7af89ecfbd11749a6ae1bc20e81f2cc8) Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/fetch2')
-rw-r--r--bitbake/lib/bb/fetch2/npm.py64
1 files changed, 45 insertions, 19 deletions
diff --git a/bitbake/lib/bb/fetch2/npm.py b/bitbake/lib/bb/fetch2/npm.py
index df27669c58..3ff11e9c27 100644
--- a/bitbake/lib/bb/fetch2/npm.py
+++ b/bitbake/lib/bb/fetch2/npm.py
@@ -142,36 +142,62 @@ class Npm(FetchMethod):
142 142
143 self._unpackdep(ud, ud.pkgname, workobj, "%s/npmpkg" % destdir, dldir, d) 143 self._unpackdep(ud, ud.pkgname, workobj, "%s/npmpkg" % destdir, dldir, d)
144 144
145 def _getdependencies(self, pkg, data, version, d, ud): 145 def _parse_view(self, output):
146 '''
147 Parse the output of npm view --json; the last JSON result
148 is assumed to be the one that we're interested in.
149 '''
150 pdata = None
151 outdeps = {}
152 datalines = []
153 bracelevel = 0
154 for line in output.splitlines():
155 if bracelevel:
156 datalines.append(line)
157 elif '{' in line:
158 datalines = []
159 datalines.append(line)
160 bracelevel = bracelevel + line.count('{') - line.count('}')
161 if datalines:
162 pdata = json.loads('\n'.join(datalines))
163 return pdata
164
165 def _getdependencies(self, pkg, data, version, d, ud, optional=False):
146 pkgfullname = pkg 166 pkgfullname = pkg
147 if version != '*' and not '/' in version: 167 if version != '*' and not '/' in version:
148 pkgfullname += "@'%s'" % version 168 pkgfullname += "@'%s'" % version
149 logger.debug(2, "Calling getdeps on %s" % pkg) 169 logger.debug(2, "Calling getdeps on %s" % pkg)
150 fetchcmd = "npm view %s dist.tarball --registry %s" % (pkgfullname, ud.registry) 170 fetchcmd = "npm view %s --json --registry %s" % (pkgfullname, ud.registry)
151 output = runfetchcmd(fetchcmd, d, True) 171 output = runfetchcmd(fetchcmd, d, True)
152 # npm may resolve multiple versions 172 pdata = self._parse_view(output)
153 outputarray = output.strip().splitlines() 173 if not pdata:
154 if not outputarray:
155 raise FetchError("The command '%s' returned no output" % fetchcmd) 174 raise FetchError("The command '%s' returned no output" % fetchcmd)
156 # we just take the latest version npm resolved 175 if optional:
176 pkg_os = pdata.get('os', None)
177 if pkg_os:
178 if not isinstance(pkg_os, list):
179 pkg_os = [pkg_os]
180 if 'linux' not in pkg_os or '!linux' in pkg_os:
181 logger.debug(2, "Skipping %s since it's incompatible with Linux" % pkg)
182 return
157 #logger.debug(2, "Output URL is %s - %s - %s" % (ud.basepath, ud.basename, ud.localfile)) 183 #logger.debug(2, "Output URL is %s - %s - %s" % (ud.basepath, ud.basename, ud.localfile))
158 outputurl = outputarray[len(outputarray)-1].rstrip() 184 outputurl = pdata['dist']['tarball']
159 if (len(outputarray) > 1):
160 # remove the preceding version/name from npm output and then the
161 # first and last quotes
162 outputurl = outputurl.split(" ")[1][1:-1]
163 data[pkg] = {} 185 data[pkg] = {}
164 data[pkg]['tgz'] = os.path.basename(outputurl) 186 data[pkg]['tgz'] = os.path.basename(outputurl)
165 self._runwget(ud, d, "%s %s" % (self.basecmd, outputurl), False) 187 self._runwget(ud, d, "%s %s" % (self.basecmd, outputurl), False)
166 #fetchcmd = "npm view %s@%s dependencies --json" % (pkg, version) 188
167 fetchcmd = "npm view %s dependencies --json --registry %s" % (pkgfullname, ud.registry) 189 dependencies = pdata.get('dependencies', {})
168 output = runfetchcmd(fetchcmd, d, True) 190 optionalDependencies = pdata.get('optionalDependencies', {})
169 try: 191 depsfound = {}
170 depsfound = json.loads(output) 192 optdepsfound = {}
171 except:
172 # just assume there is no deps to be loaded here
173 return
174 data[pkg]['deps'] = {} 193 data[pkg]['deps'] = {}
194 for dep in dependencies:
195 if dep in optionalDependencies:
196 optdepsfound[dep] = dependencies[dep]
197 else:
198 depsfound[dep] = dependencies[dep]
199 for dep, version in optdepsfound.iteritems():
200 self._getdependencies(dep, data[pkg]['deps'], version, d, ud, optional=True)
175 for dep, version in depsfound.iteritems(): 201 for dep, version in depsfound.iteritems():
176 self._getdependencies(dep, data[pkg]['deps'], version, d, ud) 202 self._getdependencies(dep, data[pkg]['deps'], version, d, ud)
177 203