forked from conda-incubator/conda-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
conda-store-server/conda_store_server/action/generate_constructor_artifacts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pathlib | ||
import subprocess | ||
import sys | ||
import tempfile | ||
|
||
import yaml | ||
from conda_store_server import action, schema | ||
|
||
|
||
@action.action | ||
def action_generate_constructor_artifacts( | ||
context, | ||
conda_command: str, | ||
specification: schema.CondaSpecification, | ||
installer_dir: str, | ||
): | ||
# Helpers | ||
def print_cmd(cmd): | ||
context.log.info(f"Running command: {' '.join(cmd)}") | ||
context.log.info( | ||
subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding="utf-8") | ||
) | ||
|
||
def write_file(filename, s): | ||
with open(filename, "w") as f: | ||
context.log.info(f"{filename}:\n{s}") | ||
f.write(s) | ||
|
||
# pip dependencies are not directly supported by constructor, they will be | ||
# installed via the post_install script: | ||
# https://github.com/conda/constructor/issues/515 | ||
dependencies = [] | ||
pip_dependencies = [] | ||
for d in specification.dependencies: | ||
if type(d) is schema.CondaSpecificationPip: | ||
pip_dependencies.extend(d.pip) | ||
else: | ||
dependencies.append(d) | ||
|
||
# Creates the construct.yaml file and post_install script | ||
ext = ".exe" if sys.platform == "win32" else ".sh" | ||
installer_dir = pathlib.Path(installer_dir) | ||
installer_filename = (installer_dir / specification.name).with_suffix(ext) | ||
|
||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
tmp_dir = pathlib.Path(tmp_dir) | ||
construct_file = tmp_dir / "construct.yaml" | ||
post_install_file = tmp_dir / "post-install.sh" | ||
env_dir = tmp_dir / "env" | ||
|
||
construct = { | ||
"installer_filename": str(installer_filename), | ||
"post_install": str(post_install_file), | ||
"name": specification.name, | ||
"channels": specification.channels, | ||
"specs": dependencies, | ||
# XXX: This is required: use the env hash and datetime? | ||
"version": 1, | ||
} | ||
|
||
# XXX: Support Windows | ||
post_install = """\ | ||
#!/usr/bin/env bash | ||
set -euxo pipefail | ||
""" | ||
if pip_dependencies: | ||
post_install += f""" | ||
conda run -p "$PREFIX" pip install {' '.join(pip_dependencies)} | ||
""" | ||
|
||
# Writes files to disk | ||
write_file(construct_file, yaml.dump(construct)) | ||
write_file(post_install_file, post_install) | ||
|
||
# Installs constructor | ||
command = [ | ||
conda_command, | ||
"create", | ||
"-y", | ||
"-p", | ||
str(env_dir), | ||
"constructor", | ||
] | ||
print_cmd(command) | ||
|
||
# Calls constructor | ||
command = [ | ||
conda_command, | ||
"run", | ||
"-p", | ||
str(env_dir), | ||
"--no-capture-output", | ||
"constructor", | ||
str(tmp_dir), | ||
] | ||
print_cmd(command) | ||
|
||
return installer_filename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters