Example #1
0
    def test_get_random_first_unresolved_dependency(self) -> None:
        """Test getting random first unresolved dependency."""
        state = State(score=1.0)
        package_tuple1 = ("tensorflow", "2.1.0", "https://pypi.org/simple")
        package_tuple2 = ("selinon", "1.0.0", "https://pypi.org/simple")

        state.add_unresolved_dependency(package_tuple1)

        assert state.get_random_first_unresolved_dependency() is package_tuple1

        state.add_unresolved_dependency(package_tuple2)

        random_state = random.getstate()
        try:
            random.seed(42)
            assert state.get_random_first_unresolved_dependency(
            ) is package_tuple1
            assert state.get_random_first_unresolved_dependency(
            ) is package_tuple1
            assert state.get_random_first_unresolved_dependency(
            ) is package_tuple2
            assert state.get_random_first_unresolved_dependency(
            ) is package_tuple1
        finally:
            random.setstate(random_state)
Example #2
0
    def test_set_reward_signal_nan_inf(self, float_case: float) -> None:
        """Test (not) keeping the reward signal for nan/inf."""
        predictor = TemporalDifference()
        state = State()
        state.add_resolved_dependency(
            ("tensorflow", "2.3.0", "https://pypi.org/simple"))
        state.add_resolved_dependency(
            ("flask", "0.12", "https://pypi.org/simple"))
        state.add_unresolved_dependency(
            ("termial-random", "0.0.2", "https://pypi.org/simple"))
        predictor._policy = {
            ("flask", "0.12", "https://pypi.org/simple"): [0.2, 1],
        }
        predictor._steps_taken = 2
        predictor._steps_reward = 1.2
        predictor._next_state = state

        assert (predictor.set_reward_signal(
            state, ("tensorflow", "2.0.0", "https://pypi.org/simple"),
            float_case) is None)

        assert predictor._policy == {
            ("flask", "0.12", "https://pypi.org/simple"): [1.4, 2],
            ("tensorflow", "2.3.0", "https://pypi.org/simple"): [1.2, 1],
        }
        assert predictor._steps_taken == 0
        assert predictor._steps_reward == 0.0
        assert predictor._next_state is None
Example #3
0
 def test_add_unresolved_dependency(self, final_state: State) -> None:
     """Test adding unresolved dependencies into a state."""
     # This is actually never done in the code (from final state to a non-final state), but
     # let's test turning the switch.
     assert final_state.is_final()
     final_state.add_unresolved_dependency(
         ("selinon", "1.0.0", "https://pypi.org/simple"))
     assert not final_state.is_final()
     final_state.unresolved_dependencies.pop("selinon")
     assert final_state.is_final()
Example #4
0
    def run(self, state: State, package_version: PackageVersion) -> None:
        """Run main entry-point for steps to skip packages."""
        if not self._index_url_check(self._index_url, package_version.index.url):
            return None

        if self._specifier and package_version.locked_version not in self._specifier:
            return None

        if self._develop is not None and package_version.develop != self._develop:
            return None

        if not self._run_state_with_initiator(state, package_version):
            return None

        add_package_version = self.run_prescription["package_version"]
        add_package_version_name = add_package_version["name"]
        add_package_version_version = add_package_version["locked_version"][2:]
        add_package_version_index_url = add_package_version["index_url"]
        add_package_version_develop = add_package_version["develop"]

        add_package_version_tuple = (
            add_package_version_name,
            add_package_version_version,
            add_package_version_index_url,
        )

        resolved = state.resolved_dependencies.get(add_package_version_name)
        if resolved:
            if resolved == add_package_version_tuple:
                _LOGGER.debug(
                    "%s: Not adding package %r as it is already in the resolved listing",
                    self.get_unit_name(),
                    add_package_version_tuple,
                )
            else:
                _LOGGER.debug(
                    "%s: Not adding package %r as another package %r is already present in the resolved listing",
                    self.get_unit_name(),
                    add_package_version_tuple,
                    resolved,
                )

            return None

        runtime_env = self.context.project.runtime_environment
        py_ver = runtime_env.python_version.replace(".", "")
        # XXX: this could be moved to thoth-common
        solver_name = f"solver-{runtime_env.operating_system.name}-{runtime_env.operating_system.version}-py{py_ver}"
        if not self.context.graph.python_package_version_exists(
            add_package_version_name,
            add_package_version_version,
            add_package_version_index_url,
            solver_name=solver_name,
        ):
            _LOGGER.debug(
                "%s: Not adding package %r as the given package was not solved by %r",
                self.get_unit_name(),
                add_package_version_tuple,
                solver_name,
            )
            return None

        try:
            if not self.context.graph.is_python_package_index_enabled(add_package_version_index_url):
                _LOGGER.debug(
                    "%s: Not adding package %r as index %r is not enabled",
                    self.get_unit_name(),
                    add_package_version_tuple,
                    add_package_version_index_url,
                )
                return None
        except NotFoundError:
            _LOGGER.debug(
                "%s: Not adding package %r as index %r is not known to the resolver",
                self.get_unit_name(),
                add_package_version_tuple,
                add_package_version_index_url,
            )
            return None

        try:
            self._run_base()
        finally:
            self._configuration["prescription"]["run"] = True

        pv = PackageVersion(
            name=add_package_version_name,
            version=add_package_version["locked_version"],
            index=Source(add_package_version_index_url),
            develop=add_package_version_develop,
        )
        self.context.register_package_version(pv)
        state.add_unresolved_dependency(add_package_version_tuple)