Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle relative paths and writing nonexistent directories for CONDA_PROJECT_ENVS_PATH #161

Merged
merged 13 commits into from
Apr 22, 2024

Conversation

jkong-anaconda
Copy link
Contributor

Update some behavior around the CONDA_PROJECT_ENVS_PATH variable so that relative paths are always interpreted relative to the project root and non-existent directories are created.

@jkong-anaconda jkong-anaconda requested review from AlbertDeFusco and a team April 11, 2024 20:57
if os.access(path, os.W_OK):
env_path = Path(path)
if os.access(path, os.W_OK) or not os.path.exists(path):
env_path = Path(os.path.join(self.directory, path))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlbertDeFusco This should handle always making the prefix an absolute path (and you can take a look at the relative path test below, which verifies the prefix).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do it like this?

env_path = self.directory / path

since self.directory is already a Path

Copy link
Contributor

@AlbertDeFusco AlbertDeFusco Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, i think there might be something missing with relative and absolute handling. For the case

CONDA_PROJECT_ENVS_PATH=/opt/conda/envs:conda_envs:/home/.conda/envs

maybe this is a little extra explicit and there could be a shorter variant

for path in env_paths:
    if not path:
        continue

    ## first setup the Path object
    if path.startswith('/'):
        path = Path(path)
    else:
        # this helps cover an edge case where your project directory is not writable
        # but there may be a writable location elsewhere in the env var
        path = self.directory / path

    if path.exists():
        # the requested path exists, can we write the project envs into it?
        if os.access(path, os.W_OK):
            env_path = path
            break
        else:
            continue
    else:
        # can we create the requsted path and then puth the project envs there?
        if os.access(path.parent, os.W_OK):
            env_path = path
            break
        else:
            continue

envs = OrderedDict()
for env_name, sources in self._project_file.environments.items():
    envs[env_name] = Environment(
        name=env_name,
        sources=tuple([self.directory / str(s) for s in sources]),
        prefix=(env_path / env_name).absolute(),
        lockfile=self.directory / f"conda-lock.{env_name}.yml",
        project=weakref.proxy(self),
    )
Environments = create_model(
    "Environments",
    **{k: (Environment, ...) for k in envs},
    __base__=BaseEnvironments,
)
return Environments(**envs)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated + added tests to handle the cases where a non-existent path's parent isn't writable, and where the project directory itself is not writable but a path in the environment variable is.

@jkong-anaconda jkong-anaconda marked this pull request as draft April 11, 2024 22:28
Copy link

codecov bot commented Apr 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.09%. Comparing base (e4cbec3) to head (323e123).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #161      +/-   ##
==========================================
+ Coverage   97.40%   98.09%   +0.69%     
==========================================
  Files           9        9              
  Lines        1000      996       -4     
==========================================
+ Hits          974      977       +3     
+ Misses         26       19       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jkong-anaconda jkong-anaconda marked this pull request as ready for review April 12, 2024 20:32
@AlbertDeFusco AlbertDeFusco merged commit 8aab898 into main Apr 22, 2024
88 checks passed
@AlbertDeFusco AlbertDeFusco deleted the envs-path-updates branch April 22, 2024 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants