def env_exists(name=None, prefix=None): """ Check if a conda environment exists. """ if not prefix: # search in default env dir command = "conda info -e | grep -e '^%(name)s\s'" % locals() else: # check if just a prefix or prefix & name are given: if name: base = prefix else: # we were given a full path to the environment. # we split this up into the parent directory + the environment path # so we can call 'conda info' with CONDA_ENVS_PATH set to the # parent dir prefix = utils.abspath(prefix) base, name = prefix, '' while name == '': base, name = os.path.split(base) command = "CONDA_ENVS_PATH=%(base)s conda info -e | grep -e '^%(name)s\s'" % locals( ) with settings(hide('running', 'warnings', 'stderr', 'stdout'), warn_only=True): res = run(command, shell_escape=False) return res.succeeded
def test_require_conda(): if conda.is_conda_installed(): prefix = conda.get_sysprefix() run('rm -rf ' + utils.abspath(prefix)) assert conda.is_conda_installed() == False require.conda.conda() assert conda.is_conda_installed()
def install(packages=None, yes=True, force=False, file=None, unknown=False, channels=None, override_channels=False, name=None, prefix=None, quiet=True): """ Install conda package(s). :param packages: package versions to install into conda environment :param yes: do not ask for confirmation :param force: force install (even when package already installed), implies --no-deps :param file: read package versions from FILE :param unknown: use index metadata from the local package cache (which are from unknown channels) :param channels: additional channel to search for packages. These are URLs searched in the order they are given (including file:// for local directories). Then, the defaults or channels from .condarc are searched (unless `override-channels` is given). You can use 'defaults' to get the default packages for conda, and 'system' to get the system packages, which also takes .condarc into account. You can also use any name and the .condarc channel_alias value will be prepended. The default channel_alias is http://conda.binstar.org/ :param override_channels: Do not search default or .condarc channels. Requires `channels` .True or False :param name: name of environment (in conda environment directory) :param prefix: full path to environment prefix :param quiet: do not display progress bar """ if isinstance(packages, basestring): packages = [packages] options = [] if override_channels: options.append('--override_channels') if name: options.append('--name ' + quote(name)) if prefix: options.append('--prefix ' + quote(utils.abspath(prefix))) if yes: options.append('--yes') if quiet: options.append('--quiet') if force: options.append('--force') if unknown: options.append('--unknown') if file: options.append('--file ' + quote(file)) for ch in channels or []: options.append('-c ' + quote(ch)) options.extend(packages or ['python']) options = ' '.join(options) command = 'conda install ' + options run(command)
def create_env(name=None, prefix=None, yes=True, override_channels=False, channels=None, packages=None, quiet=True, use_sudo=False, user=None): """ Create a conda environment. :param name: name of environment (in conda environment directory) :param prefix: full path to environment prefix :param yes: do not ask for confirmation :param quiet: do not display progress bar :param override_channels: Do not search default or .condarc channels. Requires `channels` .True or False :param channels: additional channel to search for packages. These are URLs searched in the order they are given (including file:// for local directories). Then, the defaults or channels from .condarc are searched (unless `override-channels` is given). You can use 'defaults' to get the default packages for conda, and 'system' to get the system packages, which also takes .condarc into account. You can also use any name and the .condarc channel_alias value will be prepended. The default channel_alias is http://conda.binstar.org/ :param packages: package versions to install into conda environment :param use_sudo: Use sudo :param user: sudo user :: import fabtools fabtools.conda.create_(path='/path/to/venv') """ options = [] if override_channels: options.append('--override_channels') if name: options.append('--name ' + quote(name)) if prefix: options.append('--prefix ' + quote(utils.abspath(prefix))) if yes: options.append('--yes') if quiet: options.append('--quiet') for ch in channels or []: options.append('-c ' + quote(ch)) options.extend(packages or ['python']) options = ' '.join(options) command = 'conda create ' + options if use_sudo: sudo(command, user=user) else: run(command)
def test_conda_install_and_check(): assert conda.is_conda_installed() == False conda.install_miniconda(keep_installer=True) assert conda.is_conda_installed() run('rm -rf miniconda') assert conda.is_conda_installed() == False conda.install_miniconda(prefix='~/myminiconda', keep_installer=True) assert conda.get_sysprefix() == utils.abspath('myminiconda') assert conda.is_conda_installed() run('rm -rf myminiconda') assert conda.is_conda_installed() == False conda.install_miniconda(keep_installer=True)
def nodeenv(directory, local=False): """ Context manager to activate an existing Python `virtual environment`_. :: from fabric.api import run from fabtools.python import nodeenv with nodeenv('/path/to/nodeenv'): run('python -V') .. _virtual environment: http://www.nodeenv.org/ """ # Build absolute path to the nodeenv activation script nodeenv_path = abspath(directory, local) activate_path = join(nodeenv_path, 'bin', 'activate') # Source the activation script with prefix('. %s' % quote(activate_path)): yield
def is_installed(package, name=None, prefix=None): """ Check if a conda package is installed. :param name: name of environment (in conda environment directory) :param prefix: full path to environment prefix """ options = [] if name: options.append('--name ' + quote(name)) if prefix: options.append('--prefix ' + quote(utils.abspath(prefix))) options = ' '.join(options) command = 'conda list %(options)s | grep -q %(package)s' % locals() with settings(hide('running', 'warnings', 'stderr', 'stdout'), warn_only=True): res = run(command) return res.succeeded
def env_exists(name=None, prefix=None): """ Check if a conda environment exists. """ if not prefix: # search in default env dir command = "conda info -e | grep -e '^%(name)s\s'" % locals() else: # check if just a prefix or prefix & name are given: if name: base = prefix else: # we were given a full path to the environment. # we split this up into the parent directory + the environment path # so we can call 'conda info' with CONDA_ENVS_PATH set to the # parent dir prefix = utils.abspath(prefix) base, name = prefix, '' while name == '': base, name = os.path.split(base) command = "CONDA_ENVS_PATH=%(base)s conda info -e | grep -e '^%(name)s\s'" % locals() with settings(hide('running', 'warnings', 'stderr', 'stdout'), warn_only=True): res = run(command, shell_escape=False) return res.succeeded
def virtualenv(directory, local=False): """ Context manager to activate an existing Python `virtual environment`_. :: from fabric.api import run from fabtools.python import virtualenv with virtualenv('/path/to/virtualenv'): run('python -V') .. _virtual environment: http://www.virtualenv.org/ """ path_mod = os.path if local else posixpath # Build absolute path to the virtualenv activation script venv_path = abspath(directory) activate_path = path_mod.join(venv_path, 'bin', 'activate') # Source the activation script with prefix('. %s' % quote(activate_path)): yield
def working_copy(source, target=None, version=None, update=True, force=False, use_sudo=False, user=None): """ Require a working copy of the repository from ``source``. If ``source`` is a URL to a remote branch, that branch will be cloned/pulled on the remote host. If ``source`` refers to a local branch, that branch will be pushed from local host to the remote. This requires Bazaar client to be installed on the local host. The ``target`` is optional, and defaults to the last segment of the source repository URL. If the ``target`` does not exist, this will clone the specified source branch into ``target`` path. If the ``target`` exists and ``update`` is ``True``, it will transfer changes from the source branch, then update the working copy. If the ``target`` exists and ``update`` is ``False``, nothing will be done. :param source: URL/path of the source branch :type source: str :param target: Absolute or relative path of the working copy on the filesystem. If this directory doesn't exist yet, a new working copy is created. If the directory does exist *and* ``update == True`` it will be updated. If ``target is None`` last segment of ``source`` is used. :type target: str :param version: Revision to check out / switch to :type version: str :param update: Whether or not to pull and update remote changesets. :type update: bool :param force: If ``True`` ignore differences and overwrite the ``target`` branch unconditionally, also create leading directories and the target branch even if remote directory already exists but is not a branch or working tree :type force: bool :param use_sudo: If ``True`` execute ``bzr`` with :func:`fabric.operations.sudo`, else with :func:`fabric.operations.run`. :type use_sudo: bool :param user: If ``use_sudo is True``, run :func:`fabric.operations.sudo` with the given user. If ``use_sudo is False`` this parameter has no effect. :type user: str """ command() suargs = dict(use_sudo=use_sudo, user=user) vfsargs = dict(version=version, force=force, **suargs) # vers, force, su before = None exists = False local_mods = False src_url = urlparse(source) def ensure_tree(dir): if not is_dir(posixpath.join(dir, '.bzr', 'checkout'), use_sudo=use_sudo): bazaar.checkout(dir, **suargs) if target is None: src_path = os.getcwd() if src_url.path == '.' else src_url.path target = src_path.split('/')[-1] if is_dir(target, use_sudo=use_sudo) and not update: puts(("Working tree '%s' already exists, " "not updating (update=False)") % target) return if is_dir(posixpath.join(target, '.bzr'), use_sudo=use_sudo): ensure_tree(target) before = bazaar.get_version(target) exists = True local_mods = bazaar.has_local_mods(target) if local_mods and force: bazaar.reset(target, **suargs) elif local_mods: abort(("Working tree '%s' has local modifications; " "use force=True to discard them") % target) if src_url.scheme in ('', 'file'): # local source target_url = 'bzr+ssh://%s/%s' % (env.host_string, utils.abspath(target)) bazaar.push(target_url, source=source, version=version, force=force) ensure_tree(target) bazaar.switch_version(target, version=version, **suargs) else: # remote source if exists: bazaar.pull(target, location=source, **vfsargs) bazaar.switch_version(target, version=version, **suargs) else: bazaar.clone(source, target, **vfsargs) after = bazaar.get_version(target) if before != after or local_mods: chg = 'created at revision' if before: mods = ' (with local modifications)' if local_mods else '' chg = 'changed from revision %s%s to' % (before, mods) puts(cyan('Working tree %r %s %s' % (target, chg, after))) else: puts(cyan('Working tree %r unchanged (no updates)' % target))
def working_copy(source, target=None, version=None, update=True, force=False, use_sudo=False, user=None): """ Require a working copy of the repository from ``source``. If ``source`` is a URL to a remote branch, that branch will be cloned/pulled on the remote host. If ``source`` refers to a local branch, that branch will be pushed from local host to the remote. This requires Bazaar client to be installed on the local host. The ``target`` is optional, and defaults to the last segment of the source repository URL. If the ``target`` does not exist, this will clone the specified source branch into ``target`` path. If the ``target`` exists and ``update`` is ``True``, it will transfer changes from the source branch, then update the working copy. If the ``target`` exists and ``update`` is ``False``, nothing will be done. :param source: URL/path of the source branch :type source: str :param target: Absolute or relative path of the working copy on the filesystem. If this directory doesn't exist yet, a new working copy is created. If the directory does exist *and* ``update == True`` it will be updated. If ``target is None`` last segment of ``source`` is used. :type target: str :param version: Revision to check out / switch to :type version: str :param update: Whether or not to pull and update remote changesets. :type update: bool :param force: If ``True`` ignore differences and overwrite the ``target`` branch unconditionally, also create leading directories and the target branch even if remote directory already exists but is not a branch or working tree :type force: bool :param use_sudo: If ``True`` execute ``bzr`` with :func:`fabric.operations.sudo`, else with :func:`fabric.operations.run`. :type use_sudo: bool :param user: If ``use_sudo is True``, run :func:`fabric.operations.sudo` with the given user. If ``use_sudo is False`` this parameter has no effect. :type user: str """ command() suargs = dict(use_sudo=use_sudo, user=user) vfsargs = dict(version=version, force=force, **suargs) # vers, force, su before = None exists = False local_mods = False src_url = urlparse(source) def ensure_tree(dir): if not is_dir(posixpath.join(dir, '.bzr', 'checkout'), use_sudo=use_sudo): bazaar.checkout(dir, **suargs) if target is None: src_path = os.getcwd() if src_url.path == '.' else src_url.path target = src_path.split('/')[-1] if is_dir(target, use_sudo=use_sudo) and not update: puts(("Working tree '%s' already exists, " "not updating (update=False)") % target) return if is_dir(posixpath.join(target, '.bzr'), use_sudo=use_sudo): ensure_tree(target) before = bazaar.get_version(target) exists = True local_mods = bazaar.has_local_mods(target) if local_mods and force: bazaar.reset(target, **suargs) elif local_mods: abort(("Working tree '%s' has local modifications; " "use force=True to discard them") % target) if src_url.scheme in ('', 'file'): # local source target_url = 'bzr+ssh://%s/%s' % ( env.host_string, utils.abspath(target)) bazaar.push(target_url, source=source, version=version, force=force) ensure_tree(target) bazaar.switch_version(target, version=version, **suargs) else: # remote source if exists: bazaar.pull(target, location=source, **vfsargs) bazaar.switch_version(target, version=version, **suargs) else: bazaar.clone(source, target, **vfsargs) after = bazaar.get_version(target) if before != after or local_mods: chg = 'created at revision' if before: mods = ' (with local modifications)' if local_mods else '' chg = 'changed from revision %s%s to' % (before, mods) puts(cyan('Working tree %r %s %s' % (target, chg, after))) else: puts(cyan('Working tree %r unchanged (no updates)' % target))