Skip to content

Commit 70423bc

Browse files
committed
Adding new arg --cache-only to autobuild install command.
Useful for preparing an environment for future offline use where packages can't be fetched.
1 parent 101d21d commit 70423bc

2 files changed

Lines changed: 63 additions & 7 deletions

File tree

autobuild/autobuild_tool_install.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def get_package_file(package_name, package_url, hash_algorithm='md5', expected_h
301301
return cache_file
302302

303303

304-
def do_install(packages, config_file, installed, platform, install_dir, dry_run, local_archives=[]):
304+
def do_install(packages, config_file, installed, platform, install_dir, dry_run, local_archives=[], cache_only=False):
305305
"""
306306
Install the specified list of packages. By default this will download the
307307
packages to the local cache, extract the contents of those
@@ -322,10 +322,12 @@ def do_install(packages, config_file, installed, platform, install_dir, dry_run,
322322

323323
# Existing tarball install, or new package install of either kind
324324
if pname in local_archives:
325-
if _install_local(pname, platform, package, local_archives[pname], install_dir, installed, dry_run):
326-
installed_pkgs.append(pname)
325+
if not cache_only:
326+
# local packages don't need to be placed in the cache
327+
if _install_local(pname, platform, package, local_archives[pname], install_dir, installed, dry_run):
328+
installed_pkgs.append(pname)
327329
else:
328-
if _install_binary(pname, platform, package, config_file, install_dir, installed, dry_run):
330+
if _install_binary(pname, platform, package, config_file, install_dir, installed, dry_run, cache_only=cache_only):
329331
installed_pkgs.append(pname)
330332
return installed_pkgs
331333

@@ -354,7 +356,7 @@ def _install_local(configured_name, platform, package, package_path, install_dir
354356
else:
355357
return False
356358

357-
def _install_binary(configured_name, platform, package, config_file, install_dir, installed, dry_run):
359+
def _install_binary(configured_name, platform, package, config_file, install_dir, installed, dry_run, cache_only=False):
358360
# Check that we have a platform-specific or common url to use.
359361
req_plat = package.get_platform(platform)
360362
package_name = getattr(package, 'name', '(undefined)')
@@ -387,6 +389,9 @@ def _install_binary(configured_name, platform, package, config_file, install_dir
387389
if cachefile is None:
388390
raise InstallError("Failed to download package '%s' from '%s'" % (package_name, archive.url))
389391

392+
if cache_only:
393+
return True
394+
390395
metadata, files = _install_common(configured_name, platform, package, cachefile, install_dir, installed, dry_run)
391396
if metadata:
392397
installed_package = package.copy()
@@ -761,9 +766,9 @@ def install_packages(args, config_file, install_dir, platform, packages):
761766

762767
# do the actual install of any new/updated packages
763768
packages = do_install(packages, config_file, installed, platform, install_dir,
764-
args.dry_run, local_archives=local_archives)
769+
args.dry_run, local_archives=local_archives, cache_only=args.cache_only)
765770

766-
if not args.dry_run:
771+
if not args.dry_run and not args.cache_only:
767772
# update the installed-packages.xml file
768773
try:
769774
# in case we got this far without ever having created installed_file's
@@ -886,6 +891,11 @@ def register(self, parser):
886891
help="install packages for a specific build configuration\n(may be specified as comma separated values in $AUTOBUILD_CONFIGURATION)",
887892
metavar='CONFIGURATION',
888893
default=self.configurations_from_environment())
894+
parser.add_argument('--cache-only',
895+
action='store_true',
896+
default=False,
897+
dest='cache_only',
898+
help="fetch remote pacakges and populate the cache but do not install.\nintended for use preparing environment for offline operation")
889899

890900
def run(self, args):
891901
platform=common.get_current_platform()

tests/test_install.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def __init__(self,
156156
addrsize=32,
157157
package=[],
158158
skip_source_environment=False,
159+
cache_only=False,
159160
):
160161
# Take all constructor params and assign as object attributes.
161162
params = locals().copy()
@@ -782,3 +783,48 @@ def test_download_github_without_creds(self, mock_urlopen: MagicMock):
782783
with envvar("AUTOBUILD_GITHUB_TOKEN", None):
783784
with self.assertRaises(CredentialsNotFoundError):
784785
autobuild_tool_install.download_package("https://example.org/foo.tar.bz2", creds="github")
786+
787+
# ------------------------------------- -------------------------------------
788+
class TestInstallCacheOnly(BaseTest):
789+
def setup_method(self, module):
790+
super(TestInstallCacheOnly, self).setup_method(module)
791+
792+
self.pkg = "bogus"
793+
# Set up options with cache_only flag
794+
self.options.package = [self.pkg]
795+
self.options.cache_only = True
796+
797+
# Ensure the package is not in the cache initially
798+
cache_file = os.path.join(common.get_install_cache_dir(), "bogus-0.1-common-111.tar.bz2")
799+
clean_file(cache_file)
800+
assert not os.path.exists(cache_file)
801+
802+
def test_cache_only_success(self):
803+
# Run install with --cache-only flag
804+
autobuild_tool_install.AutobuildTool().run(self.options)
805+
806+
# Verify package was downloaded to cache
807+
cache_file = os.path.join(common.get_install_cache_dir(), "bogus-0.1-common-111.tar.bz2")
808+
assert os.path.exists(cache_file)
809+
810+
# Verify package was NOT installed to install directory
811+
assert not os.path.exists(os.path.join(INSTALL_DIR, "lib", "bogus.lib"))
812+
assert not os.path.exists(os.path.join(INSTALL_DIR, "include", "bogus.h"))
813+
814+
# Verify package is not listed in installed manifest
815+
assert_not_in(self.pkg, query_manifest(self.options))
816+
817+
def test_cache_only_with_local_archives(self):
818+
# Set up options with cache_only flag and local archives
819+
cache_opts = self.options.copy()
820+
cache_opts.local_archives = [os.path.join(mydir, "data", "bogus-0.1-common-111.tar.bz2")]
821+
822+
# Run install with --cache-only flag
823+
autobuild_tool_install.AutobuildTool().run(cache_opts)
824+
825+
# Verify nothing was installed
826+
assert not os.path.exists(os.path.join(INSTALL_DIR, "lib", "bogus.lib"))
827+
assert not os.path.exists(os.path.join(INSTALL_DIR, "include", "bogus.h"))
828+
829+
# Verify package is not listed in installed manifest
830+
assert_not_in(self.pkg, query_manifest(self.options))

0 commit comments

Comments
 (0)