Exemplo n.º 1
0
    def _check_solution(self, filename, prefer_installed=True):
        # Test that the solution described in the scenario file matches with
        # what the SAT solver computes.

        # Given
        scenario = Scenario.from_yaml(
            os.path.join(os.path.dirname(__file__), filename)
        )
        request = scenario.request

        # When
        pool = Pool(scenario.remote_repositories)
        pool.add_repository(scenario.installed_repository)
        policy = InstalledFirstPolicy(pool, scenario.installed_repository,
                                      prefer_installed=prefer_installed)
        solver = DependencySolver(
            pool, scenario.remote_repositories, scenario.installed_repository,
            policy=policy,
        )

        # Then
        try:
            transaction = solver.solve(request)
        except SatisfiabilityError as failure:
            if not scenario.failed:
                msg = "Solver unexpectedly failed"
                if failure.reason:
                    msg += " because {0}".format(failure.reason)
                self.fail(msg)
        else:
            if scenario.failed:
                msg = "Solver unexpectedly succeeded, but {0}."
                self.fail(msg.format(scenario.failure))
            self.assertEqualOperations(transaction.operations,
                                       scenario.operations)
Exemplo n.º 2
0
    def _check_solution(self, filename, prefer_installed=True):
        # Test that the solution described in the scenario file matches with
        # what the SAT solver computes.

        # Given
        scenario = Scenario.from_yaml(
            os.path.join(os.path.dirname(__file__), filename)
        )
        request = scenario.request

        # When
        pool = Pool(scenario.remote_repositories)
        pool.add_repository(scenario.installed_repository)

        solver = DependencySolver(
            pool, scenario.remote_repositories, scenario.installed_repository,
        )

        # Then
        try:
            transaction = solver.solve(request)
        except SatisfiabilityError as failure:
            self.assertFailureEqual(pool, failure, scenario)
        else:
            if scenario.failed:
                msg = "Solver unexpectedly succeeded, but {0}"
                self.fail(msg.format(scenario.failure['raw']))
            self.assertEqualOperations(transaction.operations,
                                       scenario.operations)
            if (scenario.pretty_operations or
                    transaction.operations != transaction.pretty_operations):
                self.assertEqualOperations(transaction.pretty_operations,
                                           scenario.pretty_operations)
Exemplo n.º 3
0
def solve_and_print(request, remote_repositories, installed_repository,
                    print_ids, prune=True, prefer_installed=True, debug=0,
                    simple=False, strict=False):
    pool = Pool(remote_repositories)
    pool.add_repository(installed_repository)

    policy = InstalledFirstPolicy(pool, installed_repository,
                                  prefer_installed=prefer_installed)
    solver = DependencySolver(
        pool, remote_repositories, installed_repository,
        policy=policy, use_pruning=prune, strict=strict)

    fmt = "ELAPSED : {description:20} : {elapsed:e}"
    try:
        transaction = solver.solve(request)
        if simple:
            print(transaction.to_simple_string())
        else:
            print(transaction)
    except SatisfiabilityError as e:
        msg = "UNSATISFIABLE: {}"
        print(msg.format(e.unsat.to_string(pool)))
        print(e.unsat._find_requirement_time.pretty(fmt), file=sys.stderr)

    if debug:
        counts, hist = solver._policy._log_histogram()
        print(hist, file=sys.stderr)
        report = solver._policy._log_report(with_assignments=debug > 1)
        print(report, file=sys.stderr)
    print(solver._last_rules_time.pretty(fmt), file=sys.stderr)
    print(solver._last_solver_init_time.pretty(fmt), file=sys.stderr)
    print(solver._last_solve_time.pretty(fmt), file=sys.stderr)
Exemplo n.º 4
0
def print_rules(request, remote_repositories, installed_repository):
    pool = Pool(remote_repositories)
    pool.add_repository(installed_repository)

    solver = DependencySolver(pool, remote_repositories, installed_repository)
    _, rules = solver._create_rules_and_initialize_policy(request)
    for rule in rules:
        print(rule.to_string(pool))
Exemplo n.º 5
0
def print_rules(request, remote_repositories, installed_repository):
    pool = Pool(remote_repositories)
    pool.add_repository(installed_repository)

    solver = DependencySolver(pool, remote_repositories, installed_repository)
    _, rules = solver._create_rules_and_initialize_policy(request)
    for rule in rules:
        print(rule.to_string(pool))
Exemplo n.º 6
0
    def _solve(self, top_core, flags={}, only_matching_vlnv=False):
        def eq_vln(this, that):
            return \
                this.vendor  == that.vendor and \
                this.library == that.library and \
                this.name    == that.name

        repo = Repository()
        _flags = flags.copy()
        cores = [x['core'] for x in self._cores.values()]
        for core in cores:
            if only_matching_vlnv:
                if not eq_vln(core.name, top_core):
                    continue

            package_str = "{} {}-{}".format(self._package_name(core.name),
                                            core.name.version,
                                            core.name.revision)
            if not only_matching_vlnv:
                _flags['is_toplevel'] = (core.name == top_core)
                _depends = core.get_depends(_flags)
                if _depends:
                    _s = "; depends ( {} )"
                    package_str += _s.format(self._parse_depend(_depends))
            parser = PrettyPackageStringParser(EnpkgVersion.from_string)

            package = parser.parse_to_package(package_str)
            package.core = core

            repo.add_package(package)

        request = Request()
        _top_dep = "{} {} {}".format(self._package_name(top_core),
                                     top_core.relation,
                                     self._package_version(top_core))
        requirement = Requirement._from_string(_top_dep)
        request.install(requirement)
        installed_repository = Repository()
        pool = Pool([repo])
        pool.add_repository(installed_repository)
        solver = DependencySolver(pool, [repo], installed_repository)

        try:
            transaction = solver.solve(request)
        except SatisfiabilityError as e:
            raise DependencyError(top_core.name,
                                  msg=e.unsat.to_string(pool))
        except NoPackageFound as e:
            raise DependencyError(top_core.name)

        return [op.package.core for op in transaction.operations]
Exemplo n.º 7
0
def initialize(request, remote_repositories, installed_repository, debug=0):
    pool = Pool(remote_repositories)
    pool.add_repository(installed_repository)

    policy = InstalledFirstPolicy(pool, installed_repository)
    solver = DependencySolver(
        pool, remote_repositories, installed_repository,
        policy=policy)

    requirement_ids, rules = solver._create_rules_and_initialize_policy(
        request)
    sat_solver = MiniSATSolver.from_rules(rules, policy)

    return pool, sat_solver, requirement_ids
Exemplo n.º 8
0
def initialize(request, remote_repositories, installed_repository, debug=0):
    pool = Pool(remote_repositories)
    pool.add_repository(installed_repository)

    policy = InstalledFirstPolicy(pool, installed_repository)
    solver = DependencySolver(pool,
                              remote_repositories,
                              installed_repository,
                              policy=policy)

    requirement_ids, rules = solver._create_rules_and_initialize_policy(
        request)
    sat_solver = MiniSATSolver.from_rules(rules, policy)

    return pool, sat_solver, requirement_ids
Exemplo n.º 9
0
def solve_and_print(request, remote_repositories, installed_repository,
                    print_ids, prune=True, prefer_installed=True, debug=False):
    pool = Pool(remote_repositories)
    pool.add_repository(installed_repository)

    policy = InstalledFirstPolicy(pool, installed_repository,
                                  prefer_installed=prefer_installed)
    solver = DependencySolver(
        pool, remote_repositories, installed_repository,
        policy=policy, use_pruning=prune)
    transaction = solver.solve(request)
    print(transaction)
    fmt = "ELAPSED : {description:20} : {elapsed:e}"
    print(solver._last_rules_time.pretty(fmt), file=sys.stderr)
    print(solver._last_solver_init_time.pretty(fmt), file=sys.stderr)
    print(solver._last_solve_time.pretty(fmt), file=sys.stderr)
    if debug:
        print(solver._policy._log_report(), file=sys.stderr)
