Example #1
0
def test_extract_packages_from_logs(spec):
    """Test parsing the packges from action item"""
    log = Logs.create(command=f"conda install --name test {spec}")
    packages = log.extract_packages(index=0,
                                    packages=Packages.from_specs(spec))

    assert packages[0] == Package.from_spec(spec)
Example #2
0
    def infer(cls, name: str, packages: Packages, channels: ListLike = None):
        """create conda_env_tracker environment by inferring to existing conda environment"""
        if name == "base":
            raise CondaEnvTrackerCondaError(
                "Environment can not be created using default name base"
            )

        if name not in get_all_existing_environment():
            raise CondaEnvTrackerCondaError(
                f"Environment {name} can not be inferred, does not exist"
            )

        existing_packages = get_dependencies(name=name)
        if "r-base" in existing_packages["conda"]:
            existing_r_packages = get_r_dependencies(name=name)
        else:
            existing_r_packages = {}

        user_packages = {"conda": Packages(), "pip": Packages(), "r": Packages()}
        for package in packages:
            if package.name in existing_packages.get("conda", Packages()):
                user_packages["conda"].append(package)
            elif package.name in existing_packages.get("pip", Packages()):
                user_packages["pip"].append(package)
            elif package.name in existing_r_packages:
                raise RError(
                    "Cannot infer R packages, must run follow-up R install command.\n"
                    f"Found '{package.name}' in installed R packages {existing_r_packages}."
                )
            else:
                raise CondaEnvTrackerCondaError(
                    f"Environment {name} does not have {package.spec} installed"
                )

        conda_create_cmd = get_conda_create_command(
            name, user_packages["conda"], channels
        )

        specs = Actions.get_package_specs(
            packages=user_packages["conda"], dependencies=existing_packages["conda"]
        )
        history = History(
            name=name,
            channels=Channels(channels),
            packages=HistoryPackages.create(user_packages["conda"]),
            logs=Logs.create(conda_create_cmd),
            actions=Actions.create(name=name, specs=specs, channels=Channels(channels)),
            debug=Debug.create(name=name),
        )

        env = cls(name=name, history=history)
        if user_packages["pip"]:
            handler = PipHandler(env=env)
            handler.update_history_install(packages=user_packages["pip"])
            env = handler.env
        env.export()

        return env
Example #3
0
    def create(
        cls,
        name: str,
        packages: Packages,
        channels: ListLike = None,
        yes: bool = False,
        strict_channel_priority: bool = True,
    ):
        """Creating a conda environment from a list of packages."""
        if name == "base":
            raise CondaEnvTrackerCondaError(
                "Environment can not be created using default name base"
            )

        if name in get_all_existing_environment():
            raise CondaEnvTrackerCondaError(f"Environment {name} already exist")
        logger.debug(f"creating conda env {name}")

        conda_create(
            name=name,
            packages=packages,
            channels=channels,
            yes=yes,
            strict_channel_priority=strict_channel_priority,
        )
        create_cmd = get_conda_create_command(
            name=name,
            packages=packages,
            channels=channels,
            strict_channel_priority=strict_channel_priority,
        )
        specs = Actions.get_package_specs(
            packages=packages, dependencies=get_dependencies(name=name)["conda"]
        )

        if not channels:
            channels = get_conda_channels()

        history = History(
            name=name,
            channels=Channels(channels),
            packages=HistoryPackages.create(packages),
            logs=Logs.create(create_cmd),
            actions=Actions.create(
                name=name,
                specs=specs,
                channels=Channels(channels),
                strict_channel_priority=strict_channel_priority,
            ),
            debug=Debug.create(name=name),
        )
        env = cls(name=name, history=history)
        env.export()

        return env
Example #4
0
def expected_update(mocker):
    """Set up for update function"""
    packages = Packages.from_specs("pandas")
    channel = "conda-forge"
    create_cmd = "conda install pandas"
    name = "test-update"
    mocker.patch("conda_env_tracker.gateways.io.Path.mkdir")
    mocker.patch("conda_env_tracker.gateways.io.Path.write_text")
    mocker.patch("conda_env_tracker.gateways.io.Path.iterdir")
    mocker.patch("conda_env_tracker.history.get_pip_version",
                 return_value=None)
    history = History(
        name=name,
        channels=Channels([channel]),
        packages=HistoryPackages.create(packages),
        logs=Logs.create(create_cmd),
        actions=Actions.create(name="test-update",
                               specs=["pandas"],
                               channels=Channels([channel])),
        debug=Debug(),
    )
    mocker.patch(
        "conda_env_tracker.env.get_dependencies",
        return_value={
            "conda": {
                "pandas": Package("pandas", "pandas", "0.23", "py36"),
                "pytest": Package("pytest", "pytest", "0.1", "py36"),
            },
            "pip": {},
        },
    )
    mocker.patch("conda_env_tracker.env.EnvIO.export_packages")
    mocker.patch(
        "conda_env_tracker.main.Environment.read",
        return_value=Environment(name=name, history=history),
    )
    return history
Example #5
0
def test_extract_removed_packages_from_logs(log, expected):
    logs = Logs.create(command=f"conda create --name test python=3.7")
    logs.append(log)
    actual = logs.extra_removed_packages(index=1)
    assert actual == expected