Skip to content

Write the modified conda env to a temporary file #8585

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions readthedocs/doc_builder/python_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os
import shutil
import tarfile
import tempfile
from pathlib import Path

import yaml
from django.conf import settings
Expand Down Expand Up @@ -689,9 +691,12 @@ def setup_base(self):
if self.project.has_feature(Feature.UPDATE_CONDA_STARTUP):
self._update_conda_startup()

conda_env_file = self.config.conda.environment
if self.project.has_feature(Feature.CONDA_APPEND_CORE_REQUIREMENTS):
self._append_core_requirements()
self._show_environment_yaml()
conda_env_file = self._append_core_requirements()
log.warning("Exists: %s", Path(conda_env_file).exists())
self._show_environment_yaml(conda_env_file)
log.warning(self.config)

if all([
# The project has CONDA_USES_MAMBA feature enabled and,
Expand All @@ -710,16 +715,17 @@ def setup_base(self):
'--name',
self.version.slug,
'--file',
self.config.conda.environment,
conda_env_file,
bin_path=None, # Don't use conda bin that doesn't exist yet
cwd=self.checkout_path,
)

def _show_environment_yaml(self):
def _show_environment_yaml(self, conda_env_file=None):
"""Show ``environment.yml`` file in the Build output."""
conda_env_file = conda_env_file or self.config.conda.environment
self.build_env.run(
'cat',
self.config.conda.environment,
conda_env_file,
cwd=self.checkout_path,
)

Expand Down Expand Up @@ -767,16 +773,16 @@ def _append_core_requirements(self):
dependencies.append(pip_dependencies)
dependencies.extend(conda_requirements)
environment.update({'dependencies': dependencies})
_, output_filename = tempfile.mkstemp(dir=Path(self.checkout_path) / ".." / "..", suffix=".yml")
Copy link
Author

Choose a reason for hiding this comment

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

The main issue I had was working out what the correct base path for this tempfile should be. I tried /tmp but then the shell commands couldn't find the file, this was the only compromise I could get to work. I am sure someone can tell me a better answer.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this is because this code is executed in the host, and then the file is read from inside the container. So, the only way to share a file between them is by using a path under the one that is mounted from the host into the container (self.checkout_path)

try:
outputfile = codecs.open(
os.path.join(
self.checkout_path,
self.config.conda.environment,
),
output_filename,
encoding='utf-8',
mode='w',
)
yaml.safe_dump(environment, outputfile)

return output_filename
except IOError:
log.warning(
'There was an error while writing the new Conda '
Expand Down