def pip_update(**pip_package_specs): """Updates pip packages in their respective conda environments. Keyword arguments: **pip_package_specs -- The key is the name of the environment, and the value is an iterable of the pip package names in that environment you want to update. Example usage: pip_package_specs = {'conda_env1':('autobahn','six','txaio',), 'conda_env2':('pika',)} pip_update(**pip_package_specs) This will update autobahn, six, and txaio in the conda environment 'conda_env1', and pika in the environment 'conda_env2'. """ if pip_package_specs: conda_api.set_root_prefix(get_root_prefix()) for env, packages in pip_package_specs.items(): pip_args = ['install', '-U'] pip_args.extend(packages) # Equivalent of running 'pip install -U package1 package2 ...', # but runs it inside the appropriate conda environment. p = conda_api.process( name=env, cmd='pip', args=pip_args, stdout=subprocess.PIPE ) stdout, _ = p.communicate() print('Pip update result from environment {0}:\n{1}'.format(env, stdout))
def main(prefix, name): # Set prefix conda.set_root_prefix(prefix=prefix) # Remove environment conda.remove_environment(name=name)
def main(prefix, yml, name): # Set prefix conda.set_root_prefix(prefix=prefix) # Create new environment conda.export_yml(yml=yml, name=name)
def main(prefix): # Set prefix conda.set_root_prefix(prefix=prefix) # print general conda info print(json.dumps(conda.info(), cls=SetEncoder))
def main(prefix, newEnv, cloneEnv): # Set prefix conda.set_root_prefix(prefix=prefix) # Fix naming (if needed) name_new = ''.join(e for e in newEnv if e.isalnum() or e in "_.") # Create new environment conda.clone_environment(name=name_new, clone=cloneEnv)
def main(prefix, yml): # Set prefix conda.set_root_prefix(prefix = prefix) # Verify the env name does already exist (TODO) #txt = open("/Users/tbattaglia/environment.yml") #yml_name = txt.readline().replace("name: ", "") # Create new environment conda.create_yml(yml = yml)
def main(prefix): # Set root prefix conda.set_root_prefix(prefix = prefix) # Create a dictionary of Location and Prefix for each env env = [{'location': prefix, 'version' : get_version(prefix), 'prefix': path.basename(prefix), 'prefix_sanitized': sanitize(path.basename(prefix)), 'packages' : get_packages(prefix)} for prefix in conda.get_envs()] # Print for nodejs print(json.dumps(env, cls = SetEncoder))
def main(prefix, name, version, pkgs): # Set prefix conda.set_root_prefix(prefix = prefix) # Remove special characters from environment name name_new = ''.join(e for e in name if e.isalnum() or e in "_.") # Split list into string pkgsplit = pkgs.split(",") pythonversion = "python=" + str(version) final = pkgsplit + [pythonversion] # Create new environment conda.create(name = name_new, pkgs = final)
def remove_package_with_dependencies(environment, package): # Before we do anything, set the ROOT_PREFIX # variable so conda_api knows where to work from. conda_api.set_root_prefix(get_root_prefix()) # Set the name of the temporary conda environment # we'll work in. temp_env = 'temp_env' # Create the temporary environment in which to do our install test. conda_api.create(temp_env, pkgs=['python']) # Do a fake install of the package we want to remove, # in order to get its "dependencies". install_cmd = shlex.split('{0} install -n {1} {2} --json --dry-run' .format(os.path.join(conda_api.ROOT_PREFIX, 'bin/conda'), temp_env, package)) p = subprocess.Popen(install_cmd, stdout=subprocess.PIPE) stdout, _ = p.communicate() install_response = json.loads(stdout) # Get the dependencies from the JSON that was written # by the conda install command above. regex = re.compile('\S*(?=-[0-9]+)') packages_to_remove = [regex.match(item.split(' ')[0]).group() for item in install_response['actions']['LINK']] # Remove these packages from the actual target environment. remove_response = conda_api.remove(*packages_to_remove, name=environment) print(remove_response) # The below condition should always evaluate to True. # We can expect the above call to conda_api.remove() to throw # a CondaError error if something goes wrong. if remove_response['success']: print('\nThe following packages were successfully removed from {0}:\n\n{1}\n' .format(conda_api.get_prefix_envname(environment), '\n'.join(packages_to_remove))) # Cleanup - delete the temporary conda env. conda_api.remove_environment(temp_env)
def update_all(update_root=True, *blacklist_envs): """Updates all conda packages in all installed conda environments. Required arguments: update_root -- A Boolean flag that specifies whether the root conda environment should be updated (default True). Optional arguments: *blacklist_envs -- Names of environments you don't want updated. Example usage: update_all(True, 'special_env1', 'special_env2') This will update all conda environments (including root) but excluding special_env1 and special_env2. """ # Before we do anything, set the ROOT_PREFIX # variable so conda_api knows where to work from. conda_api.set_root_prefix(get_root_prefix()) # Get all active environments, excluding the ones in the blacklist. # The root environment will be the first element in this list, # so exclude that also. envs = [ os.path.basename(env) for env in conda_api.get_envs() if os.path.basename(env) not in blacklist_envs ][1:] print('ROOT_PREFIX is set to: {0}'.format(conda_api.ROOT_PREFIX)) if update_root: root_update_result = conda_api.update(use_local=True, all=True, env='base') print('Result from environment root:\n{0}'.format(root_update_result)) for env_name in envs: # Update all packages in the environment. env_update_result = conda_api.update(env=env_name, all=True) print('Result from environment {0}:\n{1}'.format(env_name, env_update_result))
import sys from os.path import expanduser import conda_api assert (conda_api.split_canonical_name('redis-py-2.4.3-py27_0') == ('redis-py', '2.4.3', 'py27_0')) print('conda_api.__version__: %s' % conda_api.__version__) if sys.platform.startswith('win32'): root_prefix = r'C:\Python27' else: root_prefix = expanduser('~/minonda') conda_api.set_root_prefix(root_prefix) conda_api.test() assert conda_api.__version__ == '1.2.1'
from subprocess import PIPE, Popen from Queue import Empty from IPython.nbformat.current import reads_json as nb_read_json, new_text_cell, new_notebook, new_worksheet from runipy.notebook_runner import NotebookRunner, NotebookError from ipyapp.slugify import slugify from ipyapp.config import MODE, FORMAT, TIMEOUT, FIXED_DEPS, LOG_LEVEL logging.basicConfig(level=LOG_LEVEL) log = logging.getLogger(__name__) try: import conda_api conda_api.set_root_prefix() CONDA_API_AVAILABLE = True except ImportError: log.warn('conda_api package not available, so app dependencies will be ignored') CONDA_API_AVAILABLE = False class NotebookAppError(Exception): " General Notebook App error " pass class NotebookAppExecutionError(NotebookAppError): " Notebook App error during execution " pass class NotebookAppFormatError(NotebookAppError): " Notebook Apps need to be JSON and contain app meta-data"
def test_ROOT_PREFIX_gets_set_correctly(self): conda_api.set_root_prefix(get_root_prefix()) self.assertEqual(conda_api.ROOT_PREFIX, '/home/josh/miniconda')
def setUpClass(self): conda_api.set_root_prefix() self.prefix = tempfile.mkdtemp() handle, fpath = tempfile.mkstemp() self.config = fpath
def setUpClass(self): conda_api.set_root_prefix() self.prefix = tempfile.mkdtemp() handle, fpath = tempfile.mkstemp() self.config = fpath