def test_single_constraint(self):
        # Given
        request = Request()
        request.install(Requirement._from_string("numpy < 1.8"))
        r_php_parts = [
            ('install', 'numpy', ('VersionConstraint("<", "1.8.0.0")',)),
        ]

        # When
        php_parts = request_to_php_parts(request)

        # Then
        self.assertEqual(php_parts, r_php_parts)
    def test_no_constraints(self):
        # Given
        request = Request()
        request.install(Requirement._from_string("numpy"))
        r_php_parts = [
            ('install', 'numpy', tuple()),
        ]

        # When
        php_parts = request_to_php_parts(request)

        # Then
        self.assertEqual(php_parts, r_php_parts)
Beispiel #3
0
    def from_yaml(cls, filename):
        with open(filename) as fp:
            data = yaml.load(fp)

        packages = collections.OrderedDict(
            parse_package_list(data.get("packages", [])))
        operations = data.get("request", [])

        request = Request()

        for operation in operations:
            kind = operation["operation"]
            requirement = Requirement._from_string(operation["requirement"])
            getattr(request, kind)(requirement)

        decisions = data.get("decisions", {})

        operations = []
        for operation in data.get("transaction", []):
            if operation["kind"] == "install":
                operations.append(InstallOperation(operation["package"]))
            elif operation["kind"] == "update":
                operations.append(
                    UpdateOperation(operation["from"], operation["to"]))
            elif operation["kind"] == "remove":
                operations.append(RemoveOperation(operation["package"]))
            else:
                msg = "invalid operation kind {!r}".format(operation["kind"])
                raise ValueError(msg)

        return cls(packages, [remote_repository(data, packages)],
                   installed_repository(data, packages), request, decisions,
                   operations)
Beispiel #4
0
def generate_rules_for_requirement(pool, requirement, installed_map=None):
    """Generate CNF rules for a requirement.

    Parameters
    ----------
    pool: Pool
        Package constraints.
    requirement: Requirement
        Package to be installed.

    Returns
    -------
    rules: list
        Package rules describing the given scenario.
    """
    request = Request()
    request.install(requirement)

    rules_generator = RulesGenerator(pool, request, installed_map)
    rules = list(rules_generator.iter_rules())
    return rules
Beispiel #5
0
def generate_rules_for_requirement(pool, requirement, installed_map=None):
    """Generate CNF rules for a requirement.

    Parameters
    ----------
    pool: Pool
        Package constraints.
    requirement: Requirement
        Package to be installed.

    Returns
    -------
    rules: list
        Package rules describing the given scenario.
    """
    request = Request()
    request.install(requirement)

    rules_generator = RulesGenerator(pool, request, installed_map)
    rules = list(rules_generator.iter_rules())
    return rules
Beispiel #6
0
def remove():
    """ Remove the given requirement.

    Parameter is expected to be a JSON UTF8 encoded bytestring passed through
    stdin.
    """
    json_string = _read_stdin_as_bytes()
    config, requirement = install_parse_json_string(json_string)

    session = Session.authenticated_from_configuration(config)
    repository = repository_factory(session, config.repositories)

    progress_bar_context = ProgressBarContext(console_progress_manager_factory,
                                              fetch=fetch_progress_factory)
    enpkg = Enpkg(repository, session, [sys.prefix], progress_bar_context, False)

    solver = enpkg._solver_factory()
    try:
        request = Request()
        request.remove(requirement)
        enpkg.execute(solver.resolve(request))
    except NotInstalledPackage as e:
        print(str(e))