Exemplo n.º 10
0
    def solve(self, top_core, tool):
        repo = Repository()
        for core in self._cores.values():
            package_str = "{} {}-{}".format(self._package_name(core.name),
                                            core.name.version,
                                            core.name.revision)
            _depends = core.depend
            try:
                _depends += getattr(core, tool).depend
            except (AttributeError, KeyError):
                pass

            if _depends:
                _s = "; depends ( {} )"
                package_str += _s.format(self._parse_depend(_depends))
            parser = PrettyPackageStringParser(EnpkgVersion.from_string)

            package = parser.parse_to_package(package_str)
            package.core = core

            repo.add_package(package)

        request = Request()
        _top_dep = "{} {} {}".format(self._package_name(top_core),
                                     top_core.relation,
                                     self._package_version(top_core))
        requirement = Requirement._from_string(_top_dep)
        request.install(requirement)
        installed_repository = Repository()
        pool = Pool([repo])
        pool.add_repository(installed_repository)
        solver = DependencySolver(pool, repo, installed_repository)

        try:
            transaction = solver.solve(request)
        except SatisfiabilityError as e:
            msg = "UNSATISFIABLE: {}"
            raise RuntimeError(msg.format(e.unsat.to_string(pool)))
        except NoPackageFound as e:
            raise DependencyError(top_core.name)

        return [op.package.core for op in transaction.operations]
Exemplo n.º 11
0
    def _solve(self, top_core, flags={}, only_matching_vlnv=False):
        def eq_vln(this, that):
            return (
                this.vendor == that.vendor
                and this.library == that.library
                and this.name == that.name
            )

        # Try to return a cached result
        solver_cache_key = (top_core, self._hash_flags_dict(flags), only_matching_vlnv)
        cached_solution = self._solver_cache_lookup(solver_cache_key)
        if cached_solution:
            return cached_solution

        repo = Repository()
        _flags = flags.copy()
        cores = [x["core"] for x in self._cores.values()]
        for core in cores:
            if only_matching_vlnv:
                if not eq_vln(core.name, top_core):
                    continue

            package_str = "{} {}-{}".format(
                self._package_name(core.name), core.name.version, core.name.revision
            )
            if not only_matching_vlnv:
                _flags["is_toplevel"] = core.name == top_core
                _depends = core.get_depends(_flags)
                if _depends:
                    _s = "; depends ( {} )"
                    package_str += _s.format(self._parse_depend(_depends))
            parser = PrettyPackageStringParser(EnpkgVersion.from_string)

            package = parser.parse_to_package(package_str)
            package.core = core

            repo.add_package(package)

        request = Request()
        simplevlnvs = top_core.simpleVLNVs()
        for sv in simplevlnvs:
            _top_dep = "{} {} {}".format(
                self._package_name(top_core),
                top_core.relation,
                self._package_version(top_core),
            )
            request.install(Requirement._from_string(_top_dep))

        installed_repository = Repository()
        pool = Pool([repo])
        pool.add_repository(installed_repository)
        solver = DependencySolver(pool, [repo], installed_repository)

        try:
            transaction = solver.solve(request)
        except SatisfiabilityError as e:
            raise DependencyError(top_core.name, msg=e.unsat.to_string(pool))
        except NoPackageFound as e:
            raise DependencyError(top_core.name)

        objdict = {}
        depdict = {}
        if len(transaction.operations) > 1:
            for op in transaction.operations:
                objdict[op.package._name] = str(op.package.core.name)
                depdict[str(op.package.core.name)] = [
                    objdict[n[0]] for n in op.package.install_requires
                ]
                op.package.core.direct_deps = [
                    objdict[n[0]] for n in op.package.install_requires
                ]
        result = [op.package.core for op in transaction.operations]

        # Cache the solution for further lookups
        self._solver_cache_store(solver_cache_key, result)

        return result