Skip to content

Instantly share code, notes, and snippets.

@KristoforMaynard
Last active June 28, 2018 19:55
Show Gist options
  • Save KristoforMaynard/d74999de6129dbb75bdecfb76bc3aa52 to your computer and use it in GitHub Desktop.
Save KristoforMaynard/d74999de6129dbb75bdecfb76bc3aa52 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Install a new package into an anaconda environment and copy new
entries in Anaconda's cache to ``./conda-cache/``. You can bring
``./conda-cache`` to an air-gapped computer and install the packages
using
$ conda install --offline --use-local --use-index-cache conda-cache/*
Note:
There are a couple gotchas about this method.
1. This script will only grab packages that were not already
in Anaconda's package cache. You really should only run
this script in a fresh anaconda install (NOT a fresh environment)
to make sure that you capture all dependancies.
2. This script must be run with the same archetecture and python
version (and probably the same Anaconda version) as the target
machine.
"""
from __future__ import print_function
import os
import shutil
import sys
import conda.cli
if len(sys.argv) == 1 or '-h' in sys.argv or '--help' in sys.argv:
print("Usage:\n"
"\n"
" $ python prepare_offline.py INSTALL_ARGS\n"
"\n"
"Args:\n"
" INSTALL_ARGS - arguments that are passed directly to\n"
" `conda install`. This will usually be\n"
" package names and channels.")
sys.exit(1)
conda_prefix = os.environ['CONDA_PREFIX']
conda_pkg_dir = os.path.join(conda_prefix, 'pkgs')
dest_dir = "conda-cache"
if os.path.isdir(dest_dir):
raise RuntimeError("destination dir '{0}' already exists".format(dest_dir))
else:
os.makedirs(dest_dir)
pkg_digest_old = set(os.listdir(conda_pkg_dir))
conda.cli.main('conda', 'install', *sys.argv[1:])
pkg_digest_new = set(os.listdir(conda_pkg_dir))
new_pkgs = [s for s in pkg_digest_new - pkg_digest_old if s.endswith('.tar.bz2')]
for pkg in new_pkgs:
_from = os.path.join(conda_pkg_dir, pkg)
_to = os.path.join(dest_dir, pkg)
print("Copying", _from, "->", _to)
shutil.copy(_from, _to)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment