Пример #1
0
def make_temp_condarc(value=None):
    try:
        tempfile = NamedTemporaryFile(suffix='.yml', delete=False)
        tempfile.close()
        temp_path = tempfile.name
        if value:
            with open(temp_path, 'w') as f:
                f.write(value)
        reset_context([temp_path])
        yield temp_path
    finally:
        rm_rf(temp_path)
Пример #2
0
def make_temp_condarc(value=None):
    try:
        tempfile = NamedTemporaryFile(suffix='.yml', delete=False)
        tempfile.close()
        temp_path = tempfile.name
        if value:
            with open(temp_path, 'w') as f:
                f.write(value)
        reset_context([temp_path])
        yield temp_path
    finally:
        rm_rf(temp_path)
Пример #3
0
def execute(args, parser):
    name = args.remote_definition or args.name

    try:
        spec = specs.detect(name=name,
                            filename=args.file,
                            directory=os.getcwd())
        env = spec.environment

        # FIXME conda code currently requires args to have a name or prefix
        # don't overwrite name if it's given. gh-254
        if args.prefix is None and args.name is None:
            args.name = env.name

    except exceptions.SpecNotFound:
        raise

    prefix = get_prefix(args, search=False)

    if args.force and not is_root_prefix(prefix) and os.path.exists(prefix):
        rm_rf(prefix)
    cli_install.check_prefix(prefix, json=args.json)

    # TODO, add capability
    # common.ensure_override_channels_requires_channel(args)
    # channel_urls = args.channel or ()

    # special case for empty environment
    if not env.dependencies:
        from conda.install import symlink_conda
        from conda.base.context import context
        symlink_conda(prefix, context.root_dir)

    for installer_type, pkg_specs in env.dependencies.items():
        try:
            installer = get_installer(installer_type)
            installer.install(prefix, pkg_specs, args, env)
        except InvalidInstaller:
            sys.stderr.write(
                textwrap.dedent("""
                Unable to install package for {0}.

                Please double check and ensure you dependencies file has
                the correct spelling.  You might also try installing the
                conda-env-{0} package to see if provides the required
                installer.
                """).lstrip().format(installer_type))
            return -1

    touch_nonadmin(prefix)
    if not args.json:
        print(cli_install.print_activate(args.name if args.name else prefix))
Пример #4
0
def execute(args, parser):
    name = args.remote_definition or args.name

    try:
        spec = specs.detect(name=name, filename=args.file,
                            directory=os.getcwd())
        env = spec.environment

        # FIXME conda code currently requires args to have a name or prefix
        # don't overwrite name if it's given. gh-254
        if args.prefix is None and args.name is None:
            args.name = env.name

    except exceptions.SpecNotFound:
        raise

    prefix = get_prefix(args, search=False)

    if args.force and not is_root_prefix(prefix) and os.path.exists(prefix):
        rm_rf(prefix)
    cli_install.check_prefix(prefix, json=args.json)

    # TODO, add capability
    # common.ensure_override_channels_requires_channel(args)
    # channel_urls = args.channel or ()

    # special case for empty environment
    if not env.dependencies:
        from conda.install import symlink_conda
        from conda.base.context import context
        symlink_conda(prefix, context.root_dir)

    for installer_type, pkg_specs in env.dependencies.items():
        try:
            installer = get_installer(installer_type)
            installer.install(prefix, pkg_specs, args, env)
        except InvalidInstaller:
            sys.stderr.write(textwrap.dedent("""
                Unable to install package for {0}.

                Please double check and ensure you dependencies file has
                the correct spelling.  You might also try installing the
                conda-env-{0} package to see if provides the required
                installer.
                """).lstrip().format(installer_type)
            )
            return -1

    touch_nonadmin(prefix)
    if not args.json:
        print(cli_install.print_activate(args.name if args.name else prefix))
Пример #5
0
    def test_install_mkdir(self):
        try:
            prefix = make_temp_prefix()
            assert isdir(prefix)
            run_command(Commands.INSTALL, prefix, "python=3.5", "--mkdir")
            assert_package_is_installed(prefix, "python-3.5")

            rm_rf(prefix)
            assert not isdir(prefix)
            run_command(Commands.INSTALL, prefix, "python=3.5", "--mkdir")
            assert_package_is_installed(prefix, "python-3.5")

        finally:
            rmtree(prefix, ignore_errors=True)
Пример #6
0
def ensure_linked_actions(dists, prefix, index=None, force=False,
                          always_copy=False):
    actions = defaultdict(list)
    actions[inst.PREFIX] = prefix
    actions['op_order'] = (inst.RM_FETCHED, inst.FETCH, inst.RM_EXTRACTED,
                           inst.EXTRACT, inst.UNLINK, inst.LINK, inst.SYMLINK_CONDA)
    for dist in dists:
        fetched_in = is_fetched(dist)
        extracted_in = is_extracted(dist)

        if fetched_in and index is not None:
            # Test the MD5, and possibly re-fetch
            fn = dist + '.tar.bz2'
            try:
                if md5_file(fetched_in) != index[fn]['md5']:
                    # RM_FETCHED now removes the extracted data too
                    actions[inst.RM_FETCHED].append(dist)
                    # Re-fetch, re-extract, re-link
                    fetched_in = extracted_in = None
                    force = True
            except KeyError:
                sys.stderr.write('Warning: cannot lookup MD5 of: %s' % fn)

        if not force and is_linked(prefix, dist):
            continue

        if extracted_in and force:
            # Always re-extract in the force case
            actions[inst.RM_EXTRACTED].append(dist)
            extracted_in = None

        # Otherwise we need to extract, and possibly fetch
        if not extracted_in and not fetched_in:
            # If there is a cache conflict, clean it up
            fetched_in, conflict = find_new_location(dist)
            fetched_in = join(fetched_in, dist2filename(dist))
            if conflict is not None:
                actions[inst.RM_FETCHED].append(conflict)
            actions[inst.FETCH].append(dist)

        if not extracted_in:
            actions[inst.EXTRACT].append(dist)

        fetched_dist = extracted_in or fetched_in[:-8]
        fetched_dir = dirname(fetched_dist)

        try:
            # Determine what kind of linking is necessary
            if not extracted_in:
                # If not already extracted, create some dummy
                # data to test with
                rm_rf(fetched_dist)
                ppath = join(fetched_dist, 'info')
                os.makedirs(ppath)
                index_json = join(ppath, 'index.json')
                with open(index_json, 'w'):
                    pass
            if context.always_copy or always_copy:
                lt = LINK_COPY
            elif try_hard_link(fetched_dir, prefix, dist):
                lt = LINK_HARD
            elif context.allow_softlinks and not on_win:
                lt = LINK_SOFT
            else:
                lt = LINK_COPY
            actions[inst.LINK].append('%s %d' % (dist, lt))

        except (OSError, IOError):
            actions[inst.LINK].append('%s %d' % (dist, LINK_COPY))
        finally:
            if not extracted_in:
                # Remove the dummy data
                try:
                    rm_rf(fetched_dist)
                except (OSError, IOError):
                    pass

    return actions
Пример #7
0
def test_remove_link_to_file(tmpdir):
    dst_link = join(text_type(tmpdir), "test_link")
    src_file = join(text_type(tmpdir), "test_file")
    _write_file(src_file, "welcome to the ministry of silly walks")
    os.symlink(src_file, dst_link)
    assert rm_rf(dst_link) is True
Пример #8
0
 def __exit__(self, exc_type, exc_value, traceback):
     from conda.common.disk import rm_rf
     rm_rf(self.lock_file_path)
