Exemplo n.º 1
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
Exemplo n.º 2
0
 def __init__(self, name: str, history: Optional[History] = None):
     self.name = name
     self.history = history
     self.local_io = EnvIO(env_directory=USER_ENVS_DIR / name)
     self.dependencies = {}
     if self.history:
         self.dependencies = get_dependencies(name=self.name)
         if self.history.packages.get("r"):
             self.dependencies["r"] = get_r_dependencies(name=self.name)
Exemplo n.º 3
0
 def __init__(
     self,
     name: str,
     history: Optional[History] = None,
     dependencies: Optional[dict] = None,
 ):
     self.name = name
     self.history = history
     self.local_io = EnvIO(env_directory=USER_ENVS_DIR / name)
     if dependencies:
         self.dependencies = dependencies
     else:
         self.dependencies = {}
         if self.history:
             self.dependencies = get_dependencies(name=self.name)
             if self.history.packages.get("r"):
                 self.dependencies["r"] = get_r_dependencies(name=self.name)
     if history:
         self.history.packages.update_versions(dependencies=self.dependencies)
Exemplo n.º 4
0
def test_parse_r_packages(mocker):
    stdout = f"""> {r.LIST_R_PACKAGES}
          Package Version
       checkpoint   0.4.5
         jsonlite     1.6
           praise   1.0.0
             utf8   1.1.4
> 
> 
"""
    run_mock = mocker.patch("conda_env_tracker.gateways.r.subprocess.run")
    run_mock.configure_mock(**{
        "return_value.stdout": stdout,
        "return_value.returncode": 0
    })

    actual = r.get_r_dependencies(name="env_name")

    escaped_list_r_packages = r.LIST_R_PACKAGES.replace('"', r"\"")

    expected_command = (f"source {CONDA_SH_PATH} && "
                        "conda activate env_name && "
                        f'R --quiet --vanilla -e "{escaped_list_r_packages}"')

    run_mock.assert_called_once_with(
        expected_command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True,
        encoding="UTF-8",
    )

    expected = {
        "checkpoint": Package("checkpoint", "checkpoint", "0.4.5"),
        "jsonlite": Package("jsonlite", "jsonlite", "1.6"),
        "praise": Package("praise", "praise", "1.0.0"),
        "utf8": Package("utf8", "utf8", "1.1.4"),
    }

    assert actual == expected
Exemplo n.º 5
0
 def update_dependencies(self, update_r_dependencies=False):
     """Update the list of all conda, pip, and R dependencies installed."""
     self.dependencies = get_dependencies(name=self.name)
     if update_r_dependencies:
         self.dependencies["r"] = get_r_dependencies(name=self.name)