aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>2021-10-06 16:35:53 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2021-10-08 16:44:31 +0100
commit2c2df49b06a2bad7a5b8872a9998338a4660498f (patch)
tree764e9930ff52cf48ee4f4972f398b7161efaa4a5
parent60cbd34d3da8f0f523281aad7eec93eec9cd4db8 (diff)
downloadbitbake-2c2df49b06a2bad7a5b8872a9998338a4660498f.tar.gz
fetch2: npm: Create config npmrc in environment instantiation
Create a configuration npmrc per npm environment to avoid repeated creation of the same configuration file. Create the file via python to avoid multiple npm config calls and add the ability to pass a file path instead of a temporary file. Deprecate the npm configs argument of the run function. The configs should be passed to npm environment or as command specific arguments. Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--lib/bb/fetch2/npm.py42
1 files changed, 27 insertions, 15 deletions
diff --git a/lib/bb/fetch2/npm.py b/lib/bb/fetch2/npm.py
index a39f1c199..e497c38dc 100644
--- a/lib/bb/fetch2/npm.py
+++ b/lib/bb/fetch2/npm.py
@@ -79,9 +79,25 @@ class NpmEnvironment(object):
Using a npm config file seems more reliable than using cli arguments.
This class allows to create a controlled environment for npm commands.
"""
- def __init__(self, d, configs=None):
+ def __init__(self, d, configs=None, npmrc=None):
self.d = d
- self.configs = configs
+
+ if configs:
+ self.user_config = tempfile.NamedTemporaryFile(mode="w", buffering=1)
+ self.user_config_name = self.user_config.name
+ for key, value in configs:
+ self.user_config.write("%s=%s\n" % (key, value))
+ else:
+ self.user_config_name = "/dev/null"
+
+ if npmrc:
+ self.global_config_name = npmrc
+ else:
+ self.global_config_name = "/dev/null"
+
+ def __del__(self):
+ if self.user_config:
+ self.user_config.close()
def run(self, cmd, args=None, configs=None, workdir=None):
"""Run npm command in a controlled environment"""
@@ -89,23 +105,19 @@ class NpmEnvironment(object):
d = bb.data.createCopy(self.d)
d.setVar("HOME", tmpdir)
- cfgfile = os.path.join(tmpdir, "npmrc")
-
if not workdir:
workdir = tmpdir
def _run(cmd):
- cmd = "NPM_CONFIG_USERCONFIG=%s " % cfgfile + cmd
- cmd = "NPM_CONFIG_GLOBALCONFIG=%s " % cfgfile + cmd
+ cmd = "NPM_CONFIG_USERCONFIG=%s " % (self.user_config_name) + cmd
+ cmd = "NPM_CONFIG_GLOBALCONFIG=%s " % (self.global_config_name) + cmd
return runfetchcmd(cmd, d, workdir=workdir)
- if self.configs:
- for key, value in self.configs:
- _run("npm config set %s %s" % (key, shlex.quote(value)))
-
if configs:
+ bb.warn("Use of configs argument of NpmEnvironment.run() function"
+ " is deprecated. Please use args argument instead.")
for key, value in configs:
- _run("npm config set %s %s" % (key, shlex.quote(value)))
+ cmd += " --%s=%s" % (key, shlex.quote(value))
if args:
for key, value in args:
@@ -167,14 +179,14 @@ class Npm(FetchMethod):
def _resolve_proxy_url(self, ud, d):
def _npm_view():
- configs = []
- configs.append(("json", "true"))
- configs.append(("registry", ud.registry))
+ args = []
+ args.append(("json", "true"))
+ args.append(("registry", ud.registry))
pkgver = shlex.quote(ud.package + "@" + ud.version)
cmd = ud.basecmd + " view %s" % pkgver
env = NpmEnvironment(d)
check_network_access(d, cmd, ud.registry)
- view_string = env.run(cmd, configs=configs)
+ view_string = env.run(cmd, args=args)
if not view_string:
raise FetchError("Unavailable package %s" % pkgver, ud.url)