Beispiel #7
0
def dispatch_commands_with_enpkg(args, enpkg, config, prefix, session, parser,
                                 pat):
    if args.dry_run:
        def print_actions(actions):
            for item in actions:
                print('%-8s %s' % item)
        enpkg.execute = print_actions

    if args.imports:                              # --imports
        repository = Repository._from_prefixes(enpkg.prefixes)
        imports_option(repository)
        return

    if args.revert:                               # --revert
        revert(enpkg, args.revert)
        return

    # Try to auto-update enstaller
    if config.autoupdate:
        if update_enstaller(session, enpkg._remote_repository, args):
            print("Enstaller has been updated.\n"
                  "Please re-run your previous command.")
            return

    if args.search:                               # --search
        search(enpkg._remote_repository, enpkg._installed_repository,
               config, session, pat)
        return

    if args.info:                                 # --info
        if len(args.cnames) != 1:
            parser.error("Option requires one argument (name of package)")
        info_option(enpkg._remote_repository, enpkg._installed_repository,
                    args.cnames[0])
        return

    if args.whats_new:                            # --whats-new
        whats_new(enpkg._remote_repository, enpkg._installed_repository)
        return

    if args.update_all:                           # --update-all
        update_all(enpkg, config, args)
        return

    if args.requirements:
        install_from_requirements(enpkg, config, args)
        return

    if len(args.cnames) == 0 and not args.remove_enstaller:
        parser.error("Requirement(s) missing")
    elif len(args.cnames) == 2:
        pat = re.compile(r'\d+\.\d+')
        if pat.match(args.cnames[1]):
            args.cnames = ['-'.join(args.cnames)]

    reqs = _compute_reqs(args.cnames)

    # This code assumes we have already upgraded enstaller if needed
    if needs_to_downgrade_enstaller(reqs):
        msg = "Enstaller in requirement list: enstaller will be downgraded !"
        warnings.warn(msg)
    else:
        logger.debug("Enstaller is up to date, not updating")
        reqs = [req for req in reqs if req.name != "enstaller"]

    logger.info("Requirements:")
    for req in reqs:
        logger.info('    %r', req)

    logger.info("prefix: %r", prefix)

    REMOVE_ENSTALLER_WARNING = ("Removing enstaller package will break enpkg "
                                "and is not recommended.")
    if args.remove:
        if any(req.name == 'enstaller' for req in reqs):
            print(REMOVE_ENSTALLER_WARNING)
            print("If you are sure you wish to remove enstaller, use:")
            print("    enpkg --remove-enstaller")
            return

    if args.remove_enstaller:
        print(REMOVE_ENSTALLER_WARNING)
        if prompt_yes_no("Really remove enstaller? (y/[n]) ", args.yes):
            args.remove = True
            reqs = [Requirement('enstaller')]

    if any(req.name == 'epd' for req in reqs):
        if args.remove:
            parser.error("Can't remove 'epd'")
        elif len(reqs) > 1:
            parser.error("Can't combine 'enpkg epd' with other packages.")
        elif not epd_install_confirm(args.yes):
            return

    if args.remove:
        for req in reqs:
            solver = enpkg._solver_factory()
            try:
                request = Request()
                request.remove(req)
                enpkg.execute(solver.resolve(request))
            except EnpkgError as e:
                print(str(e))
    else:
        for req in reqs:
            install_req(enpkg, config, req, args)
Beispiel #8
0
    # Step 2: recurse
    for dependency in best_dependencies:
        solution = optimize_at_level(pool, dependency, new_rules, solution)

    return solution


def optimize(pool, requirement, rules):
    best_package = find_best_candidate(pool, requirement, rules)
    solution = [best_package]
    return optimize_at_level(pool, best_package, rules, solution)


if __name__ == '__main__':
    repository = repository_from_index("full_index.json")
    pool = Pool([repository])

    requirement_str = "scikit_learn < 0.14"
    requirement = Requirement._from_string(requirement_str)

    request = Request()
    request.install(requirement)

    rules_generator = RulesGenerator(pool, request)
    rules = list(rules_generator.iter_rules())

    solution = optimize(pool, requirement, rules)
    for decision in solution:
        print(decision.name, str(decision.version))
Beispiel #9
0
    # Step 2: recurse
    for dependency in best_dependencies:
        solution = optimize_at_level(pool, dependency, new_rules, solution)

    return solution


def optimize(pool, requirement, rules):
    best_package = find_best_candidate(pool, requirement, rules)
    solution = [best_package]
    return optimize_at_level(pool, best_package, rules, solution)


if __name__ == '__main__':
    repository = repository_from_index("full_index.json")
    pool = Pool([repository])

    requirement_str = "scikit_learn < 0.14"
    requirement = Requirement._from_string(requirement_str)

    request = Request()
    request.install(requirement)

    rules_generator = RulesGenerator(pool, request)
    rules = list(rules_generator.iter_rules())

    solution = optimize(pool, requirement, rules)
    for decision in solution:
        print(decision.name, str(decision.version))
Beispiel #10
0
def install_req(enpkg, config, req, opts):
    """
    Try to execute the install actions.
    """
    # Unix exit-status codes
    FAILURE = 1
    req = Requirement.from_anything(req)
    request = Request()
    request.install(req)

    def _done(exit_status):
        sys.exit(exit_status)

    def _get_unsupported_packages(actions):
        ret = []
        for opcode, egg in actions:
            if opcode == "install":
                name, version = egg_name_to_name_version(egg)
                package = enpkg._remote_repository.find_package(name, version)
                if package.product == "pypi":
                    ret.append(package)
        return ret

    def _ask_pypi_confirmation(package_list_string):
        msg = textwrap.dedent("""\
        The following packages/requirements are coming from the PyPi repo:

        {0}

        The PyPi repository which contains >10,000 untested ("as is")
        packages. Some packages are licensed under GPL or other licenses
        which are prohibited for some users. Dependencies may not be
        provided. If you need an updated version or if the installation
        fails due to unmet dependencies, the Knowledge Base article
        Installing external packages into Canopy Python
        (https://support.enthought.com/entries/23389761) may help you with
        installing it.
        """.format(package_list_string))
        print(msg)

        msg = "Are you sure that you wish to proceed?  (y/[n])"
        if not prompt_yes_no(msg, opts.yes):
            sys.exit(0)

    def _ask_pypi_confirmation_from_actions(actions):
        unsupported_packages = _get_unsupported_packages(actions)
        if len(unsupported_packages) > 0:
            package_list = sorted("'{0}-{1}'".format(p.name, p.full_version)
                                  for p in unsupported_packages)
            package_list_string = "\n".join(package_list)
            _ask_pypi_confirmation(package_list_string)

    try:
        mode = 'root' if opts.no_deps else 'recur'
        pypi_asked = False
        solver = enpkg._solver_factory(mode, opts.force, opts.forceall)

        pypi_requirements = _requirement_from_pypi(request,
                                                   enpkg._remote_repository)

        try:
            actions = solver.resolve(request)
        except MissingDependency as e:
            if len(pypi_requirements) > 0:
                msg = _BROKEN_PYPI_TEMPLATE.format(requested=e.requester,
                                                   dependency=e.requirement)
                print(msg)
            else:
                print("One of the requested package has broken dependencies")
                print("(Dependency solving error: {0})".format(e))
            _done(FAILURE)

        if len(pypi_requirements) > 0:
            package_list = sorted(str(p) for p in pypi_requirements)
            _ask_pypi_confirmation("\n".join(package_list))
            pypi_asked = True

        installed = (egg for opcode, egg in actions if opcode == "install")
        actions = [("fetch", egg) for egg in installed] + actions

        if _is_any_package_unavailable(enpkg._remote_repository, actions):
            _notify_unavailable_package(config, req, enpkg._session)
            _done(FAILURE)
        if not pypi_asked:
            _ask_pypi_confirmation_from_actions(actions)
        enpkg.execute(actions)
        if len(actions) == 0:
            print("No update necessary, %r is up-to-date." % req.name)
            print(install_time_string(enpkg._installed_repository,
                                      req.name))
    except NoPackageFound as e:
        print(str(e))
        _done(FAILURE)
    except OSError as e:
        if e.errno == errno.EACCES and sys.platform == 'darwin':
            print("Install failed. OSX install requires admin privileges.")
            print("You should add 'sudo ' before the 'enpkg' command.")
            _done(FAILURE)
        else:
            raise
Beispiel #11
0
def dispatch_commands_with_enpkg(args, enpkg, config, prefix, session, parser,
                                 pat):
    if args.dry_run:

        def print_actions(actions):
            for item in actions:
                print('%-8s %s' % item)

        enpkg.execute = print_actions

    if args.imports:  # --imports
        repository = Repository._from_prefixes(enpkg.prefixes)
        imports_option(repository)
        return

    if args.revert:  # --revert
        revert(enpkg, args.revert)
        return

    # Try to auto-update enstaller
    if config.autoupdate:
        if update_enstaller(session, enpkg._remote_repository, args):
            print("Enstaller has been updated.\n"
                  "Please re-run your previous command.")
            return

    if args.search:  # --search
        search(enpkg._remote_repository, enpkg._installed_repository, config,
               session, pat)
        return

    if args.info:  # --info
        if len(args.cnames) != 1:
            parser.error("Option requires one argument (name of package)")
        info_option(enpkg._remote_repository, enpkg._installed_repository,
                    args.cnames[0])
        return

    if args.whats_new:  # --whats-new
        whats_new(enpkg._remote_repository, enpkg._installed_repository)
        return

    if args.update_all:  # --update-all
        update_all(enpkg, config, args)
        return

    if args.requirements:
        install_from_requirements(enpkg, config, args)
        return

    if len(args.cnames) == 0 and not args.remove_enstaller:
        parser.error("Requirement(s) missing")
    elif len(args.cnames) == 2:
        pat = re.compile(r'\d+\.\d+')
        if pat.match(args.cnames[1]):
            args.cnames = ['-'.join(args.cnames)]

    reqs = _compute_reqs(args.cnames)

    # This code assumes we have already upgraded enstaller if needed
    if needs_to_downgrade_enstaller(reqs):
        msg = "Enstaller in requirement list: enstaller will be downgraded !"
        warnings.warn(msg)
    else:
        logger.debug("Enstaller is up to date, not updating")
        reqs = [req for req in reqs if req.name != "enstaller"]

    logger.info("Requirements:")
    for req in reqs:
        logger.info('    %r', req)

    logger.info("prefix: %r", prefix)

    REMOVE_ENSTALLER_WARNING = ("Removing enstaller package will break enpkg "
                                "and is not recommended.")
    if args.remove:
        if any(req.name == 'enstaller' for req in reqs):
            print(REMOVE_ENSTALLER_WARNING)
            print("If you are sure you wish to remove enstaller, use:")
            print("    enpkg --remove-enstaller")
            return

    if args.remove_enstaller:
        print(REMOVE_ENSTALLER_WARNING)
        if prompt_yes_no("Really remove enstaller? (y/[n]) ", args.yes):
            args.remove = True
            reqs = [Requirement('enstaller')]

    if any(req.name == 'epd' for req in reqs):
        if args.remove:
            parser.error("Can't remove 'epd'")
        elif len(reqs) > 1:
            parser.error("Can't combine 'enpkg epd' with other packages.")
        elif not epd_install_confirm(args.yes):
            return

    if args.remove:
        for req in reqs:
            solver = enpkg._solver_factory()
            try:
                request = Request()
                request.remove(req)
                enpkg.execute(solver.resolve(request))
            except EnpkgError as e:
                print(str(e))
    else:
        for req in reqs:
            install_req(enpkg, config, req, args)
Beispiel #12
0
def install_req(enpkg, config, req, opts):
    """
    Try to execute the install actions.
    """
    # Unix exit-status codes
    FAILURE = 1
    req = Requirement.from_anything(req)
    request = Request()
    request.install(req)

    def _done(exit_status):
        sys.exit(exit_status)

    def _get_unsupported_packages(actions):
        ret = []
        for opcode, egg in actions:
            if opcode == "install":
                name, version = egg_name_to_name_version(egg)
                package = enpkg._remote_repository.find_package(name, version)
                if package.product == "pypi":
                    ret.append(package)
        return ret

    def _ask_pypi_confirmation(package_list_string):
        msg = textwrap.dedent("""\
        The following packages/requirements are coming from the PyPi repo:

        {0}

        The PyPi repository which contains >10,000 untested ("as is")
        packages. Some packages are licensed under GPL or other licenses
        which are prohibited for some users. Dependencies may not be
        provided. If you need an updated version or if the installation
        fails due to unmet dependencies, the Knowledge Base article
        Installing external packages into Canopy Python
        (https://support.enthought.com/entries/23389761) may help you with
        installing it.
        """.format(package_list_string))
        print(msg)

        msg = "Are you sure that you wish to proceed?  (y/[n])"
        if not prompt_yes_no(msg, opts.yes):
            sys.exit(0)

    def _ask_pypi_confirmation_from_actions(actions):
        unsupported_packages = _get_unsupported_packages(actions)
        if len(unsupported_packages) > 0:
            package_list = sorted("'{0}-{1}'".format(p.name, p.full_version)
                                  for p in unsupported_packages)
            package_list_string = "\n".join(package_list)
            _ask_pypi_confirmation(package_list_string)

    try:
        mode = 'root' if opts.no_deps else 'recur'
        pypi_asked = False
        solver = enpkg._solver_factory(mode, opts.force, opts.forceall)

        pypi_requirements = _requirement_from_pypi(request,
                                                   enpkg._remote_repository)

        try:
            actions = solver.resolve(request)
        except MissingDependency as e:
            if len(pypi_requirements) > 0:
                msg = _BROKEN_PYPI_TEMPLATE.format(requested=e.requester,
                                                   dependency=e.requirement)
                print(msg)
            else:
                print("One of the requested package has broken dependencies")
                print("(Dependency solving error: {0})".format(e))
            _done(FAILURE)

        if len(pypi_requirements) > 0:
            package_list = sorted(str(p) for p in pypi_requirements)
            _ask_pypi_confirmation("\n".join(package_list))
            pypi_asked = True

        installed = (egg for opcode, egg in actions if opcode == "install")
        actions = [("fetch", egg) for egg in installed] + actions

        if _is_any_package_unavailable(enpkg._remote_repository, actions):
            _notify_unavailable_package(config, req, enpkg._session)
            _done(FAILURE)
        if not pypi_asked:
            _ask_pypi_confirmation_from_actions(actions)
        enpkg.execute(actions)
        if len(actions) == 0:
            print("No update necessary, %r is up-to-date." % req.name)
            print(install_time_string(enpkg._installed_repository, req.name))
    except NoPackageFound as e:
        print(str(e))
        _done(FAILURE)
    except OSError as e:
        if e.errno == errno.EACCES and sys.platform == 'darwin':
            print("Install failed. OSX install requires admin privileges.")
            print("You should add 'sudo ' before the 'enpkg' command.")
            _done(FAILURE)
        else:
            raise