From 3bc653e69184148910468b3b1b7249c0bd222667 Mon Sep 17 00:00:00 2001 From: Dylan Eustice Date: Wed, 17 Dec 2025 12:45:25 -0500 Subject: [PATCH] Allow pip to take arguments coming after install Signed-off-by: Dylan Eustice Replace index_url with more general install_args There could be other arguments that need to be passed to 'pip install' that must come *after* the install. Include all of them. Signed-off-by: Dylan Eustice Add unit test for pip install args Signed-off-by: Dylan Eustice Update docs Switch unit test to single arg Signed-off-by: Dylan Eustice --- docs/building_blocks.md | 3 +++ hpccm/building_blocks/pip.py | 7 +++++++ test/test_pip.py | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/docs/building_blocks.md b/docs/building_blocks.md index a36694f..f776140 100644 --- a/docs/building_blocks.md +++ b/docs/building_blocks.md @@ -3911,6 +3911,9 @@ empty. upgraded prior to installing any PyPi packages. The default is False. +- __install_args__: List of arguments to pass to `pip install`. The +default is an empty list. Only applies to the `packages` parameter. + __Examples__ diff --git a/hpccm/building_blocks/pip.py b/hpccm/building_blocks/pip.py index 178b46c..d1a7176 100644 --- a/hpccm/building_blocks/pip.py +++ b/hpccm/building_blocks/pip.py @@ -66,6 +66,9 @@ class pip(bb_base, hpccm.templates.rm): upgraded prior to installing any PyPi packages. The default is False. + install_args: List of arguments to pass to `pip install`. The + default is an empty list. Only applies to the `packages` parameter. + # Examples ```python @@ -96,6 +99,7 @@ def __init__(self, **kwargs): self.__requirements = kwargs.get('requirements', None) self.__upgrade = kwargs.get('upgrade', False) self.__wd = kwargs.get('wd', hpccm.config.g_wd) # working directory + self.__install_args = kwargs.get('install_args', []) self.__debs = [] # Filled in below self.__rpms = [] # Filled in below @@ -170,4 +174,7 @@ def __instructions(self): quoted_packages = [shlex_quote(pkg) for pkg in self.__packages] cmds.append('{0} install {1}'.format(self.__pip, ' '.join(quoted_packages))) + if self.__install_args: + cmds[-1] += ' {0}'.format(' '.join(self.__install_args)) + self += shell(commands=cmds) diff --git a/test/test_pip.py b/test/test_pip.py index 1bc6498..d79f0ed 100644 --- a/test/test_pip.py +++ b/test/test_pip.py @@ -175,3 +175,13 @@ def test_package_with_version(self): self.assertEqual(str(p), r"""# pip RUN pip --no-cache-dir install 'hpccm>=1.0'""") + + @ubuntu + @docker + def test_install_args(self): + """install_args option""" + p = pip(ospackages=[], packages=['hpccm'], + install_args=['--index-url https://my-index.com']) + self.assertEqual(str(p), +r'''# pip +RUN pip --no-cache-dir install hpccm --index-url https://my-index.com''')