def list_packages(prefix, regex=None, verbose=True): import re import conda.install as install pat = re.compile(regex, re.I) if regex else None if verbose: print('# packages in environment at %s:' % prefix) print('#') packages = False for dist in sorted(install.linked(prefix)): name = dist.rsplit('-', 2)[0] if pat and pat.search(name) is None: continue if not verbose: print(dist) continue try: info = install.is_linked(prefix, dist) features = set(info.get('features', '').split()) print('%-25s %-15s %15s %s' % (info['name'], info['version'], info['build'], common.disp_features(features)) ) except: # IOError, KeyError, ValueError print('%-25s %-15s %15s' % tuple(dist.rsplit('-', 2))) packages = True if not packages: # Be Unix-friendly sys.exit(1)
def remove_actions(prefix, specs, index, force=False, pinned=True): r = Resolve(index) linked = [d+'.tar.bz2' for d in install.linked(prefix)] mss = list(map(MatchSpec, specs)) if force: nlinked = {r.package_name(fn):fn[:-8] for fn in linked if not any(r.match(ms, fn) for ms in mss)} else: if config.track_features: specs.extend(x + '@' for x in config.track_features) nlinked = {r.package_name(fn):fn[:-8] for fn in r.remove(specs, linked)} if pinned: pinned_specs = get_pinned_specs(prefix) log.debug("Pinned specs=%s" % pinned_specs) linked = {r.package_name(fn):fn[:-8] for fn in linked} actions = ensure_linked_actions(r.dependency_sort(nlinked), prefix) for old_fn in reversed(r.dependency_sort(linked)): dist = old_fn + '.tar.bz2' name = r.package_name(dist) if old_fn == nlinked.get(name,''): continue if pinned and any(r.match(ms, dist) for ms in pinned_specs): raise RuntimeError( "Cannot remove %s because it is pinned. Use --no-pin to override." % dist) if name == 'conda' and name not in nlinked: if any(ms.name == 'conda' for ms in mss): sys.exit("Error: 'conda' cannot be removed from the root environment") else: sys.exit("Error: this 'remove' command cannot be executed because it\nwould require removing 'conda' dependencies.") add_unlink(actions, old_fn) return actions
def from_environment(name, prefix, no_builds=False, ignore_channels=False): """ Get environment object from prefix Args: name: The name of environment prefix: The path of prefix no_builds: Whether has build requirement ignore_channels: whether ingore_channels Returns: Environment obejct """ installed = install.linked(prefix, ignore_channels=ignore_channels) conda_pkgs = copy(installed) # json=True hides the output, data is added to installed add_pip_installed(prefix, installed, json=True) pip_pkgs = sorted(installed - conda_pkgs) if no_builds: dependencies = ['='.join(a.rsplit('-', 2)[0:2]) for a in sorted(conda_pkgs)] else: dependencies = ['='.join(a.rsplit('-', 2)) for a in sorted(conda_pkgs)] if len(pip_pkgs) > 0: dependencies.append({'pip': ['=='.join(a.rsplit('-', 2)[:2]) for a in pip_pkgs]}) # conda uses ruamel_yaml which returns a ruamel_yaml.comments.CommentedSeq # this doesn't dump correctly using pyyaml channels = context.channels return Environment(name=name, dependencies=dependencies, channels=channels, prefix=prefix)
def execute(args, parser): if not args.subcommand: parser.print_help() with Locked(config.croot): if args.subcommand == 'linkages': if not sys.platform.startswith('linux'): sys.exit("Error: conda inspect linkages is only implemented in Linux") prefix = get_prefix(args) installed = ci.linked(prefix) for pkg in args.packages: for dist in installed: if pkg == dist.rsplit('-', 2)[0]: break else: sys.exit("Package %s is not installed in %s" % (pkg, prefix)) linkages = get_package_linkages(dist, prefix) depmap = defaultdict(list) for binary in linkages: for lib, path in linkages[binary]: path = abspath(path) if path not in {'', 'not found'} else path if path.startswith(prefix): deps = list(which_package(path)) if len(deps) > 1: print("Warning: %s comes from multiple packages: %s" % (path, ' and '.join(deps)), file=sys.stderr) for d in deps: depmap[d].append((lib, path.split(prefix + '/', 1)[-1], binary)) elif path == 'not found': depmap['not found'].append((lib, path, binary)) else: depmap['system'].append((lib, path, binary)) print_linkages(depmap, show_files=args.show_files)
def remove_actions(prefix, specs, index=None, pinned=True): linked = install.linked(prefix) mss = [MatchSpec(spec) for spec in specs] if index: r = Resolve(index) else: r = None pinned_specs = get_pinned_specs(prefix) actions = defaultdict(list) actions[inst.PREFIX] = prefix for dist in sorted(linked): fn = dist + '.tar.bz2' if any(ms.match(fn) for ms in mss): if pinned and any(MatchSpec(spec).match('%s.tar.bz2' % dist) for spec in pinned_specs): raise RuntimeError("Cannot remove %s because it is pinned. Use --no-pin to override." % dist) actions[inst.UNLINK].append(dist) if r and fn in index and r.track_features(fn): features_actions = remove_features_actions(prefix, index, r.track_features(fn)) for action in features_actions: if isinstance(actions[action], list): for item in features_actions[action]: if item not in actions[action]: actions[action].append(item) else: assert actions[action] == features_actions[action] return actions
def warn_on_old_conda_build(index): root_linked = linked(cc.root_dir) vers_inst = [dist.rsplit("-", 2)[1] for dist in root_linked if dist.rsplit("-", 2)[0] == "conda-build"] if not len(vers_inst) == 1: print("WARNING: Could not detect installed version of conda-build", file=sys.stderr) return r = Resolve(index) try: pkgs = sorted(r.get_pkgs(MatchSpec("conda-build"))) except NoPackagesFound: print("WARNING: Could not find any versions of conda-build in the channels", file=sys.stderr) return if pkgs[-1].version != vers_inst[0]: print( """ WARNING: conda-build appears to be out of date. You have version %s but the latest version is %s. Run conda update -n root conda-build to get the latest version. """ % (vers_inst[0], pkgs[-1].version), file=sys.stderr, )
def print_packages(prefix, regex=None, format='human', piplist=False, json=False): if not isdir(prefix): common.error_and_exit("""\ Error: environment does not exist: %s # # Use 'conda create' to create an environment before listing its packages.""" % prefix, json=json, error_type="NoEnvironmentFound") if not json: if format == 'human': print('# packages in environment at %s:' % prefix) print('#') if format == 'export': print_export_header() installed = install.linked(prefix) if piplist and config.use_pip and format == 'human': add_pip_installed(prefix, installed, json=json) exitcode, output = list_packages(prefix, installed, regex, format=format) if not json: print('\n'.join(output)) else: common.stdout_json(output) return exitcode
def print_packages( prefix, regex=None, format="human", piplist=False, json=False, show_channel_urls=config.show_channel_urls ): if not isdir(prefix): common.error_and_exit( """\ Error: environment does not exist: %s # # Use 'conda create' to create an environment before listing its packages.""" % prefix, json=json, error_type="NoEnvironmentFound", ) if not json: if format == "human": print("# packages in environment at %s:" % prefix) print("#") if format == "export": print_export_header() installed = install.linked(prefix) if piplist and config.use_pip and format == "human": add_pip_installed(prefix, installed, json=json) exitcode, output = list_packages(prefix, installed, regex, format=format, show_channel_urls=show_channel_urls) if not json: print("\n".join(output)) else: common.stdout_json(output) return exitcode
def execute(args, parser): prefix = get_prefix(args) if not isdir(prefix): sys.exit("""\ Error: environment does not exist: %s # # Use 'conda create' to create the environment first. #""" % prefix) for package in linked(prefix): name, ver, _ = package .rsplit('-', 2) if name == 'python': py_ver = ver[:3] # x.y break else: raise RuntimeError("python is not installed in %s" % prefix) for path in args.source: pkg_path = abspath(expanduser(path)) if not args.no_pth_file: stdlib_dir = join(prefix, 'Lib' if sys.platform == 'win32' else 'lib/python%s' % py_ver) sp_dir = join(stdlib_dir, 'site-packages') # todo: # build the package if setup.py is found then invoke it with # build_ext --inplace - this only exists for extensions write_to_conda_pth(sp_dir, pkg_path) # go through the source looking for compiled extensions and make sure # they use the conda environment for loading libraries at runtime relink_sharedobjects(pkg_path, prefix) print("completed operation for: " + pkg_path)
def execute(args, parser): import sys import conda.plan as plan from conda.api import get_index from conda.cli import pscheck from conda.install import rm_rf, linked if not (args.all or args.package_names): sys.exit('Error: no package names supplied,\n' ' try "conda remove -h" for more details') prefix = common.get_prefix(args) common.check_write('remove', prefix) index = None if args.features: channel_urls = args.channel or () common.ensure_override_channels_requires_channel(args) index = get_index(channel_urls=channel_urls, prepend=not args.override_channels) features = set(args.package_names) actions = plan.remove_features_actions(prefix, index, features) elif args.all: if plan.is_root_prefix(prefix): sys.exit('Error: cannot remove root environment,\n' ' add -n NAME or -p PREFIX option') actions = {plan.PREFIX: prefix, plan.UNLINK: sorted(linked(prefix))} else: specs = common.specs_from_args(args.package_names) if (plan.is_root_prefix(prefix) and common.names_in_specs(common.root_no_rm, specs)): sys.exit('Error: cannot remove %s from root environment' % ', '.join(common.root_no_rm)) actions = plan.remove_actions(prefix, specs) if plan.nothing_to_do(actions): if args.all: rm_rf(prefix) return sys.exit('Error: no packages found to remove from ' 'environment: %s' % prefix) print() print("Package plan for package removal in environment %s:" % prefix) plan.display_actions(actions) if not pscheck.main(args): common.confirm_yn(args) plan.execute_actions(actions, index, verbose=not args.quiet) if args.all: rm_rf(prefix)
def install_actions(prefix, index, specs, force=False, only_names=None, pinned=True, minimal_hint=False, update_deps=True): r = Resolve(index) linked = install.linked(prefix) if config.self_update and is_root_prefix(prefix): specs.append('conda') if pinned: pinned_specs = get_pinned_specs(prefix) log.debug("Pinned specs=%s" % pinned_specs) specs += pinned_specs # TODO: Improve error messages here add_defaults_to_specs(r, linked, specs) must_have = {} for fn in r.solve(specs, [d + '.tar.bz2' for d in linked], config.track_features, minimal_hint=minimal_hint, update_deps=update_deps): dist = fn[:-8] name = install.name_dist(dist) if only_names and name not in only_names: continue must_have[name] = dist if is_root_prefix(prefix): if install.on_win: for name in install.win_ignore_root: if name in must_have: del must_have[name] for name in config.foreign: if name in must_have: del must_have[name] elif basename(prefix).startswith('_'): # anything (including conda) can be installed into environments # starting with '_', mainly to allow conda-build to build conda pass else: # disallow conda from being installed into all other environments if 'conda' in must_have: sys.exit("Error: 'conda' can only be installed into the " "root environment") smh = r.graph_sort(must_have) if force: actions = force_linked_actions(smh, index, prefix) else: actions = ensure_linked_actions(smh, prefix) if actions[inst.LINK] and sys.platform != 'win32' and prefix != config.root_dir: actions[inst.SYMLINK_CONDA] = [config.root_dir] for dist in sorted(linked): name = install.name_dist(dist) if name in must_have and dist != must_have[name]: add_unlink(actions, dist) return actions
def install_actions(prefix, index, specs, force=False, only_names=None, pinned=True, minimal_hint=False, update_deps=True, prune=False): r = Resolve(index) linked = install.linked(prefix) if config.self_update and is_root_prefix(prefix): specs.append('conda') if pinned: pinned_specs = get_pinned_specs(prefix) log.debug("Pinned specs=%s" % pinned_specs) specs += pinned_specs must_have = {} if config.track_features: specs.extend(x + '@' for x in config.track_features) pkgs = r.install(specs, [d + '.tar.bz2' for d in linked], update_deps=update_deps) for fn in pkgs: dist = fn[:-8] name = install.name_dist(dist) if not name or only_names and name not in only_names: continue must_have[name] = dist if is_root_prefix(prefix): for name in config.foreign: if name in must_have: del must_have[name] elif basename(prefix).startswith('_'): # anything (including conda) can be installed into environments # starting with '_', mainly to allow conda-build to build conda pass else: # disallow conda from being installed into all other environments if 'conda' in must_have or 'conda-env' in must_have: sys.exit("Error: 'conda' can only be installed into the " "root environment") smh = r.dependency_sort(must_have) if force: actions = force_linked_actions(smh, index, prefix) else: actions = ensure_linked_actions(smh, prefix) if actions[inst.LINK]: actions[inst.SYMLINK_CONDA] = [config.root_dir] for dist in sorted(linked): name = install.name_dist(dist) replace_existing = name in must_have and dist != must_have[name] prune_it = prune and dist not in smh if replace_existing or prune_it: add_unlink(actions, dist) return actions
def get_requires(prefix): res = [] for dist in install.linked(prefix): meta = install.is_linked(prefix, dist) assert meta if 'file_hash' not in meta: res.append('%(name)s %(version)s %(build)s' % meta) res.sort() return res
def execute(args, parser): import re import conda.install as install from conda.api import get_index from conda.resolve import MatchSpec, Resolve if args.regex: pat = re.compile(args.regex, re.I) else: pat = None prefix = common.get_prefix(args) if not args.canonical: linked = install.linked(prefix) # XXX: Make this work with more than one platform platform = args.platform or '' common.ensure_override_channels_requires_channel(args, dashc=False) channel_urls = args.channel or () index = get_index(channel_urls=channel_urls, prepend=not args.override_channels, platform=args.platform) r = Resolve(index) for name in sorted(r.groups): disp_name = name if pat and pat.search(name) is None: continue if args.outdated: vers_inst = [dist.rsplit('-', 2)[1] for dist in linked if dist.rsplit('-', 2)[0] == name] if not vers_inst: continue assert len(vers_inst) == 1, name pkgs = sorted(r.get_pkgs(MatchSpec(name))) if not pkgs: continue latest = pkgs[-1] if latest.version == vers_inst[0]: continue for pkg in sorted(r.get_pkgs(MatchSpec(name))): dist = pkg.fn[:-8] if args.canonical: print(dist) continue inst = '*' if dist in linked else ' ' print('%-25s %s %-15s %15s %-15s %s' % ( disp_name, inst, pkg.version, r.index[pkg.fn]['build'], canonical_channel_name(pkg.channel), common.disp_features(r.features(pkg.fn)), )) disp_name = ''
def update(self): """ update the history file (creating a new one if necessary) """ self.init_log_file() last = self.get_state() curr = set(install.linked(self.prefix)) if last == curr: return self.write_changes(last, curr)
def list_package_sources(prefix): """ List the sources of all the packages installed in the given environment. """ installed = install.linked(prefix) sources = [] for dist in conda.cli.main_list.get_packages(installed, None): info = install.is_linked(prefix, dist) sources.append(info['link']['source']) return sources
def conda_installed_files(prefix, exclude_self_build=False): """ Return the set of files which have been installed (using conda) into a given prefix. """ res = set() for dist in install.linked(prefix): meta = install.is_linked(prefix, dist) if exclude_self_build and 'file_hash' in meta: continue res.update(set(meta['files'])) return res
def remove_actions(prefix, specs): linked = install.linked(prefix) mss = [MatchSpec(spec) for spec in specs] actions = defaultdict(list) actions[PREFIX] = prefix for dist in sorted(linked): if any(ms.match("%s.tar.bz2" % dist) for ms in mss): actions[UNLINK].append(dist) return actions
def install_actions(prefix, index, specs, force=False, only_names=None, pinned=True, minimal_hint=False): r = Resolve(index) linked = install.linked(prefix) if config.self_update and is_root_prefix(prefix): specs.append('conda') add_defaults_to_specs(r, linked, specs) if pinned: pinned_specs = get_pinned_specs(prefix) specs += pinned_specs # TODO: Improve error messages here must_have = {} for fn in r.solve(specs, [d + '.tar.bz2' for d in linked], config.track_features, minimal_hint=minimal_hint): dist = fn[:-8] name = install.name_dist(dist) if only_names and name not in only_names: continue must_have[name] = dist if is_root_prefix(prefix): if install.on_win: for name in install.win_ignore_root: if name in must_have: del must_have[name] for name in config.foreign: if name in must_have: del must_have[name] else: # discard conda from other environments if 'conda' in must_have: sys.exit("Error: 'conda' can only be installed into " "root environment") smh = r.graph_sort(must_have) if force: actions = force_linked_actions(smh, index, prefix) else: actions = ensure_linked_actions(smh, prefix) if actions[LINK] and sys.platform != 'win32': actions[SYMLINK_CONDA] = [config.root_dir] for dist in sorted(linked): name = install.name_dist(dist) if name in must_have and dist != must_have[name]: actions[UNLINK].append(dist) return actions
def remove_features_actions(prefix, index, features): linked = install.linked(prefix) r = Resolve(index) actions = defaultdict(list) actions[inst.PREFIX] = [prefix] _linked = [d + '.tar.bz2' for d in linked] to_link = [] for dist in sorted(linked): fn = dist + '.tar.bz2' if fn not in index: continue if r.track_features(fn).intersection(features):
def from_environment(name, prefix): installed = install.linked(prefix) conda_pkgs = copy(installed) # json=True hides the output, data is added to installed main_list.add_pip_installed(prefix, installed, json=True) pip_pkgs = sorted(installed - conda_pkgs) dependencies = ['='.join(a.rsplit('-', 2)) for a in sorted(conda_pkgs)] if len(pip_pkgs) > 0: dependencies.append({'pip': ['=='.join(a.rsplit('-', 2)[:2]) for a in pip_pkgs]}) return Environment(name=name, dependencies=dependencies)
def execute(args, parser): import sys import conda.install as ci import conda.config as config import conda.plan as plan from conda.api import get_index from conda.cli import pscheck if len(args.pkg_names) == 0: sys.exit("""Error: no package names supplied # If you want to update to a newer version of Anaconda, type: # # $ conda update anaconda """) prefix = common.get_prefix(args) config.set_pkgs_dirs(prefix) linked = set(ci.name_dist(d) for d in ci.linked(prefix)) for name in args.pkg_names: common.arg2spec(name) if '=' in name: sys.exit("Invalid package name: '%s'" % (name)) if name not in linked: sys.exit("Error: package '%s' is not installed in %s" % (name, prefix)) common.ensure_override_channels_requires_channel(args) channel_urls = args.channel or () index = get_index(channel_urls=channel_urls, prepend=not args.override_channels) actions = plan.install_actions(prefix, index, args.pkg_names) if plan.nothing_to_do(actions): from conda.cli.main_list import list_packages regex = '^(%s)$' % '|'.join(args.pkg_names) print('# All packages already at latest version, nothing to do.') list_packages(prefix, regex) return print("Updating conda environment at %s" % prefix) plan.display_actions(actions, index) common.check_write('update', prefix) if not pscheck.main(args): common.confirm_yn(args) plan.execute_actions(actions, index, verbose=not args.quiet)
def which_package(path): """ given the path (of a (presumably) conda installed file) iterate over the conda packages the file came from. Usually the iteration yields only one package. """ path = abspath(path) prefix = which_prefix(path) if prefix is None: raise RuntimeError("could not determine conda prefix from: %s" % path) for dist in install.linked(prefix): meta = install.is_linked(prefix, dist) if any(abspath(join(prefix, f)) == path for f in meta['files']): yield dist
def clone_env(prefix1, prefix2, verbose=True, quiet=False, index=None): """ clone existing prefix1 into new prefix2 """ untracked_files = untracked(prefix1) dists = discard_conda(install.linked(prefix1)) if verbose: print('Packages: %d' % len(dists)) print('Files: %d' % len(untracked_files)) for f in untracked_files: src = join(prefix1, f) dst = join(prefix2, f) dst_dir = dirname(dst) if islink(dst_dir) or isfile(dst_dir): os.unlink(dst_dir) if not isdir(dst_dir): os.makedirs(dst_dir) if islink(src): os.symlink(os.readlink(src), dst) continue try: with open(src, 'rb') as fi: data = fi.read() except IOError: continue try: s = data.decode('utf-8') s = s.replace(prefix1, prefix2) data = s.encode('utf-8') except UnicodeDecodeError: # data is binary pass with open(dst, 'wb') as fo: fo.write(data) shutil.copystat(src, dst) if index is None: index = get_index() r = Resolve(index) sorted_dists = r.dependency_sort(dists) actions = ensure_linked_actions(sorted_dists, prefix2) execute_actions(actions, index=index, verbose=not quiet) return actions, untracked_files
def check_install(packages, platform=None, channel_urls=(), prepend=True, minimal_hint=False): try: prefix = tempfile.mkdtemp('conda') specs = common.specs_from_args(packages) index = get_index(channel_urls=channel_urls, prepend=prepend, platform=platform, prefix=prefix) linked = ci.linked(prefix) plan.add_defaults_to_specs(Resolve(index), linked, specs) actions = plan.install_actions(prefix, index, specs, pinned=False, minimal_hint=minimal_hint) plan.display_actions(actions, index) return actions finally: ci.rm_rf(prefix)
def update(self): """ update the history file (creating a new one if necessary) """ self.init_log_file() try: last = self.get_state() except CondaHistoryException as e: warnings.warn("Error in %s: %s" % (self.path, e), CondaHistoryWarning) return curr = set(install.linked(self.prefix)) if last == curr: return self.write_changes(last, curr)
def force_extract_and_link(dists, prefix, verbose=False): actions = defaultdict(list) actions['PREFIX'] = prefix actions['op_order'] = RM_EXTRACTED, EXTRACT, UNLINK, LINK # maps names of installed packages to dists linked = {install.name_dist(dist): dist for dist in install.linked(prefix)} for dist in dists: actions[RM_EXTRACTED].append(dist) actions[EXTRACT].append(dist) # unlink any installed package with that name name = install.name_dist(dist) if name in linked: actions[UNLINK].append(linked[name]) actions[LINK].append(dist) execute_actions(actions, verbose=verbose)
def create_bundle(prefix=None, data_path=None, bundle_name=None, extra_meta=None): """ Create a "bundle" of the environment located in `prefix`, and return the full path to the created package, which is going to be located in the current working directory, unless specified otherwise. """ meta = dict( name='bundle', build='0', build_number=0, type='bundle', bundle_name=bundle_name, creator=os.getenv('USER'), platform=platform, arch=arch_name, ctime=time.strftime(ISO8601), depends=[], ) meta['version'] = get_version(meta) tar_path = join('bundle-%(version)s-0.tar.bz2' % meta) t = tarfile.open(tar_path, 'w:bz2') if prefix: prefix = abspath(prefix) if not prefix.startswith('/opt/anaconda'): for f in sorted(untracked(prefix, exclude_self_build=True)): if f.startswith(BDP): raise RuntimeError('bad untracked file: %s' % f) if f.startswith('info/'): continue path = join(prefix, f) add_file(t, path, f) meta['bundle_prefix'] = prefix meta['depends'] = [' '.join(dist.rsplit('-', 2)) for dist in sorted(install.linked(prefix))] if data_path: add_data(t, data_path) if extra_meta: meta.update(extra_meta) add_info_files(t, meta) t.close() return tar_path
def remove_actions(prefix, specs, pinned=True): linked = install.linked(prefix) mss = [MatchSpec(spec) for spec in specs] pinned_specs = get_pinned_specs(prefix) actions = defaultdict(list) actions[PREFIX] = prefix for dist in sorted(linked): if any(ms.match('%s.tar.bz2' % dist) for ms in mss): if pinned and any(MatchSpec(spec).match('%s.tar.bz2' % dist) for spec in pinned_specs): raise RuntimeError("Cannot remove %s because it is pinned. Use --no-pin to override." % dist) actions[UNLINK].append(dist) return actions
def remove_actions(prefix, specs, index=None, pinned=True): linked = install.linked(prefix) mss = [MatchSpec(spec) for spec in specs] if index: r = Resolve(index) else: r = None pinned_specs = get_pinned_specs(prefix) actions = defaultdict(list) actions[inst.PREFIX] = [prefix] for dist in sorted(linked): fn = dist + '.tar.bz2' if any(ms.match(fn) for ms in mss):
def print_packages(prefix, regex=None, format='human', piplist=False, json=False, show_channel_urls=config.show_channel_urls): if not isdir(prefix): common.error_and_exit("""\ Error: environment does not exist: %s # # Use 'conda create' to create an environment before listing its packages.""" % prefix, json=json, error_type="NoEnvironmentFound") if not json: if format == 'human': print('# packages in environment at %s:' % prefix) print('#') if format == 'export': print_export_header() installed = install.linked(prefix) if piplist and config.use_pip and format == 'human': add_pip_installed(prefix, installed, json=json) exitcode, output = list_packages(prefix, installed, regex, format=format, show_channel_urls=show_channel_urls) if not json: print('\n'.join(output)) else: common.stdout_json(output) return exitcode
def build(m, verbose=True, channel_urls=(), override_channels=False, wheel_dir="./build"): ''' Build the package with the specified metadata. :param m: Package metadata :type m: Metadata ''' if (m.get_value('build/detect_binary_files_with_prefix') or m.binary_has_prefix_files()): # We must use a long prefix here as the package will only be # installable into prefixes shorter than this one. config.use_long_build_prefix = True else: # In case there are multiple builds in the same process config.use_long_build_prefix = False if m.skip(): print("Skipped: The %s recipe defines build/skip for this " "configuration." % m.dist()) return print("Removing old build environment") if on_win: if isdir(config.short_build_prefix): move_to_trash(config.short_build_prefix, '') if isdir(config.long_build_prefix): move_to_trash(config.long_build_prefix, '') else: rm_rf(config.short_build_prefix) rm_rf(config.long_build_prefix) print("Removing old work directory") if on_win: if isdir(source.WORK_DIR): move_to_trash(source.WORK_DIR, '') else: rm_rf(source.WORK_DIR) # Display the name only # Version number could be missing due to dependency on source info. print("BUILD START:", m.dist()) create_env(config.build_prefix, [ms.spec for ms in m.ms_depends('build')], verbose=verbose, channel_urls=channel_urls, override_channels=override_channels) if m.name() in [i.rsplit('-', 2)[0] for i in linked(config.build_prefix)]: print("%s is installed as a build dependency. Removing." % m.name()) index = get_build_index(clear_cache=False, channel_urls=channel_urls, override_channels=override_channels) actions = plan.remove_actions(config.build_prefix, [m.name()], index=index) assert not plan.nothing_to_do(actions), actions plan.display_actions(actions, index) plan.execute_actions(actions, index) # downlaod source code... source.provide(m.path, m.get_section('source')) # Parse our metadata again because we did not initialize the source # information before. m.parse_again() print("Package:", m.dist()) assert isdir(source.WORK_DIR) src_dir = source.get_dir() contents = os.listdir(src_dir) if contents: print("source tree in:", src_dir) else: print("no source") rm_rf(config.info_dir) files1 = prefix_files() for pat in m.always_include_files(): has_matches = False for f in set(files1): if fnmatch.fnmatch(f, pat): print("Including in package existing file", f) files1.discard(f) has_matches = True if not has_matches: sys.exit( "Error: Glob %s from always_include_files does not match any files" % pat) # Save this for later with open(join(config.croot, 'prefix_files.txt'), 'w') as f: f.write(u'\n'.join(sorted(list(files1)))) f.write(u'\n') print("Source dir: %s" % src_dir) if sys.platform == 'win32': windows_build(m) else: env = environ.get_dict(m) build_file = join(m.path, 'build_wheel.sh') if not isfile(build_file): print("Using plain 'python setup.py bdist_wheel' as build script") build_file = join(src_dir, 'build_wheel.sh') with open(build_file, 'w') as fo: fo.write('\n') fo.write('# Autogenerated build command:\n') fo.write('python setup.py bdist_wheel\n') fo.write('\n') cmd = [shell_path, '-x', '-e', build_file] _check_call(cmd, env=env, cwd=src_dir) all_wheels = glob(join(src_dir, "dist", '*.whl')) if len(all_wheels) == 0: print("No wheels produced!") else: if len(all_wheels) == 1: print("More than one wheel produced!") try: os.makedirs(wheel_dir) print("Created wheel dir: %s:" % wheel_dir) except OSError: if not isdir(wheel_dir): raise print("Copying to %s:" % wheel_dir) for wheel in all_wheels: shutil.copy(wheel, wheel_dir) print(" %s" % basename(wheel))
def build(m, get_src=True, verbose=True, post=None, channel_urls=(), override_channels=False, include_recipe=True): ''' Build the package with the specified metadata. :param m: Package metadata :type m: Metadata :param get_src: Should we download the source? :type get_src: bool :type post: bool or None. None means run the whole build. True means run post only. False means stop just before the post. ''' if (m.get_value('build/detect_binary_files_with_prefix') or m.binary_has_prefix_files()): # We must use a long prefix here as the package will only be # installable into prefixes shorter than this one. config.use_long_build_prefix = True else: # In case there are multiple builds in the same process config.use_long_build_prefix = False if post in [False, None]: print("Removing old build environment") if on_win: if isdir(config.short_build_prefix): move_to_trash(config.short_build_prefix, '') if isdir(config.long_build_prefix): move_to_trash(config.long_build_prefix, '') else: rm_rf(config.short_build_prefix) rm_rf(config.long_build_prefix) print("Removing old work directory") if on_win: if isdir(source.WORK_DIR): move_to_trash(source.WORK_DIR, '') else: rm_rf(source.WORK_DIR) # Display the name only # Version number could be missing due to dependency on source info. print("BUILD START:", m.dist()) create_env(config.build_prefix, [ms.spec for ms in m.ms_depends('build')], verbose=verbose, channel_urls=channel_urls, override_channels=override_channels) if m.name() in [ i.rsplit('-', 2)[0] for i in linked(config.build_prefix) ]: print("%s is installed as a build dependency. Removing." % m.name()) index = get_build_index(clear_cache=False, channel_urls=channel_urls, override_channels=override_channels) actions = plan.remove_actions(config.build_prefix, [m.name()], index=index) assert not plan.nothing_to_do(actions), actions plan.display_actions(actions, index) plan.execute_actions(actions, index) if get_src: source.provide(m.path, m.get_section('source')) # Parse our metadata again because we did not initialize the source # information before. m.parse_again() print("Package:", m.dist()) assert isdir(source.WORK_DIR) src_dir = source.get_dir() contents = os.listdir(src_dir) if contents: print("source tree in:", src_dir) else: print("no source") rm_rf(config.info_dir) files1 = prefix_files() for pat in m.always_include_files(): has_matches = False for f in set(files1): if fnmatch.fnmatch(f, pat): print("Including in package existing file", f) files1.discard(f) has_matches = True if not has_matches: sys.exit( "Error: Glob %s from always_include_files does not match any files" % pat) # Save this for later with open(join(config.croot, 'prefix_files.txt'), 'w') as f: f.write(u'\n'.join(sorted(list(files1)))) f.write(u'\n') if sys.platform == 'win32': import conda_build.windows as windows windows.build(m) else: env = environ.get_dict(m) build_file = join(m.path, 'build.sh') script = m.get_value('build/script', None) if script: if isinstance(script, list): script = '\n'.join(script) build_file = join(source.get_dir(), 'conda_build.sh') with open(build_file, 'w') as bf: bf.write(script) os.chmod(build_file, 0o766) if isfile(build_file): cmd = ['/bin/bash', '-x', '-e', build_file] _check_call(cmd, env=env, cwd=src_dir) if post in [True, None]: if post == True: with open(join(config.croot, 'prefix_files.txt'), 'r') as f: files1 = set(f.read().splitlines()) get_build_metadata(m) create_post_scripts(m) create_entry_points(m.get_value('build/entry_points')) assert not exists(config.info_dir) files2 = prefix_files() post_process(sorted(files2 - files1), preserve_egg_dir=bool( m.get_value('build/preserve_egg_dir'))) # The post processing may have deleted some files (like easy-install.pth) files2 = prefix_files() if any(config.meta_dir in join(config.build_prefix, f) for f in files2 - files1): sys.exit( indent( """Error: Untracked file(s) %s found in conda-meta directory. This error usually comes from using conda in the build script. Avoid doing this, as it can lead to packages that include their dependencies.""" % (tuple(f for f in files2 - files1 if config.meta_dir in join( config.build_prefix, f)), ))) post_build(m, sorted(files2 - files1)) create_info_files(m, sorted(files2 - files1), include_recipe=bool(m.path) and include_recipe) if m.get_value('build/noarch_python'): import conda_build.noarch_python as noarch_python noarch_python.transform(m, sorted(files2 - files1)) files3 = prefix_files() fix_permissions(files3 - files1) path = bldpkg_path(m) t = tarfile.open(path, 'w:bz2') for f in sorted(files3 - files1): t.add(join(config.build_prefix, f), f) t.close() print("BUILD END:", m.dist()) # we're done building, perform some checks tarcheck.check_all(path) update_index(config.bldpkgs_dir) else: print("STOPPING BUILD BEFORE POST:", m.dist())
def init_log_file(self, force=False): if not force and isfile(self.path): return self.write_dists(install.linked(self.prefix))
def install(args, parser, command='install'): """ conda install, conda update, and conda create """ newenv = command == 'create' if newenv: common.ensure_name_or_prefix(args, command) prefix = common.get_prefix(args, search=not newenv) if newenv: check_prefix(prefix) config.set_pkgs_dirs(prefix) if command == 'update': if len(args.packages) == 0: sys.exit("""Error: no package names supplied # If you want to update to a newer version of Anaconda, type: # # $ conda update --prefix %s anaconda """ % prefix) if command == 'update': linked = ci.linked(prefix) for name in args.packages: common.arg2spec(name) if '=' in name: sys.exit("Invalid package name: '%s'" % (name)) if name not in set(ci.name_dist(d) for d in linked): sys.exit("Error: package '%s' is not installed in %s" % (name, prefix)) if newenv and args.clone: if args.packages: sys.exit('Error: did not expect any arguments for --clone') clone(args.clone, prefix) touch_nonadmin(prefix) print_activate(args.name if args.name else prefix) return if newenv and not args.no_default_packages: default_packages = config.create_default_packages[:] # Override defaults if they are specified at the command line for default_pkg in config.create_default_packages: if any(pkg.split('=')[0] == default_pkg for pkg in args.packages): default_packages.remove(default_pkg) args.packages.extend(default_packages) common.ensure_override_channels_requires_channel(args) channel_urls = args.channel or () if args.use_local: from conda.fetch import fetch_index from conda.utils import url_path try: from conda_build import config as build_config except ImportError: sys.exit("Error: you need to have 'conda-build' installed" " to use the --use-local option") # remove the cache such that a refetch is made, # this is necessary because we add the local build repo URL fetch_index.cache = {} index = get_index([url_path(build_config.croot)], use_cache=args.use_cache) else: index = get_index(channel_urls=channel_urls, prepend=not args.override_channels, use_cache=args.use_cache) # Don't update packages that are already up-to-date if command == 'update': r = Resolve(index) orig_packages = args.packages[:] for name in orig_packages: vers_inst = [dist.rsplit('-', 2)[1] for dist in linked if dist.rsplit('-', 2)[0] == name] build_inst = [dist.rsplit('-', 2)[2].rsplit('.tar.bz2', 1)[0] for dist in linked if dist.rsplit('-', 2)[0] == name] assert len(vers_inst) == 1, name assert len(build_inst) == 1, name pkgs = sorted(r.get_pkgs(MatchSpec(name))) if not pkgs: # Shouldn't happen? continue # This won't do the right thing for python 2 latest = pkgs[-1] if latest.version == vers_inst[0] and latest.build == build_inst[0]: args.packages.remove(name) if not args.packages: from conda.cli.main_list import list_packages regex = '^(%s)$' % '|'.join(orig_packages) print('# All requested packages already installed.') list_packages(prefix, regex) return # handle tar file containing conda packages if len(args.packages) == 1: tar_path = args.packages[0] if tar_path.endswith('.tar'): install_tar(prefix, tar_path, verbose=not args.quiet) return # handle explicit installs of conda packages if args.packages and all(s.endswith('.tar.bz2') for s in args.packages): from conda.misc import install_local_packages install_local_packages(prefix, args.packages, verbose=not args.quiet) return if any(s.endswith('.tar.bz2') for s in args.packages): sys.exit("cannot mix specifications with conda package filenames") if args.force: args.no_deps = True if args.file: specs = common.specs_from_url(args.file) else: specs = common.specs_from_args(args.packages) common.check_specs(prefix, specs) spec_names = set(s.split()[0] for s in specs) if args.no_deps: only_names = spec_names else: only_names = None if not isdir(prefix) and not newenv: if args.mkdir: try: os.makedirs(prefix) except OSError: sys.exit("Error: could not create directory: %s" % prefix) else: sys.exit("""\ Error: environment does not exist: %s # # Use 'conda create' to create an environment before installing packages # into it. #""" % prefix) actions = plan.install_actions(prefix, index, specs, force=args.force, only_names=only_names) if plan.nothing_to_do(actions): from conda.cli.main_list import list_packages regex = '^(%s)$' % '|'.join(spec_names) print('# All requested packages already installed.') list_packages(prefix, regex) return print() print("Package plan for installation in environment %s:" % prefix) plan.display_actions(actions, index) if command in {'install', 'update'}: common.check_write(command, prefix) if not pscheck.main(args): common.confirm_yn(args) plan.execute_actions(actions, index, verbose=not args.quiet) if newenv: touch_nonadmin(prefix) print_activate(args.name if args.name else prefix)
def execute(args, parser): import sys import conda.plan as plan from conda.cli import pscheck from conda.install import rm_rf, linked from conda import config if not (args.all or args.package_names): common.error_and_exit( 'no package names supplied,\n' ' try "conda remove -h" for more details', json=args.json, error_type="ValueError") prefix = common.get_prefix(args) common.check_write('remove', prefix, json=args.json) common.ensure_override_channels_requires_channel(args, json=args.json) channel_urls = args.channel or () index = common.get_index_trap(channel_urls=channel_urls, use_cache=args.use_index_cache, prepend=not args.override_channels, json=args.json) if args.features: features = set(args.package_names) actions = plan.remove_features_actions(prefix, index, features) elif args.all: if plan.is_root_prefix(prefix): common.error_and_exit( 'cannot remove root environment,\n' ' add -n NAME or -p PREFIX option', json=args.json, error_type="CantRemoveRoot") actions = {plan.PREFIX: prefix, plan.UNLINK: sorted(linked(prefix))} else: specs = common.specs_from_args(args.package_names) if (plan.is_root_prefix(prefix) and common.names_in_specs(common.root_no_rm, specs)): common.error_and_exit('cannot remove %s from root environment' % ', '.join(common.root_no_rm), json=args.json, error_type="CantRemoveFromRoot") actions = plan.remove_actions(prefix, specs, pinned=args.pinned) if plan.nothing_to_do(actions): if args.all: rm_rf(prefix) if args.json: common.stdout_json({'success': True, 'actions': actions}) return common.error_and_exit('no packages found to remove from ' 'environment: %s' % prefix, json=args.json, error_type="PackageNotInstalled") if not args.json: print() print("Package plan for package removal in environment %s:" % prefix) plan.display_actions(actions, index) if args.json and args.dry_run: common.stdout_json({ 'success': True, 'dry_run': True, 'actions': actions }) return if not args.json: if not pscheck.main(args): common.confirm_yn(args) elif (sys.platform == 'win32' and not args.force_pscheck and not pscheck.check_processes(verbose=False)): common.error_and_exit( "Cannot continue removal while processes " "from packages are running without --force-pscheck.", json=True, error_type="ProcessesStillRunning") if args.json and not args.quiet: with json_progress_bars(): plan.execute_actions(actions, index, verbose=not args.quiet) else: plan.execute_actions(actions, index, verbose=not args.quiet) if args.all: rm_rf(prefix) if args.json: common.stdout_json({'success': True, 'actions': actions})
def install(args, parser, command='install'): """ conda install, conda update, and conda create """ newenv = bool(command == 'create') if newenv: common.ensure_name_or_prefix(args, command) prefix = common.get_prefix(args, search=not newenv) if newenv: check_prefix(prefix, json=args.json) if config.force_32bit and plan.is_root_prefix(prefix): common.error_and_exit("cannot use CONDA_FORCE_32BIT=1 in root env") if command == 'update': if not args.file: if not args.all and len(args.packages) == 0: common.error_and_exit("""no package names supplied # If you want to update to a newer version of Anaconda, type: # # $ conda update --prefix %s anaconda """ % prefix, json=args.json, error_type="ValueError") if command == 'update' and not args.all: linked = ci.linked(prefix) for name in args.packages: common.arg2spec(name, json=args.json) if '=' in name: common.error_and_exit("Invalid package name: '%s'" % (name), json=args.json, error_type="ValueError") if name not in set(ci.name_dist(d) for d in linked): common.error_and_exit("package '%s' is not installed in %s" % (name, prefix), json=args.json, error_type="ValueError") if newenv and not args.no_default_packages: default_packages = config.create_default_packages[:] # Override defaults if they are specified at the command line for default_pkg in config.create_default_packages: if any(pkg.split('=')[0] == default_pkg for pkg in args.packages): default_packages.remove(default_pkg) args.packages.extend(default_packages) else: default_packages = [] common.ensure_override_channels_requires_channel(args) channel_urls = args.channel or () specs = [] if args.file: for fpath in args.file: specs.extend(common.specs_from_url(fpath, json=args.json)) elif getattr(args, 'all', False): linked = ci.linked(prefix) if not linked: common.error_and_exit("There are no packages installed in the " "prefix %s" % prefix) for pkg in linked: name, ver, build = pkg.rsplit('-', 2) if name in getattr(args, '_skip', ['anaconda']): continue if name == 'python' and ver.startswith('2'): # Oh Python 2... specs.append('%s >=%s,<3' % (name, ver)) else: specs.append('%s' % name) specs.extend(common.specs_from_args(args.packages, json=args.json)) if command == 'install' and args.revision: get_revision(args.revision, json=args.json) elif not (newenv and args.clone): common.check_specs(prefix, specs, json=args.json, create=(command == 'create')) num_cp = sum(s.endswith('.tar.bz2') for s in args.packages) if num_cp: if num_cp == len(args.packages): depends = misc.install_local_packages(prefix, args.packages, verbose=not args.quiet) if args.no_deps: depends = [] specs = list(set(depends)) args.unknown = True else: common.error_and_exit( "cannot mix specifications with conda package filenames", json=args.json, error_type="ValueError") # handle tar file containing conda packages if len(args.packages) == 1: tar_path = args.packages[0] if tar_path.endswith('.tar'): depends = install_tar(prefix, tar_path, verbose=not args.quiet) if args.no_deps: depends = [] specs = list(set(depends)) args.unknown = True if args.use_local: from conda.fetch import fetch_index from conda.utils import url_path try: from conda_build.config import croot except ImportError: common.error_and_exit( "you need to have 'conda-build >= 1.7.1' installed" " to use the --use-local option", json=args.json, error_type="RuntimeError") # remove the cache such that a refetch is made, # this is necessary because we add the local build repo URL fetch_index.cache = {} if exists(croot): channel_urls = [url_path(croot)] + list(channel_urls) index = common.get_index_trap(channel_urls=channel_urls, prepend=not args.override_channels, use_cache=args.use_index_cache, unknown=args.unknown, json=args.json, offline=args.offline) if newenv and args.clone: if set(args.packages) - set(default_packages): common.error_and_exit('did not expect any arguments for --clone', json=args.json, error_type="ValueError") clone(args.clone, prefix, json=args.json, quiet=args.quiet, index=index) misc.append_env(prefix) misc.touch_nonadmin(prefix) if not args.json: print_activate(args.name if args.name else prefix) return # Don't update packages that are already up-to-date if command == 'update' and not (args.all or args.force): r = Resolve(index) orig_packages = args.packages[:] for name in orig_packages: installed_metadata = [ci.is_linked(prefix, dist) for dist in linked] vers_inst = [dist.rsplit('-', 2)[1] for dist in linked if dist.rsplit('-', 2)[0] == name] build_inst = [m['build_number'] for m in installed_metadata if m['name'] == name] try: assert len(vers_inst) == 1, name assert len(build_inst) == 1, name except AssertionError as e: if args.json: common.exception_and_exit(e, json=True) else: raise pkgs = sorted(r.get_pkgs(MatchSpec(name))) if not pkgs: # Shouldn't happen? continue latest = pkgs[-1] if (latest.version == vers_inst[0] and latest.build_number == build_inst[0]): args.packages.remove(name) if not args.packages: from conda.cli.main_list import print_packages if not args.json: regex = '^(%s)$' % '|'.join(orig_packages) print('# All requested packages already installed.') print_packages(prefix, regex) else: common.stdout_json_success( message='All requested packages already installed.') return if args.force: args.no_deps = True spec_names = set(s.split()[0] for s in specs) if args.no_deps: only_names = spec_names else: only_names = None if not isdir(prefix) and not newenv: if args.mkdir: try: os.makedirs(prefix) except OSError: common.error_and_exit("Error: could not create directory: %s" % prefix, json=args.json, error_type="OSError") else: common.error_and_exit("""\ environment does not exist: %s # # Use 'conda create' to create an environment before installing packages # into it. #""" % prefix, json=args.json, error_type="NoEnvironmentFound") try: if command == 'install' and args.revision: actions = plan.revert_actions(prefix, get_revision(args.revision)) else: with common.json_progress_bars(json=args.json and not args.quiet): actions = plan.install_actions(prefix, index, specs, force=args.force, only_names=only_names, pinned=args.pinned, minimal_hint=args.alt_hint, update_deps=args.update_deps) if config.always_copy or args.copy: new_link = [] for pkg in actions["LINK"]: dist, pkgs_dir, lt = inst.split_linkarg(pkg) lt = ci.LINK_COPY new_link.append("%s %s %d" % (dist, pkgs_dir, lt)) actions["LINK"] = new_link except NoPackagesFound as e: error_message = e.args[0] if command == 'update' and args.all: # Packages not found here just means they were installed but # cannot be found any more. Just skip them. if not args.json: print("Warning: %s, skipping" % error_message) else: # Not sure what to do here pass args._skip = getattr(args, '_skip', ['anaconda']) for pkg in e.pkgs: p = pkg.split()[0] if p in args._skip: # Avoid infinite recursion. This can happen if a spec # comes from elsewhere, like --file raise args._skip.append(p) return install(args, parser, command=command) else: packages = {index[fn]['name'] for fn in index} for pkg in e.pkgs: close = get_close_matches(pkg, packages, cutoff=0.7) if close: error_message += ("\n\nDid you mean one of these?" "\n\n %s" % (', '.join(close))) error_message += '\n\nYou can search for this package on anaconda.org with' error_message += '\n\n anaconda search -t conda %s' % pkg if len(e.pkgs) > 1: # Note this currently only happens with dependencies not found error_message += '\n\n (and similarly for the other packages)' if not find_executable('anaconda', include_others=False): error_message += '\n\nYou may need to install the anaconda-client command line client with' error_message += '\n\n conda install anaconda-client' pinned_specs = plan.get_pinned_specs(prefix) if pinned_specs: error_message += "\n\nNote that you have pinned specs in %s:" % join(prefix, 'conda-meta', 'pinned') error_message += "\n\n %r" % pinned_specs common.error_and_exit(error_message, json=args.json) except SystemExit as e: # Unsatisfiable package specifications/no such revision/import error error_type = 'UnsatisfiableSpecifications' if e.args and 'could not import' in e.args[0]: error_type = 'ImportError' common.exception_and_exit(e, json=args.json, newline=True, error_text=False, error_type=error_type) if plan.nothing_to_do(actions): from conda.cli.main_list import print_packages if not args.json: regex = '^(%s)$' % '|'.join(spec_names) print('\n# All requested packages already installed.') print_packages(prefix, regex) else: common.stdout_json_success( message='All requested packages already installed.') return if not args.json: print() print("Package plan for installation in environment %s:" % prefix) plan.display_actions(actions, index, show_channel_urls=args.show_channel_urls) if command in {'install', 'update'}: common.check_write(command, prefix) if not args.json: common.confirm_yn(args) elif args.dry_run: common.stdout_json_success(actions=actions, dry_run=True) sys.exit(0) with common.json_progress_bars(json=args.json and not args.quiet): try: plan.execute_actions(actions, index, verbose=not args.quiet) if not (command == 'update' and args.all): try: with open(join(prefix, 'conda-meta', 'history'), 'a') as f: f.write('# %s specs: %s\n' % (command, specs)) except IOError as e: if e.errno == errno.EACCES: log.debug("Can't write the history file") else: raise except RuntimeError as e: if len(e.args) > 0 and "LOCKERROR" in e.args[0]: error_type = "AlreadyLocked" else: error_type = "RuntimeError" common.exception_and_exit(e, error_type=error_type, json=args.json) except SystemExit as e: common.exception_and_exit(e, json=args.json) if newenv: misc.append_env(prefix) misc.touch_nonadmin(prefix) if not args.json: print_activate(args.name if args.name else prefix) if args.json: common.stdout_json_success(actions=actions)
def build(m, post=None, include_recipe=True, keep_old_work=False, need_source_download=True, verbose=True, dirty=False, activate=True): ''' Build the package with the specified metadata. :param m: Package metadata :type m: Metadata :type post: bool or None. None means run the whole build. True means run post only. False means stop just before the post. :type keep_old_work: bool: Keep any previous work directory. :type need_source_download: bool: if rendering failed to download source (due to missing tools), retry here after build env is populated ''' if (m.get_value('build/detect_binary_files_with_prefix') or m.binary_has_prefix_files()) and not on_win: # We must use a long prefix here as the package will only be # installable into prefixes shorter than this one. config.use_long_build_prefix = True else: # In case there are multiple builds in the same process config.use_long_build_prefix = False if m.skip(): print("Skipped: The %s recipe defines build/skip for this " "configuration." % m.dist()) return with Locked(cc.root_dir): # If --keep-old-work, then move the contents of source.WORK_DIR to a # temporary directory for the duration of the build. # The source unpacking procedure is too varied and complex # to allow this to be written cleanly (see source.get_dir() for example) if keep_old_work: old_WORK_DIR = tempfile.mkdtemp() old_sub_dirs = [ name for name in os.listdir(source.WORK_DIR) if os.path.isdir(os.path.join(source.WORK_DIR, name)) ] if len(old_sub_dirs): print("Keeping old work directory backup: %s => %s" % (old_sub_dirs, old_WORK_DIR)) for old_sub in old_sub_dirs: shutil.move(os.path.join(source.WORK_DIR, old_sub), old_WORK_DIR) if post in [False, None]: print("Removing old build environment") print("BUILD START:", m.dist()) if on_win: if isdir(config.short_build_prefix): move_to_trash(config.short_build_prefix, '') if isdir(config.long_build_prefix): move_to_trash(config.long_build_prefix, '') else: rm_rf(config.short_build_prefix) rm_rf(config.long_build_prefix) # Display the name only # Version number could be missing due to dependency on source info. create_env(config.build_prefix, [ms.spec for ms in m.ms_depends('build')]) if need_source_download: # Execute any commands fetching the source (e.g., git) in the _build environment. # This makes it possible to provide source fetchers (eg. git, hg, svn) as build # dependencies. m, need_source_download = parse_or_try_download( m, no_download_source=False, force_download=True, verbose=verbose, dirty=dirty) assert not need_source_download, "Source download failed. Please investigate." if m.name() in [ i.rsplit('-', 2)[0] for i in linked(config.build_prefix) ]: print("%s is installed as a build dependency. Removing." % m.name()) index = get_build_index(clear_cache=False) actions = plan.remove_actions(config.build_prefix, [m.name()], index=index) assert not plan.nothing_to_do(actions), actions plan.display_actions(actions, index) plan.execute_actions(actions, index) print("Package:", m.dist()) assert isdir(source.WORK_DIR) src_dir = source.get_dir() contents = os.listdir(src_dir) if contents: print("source tree in:", src_dir) else: print("no source") rm_rf(config.info_dir) files1 = prefix_files() for pat in m.always_include_files(): has_matches = False for f in set(files1): if fnmatch.fnmatch(f, pat): print("Including in package existing file", f) files1.discard(f) has_matches = True if not has_matches: sys.exit( "Error: Glob %s from always_include_files does not match any files" % pat) # Save this for later with open(join(config.croot, 'prefix_files.txt'), 'w') as f: f.write(u'\n'.join(sorted(list(files1)))) f.write(u'\n') # Use script from recipe? script = m.get_value('build/script', None) if script: if isinstance(script, list): script = '\n'.join(script) if on_win: build_file = join(m.path, 'bld.bat') if script: build_file = join(source.get_dir(), 'bld.bat') with open(join(source.get_dir(), 'bld.bat'), 'w') as bf: bf.write(script) import conda_build.windows as windows windows.build(m, build_file, dirty=dirty, activate=activate) else: build_file = join(m.path, 'build.sh') # There is no sense in trying to run an empty build script. if isfile(build_file) or script: env = environ.get_dict(m, dirty=dirty) work_file = join(source.get_dir(), 'conda_build.sh') if script: with open(work_file, 'w') as bf: bf.write(script) if activate: if isfile(build_file): data = open(build_file).read() else: data = open(work_file).read() with open(work_file, 'w') as bf: bf.write("source activate {build_prefix}\n".format( build_prefix=config.build_prefix)) bf.write(data) else: if not isfile(work_file): shutil.copy(build_file, work_file) os.chmod(work_file, 0o766) if isfile(work_file): cmd = [shell_path, '-x', '-e', work_file] _check_call(cmd, env=env, cwd=src_dir) if post in [True, None]: if post: with open(join(config.croot, 'prefix_files.txt'), 'r') as f: files1 = set(f.read().splitlines()) get_build_metadata(m) create_post_scripts(m) create_entry_points(m.get_value('build/entry_points')) assert not exists(config.info_dir) files2 = prefix_files() post_process(sorted(files2 - files1), preserve_egg_dir=bool( m.get_value('build/preserve_egg_dir'))) # The post processing may have deleted some files (like easy-install.pth) files2 = prefix_files() if any(config.meta_dir in join(config.build_prefix, f) for f in files2 - files1): sys.exit( indent( """Error: Untracked file(s) %s found in conda-meta directory. This error usually comes from using conda in the build script. Avoid doing this, as it can lead to packages that include their dependencies.""" % (tuple(f for f in files2 - files1 if config.meta_dir in join(config.build_prefix, f)), ))) post_build(m, sorted(files2 - files1)) create_info_files(m, sorted(files2 - files1), include_recipe=bool(m.path) and include_recipe) if m.get_value('build/noarch_python'): import conda_build.noarch_python as noarch_python noarch_python.transform(m, sorted(files2 - files1)) files3 = prefix_files() fix_permissions(files3 - files1) path = bldpkg_path(m) t = tarfile.open(path, 'w:bz2') def order(f): # we don't care about empty files so send them back via 100000 fsize = os.stat(join(config.build_prefix, f)).st_size or 100000 # info/* records will be False == 0, others will be 1. info_order = int(os.path.dirname(f) != 'info') return info_order, fsize # add files in order of a) in info directory, b) increasing size so # we can access small manifest or json files without decompressing # possible large binary or data files for f in sorted(files3 - files1, key=order): t.add(join(config.build_prefix, f), f) t.close() print("BUILD END:", m.dist()) # we're done building, perform some checks tarcheck.check_all(path) update_index(config.bldpkgs_dir) else: print("STOPPING BUILD BEFORE POST:", m.dist()) if keep_old_work and len(old_sub_dirs): print("Restoring old work directory backup: %s :: %s => %s" % (old_WORK_DIR, old_sub_dirs, source.WORK_DIR)) for old_sub in old_sub_dirs: if os.path.exists(os.path.join(source.WORK_DIR, old_sub)): print( "Not restoring old source directory %s over new build's version" % (old_sub)) else: shutil.move(os.path.join(old_WORK_DIR, old_sub), source.WORK_DIR) shutil.rmtree(old_WORK_DIR, ignore_errors=True)
def execute(args, parser): if not args.subcommand: parser.print_help() prefix = get_prefix(args) installed = ci.linked(prefix) if not args.packages and not args.untracked and not args.all: parser.error( "At least one package or --untracked or --all must be provided") if args.all: args.packages = sorted([i.rsplit('-', 2)[0] for i in installed]) if args.untracked: args.packages.append(untracked_package) for pkg in args.packages: if pkg == untracked_package: dist = untracked_package else: for dist in installed: if pkg == dist.rsplit('-', 2)[0]: break else: sys.exit("Package %s is not installed in %s" % (pkg, prefix)) print(pkg) print('-' * len(str(pkg))) print() if args.subcommand == 'linkages': if not sys.platform.startswith(('linux', 'darwin')): sys.exit( "Error: conda inspect linkages is only implemented in Linux and OS X" ) if dist == untracked_package: obj_files = get_untracked_obj_files(prefix) else: obj_files = get_package_obj_files(dist, prefix) linkages = get_linkages(obj_files, prefix) depmap = defaultdict(list) for binary in linkages: for lib, path in linkages[binary]: path = replace_path(binary, path, prefix) if path not in { '', 'not found' } else path if path.startswith(prefix): deps = list(which_package(path)) if len(deps) > 1: print( "Warning: %s comes from multiple packages: %s" % (path, ' and '.join(deps)), file=sys.stderr) if not deps: if exists(path): depmap['untracked'].append( (lib, path.split(prefix + '/', 1)[-1], binary)) else: depmap['not found'].append( (lib, path.split(prefix + '/', 1)[-1], binary)) for d in deps: depmap[d].append((lib, path.split(prefix + '/', 1)[-1], binary)) elif path == 'not found': depmap['not found'].append((lib, path, binary)) else: depmap['system'].append((lib, path, binary)) print_linkages(depmap, show_files=args.show_files) if args.subcommand == 'objects': if not sys.platform.startswith('darwin'): sys.exit( "Error: conda inspect objects is only implemented in OS X") if dist == untracked_package: obj_files = get_untracked_obj_files(prefix) else: obj_files = get_package_obj_files(dist, prefix) info = [] for f in obj_files: f_info = {} path = join(prefix, f) f_info['filetype'] = human_filetype(path) f_info['rpath'] = get_rpath(path) f_info['filename'] = f info.append(f_info) print_object_info(info, args.groupby)
======= >>>>>>> origin/installed ======= <<<<<<< HEAD >>>>>>> princeofdarkness76/installed ======= >>>>>>> origin/installed ======= >>>>>>> conda/installed >>>>>>> princeofdarkness76/conda ======= >>>>>>> princeofdarkness76/installed ======= >>>>>>> origin/installed r = Resolve(index) linked = install.linked(prefix) if config.self_update and is_root_prefix(prefix): specs.append('conda') if pinned: pinned_specs = get_pinned_specs(prefix) log.debug("Pinned specs=%s" % pinned_specs) specs += pinned_specs # TODO: Improve error messages here add_defaults_to_specs(r, linked, specs) must_have = {} mustnt_have = {} for fn in r.solve(specs, [d + '.tar.bz2' for d in linked], config.track_features, minimal_hint=minimal_hint,
def install_actions(prefix, index, specs, force=False, only_names=None, pinned=True, minimal_hint=False, update_deps=True): r = Resolve(index) linked = install.linked(prefix) if config.self_update and is_root_prefix(prefix): specs.append('conda') if pinned: pinned_specs = get_pinned_specs(prefix) log.debug("Pinned specs=%s" % pinned_specs) specs += pinned_specs # TODO: Improve error messages here add_defaults_to_specs(r, linked, specs) must_have = {} for fn in r.solve(specs, [d + '.tar.bz2' for d in linked], config.track_features, minimal_hint=minimal_hint, update_deps=update_deps): dist = fn[:-8] name = install.name_dist(dist) if only_names and name not in only_names: continue must_have[name] = dist if is_root_prefix(prefix): if install.on_win: for name in install.win_ignore_root: if name in must_have: del must_have[name] for name in config.foreign: if name in must_have: del must_have[name] elif basename(prefix).startswith('_'): # anything (including conda) can be installed into environments # starting with '_', mainly to allow conda-build to build conda pass else: # disallow conda from being installed into all other environments if 'conda' in must_have: sys.exit("Error: 'conda' can only be installed into the " "root environment") smh = r.graph_sort(must_have) if force: actions = force_linked_actions(smh, index, prefix) else: actions = ensure_linked_actions(smh, prefix) if actions[inst. LINK] and sys.platform != 'win32' and prefix != config.root_dir: actions[inst.SYMLINK_CONDA] = [config.root_dir] for dist in sorted(linked): name = install.name_dist(dist) if name in must_have and dist != must_have[name]: add_unlink(actions, dist) return actions
def execute(args, parser): from conda import config, plan from conda.install import linked, rm_rf prefix = common.get_prefix(args) if plan.is_root_prefix(prefix): common.error_and_exit( 'cannot remove root environment,\n' ' add -n NAME or -p PREFIX option', json=args.json, error_type="CantRemoveRoot") if prefix == config.default_prefix: # FIXME The way the "name" is determined now is handled by # looking at the basename of the prefix. This is brittle # and underlines a use-case for an Environment object that # is capable of providing a name attribute. common.error_and_exit( textwrap.dedent(""" Conda cannot remove the current environment. Please deactivate and run conda env remove again with the name specified. conda env remove --name %s """ % basename(prefix)).lstrip()) # TODO Why do we need an index for removing packages? index = common.get_index_trap(json=args.json) actions = {plan.PREFIX: prefix, plan.UNLINK: sorted(linked(prefix))} if plan.nothing_to_do(actions): # TODO Should this automatically remove even *before* confirmation? # TODO Should this display an error when removing something that # doesn't exist? rm_rf(prefix) if args.json: common.stdout_json({'success': True, 'actions': actions}) return if args.json and args.dry_run: common.stdout_json({ 'success': True, 'dry_run': True, 'actions': actions }) return if not args.json: print() print("Remove the following packages in environment %s:" % prefix) plan.display_actions(actions, index) common.confirm_yn(args) plan.execute_actions(actions, index, verbose=not args.quiet) rm_rf(prefix) if args.json: common.stdout_json({'success': True, 'actions': actions})
def get_run_dists(m): prefix = join(cc.envs_dirs[0], '_run') rm_rf(prefix) create_env(prefix, [ms.spec for ms in m.ms_depends('run')]) return sorted(linked(prefix))
def execute(args, parser): import conda.plan as plan import conda.instructions as inst from conda.install import rm_rf, linked if not (args.all or args.package_names): error_and_exit( 'no package names supplied,\n' ' try "conda remove -h" for more details', json=args.json, error_type="ValueError") prefix = get_prefix(args) if args.all and prefix == default_prefix: msg = "cannot remove current environment. deactivate and run conda remove again" error_and_exit(msg) check_write('remove', prefix, json=args.json) ensure_use_local(args) ensure_override_channels_requires_channel(args) channel_urls = args.channel or () if not args.features and args.all: index = {} else: index = get_index_trap(channel_urls=channel_urls, prepend=not args.override_channels, use_local=args.use_local, use_cache=args.use_index_cache, json=args.json, offline=args.offline, prefix=prefix) specs = None if args.features: features = set(args.package_names) actions = plan.remove_features_actions(prefix, index, features) elif args.all: if plan.is_root_prefix(prefix): error_and_exit( 'cannot remove root environment,\n' ' add -n NAME or -p PREFIX option', json=args.json, error_type="CantRemoveRoot") actions = {inst.PREFIX: prefix} for dist in sorted(linked(prefix)): plan.add_unlink(actions, dist) else: specs = specs_from_args(args.package_names) if (plan.is_root_prefix(prefix) and names_in_specs(root_no_rm, specs)): error_and_exit('cannot remove %s from root environment' % ', '.join(root_no_rm), json=args.json, error_type="CantRemoveFromRoot") actions = plan.remove_actions(prefix, specs, index=index, force=args.force, pinned=args.pinned) if plan.nothing_to_do(actions): if args.all: rm_rf(prefix) if args.json: stdout_json({'success': True, 'actions': actions}) return error_and_exit('no packages found to remove from ' 'environment: %s' % prefix, json=args.json, error_type="PackageNotInstalled") if not args.json: print() print("Package plan for package removal in environment %s:" % prefix) plan.display_actions(actions, index) if args.json and args.dry_run: stdout_json({'success': True, 'dry_run': True, 'actions': actions}) return if not args.json: confirm_yn(args) if args.json and not args.quiet: with json_progress_bars(): plan.execute_actions(actions, index, verbose=not args.quiet) else: plan.execute_actions(actions, index, verbose=not args.quiet) if specs: try: with open(join(prefix, 'conda-meta', 'history'), 'a') as f: f.write('# remove specs: %s\n' % specs) except IOError as e: if e.errno == errno.EACCES: log.debug("Can't write the history file") else: raise if args.all: rm_rf(prefix) if args.json: stdout_json({'success': True, 'actions': actions})
def execute(args, parser): if not args.subcommand: parser.print_help() exit() prefix = get_prefix(args) installed = ci.linked(prefix) if not args.packages and not args.untracked and not args.all: parser.error( "At least one package or --untracked or --all must be provided") if args.all: args.packages = sorted([i.rsplit('-', 2)[0] for i in installed]) if args.untracked: args.packages.append(untracked_package) if args.subcommand == 'linkages': pkgmap = {} for pkg in args.packages: if pkg == untracked_package: dist = untracked_package else: for dist in installed: if pkg == dist.rsplit('-', 2)[0]: break else: sys.exit("Package %s is not installed in %s" % (pkg, prefix)) if not sys.platform.startswith(('linux', 'darwin')): sys.exit( "Error: conda inspect linkages is only implemented in Linux and OS X" ) if dist == untracked_package: obj_files = get_untracked_obj_files(prefix) else: obj_files = get_package_obj_files(dist, prefix) linkages = get_linkages(obj_files, prefix) depmap = defaultdict(list) pkgmap[pkg] = depmap depmap['not found'] = [] for binary in linkages: for lib, path in linkages[binary]: path = replace_path(binary, path, prefix) if path not in { '', 'not found' } else path if path.startswith(prefix): deps = list(which_package(path)) if len(deps) > 1: print( "Warning: %s comes from multiple packages: %s" % (path, comma_join(deps)), file=sys.stderr) if not deps: if exists(path): depmap['untracked'].append( (lib, path.split(prefix + '/', 1)[-1], binary)) else: depmap['not found'].append( (lib, path.split(prefix + '/', 1)[-1], binary)) for d in deps: depmap[d].append((lib, path.split(prefix + '/', 1)[-1], binary)) elif path == 'not found': depmap['not found'].append((lib, path, binary)) else: depmap['system'].append((lib, path, binary)) if args.groupby == 'package': for pkg in args.packages: print(pkg) print('-' * len(str(pkg))) print() print_linkages(pkgmap[pkg], show_files=args.show_files) elif args.groupby == 'dependency': # {pkg: {dep: [files]}} -> {dep: {pkg: [files]}} inverted_map = defaultdict(lambda: defaultdict(list)) for pkg in pkgmap: for dep in pkgmap[pkg]: if pkgmap[pkg][dep]: inverted_map[dep][pkg] = pkgmap[pkg][dep] # print system and not found last k = sorted(set(inverted_map.keys()) - {'system', 'not found'}) for dep in k + ['system', 'not found']: print(dep) print('-' * len(str(dep))) print() print_linkages(inverted_map[dep], show_files=args.show_files) else: raise ValueError("Unrecognized groupby: %s" % args.groupby) if args.subcommand == 'objects': for pkg in args.packages: if pkg == untracked_package: dist = untracked_package else: for dist in installed: if pkg == dist.rsplit('-', 2)[0]: break else: sys.exit("Package %s is not installed in %s" % (pkg, prefix)) print(pkg) print('-' * len(str(pkg))) print() if not sys.platform.startswith('darwin'): sys.exit( "Error: conda inspect objects is only implemented in OS X") if dist == untracked_package: obj_files = get_untracked_obj_files(prefix) else: obj_files = get_package_obj_files(dist, prefix) info = [] for f in obj_files: f_info = {} path = join(prefix, f) f_info['filetype'] = human_filetype(path) f_info['rpath'] = ':'.join(get_rpaths(path)) f_info['filename'] = f info.append(f_info) print_object_info(info, args.groupby)
def execute(args, parser): import conda.plan as plan import conda.instructions as inst from conda.install import rm_rf, linked from conda import config if not (args.all or args.package_names): common.error_and_exit( 'no package names supplied,\n' ' try "conda remove -h" for more details', json=args.json, error_type="ValueError") prefix = common.get_prefix(args) if args.all and prefix == config.default_prefix: common.error_and_exit( "cannot remove current environment. deactivate and run conda remove again" ) common.check_write('remove', prefix, json=args.json) common.ensure_override_channels_requires_channel(args, json=args.json) channel_urls = args.channel or () if args.use_local: from conda.fetch import fetch_index from conda.utils import url_path try: from conda_build.config import croot except ImportError: common.error_and_exit( "you need to have 'conda-build >= 1.7.1' installed" " to use the --use-local option", json=args.json, error_type="RuntimeError") # remove the cache such that a refetch is made, # this is necessary because we add the local build repo URL fetch_index.cache = {} if exists(croot): channel_urls = [url_path(croot)] + list(channel_urls) index = common.get_index_trap(channel_urls=channel_urls, prepend=not args.override_channels, use_cache=args.use_index_cache, json=args.json, offline=args.offline) else: index = common.get_index_trap(channel_urls=channel_urls, prepend=not args.override_channels, use_cache=args.use_index_cache, json=args.json, offline=args.offline) specs = None if args.features: features = set(args.package_names) actions = plan.remove_features_actions(prefix, index, features) elif args.all: if plan.is_root_prefix(prefix): common.error_and_exit( 'cannot remove root environment,\n' ' add -n NAME or -p PREFIX option', json=args.json, error_type="CantRemoveRoot") actions = {inst.PREFIX: prefix} for dist in sorted(linked(prefix)): plan.add_unlink(actions, dist) else: specs = common.specs_from_args(args.package_names) if (plan.is_root_prefix(prefix) and common.names_in_specs(common.root_no_rm, specs)): common.error_and_exit('cannot remove %s from root environment' % ', '.join(common.root_no_rm), json=args.json, error_type="CantRemoveFromRoot") actions = plan.remove_actions(prefix, specs, index=index, pinned=args.pinned) if plan.nothing_to_do(actions): if args.all: rm_rf(prefix) if args.json: common.stdout_json({'success': True, 'actions': actions}) return common.error_and_exit('no packages found to remove from ' 'environment: %s' % prefix, json=args.json, error_type="PackageNotInstalled") if not args.json: print() print("Package plan for package removal in environment %s:" % prefix) plan.display_actions(actions, index) if args.json and args.dry_run: common.stdout_json({ 'success': True, 'dry_run': True, 'actions': actions }) return if not args.json: common.confirm_yn(args) if args.json and not args.quiet: with json_progress_bars(): plan.execute_actions(actions, index, verbose=not args.quiet) else: plan.execute_actions(actions, index, verbose=not args.quiet) if specs: try: with open(join(prefix, 'conda-meta', 'history'), 'a') as f: f.write('# remove specs: %s\n' % specs) except IOError as e: if e.errno == errno.EACCES: log.debug("Can't write the history file") else: raise if args.all: rm_rf(prefix) if args.json: common.stdout_json({'success': True, 'actions': actions})
elif args.all: if plan.is_root_prefix(prefix): common.error_and_exit('cannot remove root environment,\n' ' add -n NAME or -p PREFIX option', json=args.json, error_type="CantRemoveRoot") <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD actions = {inst.PREFIX: prefix} for dist in sorted(linked(prefix)): plan.add_unlink(actions, dist) ======= actions = {inst.PREFIX: [prefix], inst.UNLINK: [sorted(linked(prefix))]} >>>>>>> conda/feature/instruction-arguments ======= actions = {inst.PREFIX: [prefix], inst.UNLINK: [sorted(linked(prefix))]} >>>>>>> origin/feature/instruction-arguments ======= actions = {inst.PREFIX: [prefix], inst.UNLINK: [sorted(linked(prefix))]} <<<<<<< HEAD >>>>>>> princeofdarkness76/feature/instruction-arguments =======
def install(args, parser, command='install'): """ conda install, conda update, and conda create """ newenv = bool(command == 'create') if newenv: common.ensure_name_or_prefix(args, command) prefix = common.get_prefix(args, search=not newenv) if newenv: check_prefix(prefix, json=args.json) if command == 'update': if args.all: if args.packages: common.error_and_exit("""--all cannot be used with packages""", json=args.json, error_type="ValueError") else: if len(args.packages) == 0: common.error_and_exit("""no package names supplied # If you want to update to a newer version of Anaconda, type: # # $ conda update --prefix %s anaconda """ % prefix, json=args.json, error_type="ValueError") if command == 'update': linked = ci.linked(prefix) for name in args.packages: common.arg2spec(name, json=args.json) if '=' in name: common.error_and_exit("Invalid package name: '%s'" % (name), json=args.json, error_type="ValueError") if name not in set(ci.name_dist(d) for d in linked): common.error_and_exit("package '%s' is not installed in %s" % (name, prefix), json=args.json, error_type="ValueError") if newenv and args.clone: if args.packages: common.error_and_exit('did not expect any arguments for --clone', json=args.json, error_type="ValueError") clone(args.clone, prefix, json=args.json, quiet=args.quiet) touch_nonadmin(prefix) if not args.json: print_activate(args.name if args.name else prefix) return if newenv and not args.no_default_packages: default_packages = config.create_default_packages[:] # Override defaults if they are specified at the command line for default_pkg in config.create_default_packages: if any(pkg.split('=')[0] == default_pkg for pkg in args.packages): default_packages.remove(default_pkg) args.packages.extend(default_packages) common.ensure_override_channels_requires_channel(args) channel_urls = args.channel or () specs = [] if args.file: specs.extend(common.specs_from_url(args.file, json=args.json)) elif getattr(args, 'all', False): linked = ci.linked(prefix) for pkg in linked: name, ver, build = pkg.rsplit('-', 2) if name == 'python' and ver.startswith('2'): # Oh Python 2... specs.append('%s >=%s,<3' % (name, ver)) else: specs.append('%s >=%s' % (name, ver)) specs.extend(common.specs_from_args(args.packages, json=args.json)) if command == 'install' and args.revision: get_revision(args.revision, json=args.json) else: common.check_specs(prefix, specs, json=args.json) if args.use_local: from conda.fetch import fetch_index from conda.utils import url_path try: from conda_build import config as build_config except ImportError: common.error_and_exit( "you need to have 'conda-build' installed" " to use the --use-local option", json=args.json, error_type="RuntimeError") # remove the cache such that a refetch is made, # this is necessary because we add the local build repo URL fetch_index.cache = {} index = common.get_index_trap([url_path(build_config.croot)], use_cache=args.use_index_cache, unknown=args.unknown, json=args.json) else: index = common.get_index_trap(channel_urls=channel_urls, prepend=not args.override_channels, use_cache=args.use_index_cache, unknown=args.unknown, json=args.json) # Don't update packages that are already up-to-date if command == 'update' and not args.all: r = Resolve(index) orig_packages = args.packages[:] for name in orig_packages: installed_metadata = [ ci.is_linked(prefix, dist) for dist in linked ] vers_inst = [ dist.rsplit('-', 2)[1] for dist in linked if dist.rsplit('-', 2)[0] == name ] build_inst = [ m['build_number'] for m in installed_metadata if m['name'] == name ] try: assert len(vers_inst) == 1, name assert len(build_inst) == 1, name except AssertionError as e: if args.json: common.exception_and_exit(e, json=True) else: raise pkgs = sorted(r.get_pkgs(MatchSpec(name))) if not pkgs: # Shouldn't happen? continue latest = pkgs[-1] if latest.version == vers_inst[ 0] and latest.build_number == build_inst[0]: args.packages.remove(name) if not args.packages: from conda.cli.main_list import print_packages if not args.json: regex = '^(%s)$' % '|'.join(orig_packages) print('# All requested packages already installed.') print_packages(prefix, regex) else: common.stdout_json_success( message='All requested packages already installed.') return # handle tar file containing conda packages if len(args.packages) == 1: tar_path = args.packages[0] if tar_path.endswith('.tar'): install_tar(prefix, tar_path, verbose=not args.quiet) return # handle explicit installs of conda packages if args.packages and all(s.endswith('.tar.bz2') for s in args.packages): from conda.misc import install_local_packages install_local_packages(prefix, args.packages, verbose=not args.quiet) return if any(s.endswith('.tar.bz2') for s in args.packages): common.error_and_exit( "cannot mix specifications with conda package filenames", json=args.json, error_type="ValueError") if args.force: args.no_deps = True spec_names = set(s.split()[0] for s in specs) if args.no_deps: only_names = spec_names else: only_names = None if not isdir(prefix) and not newenv: if args.mkdir: try: os.makedirs(prefix) except OSError: common.error_and_exit("Error: could not create directory: %s" % prefix, json=args.json, error_type="OSError") else: common.error_and_exit("""\ environment does not exist: %s # # Use 'conda create' to create an environment before installing packages # into it. #""" % prefix, json=args.json, error_type="NoEnvironmentFound") try: if command == 'install' and args.revision: actions = plan.revert_actions(prefix, get_revision(args.revision)) else: actions = plan.install_actions(prefix, index, specs, force=args.force, only_names=only_names, pinned=args.pinned, minimal_hint=args.alt_hint) except NoPackagesFound as e: error_message = e.args[0] packages = {index[fn]['name'] for fn in index} for pkg in e.pkgs: close = get_close_matches(pkg, packages) if close: error_message += "\n\nDid you mean one of these?\n %s" % ( ', '.join(close)) error_message += '\n\nYou can search for this package on Binstar with' error_message += '\n\n binstar search -t conda %s' % pkg error_message += '\n\nYou may need to install the Binstar command line client with' error_message += '\n\n conda install binstar' common.error_and_exit(error_message, json=args.json) except SystemExit as e: # Unsatisfiable package specifications/no such revision/import error error_type = 'UnsatisfiableSpecifications' if e.args and 'could not import' in e.args[0]: error_type = 'ImportError' common.exception_and_exit(e, json=args.json, newline=True, error_text=False, error_type=error_type) if plan.nothing_to_do(actions): from conda.cli.main_list import print_packages if not args.json: regex = '^(%s)$' % '|'.join(spec_names) print('\n# All requested packages already installed.') print_packages(prefix, regex) else: common.stdout_json_success( message='All requested packages already installed.') return if not args.json: print() print("Package plan for installation in environment %s:" % prefix) plan.display_actions(actions, index) if command in {'install', 'update'}: common.check_write(command, prefix) if not args.json: if not pscheck.main(args): common.confirm_yn(args) else: if (sys.platform == 'win32' and not args.force_pscheck and not pscheck.check_processes(verbose=False)): common.error_and_exit( "Cannot continue operation while processes " "from packages are running without --force-pscheck.", json=True, error_type="ProcessesStillRunning") elif args.dry_run: common.stdout_json_success(actions=actions, dry_run=True) sys.exit(0) with common.json_progress_bars(json=args.json and not args.quiet): try: plan.execute_actions(actions, index, verbose=not args.quiet) except RuntimeError as e: if len(e.args) > 0 and "LOCKERROR" in e.args[0]: error_type = "AlreadyLocked" else: error_type = "RuntimeError" common.exception_and_exit(e, error_type=error_type, json=args.json) except SystemExit as e: common.exception_and_exit(e, json=args.json) if newenv: touch_nonadmin(prefix) if not args.json: print_activate(args.name if args.name else prefix) if args.json: common.stdout_json_success(actions=actions)
def explicit(specs, prefix, verbose=False, force_extract=True, fetch_args=None): actions = defaultdict(list) actions['PREFIX'] = prefix actions[ 'op_order'] = RM_FETCHED, FETCH, RM_EXTRACTED, EXTRACT, UNLINK, LINK linked = {install.name_dist(dist): dist for dist in install.linked(prefix)} fetch_args = fetch_args or {} index = {} verifies = [] channels = {} for spec in specs: if spec == '@EXPLICIT': continue # Format: (url|path)(:#md5)? m = url_pat.match(spec) if m is None: sys.exit('Could not parse explicit URL: %s' % spec) url, md5 = m.group('url') + '/' + m.group('fn'), m.group('md5') if not is_url(url): if not isfile(url): sys.exit('Error: file not found: %s' % url) url = utils.url_path(url) url_p, fn = url.rsplit('/', 1) # See if the URL refers to a package in our cache prefix = pkg_path = dir_path = None if url_p.startswith('file://'): prefix = install.cached_url(url) # If not, determine the channel name from the URL if prefix is None: _, schannel = url_channel(url) prefix = '' if schannel == 'defaults' else schannel + '::' fn = prefix + fn dist = fn[:-8] pkg_path = install.is_fetched(dist) dir_path = install.is_extracted(dist) # Don't re-fetch unless there is an MD5 mismatch if pkg_path and (md5 and md5_file(pkg_path) != md5): # This removes any extracted copies as well actions[RM_FETCHED].append(dist) pkg_path = dir_path = None # Don't re-extract unless forced, or if we can't check the md5 if dir_path and (force_extract or md5 and not pkg_path): actions[RM_EXTRACTED].append(dist) dir_path = None if not dir_path: if not pkg_path: _, conflict = install.find_new_location(dist) if conflict: actions[RM_FETCHED].append(conflict) channels[url_p + '/'] = (schannel, 0) actions[FETCH].append(dist) verifies.append((dist + '.tar.bz2', md5)) actions[EXTRACT].append(dist) # unlink any installed package with that name name = install.name_dist(dist) if name in linked: actions[UNLINK].append(linked[name]) actions[LINK].append(dist) # Pull the repodata for channels we are using if channels: index.update(fetch_index(channels, **fetch_args)) # Finish the MD5 verification for fn, md5 in verifies: info = index.get(fn) if info is None: sys.exit("Error: no package '%s' in index" % fn) if md5 and 'md5' not in info: sys.stderr.write('Warning: cannot lookup MD5 of: %s' % fn) if md5 and info['md5'] != md5: sys.exit('MD5 mismatch for: %s\n spec: %s\n repo: %s' % (fn, md5, info['md5'])) execute_actions(actions, index=index, verbose=verbose) return actions
def get_installed_version(prefix, name): for dist in install.linked(prefix): n, v, b = dist.rsplit('-', 2) if n == name: return v return None
def execute(args, parser): import sys import conda.plan as plan from conda.cli import pscheck from conda.install import rm_rf, linked from conda import config if not (args.all or args.package_names): common.error_and_exit( 'no package names supplied,\n' ' try "conda remove -h" for more details', json=args.json, error_type="ValueError") prefix = common.get_prefix(args) if args.all and prefix == config.default_prefix: common.error_and_exit( "cannot remove current environment. deactivate and run conda remove again" ) common.check_write('remove', prefix, json=args.json) common.ensure_override_channels_requires_channel(args, json=args.json) channel_urls = args.channel or () if args.use_local: from conda.fetch import fetch_index from conda.utils import url_path try: from conda_build.config import croot except ImportError: common.error_and_exit( "you need to have 'conda-build >= 1.7.1' installed" " to use the --use-local option", json=args.json, error_type="RuntimeError") # remove the cache such that a refetch is made, # this is necessary because we add the local build repo URL fetch_index.cache = {} index = common.get_index_trap(channel_urls=[url_path(croot)] + list(channel_urls), prepend=not args.override_channels, use_cache=args.use_index_cache, json=args.json) else: index = common.get_index_trap(channel_urls=channel_urls, prepend=not args.override_channels, use_cache=args.use_index_cache, json=args.json) if args.features: features = set(args.package_names) actions = plan.remove_features_actions(prefix, index, features) elif args.all: if plan.is_root_prefix(prefix): common.error_and_exit( 'cannot remove root environment,\n' ' add -n NAME or -p PREFIX option', json=args.json, error_type="CantRemoveRoot") actions = {plan.PREFIX: prefix, plan.UNLINK: sorted(linked(prefix))} else: specs = common.specs_from_args(args.package_names) if (plan.is_root_prefix(prefix) and common.names_in_specs(common.root_no_rm, specs)): common.error_and_exit('cannot remove %s from root environment' % ', '.join(common.root_no_rm), json=args.json, error_type="CantRemoveFromRoot") actions = plan.remove_actions(prefix, specs, index=index, pinned=args.pinned) if plan.nothing_to_do(actions): if args.all: rm_rf(prefix) if args.json: common.stdout_json({'success': True, 'actions': actions}) return common.error_and_exit('no packages found to remove from ' 'environment: %s' % prefix, json=args.json, error_type="PackageNotInstalled") if not args.json: print() print("Package plan for package removal in environment %s:" % prefix) plan.display_actions(actions, index) if args.json and args.dry_run: common.stdout_json({ 'success': True, 'dry_run': True, 'actions': actions }) return if not args.json: if not pscheck.main(args): common.confirm_yn(args) elif (sys.platform == 'win32' and not args.force_pscheck and not pscheck.check_processes(verbose=False)): common.error_and_exit( "Cannot continue removal while processes " "from packages are running without --force-pscheck.", json=True, error_type="ProcessesStillRunning") if args.json and not args.quiet: with json_progress_bars(): plan.execute_actions(actions, index, verbose=not args.quiet) else: plan.execute_actions(actions, index, verbose=not args.quiet) if args.all: rm_rf(prefix) if args.json: common.stdout_json({'success': True, 'actions': actions})