Пример #9
0
def execute(args, parser):
    import conda.plan as plan
    import conda.instructions as inst
    from conda.install import linked_data
    from conda.common.disk import rm_rf

    if not (args.all or args.package_names):
        raise CondaValueError('no package names supplied,\n'
                              '       try "conda remove -h" for more details')

    prefix = context.prefix_w_legacy_search
    if args.all and prefix == context.default_prefix:
        msg = "cannot remove current environment. deactivate and run conda remove again"
        raise CondaEnvironmentError(msg)
    check_write('remove', prefix, json=context.json)
    ensure_use_local(args)
    ensure_override_channels_requires_channel(args)
    channel_urls = args.channel or ()
    if not args.features and args.all:
        index = linked_data(prefix)
        index = {dist + '.tar.bz2': info for dist, info in iteritems(index)}
    else:
        index = get_index(channel_urls=channel_urls,
                          prepend=not args.override_channels,
                          use_local=args.use_local,
                          use_cache=args.use_index_cache,
                          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):
            raise CondaEnvironmentError(
                'cannot remove root environment,\n'
                '       add -n NAME or -p PREFIX option')
        actions = {inst.PREFIX: prefix}
        for fkey in sorted(iterkeys(index)):
            plan.add_unlink(actions, fkey[:-8])

    else:
        specs = specs_from_args(args.package_names)
        if (context.conda_in_root and plan.is_root_prefix(prefix)
                and names_in_specs(root_no_rm, specs)):
            raise CondaEnvironmentError(
                'cannot remove %s from root environment' %
                ', '.join(root_no_rm))
        actions = plan.remove_actions(prefix,
                                      specs,
                                      index=index,
                                      force=args.force,
                                      pinned=args.pinned)

    delete_trash()

    if plan.nothing_to_do(actions):
        if args.all:
            print()
            print("Remove all packages in environment %s:\n" % prefix)
            if not context.json:
                confirm_yn(args)
            rm_rf(prefix)

            if context.json:
                stdout_json({'success': True, 'actions': actions})
            return
        raise PackageNotFoundError(
            '', 'no packages found to remove from '
            'environment: %s' % prefix)

    if not context.json:
        print()
        print("Package plan for package removal in environment %s:" % prefix)
        plan.display_actions(actions, index)

    if context.json and args.dry_run:
        stdout_json({'success': True, 'dry_run': True, 'actions': actions})
        return

    if not context.json:
        confirm_yn(args)

    if context.json and not context.quiet:
        with json_progress_bars():
            plan.execute_actions(actions, index, verbose=not context.quiet)
    else:
        plan.execute_actions(actions, index, verbose=not context.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 context.json:
        stdout_json({'success': True, 'actions': actions})
Пример #10
0
def test_remove_dir(tmpdir):
    test_dir = "test"
    tmpdir.mkdir(test_dir)
    path = join(text_type(tmpdir), test_dir)
    assert rm_rf(path) is True
Пример #11
0
def execute(args, parser):
    import conda.plan as plan
    import conda.instructions as inst
    from conda.install import linked_data
    from conda.common.disk import rm_rf

    if not (args.all or args.package_names):
        raise CondaValueError('no package names supplied,\n'
                              '       try "conda remove -h" for more details')

    prefix = context.prefix_w_legacy_search
    if args.all and prefix == context.default_prefix:
        msg = "cannot remove current environment. deactivate and run conda remove again"
        raise CondaEnvironmentError(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 = linked_data(prefix)
        index = {dist + '.tar.bz2': info for dist, info in iteritems(index)}
    else:
        index = get_index(channel_urls=channel_urls,
                          prepend=not args.override_channels,
                          use_local=args.use_local,
                          use_cache=args.use_index_cache,
                          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):
            raise CondaEnvironmentError('cannot remove root environment,\n'
                                        '       add -n NAME or -p PREFIX option')
        actions = {inst.PREFIX: prefix}
        for fkey in sorted(iterkeys(index)):
            plan.add_unlink(actions, fkey[:-8])

    else:
        specs = specs_from_args(args.package_names)
        if (context.conda_in_root and plan.is_root_prefix(prefix) and
                names_in_specs(root_no_rm, specs)):
            raise CondaEnvironmentError('cannot remove %s from root environment' %
                                        ', '.join(root_no_rm))
        actions = plan.remove_actions(prefix, specs, index=index,
                                      force=args.force, pinned=args.pinned)

    if plan.nothing_to_do(actions):
        if args.all:
            print()
            print("Remove all packages in environment %s:\n" % prefix)
            if not args.json:
                confirm_yn(args)
            rm_rf(prefix)

            if args.json:
                stdout_json({
                    'success': True,
                    'actions': actions
                })
            return
        raise PackageNotFoundError('', 'no packages found to remove from '
                                   'environment: %s' % prefix)

    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
        })
Пример #12
0
def test_remove_link_to_dir(tmpdir):
    dst_link = join(text_type(tmpdir), "test_link")
    src_dir = join(text_type(tmpdir), "test_dir")
    tmpdir.mkdir("test_dir")
    os.symlink(src_dir, dst_link)
    assert rm_rf(dst_link) is True
Пример #13
0
def test_remove_link_to_file(tmpdir):
    dst_link = join(text_type(tmpdir), "test_link")
    src_file = join(text_type(tmpdir), "test_file")
    _write_file(src_file, "welcome to the ministry of silly walks")
    os.symlink(src_file, dst_link)
    assert rm_rf(dst_link) is True
Пример #14
0
def test_remove_file_to_trash(tmpdir):
    test_file = "test.txt"
    path = join(text_type(tmpdir), test_file)
    _write_file(path, "welcome to the ministry of silly walks")
    assert rm_rf(path) is True
Пример #15
0
def test_remove_dir(tmpdir):
    test_dir = "test"
    tmpdir.mkdir(test_dir)
    path = join(text_type(tmpdir), test_dir)
    assert rm_rf(path) is True
Пример #16
0
def test_remove_file_to_trash(tmpdir):
    test_file = "test.txt"
    path = join(text_type(tmpdir), test_file)
    _write_file(path, "welcome to the ministry of silly walks")
    assert rm_rf(path) is True
Пример #17
0
def test_remove_link_to_dir(tmpdir):
    dst_link = join(text_type(tmpdir), "test_link")
    src_dir = join(text_type(tmpdir), "test_dir")
    tmpdir.mkdir("test_dir")
    os.symlink(src_dir, dst_link)
    assert rm_rf(dst_link) is True
Пример #18
0
 def __exit__(self, exc_type, exc_value, traceback):
     from conda.common.disk import rm_rf
     rm_rf(self.lock_file_path)