-
Notifications
You must be signed in to change notification settings - Fork 11
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
Conversation
src/conda_project/project.py
Outdated
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)) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
test for out-of-project-directory envs_path
for more information, see https://pre-commit.ci
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.