Message ID | 20250227223436.28224-2-eric.meyers@arthrex.com |
---|---|
State | Accepted, archived |
Commit | 5fa6137b6d98544766f3152b874e67d04fafb88f |
Headers | show |
Series | NPM Fetcher Private Registry Authentication Support: | expand |
On Thu, Feb 27, 2025 at 2:34 PM Eric Meyers <eric.meyers15310@gmail.com> wrote: > Signed-off-by: Eric Meyers <eric.meyers@arthrex.com> > Cc: Geoff Parker <geoffrey.parker@arthrex.com> > Cc: Chuck Wolber <chuckwolber@gmail.com> > --- > lib/bb/fetch2/npm.py | 21 +++++++++++++++++++++ > 1 file changed, 21 insertions(+) > > diff --git a/lib/bb/fetch2/npm.py b/lib/bb/fetch2/npm.py > index c09f05044..2c7ed5340 100644 > --- a/lib/bb/fetch2/npm.py > +++ b/lib/bb/fetch2/npm.py > @@ -91,6 +91,12 @@ class NpmEnvironment(object): > self.d = d > > self.user_config = tempfile.NamedTemporaryFile(mode="w", > buffering=1) > + > + hn = self._home_npmrc(d) > + if hn is not None: > + with open(hn, 'r') as hnf: > + self.user_config.write(hnf.read()) > + > for key, value in configs: > self.user_config.write("%s=%s\n" % (key, value)) > > @@ -103,6 +109,21 @@ class NpmEnvironment(object): > if self.user_config: > self.user_config.close() > > + def _home_npmrc(self, d): > + """Function to return user's HOME .npmrc file (or None if it > doesn't exist)""" > + home_npmrc_file = None > + if d.getVar("BB_USE_HOME_NPMRC") == "1": > + home_npmrc_file = os.path.join(os.environ.get("HOME"), > ".npmrc") > + if not os.path.exists(home_npmrc_file): > + home_npmrc_file = None > + bb.warn("BB_USE_HOME_NPMRC flag is set, but NO valid > .npmrc was detected - "\ > + "check that a valid .npmrc file exists in your > HOME directory.") > + else: > + bb.warn("BB_USE_HOME_NPMRC flag is set and valid .npmrc > detected - "\ > + "npm fetcher will now use the .npmrc file in the > HOME directory.") > + > + return home_npmrc_file > + > def run(self, cmd, args=None, configs=None, workdir=None): > """Run npm command in a controlled environment""" > with tempfile.TemporaryDirectory() as tmpdir: > Nice work! Only one remaining issue that I can see. I do not think you need to warn when there is no .npmrc present. That should enable you to simplify your logic a bit to something like this: home_npmrc_file = os.path.join(os.environ.get("HOME"), ".npmrc") if d.getVar("BB_USE_HOME_NPMRC") == "1" and os.path.exists(home_npmrc_file): bb.warn(f"BB_USE_HOME_NPMRC flag set and valid .npmrc detected - "\ "npm fetcher will use {home_npmrc_file}") return home_npmrc_file return None ..Ch:W..
diff --git a/lib/bb/fetch2/npm.py b/lib/bb/fetch2/npm.py index c09f05044..2c7ed5340 100644 --- a/lib/bb/fetch2/npm.py +++ b/lib/bb/fetch2/npm.py @@ -91,6 +91,12 @@ class NpmEnvironment(object): self.d = d self.user_config = tempfile.NamedTemporaryFile(mode="w", buffering=1) + + hn = self._home_npmrc(d) + if hn is not None: + with open(hn, 'r') as hnf: + self.user_config.write(hnf.read()) + for key, value in configs: self.user_config.write("%s=%s\n" % (key, value)) @@ -103,6 +109,21 @@ class NpmEnvironment(object): if self.user_config: self.user_config.close() + def _home_npmrc(self, d): + """Function to return user's HOME .npmrc file (or None if it doesn't exist)""" + home_npmrc_file = None + if d.getVar("BB_USE_HOME_NPMRC") == "1": + home_npmrc_file = os.path.join(os.environ.get("HOME"), ".npmrc") + if not os.path.exists(home_npmrc_file): + home_npmrc_file = None + bb.warn("BB_USE_HOME_NPMRC flag is set, but NO valid .npmrc was detected - "\ + "check that a valid .npmrc file exists in your HOME directory.") + else: + bb.warn("BB_USE_HOME_NPMRC flag is set and valid .npmrc detected - "\ + "npm fetcher will now use the .npmrc file in the HOME directory.") + + return home_npmrc_file + def run(self, cmd, args=None, configs=None, workdir=None): """Run npm command in a controlled environment""" with tempfile.TemporaryDirectory() as tmpdir:
Signed-off-by: Eric Meyers <eric.meyers@arthrex.com> Cc: Geoff Parker <geoffrey.parker@arthrex.com> Cc: Chuck Wolber <chuckwolber@gmail.com> --- lib/bb/fetch2/npm.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)