Buckets:
"""CLI for creating new objects."""
from __future__ import annotations
import csv
import os
import pathlib
import shutil
import subprocess
import time
from enum import Enum
from typing import Literal
import typer
from typing_extensions import Annotated
import calkit
from calkit.cli import AliasGroup, OptionalValueCommand, raise_error, warn
from calkit.core import ryaml
new_app = typer.Typer(cls=AliasGroup, no_args_is_help=True)
class _NewProjectCommand(OptionalValueCommand):
optional_value_options = {"--hub": "default", "--cloud": "default"}
def _check_path_dir(path: str):
"""If path is in a subdirectory, check that it exists."""
dirname = os.path.dirname(path)
if dirname:
os.makedirs(dirname, exist_ok=True)
def _person_from_options(
option: str, email: str | None, orcid: str | None, with_ai: list[str]
) -> dict | None:
"""Build a ``created_by`` entry from CLI options.
Returns ``None`` when none were given. Validated through the model so
a mistyped ORCID or a ``--with-ai`` with nobody to answer for it is
refused here rather than by the next ``calkit.yaml`` validation.
"""
from pydantic import ValidationError
from calkit.models.core import _Person
if email is None and orcid is None and not with_ai:
return None
# ``with_ai`` is a list, one entry per --with-ai given. A single tool is
# written as a scalar, which is what the docs show and what reads best
# in calkit.yaml; several stay a list
with_ai_value: str | list[str] | None = None
if len(with_ai) == 1:
with_ai_value = with_ai[0]
elif with_ai:
with_ai_value = with_ai
try:
person = _Person(email=email, orcid=orcid, with_ai=with_ai_value)
except ValidationError as e:
raise_error(
f"Invalid --{option}: "
+ "; ".join(str(err["msg"]) for err in e.errors())
)
return person.model_dump(exclude_none=True)
def _split_template_subdir(
template: str, git_url: str
) -> tuple[str, str | None]:
"""Split a directory out of an 'owner/repo/path' template.
One repo can hold several self-contained example projects, so a
template may name a directory within it. Only the shorthand form is
split; a full URL is left alone, since its path is the repo's.
"""
if "://" in template or "github.com" in template:
return git_url, None
parts = template.strip("/").split("/")
if len(parts) <= 2:
return git_url, None
subdir = "/".join(parts[2:])
return git_url.rsplit("/" + subdir, 1)[0], subdir
@new_app.command(name="project", cls=_NewProjectCommand)
def new_project(
path: Annotated[str, typer.Argument(help="Where to create the project.")],
name: Annotated[
str | None,
typer.Option(
"--name",
"-n",
help=(
"Project name. Will be inferred as kebab-cased directory "
"name if not provided."
),
),
] = None,
title: Annotated[
str | None, typer.Option("--title", help="Project title.")
] = None,
description: Annotated[
str | None, typer.Option("--description", help="Project description.")
] = None,
hub: Annotated[
str | None,
typer.Option(
"--hub",
"--cloud",
help=(
"Create this project on a Calkit hub (and GitHub). "
"Optionally takes a hub URL; bare --hub (or the special "
"value 'default') uses the default_hub config value, "
"else calkit.io. --cloud is a deprecated alias."
),
),
] = None,
public: Annotated[
bool,
typer.Option(
"--public",
help="Create as a public project if --hub is selected.",
),
] = False,
git_repo_url: Annotated[
str | None,
typer.Option(
"--git-url",
help=(
"Git repo URL. "
"Usually https://github.com/{your_name}/{project_name}."
),
),
] = None,
template: Annotated[
str | None,
typer.Option(
"--template",
"-t",
help=(
"Template from which to derive the project, e.g., "
"'calkit/example-basic'."
),
),
] = None,
no_commit: Annotated[
bool | None,
typer.Option("--no-commit", help="Do not commit changes to Git."),
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite project if one already exists.",
),
] = False,
verbose: Annotated[
bool, typer.Option("--verbose", help="Print verbose output.")
] = False,
):
"""Create a new project."""
import git
from git.exc import (
GitCommandError,
InvalidGitRepositoryError,
NoSuchPathError,
)
from calkit.cli.update import update_devcontainer
from calkit.dvc import run_dvc_command
docs_url = "https://docs.calkit.org"
success_message = (
"\nCongrats on creating your new Calkit project!\n\n"
"Next, you'll probably want to start building your pipeline.\n\n"
f"Check out the docs at {docs_url}."
)
# Point subsequent hub API calls at the requested instance; 'default'
# (a bare --hub) keeps the normal resolution: CALKIT_HUB if
# set, else the default_hub config value, else calkit.io
if hub is not None and hub != "default":
if hub in ["test", "local", "staging", "production"]:
raise_error(
"--hub takes a hub URL, e.g., https://staging.calkit.io"
)
os.environ["CALKIT_HUB"] = hub
try:
calkit.hub.get_base_url()
except ValueError as e:
raise_error(str(e))
abs_path = os.path.abspath(path)
if template and os.path.exists(abs_path):
raise_error("Must specify a new directory if using --template")
try:
repo = calkit.git.get_repo(abs_path)
except (InvalidGitRepositoryError, NoSuchPathError):
repo = None
if repo is not None and git_repo_url is None:
if verbose:
typer.echo("Detecting Git repo URL from existing repo")
remote_names = [r.name for r in repo.remotes]
if remote_names:
try:
remote = repo.remotes[
remote_names.index("origin")
if "origin" in remote_names
else 0
]
git_repo_url = remote.url
# Convert to HTTPS if it's SSH
if git_repo_url.startswith("git@"):
git_repo_url = git_repo_url.replace(
"git@github.com:", "https://github.com/"
)
git_repo_url = git_repo_url.removesuffix(".git")
except Exception as e:
git_repo_url = None
raise_error(
f"Could not detect Git repo URL from existing repo: {e}"
)
elif hub is None:
raise_error(
"Existing Git repo has no remotes; "
"specify --git-url or use --hub to create one"
)
# We don't have a project name but do have a Git repo URL, the
# project name will be the Git repo name lowercased, since that's
# how the hub will create the project name by default
if name is None and git_repo_url is not None:
name = git_repo_url.split("/")[-1].lower()
# If this isn't a DVC repo, run `dvc init`
if not os.path.isfile(os.path.join(abs_path, ".dvc", "config")):
typer.echo("Initializing DVC repository")
if verbose:
typer.echo("Running 'dvc init'")
try:
result = run_dvc_command(
["init", "-q"],
cwd=abs_path,
)
if result != 0:
raise subprocess.CalledProcessError(result, "dvc init")
except subprocess.CalledProcessError as e:
raise_error(f"Failed to initialize DVC repository: {e}")
# Commit the DVC init changes
if not no_commit:
repo.git.add(".dvc")
repo.git.commit(["-m", "Initialize DVC"])
ck_info_fpath = os.path.join(abs_path, "calkit.yaml")
if os.path.isfile(ck_info_fpath) and not overwrite:
ck_info = calkit.load_calkit_info(wdir=abs_path)
# Prefer values explicitly passed on the CLI; fall back to calkit.yaml
name = name or ck_info.get("name")
title = title or ck_info.get("title")
description = description or ck_info.get("description")
typer.echo(
"Destination is already a Calkit project; "
"will use existing project info where possible"
)
else:
ck_info = {}
if os.path.isdir(abs_path) and os.listdir(abs_path) and repo is None:
warn(f"{abs_path} is not empty")
if name is None:
name = calkit.to_kebab_case(os.path.basename(abs_path))
if " " in name:
warn("Invalid name; replacing spaces with hyphens")
name = name.replace(" ", "-")
typer.echo(f"Creating project {name}")
if title is None:
title = typer.prompt("Enter a title (ex: 'My research project')")
typer.echo(f"Using title: {title}")
if hub is not None:
# The hub should allow None, which will allow us to post just the name
# NOTE: This will fail if the user hasn't logged into the hub
# in 6 months, since their GitHub refresh token stored is expired
# Strip control characters (e.g., stray newlines) the API rejects
if description is not None:
description = (
"".join(
" " if (ord(c) < 32 and c != "\t") or ord(c) == 127 else c
for c in description
).strip()
or None
)
typer.echo("Creating project on the hub")
try:
resp = calkit.hub.post(
"/projects",
json=dict(
name=name,
title=title,
description=description,
git_repo_url=git_repo_url,
is_public=public,
template=template,
),
)
except Exception as e:
msg = f"Posting new project to the hub failed: {e}"
if (
git_repo_url is not None
and "Can only create projects for yourself" in str(e)
):
parts = git_repo_url.rstrip("/").split("/")
if len(parts) >= 2:
detected_owner = parts[-2]
msg += (
f"\n\nThe owner '{detected_owner}' was detected from "
"your Git remote. If this is a GitHub organization, "
"make sure the organization exists on the hub "
"and that you have write access to it."
)
raise_error(msg)
# Now clone here
if not os.path.isdir(abs_path):
subprocess.run(["git", "clone", resp["git_repo_url"], abs_path])
elif repo is None:
typer.echo("Fetching from newly created Git repo")
repo = git.Repo.init(abs_path, initial_branch="main")
repo.git.remote(["add", "origin", resp["git_repo_url"]])
repo.git.fetch()
checkout_cmd = ["-t", "origin/main"]
if overwrite:
checkout_cmd.append("--force")
try:
repo.git.checkout(checkout_cmd)
except GitCommandError as e:
raise_error(f"Failed to check out main branch: {e}")
else:
# Create a calkit.yaml file if one does not exist
calkit_fpath = os.path.join(abs_path, "calkit.yaml")
if not os.path.isfile(calkit_fpath):
typer.echo("Creating calkit.yaml file")
with open(calkit_fpath, "w") as f:
ryaml.dump(
dict(
owner=resp["owner_account_name"],
name=resp["name"],
title=resp["title"],
description=resp["description"],
git_repo_url=resp["git_repo_url"],
),
f,
)
calkit.schema.ensure_modeline(calkit_fpath)
repo.git.add("calkit.yaml")
if not no_commit:
repo.git.commit(
["calkit.yaml", "-m", "Create calkit.yaml"]
)
else:
# Merge with existing project info in calkit.yaml
typer.echo("Updating existing calkit.yaml file")
ck_info = (
dict(
owner=resp["owner_account_name"],
name=resp["name"],
title=resp["title"],
description=resp["description"],
git_repo_url=resp["git_repo_url"],
)
| ck_info
)
with open(calkit_fpath, "w") as f:
ryaml.dump(ck_info, f)
repo.git.add("calkit.yaml")
if not no_commit and repo.git.diff("--staged"):
repo.git.commit(
["calkit.yaml", "-m", "Update calkit.yaml"]
)
# Set Git remote URL to match the one used in the hub repo
if repo.remotes:
typer.echo("Updating Git remote URL to match hub repo")
# Use origin if present, otherwise fall back to the first remote
remote_names = [r.name for r in repo.remotes]
existing_remote = repo.remotes[
remote_names.index("origin")
if "origin" in remote_names
else 0
]
current_url = existing_remote.url
new_url = resp["git_repo_url"]
if current_url.startswith("git@") and not new_url.startswith(
"git@"
):
new_url = resp["git_repo_url"].replace(
"https://github.com/", "git@github.com:"
)
repo.git.remote(["set-url", existing_remote.name, new_url])
else:
typer.echo("Adding Git remote URL for hub repo")
repo.git.remote(["add", "origin", resp["git_repo_url"]])
try:
remote_name = calkit.dvc.configure_remote(wdir=abs_path)
calkit.dvc.set_remote_auth(remote_name=remote_name, wdir=abs_path)
if not no_commit and repo is not None:
dvc_config = os.path.join(abs_path, ".dvc", "config")
if os.path.isfile(dvc_config) and repo.git.diff(dvc_config):
repo.git.add(dvc_config)
repo.git.commit(
[dvc_config, "-m", "Configure Calkit DVC remote"]
)
except Exception:
warn("Failed to setup Calkit DVC remote")
prj = calkit.detect_project_name(wdir=abs_path)
hub_url = calkit.hub.get_hub_url()
add_msg = f"\n\nYou can view your project at {hub_url}/{prj}"
typer.echo(success_message + add_msg)
return
# If using a template, clone it first
if template:
# TODO: If the template is not a Git repo URL, make a request to the
# the hub to get it?
# For now, assume consistency between hub projects and
# GitHub repo URLs
if "github.com" in template:
template_git_url = template
template_name = template.split("github.com")[-1][1:].removesuffix(
".git"
)
else:
template_name = template
template_git_url = f"https://github.com/{template}"
# A template can name a directory inside a repo, e.g.
# 'calkit/calkit/examples/markdown', so one repo can hold several
# self-contained examples.
template_git_url, template_subdir = _split_template_subdir(
template, template_git_url
)
if template_subdir is None:
# Now clone it
subprocess.run(["git", "clone", template_git_url, abs_path])
# Templates should always have DVC initialized, so no need to do
# that
repo = calkit.git.get_repo(abs_path)
git_rev = repo.git.rev_parse("HEAD")
# Rename origin remote as upstream
typer.echo("Renaming template remote as upstream")
repo.git.remote(["rename", "origin", "upstream"])
else:
# Only part of the repo is the template, so clone it somewhere
# else and copy that directory out
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
clone_path = os.path.join(tmpdir, "template")
subprocess.run(
["git", "clone", template_git_url, clone_path],
check=True,
)
git_rev = calkit.git.get_repo(clone_path).git.rev_parse("HEAD")
src_path = os.path.join(clone_path, template_subdir)
if not os.path.isdir(src_path):
raise_error(
f"Template '{template_name}' has no directory "
f"'{template_subdir}'"
)
shutil.copytree(src_path, abs_path, dirs_exist_ok=True)
typer.echo("Initializing Git repository")
subprocess.run(["git", "init", "-q"], cwd=abs_path)
repo = calkit.git.get_repo(abs_path)
repo.git.remote(["add", "upstream", template_git_url])
# Set git repo URL if provided
if git_repo_url:
typer.echo("Setting origin remote URL")
repo.git.remote(["add", "origin", git_repo_url])
# Update Calkit info in this project
ck_info = calkit.load_calkit_info(wdir=abs_path)
ck_info = ck_info | dict(
name=name,
title=title,
description=description,
git_repo_url=git_repo_url,
derived_from=dict(
project=template_name,
git_repo_url=template_git_url,
git_rev=git_rev,
),
)
# Remove questions and owner if they're there
_ = ck_info.pop("questions", None)
_ = ck_info.pop("owner", None)
# Write Calkit info
with open(os.path.join(abs_path, "calkit.yaml"), "w") as f:
ryaml.dump(ck_info, f)
calkit.schema.ensure_modeline(os.path.join(abs_path, "calkit.yaml"))
# Update the README rather than replacing it: a template's README
# is its instructions, and in a runnable README it is the pipeline
# itself, so only the title and description are this project's
readme_fpath = os.path.join(abs_path, "README.md")
if os.path.isfile(readme_fpath):
typer.echo("Updating README.md title and description")
with open(readme_fpath, encoding="utf-8") as f:
readme_txt = calkit.update_readme_content(
f.read(),
project_title=title,
project_description=description,
)
else:
typer.echo("Generating README.md")
readme_txt = calkit.make_readme_content(
project_name=name,
project_title=title,
project_description=description,
)
with open(readme_fpath, "w", encoding="utf-8", newline="\n") as f:
f.write(readme_txt)
# Update DVC remote
# TODO: This will fail because we don't know this user's account name
typer.echo("Updating Calkit DVC remote")
try:
calkit.dvc.configure_remote(wdir=abs_path)
except ValueError:
warn(
"Could not update Calkit DVC remote since "
"no Git repo URL was provided"
)
warn(
"You will need to manually run `git remote add origin` "
"and `calkit config remote`"
)
result = run_dvc_command(
["remote", "remove", "calkit", "-q"],
cwd=abs_path,
)
if result != 0:
warn("Failed to remove DVC remote")
try:
calkit.dvc.set_remote_auth(wdir=abs_path)
except Exception:
warn("Could not set authentication for Calkit DVC remote")
# Commit this stuff to Git
repo.git.add(".")
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Create new project from {template_name}"])
typer.echo(success_message)
return
os.makedirs(abs_path, exist_ok=True)
try:
repo = calkit.git.get_repo(abs_path)
except InvalidGitRepositoryError:
typer.echo("Initializing Git repository")
subprocess.run(["git", "init", "-q"], cwd=abs_path)
repo = calkit.git.get_repo(abs_path)
if not os.path.isfile(os.path.join(abs_path, ".dvc", "config")):
typer.echo("Initializing DVC repository")
result = run_dvc_command(
["init", "-q"],
cwd=abs_path,
)
if result != 0:
raise_error("Failed to initialize DVC repository")
# Create calkit.yaml file
ck_info = calkit.load_calkit_info(wdir=abs_path)
ck_info = dict(name=name, title=title, description=description) | ck_info
with open(os.path.join(abs_path, "calkit.yaml"), "w") as f:
ryaml.dump(ck_info, f)
calkit.schema.ensure_modeline(os.path.join(abs_path, "calkit.yaml"))
repo.git.add("calkit.yaml")
# Create dev container spec
update_devcontainer(wdir=abs_path)
repo.git.add(".devcontainer")
# Create README
readme_fpath = os.path.join(abs_path, "README.md")
if os.path.isfile(readme_fpath) and not overwrite:
warn("README.md already exists; not modifying")
else:
typer.echo("Generating README.md")
readme_txt = calkit.make_readme_content(
project_name=name,
project_title=title, # type: ignore
project_description=description,
)
with open(readme_fpath, "w") as f:
f.write(readme_txt)
repo.git.add("README.md")
if git_repo_url and not repo.remotes:
typer.echo(f"Adding Git remote {git_repo_url}")
repo.git.remote(["add", "origin", git_repo_url])
elif not git_repo_url and not repo.remotes:
warn("No Git remotes are configured")
# Setup Calkit DVC remote
if repo.remotes:
typer.echo("Setting up Calkit DVC remote")
try:
calkit.dvc.configure_remote(wdir=abs_path)
calkit.dvc.set_remote_auth(wdir=abs_path)
except Exception as e:
warn(f"Failed to set up Calkit DVC remote: {e}")
if repo.git.diff("--staged") and not no_commit:
repo.git.commit(["-m", "Initialize Calkit project"])
typer.echo(success_message)
@new_app.command(name="figure|fig")
def new_figure(
path: str,
title: Annotated[str, typer.Option("--title")],
description: Annotated[str, typer.Option("--description")],
stage_name: Annotated[
str | None,
typer.Option(
"--stage",
help="Name of the pipeline stage that generates this figure.",
),
] = None,
cmd: Annotated[
str | None,
typer.Option(
"--cmd", help="Command to add to the stage, if specified."
),
] = None,
deps: Annotated[
list[str], typer.Option("--dep", help="Path to stage dependency.")
] = [],
outs: Annotated[
list[str],
typer.Option(
"--out",
help=(
"Path to stage output. "
"Figure path will be added automatically."
),
),
] = [],
outs_from_stage: Annotated[
str | None,
typer.Option(
"--deps-from-stage-outs",
help="Stage name from which to add outputs as dependencies.",
),
] = None,
created_by_email: Annotated[
str | None,
typer.Option(
"--created-by-email",
help=(
"Email of whoever made this figure, for one drawn by hand "
"rather than produced by a stage."
),
),
] = None,
created_by_orcid: Annotated[
str | None,
typer.Option(
"--created-by-orcid", help="ORCID of whoever made this figure."
),
] = None,
created_with_ai: Annotated[
list[str],
typer.Option(
"--created-with-ai",
help=(
"Generative AI tool they used, e.g. 'Claude Opus 5'. "
"Repeat for several."
),
),
] = [],
no_commit: Annotated[bool, typer.Option("--no-commit")] = False,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite existing figure if one exists.",
),
] = False,
):
"""Create a new figure."""
from calkit.dvc import run_dvc_command
ck_info = calkit.load_calkit_info()
figures = ck_info.get("figures", [])
paths = [f.get("path") for f in figures]
if not overwrite and path in paths:
raise_error(f"Figure at path {path} already exists")
elif overwrite and path in paths:
figures = [fig for fig in figures if fig.get("path") != path]
if cmd is not None and stage_name is None:
raise_error("Stage name must be provided if command is specified")
if (deps or outs or outs_from_stage) and not cmd:
raise_error("Command must be provided")
if (deps or outs or outs_from_stage) and not stage_name:
raise_error("Stage name must be provided")
created_by = _person_from_options(
"created-by", created_by_email, created_by_orcid, created_with_ai
)
obj: dict = dict(path=path, title=title)
if description is not None:
obj["description"] = description
if stage_name is not None:
obj["stage"] = stage_name
if created_by is not None:
obj["created_by"] = created_by
if cmd:
if outs_from_stage:
pipeline = calkit.dvc.read_pipeline()
stages = pipeline.get("stages", {})
if outs_from_stage not in stages:
raise_error(f"Stage {outs_from_stage} does not exist")
stage = stages[outs_from_stage]
if "foreach" in stage:
for val in stage["foreach"]:
for out in stage.get("do", {}).get("outs", []):
deps.append(out.replace("${item}", val))
else:
deps += stage.get("outs", [])
if path not in outs:
outs.append(path)
deps_cmd = []
for dep in deps:
deps_cmd += ["-d", dep]
outs_cmd = []
for out in outs:
outs_cmd += ["-o", out]
result = run_dvc_command(
[
"stage",
"add",
"-n",
stage_name,
]
+ (["-f"] if overwrite else [])
+ deps_cmd
+ outs_cmd
+ [cmd]
)
if result != 0:
raise_error(f"Failed to add figure {obj}")
figures.append(obj)
ck_info["figures"] = figures
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit:
repo = calkit.git.get_repo()
repo.git.add("calkit.yaml")
if cmd:
repo.git.add("dvc.yaml")
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add figure {path}"])
def _new_simple_artifact(
kind: Literal["results", "presentations"],
path: str,
title: str | None,
description: str | None,
stage_name: str | None,
no_commit: bool,
overwrite: bool,
obj_kind: str | None = None,
key: str | None = None,
name: str | None = None,
) -> None:
"""Declare a simple artifact (path/title/description/stage) in calkit.yaml."""
singular = kind.rstrip("s")
ck_info = calkit.load_calkit_info()
objects = ck_info.get(kind, [])
# Results are identified by path and key together, since several can read
# different values out of one file; everything else is just its path
existing = [(o.get("path"), o.get("key")) for o in objects]
if (path, key) in existing:
if not overwrite:
at_key = f" with key {key}" if key is not None else ""
raise_error(
f"{singular.capitalize()} at path {path}{at_key} "
"already exists"
)
objects = [
o for o in objects if (o.get("path"), o.get("key")) != (path, key)
]
obj = dict(path=path)
if name is not None:
obj["name"] = name
if key is not None:
obj["key"] = key
if title is not None:
obj["title"] = title
if obj_kind is not None:
obj["kind"] = obj_kind
if description is not None:
obj["description"] = description
if stage_name is not None:
obj["stage"] = stage_name
objects.append(obj)
ck_info[kind] = objects
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit:
repo = calkit.git.get_repo()
repo.git.add("calkit.yaml")
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add {singular} {path}"])
@new_app.command(name="result")
def new_result(
path: str,
name: Annotated[
str | None,
typer.Option(
"--name",
help=(
"Short handle for referring to this result, which stays "
"stable if the file is renamed."
),
),
] = None,
title: Annotated[str | None, typer.Option("--title")] = None,
key: Annotated[
str | None,
typer.Option(
"--key",
help=(
"Path to the value within the file, e.g., 'metrics.mean'. "
"Omit if the whole file is the result."
),
),
] = None,
description: Annotated[str | None, typer.Option("--description")] = None,
stage_name: Annotated[
str | None,
typer.Option(
"--stage",
help="Name of the pipeline stage that generates this result.",
),
] = None,
no_commit: Annotated[bool, typer.Option("--no-commit")] = False,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite existing result if one exists.",
),
] = False,
):
"""Declare a new result."""
_new_simple_artifact(
"results",
path,
title,
description,
stage_name,
no_commit,
overwrite,
key=key,
name=name,
)
@new_app.command(name="presentation|pres")
def new_presentation(
path: str,
title: Annotated[str, typer.Option("--title")],
description: Annotated[str | None, typer.Option("--description")] = None,
kind: Annotated[
str | None,
typer.Option(
"--kind",
help="Kind of presentation, either 'slides' or 'poster'.",
),
] = None,
stage_name: Annotated[
str | None,
typer.Option(
"--stage",
help="Name of the pipeline stage that generates this presentation.",
),
] = None,
no_commit: Annotated[bool, typer.Option("--no-commit")] = False,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite existing presentation if one exists.",
),
] = False,
):
"""Declare a new presentation."""
if kind is not None and kind not in ["slides", "poster"]:
raise_error("Presentation kind must be either 'slides' or 'poster'")
_new_simple_artifact(
"presentations",
path,
title,
description,
stage_name,
no_commit,
overwrite,
obj_kind=kind,
)
@new_app.command("question")
def new_question(
question: str,
commit: Annotated[bool, typer.Option("--commit")] = False,
):
"""Add a new question."""
ck_info = calkit.load_calkit_info()
questions = ck_info.get("questions", [])
if question in questions:
raise ValueError("Question already exists")
if not question.endswith("?"):
raise ValueError("Questions must end with a question mark")
questions.append(question)
ck_info["questions"] = questions
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if commit:
repo = calkit.git.get_repo()
repo.git.add("calkit.yaml")
repo.git.commit(["-m", "Add question"])
@new_app.command("notebook|nb")
def new_notebook(
path: Annotated[str, typer.Argument(help="Notebook path (relative)")],
title: Annotated[str, typer.Option("--title")],
description: Annotated[str | None, typer.Option("--description")] = None,
stage_name: Annotated[
str | None,
typer.Option(
"--stage",
help="Name of the pipeline stage that runs this notebook.",
),
] = None,
commit: Annotated[bool, typer.Option("--commit")] = False,
):
"""Add a new notebook."""
if os.path.isabs(path):
raise ValueError("Path must be relative")
if not os.path.isfile(path):
raise ValueError("Path is not a file")
if not path.endswith(".ipynb"):
raise ValueError("Path does not have .ipynb extension")
# TODO: Add option to create stage if one doesn't exist
ck_info = calkit.load_calkit_info()
notebooks = ck_info.get("notebooks", [])
paths = [f.get("path") for f in notebooks]
if path in paths:
raise ValueError(f"Notebook at path {path} already exists")
obj = dict(path=path, title=title)
if description is not None:
obj["description"] = description
if stage_name is not None:
if stage_name not in ck_info.get("pipeline", {}).get("stages", {}):
raise ValueError(
f"Stage '{stage_name}' does not exist in the pipeline"
)
obj["stage"] = stage_name
notebooks.append(obj)
ck_info["notebooks"] = notebooks
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if commit:
repo = calkit.git.get_repo()
repo.git.add("calkit.yaml")
repo.git.commit(["calkit.yaml", "-m", f"Add notebook {path}"])
@new_app.command("docker-env")
def new_docker_env(
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
],
image_name: Annotated[
str | None,
typer.Option(
"--image",
help=(
"Image identifier. Should be unique and descriptive. "
"Will default to environment name if not specified."
),
),
] = None,
base: Annotated[
str | None,
typer.Option(
"--from",
help="Base image, e.g., 'ubuntu', if creating a Dockerfile.",
),
] = None,
path: Annotated[
str | None,
typer.Option(
"--path",
help=(
"Dockerfile path. Will default to 'Dockerfile' "
"if --from is specified."
),
),
] = None,
layers: Annotated[
list[str],
typer.Option(
"--add-layer",
help="Add a layer (options: miniforge, foampy, uv, julia).",
),
] = [],
env_vars: Annotated[
list[str],
typer.Option(
"--env-var", help="Environment variables to set in the container."
),
] = [],
gpus: Annotated[str | None, typer.Option("--gpus")] = None,
args: Annotated[
list[str],
typer.Option(
"--arg",
help="Arguments to use when running container.",
),
] = [],
inputs: Annotated[
list[str],
typer.Option(
"--input",
"--dep",
help=(
"Path to a file that gets added to the container, so "
"editing it rebuilds the image."
),
),
] = [],
wdir: Annotated[
str, typer.Option("--wdir", help="Working directory.")
] = "/work",
command_mode: Annotated[
str,
typer.Option(
"--command-mode",
help=(
"How to execute commands in the container: "
"'shell' runs shell -c, 'entrypoint' passes args "
"directly to the image entrypoint."
),
),
] = "shell",
user: Annotated[
str | None,
typer.Option(
"--user", help="User account to use to run the container."
),
] = None,
platform: Annotated[
str | None,
typer.Option(
"--platform",
help=(
"Platform to pull and run the image as, e.g., 'linux/amd64'."
),
),
] = None,
registry: Annotated[
str | None,
typer.Option(
"--registry",
help=(
"Registry prefix to push built images to and pull them from "
"instead of rebuilding, e.g., 'ghcr.io/someone/some-project', "
"or 'ghcr.io' for the project's own namespace in the GitHub "
"Container Registry."
),
),
] = None,
build_platforms: Annotated[
list[str],
typer.Option(
"--platform-build",
help=(
"Platform to build the image for, as opposed to --platform, "
"which is the one it's pulled and run as. Repeat for a "
"multi-platform image, which requires a registry."
),
),
] = [],
ports: Annotated[
list[str],
typer.Option(
"--port",
help=(
"Ports to expose in the container, e.g., '8080:80'. "
"Can be specified multiple times."
),
),
] = [],
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check environment is up-to-date after creation.",
),
] = False,
):
"""Create a new Docker environment."""
from calkit.cli.check import check_environment
from calkit.docker import LAYERS
if base is not None and path is None:
path = "Dockerfile"
if path is not None and base and os.path.isfile(path) and not overwrite:
raise_error("Output path already exists (use -f to overwrite)")
if image_name is None:
typer.echo(
"No image name specified; using project and environment name"
)
_, project_name = calkit.detect_project_name().split("/")
image_name = f"{project_name}-{name}"
if command_mode not in ["shell", "entrypoint"]:
raise_error("--command-mode must be one of: shell, entrypoint")
repo = calkit.git.get_repo()
if base and path is not None:
txt = "FROM " + base + "\n\n"
for layer in layers:
if layer not in LAYERS:
raise_error(f"Unknown layer type '{layer}'")
txt += LAYERS[layer] + "\n\n"
txt += f"RUN mkdir {wdir}\n"
txt += f"WORKDIR {wdir}\n"
with open(path, "w") as f:
f.write(txt)
# Add environment to Calkit info
ck_info = calkit.load_calkit_info()
# If environments is a list instead of a dict, reformulate it
envs = ck_info.get("environments", {})
if isinstance(envs, list):
typer.echo("Converting environments from list to dict")
envs = {env.pop("name"): env for env in envs}
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
if base:
repo.git.add(path)
typer.echo("Adding environment to calkit.yaml")
env = dict(
kind="docker",
image=image_name,
wdir=wdir,
)
if base is not None or path is not None:
env["path"] = path # type: ignore
if description is not None:
env["description"] = description
if layers:
env["layers"] = layers # type: ignore
if platform:
env["platform"] = platform
if registry:
env["registry"] = registry
if build_platforms:
env["build_platforms"] = build_platforms # type: ignore
if user:
env["user"] = user
if gpus:
env["gpus"] = gpus
if command_mode != "shell":
env["command_mode"] = command_mode
if env_vars:
env["env_vars"] = {} # type: ignore
for var in env_vars:
if "=" not in var:
raise_error(f"Invalid environment variable format: {var}")
key, value = var.split("=", 1)
env["env_vars"][key] = value
if args:
env["args"] = args # type: ignore
if inputs:
env["inputs"] = inputs # type: ignore
if ports:
env["ports"] = ports # type: ignore
envs[name] = env
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
repo.git.add("calkit.yaml")
if not no_check:
env_lock_fpath = check_environment(env_name=name)
if env_lock_fpath is not None:
repo.git.add(env_lock_fpath)
if not no_commit and repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add Docker environment {name}"])
@new_app.command(name="foreach-stage")
def new_foreach_stage(
cmd: Annotated[
str,
typer.Option(
"--cmd",
help="Command to run. Can include {var} to fill with variable.",
),
],
name: Annotated[str, typer.Option("--name", "-n", help="Stage name.")],
vals: Annotated[list[str], typer.Argument(help="Values to iterate over")],
deps: Annotated[
list[str], typer.Option("--dep", help="Path to add as a dependency.")
] = [],
outs: Annotated[
list[str], typer.Option("--out", help="Path to add as an output.")
] = [],
overwrite: Annotated[
bool,
typer.Option(
"--overwrite", "-f", help="Overwrite stage if one already exists."
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
):
"""Create a new DVC 'foreach' stage.
The list of values must be a simple list. For more complex objects,
edit dvc.yaml directly.
"""
pipeline = calkit.dvc.read_pipeline()
if name in pipeline and not overwrite:
raise_error("Stage already exists; use -f to overwrite")
if "stages" not in pipeline:
pipeline["stages"] = {}
pipeline["stages"][name] = dict(
foreach=vals,
do=dict(
cmd=cmd.replace("{var}", "${item}"),
outs=[out.replace("{var}", "${item}") for out in outs],
deps=[dep.replace("{var}", "${item}") for dep in deps],
),
)
with open("dvc.yaml", "w") as f:
calkit.ryaml.dump(pipeline, f)
repo = calkit.git.get_repo()
repo.git.add("dvc.yaml")
if not no_commit and repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add foreach stage {name}"])
@new_app.command(name="dataset")
def new_dataset(
path: str,
title: Annotated[str, typer.Option("--title")],
description: Annotated[str, typer.Option("--description")],
stage_name: Annotated[
str | None,
typer.Option(
"--stage",
help="Name of the pipeline stage that generates this dataset.",
),
] = None,
cmd: Annotated[
str | None,
typer.Option(
"--cmd", help="Command to add to the stage, if specified."
),
] = None,
deps: Annotated[
list[str], typer.Option("--dep", help="Path to stage dependency.")
] = [],
outs: Annotated[
list[str],
typer.Option(
"--out",
help=(
"Path to stage output. "
"Dataset path will be added automatically."
),
),
] = [],
outs_from_stage: Annotated[
str | None,
typer.Option(
"--deps-from-stage-outs",
help="Stage name from which to add outputs as dependencies.",
),
] = None,
created_by_email: Annotated[
str | None,
typer.Option(
"--created-by-email",
help=(
"Email of whoever collected this data for the project, "
"which marks it as primary rather than imported or computed."
),
),
] = None,
created_by_orcid: Annotated[
str | None,
typer.Option(
"--created-by-orcid",
help="ORCID of whoever collected this data.",
),
] = None,
created_with_ai: Annotated[
list[str],
typer.Option(
"--created-with-ai",
help=(
"Generative AI tool they used, e.g. 'Claude Opus 5'. "
"Repeat for several."
),
),
] = [],
no_commit: Annotated[bool, typer.Option("--no-commit")] = False,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite existing dataset if one exists.",
),
] = False,
):
"""Create a new dataset."""
from calkit.dvc import run_dvc_command
ck_info = calkit.load_calkit_info()
datasets = ck_info.get("datasets", [])
paths = [f.get("path") for f in datasets]
if not overwrite and path in paths:
raise_error(f"Dataset at path {path} already exists")
elif overwrite and path in paths:
datasets = [fig for fig in datasets if fig.get("path") != path]
if cmd is not None and stage_name is None:
raise_error("Stage name must be provided if command is specified")
if (deps or outs or outs_from_stage) and not cmd:
raise_error("Command must be provided")
if (deps or outs or outs_from_stage) and not stage_name:
raise_error("Stage name must be provided")
created_by = _person_from_options(
"created-by", created_by_email, created_by_orcid, created_with_ai
)
obj: dict = dict(path=path, title=title)
if description is not None:
obj["description"] = description
if stage_name is not None:
obj["stage"] = stage_name
if created_by is not None:
obj["created_by"] = created_by
if cmd:
if outs_from_stage:
pipeline = calkit.dvc.read_pipeline()
stages = pipeline.get("stages", {})
if outs_from_stage not in stages:
raise_error(f"Stage {outs_from_stage} does not exist")
stage = stages[outs_from_stage]
if "foreach" in stage:
for val in stage["foreach"]:
for out in stage.get("do", {}).get("outs", []):
deps.append(out.replace("${item}", val))
else:
deps += stage.get("outs", [])
if path not in outs:
outs.append(path)
deps_cmd = []
for dep in deps:
deps_cmd += ["-d", dep]
outs_cmd = []
for out in outs:
outs_cmd += ["-o", out]
result = run_dvc_command(
[
"stage",
"add",
"-n",
stage_name,
]
+ (["-f"] if overwrite else [])
+ deps_cmd
+ outs_cmd
+ [cmd]
)
if result != 0:
raise_error(f"Failed to add dataset {obj}")
datasets.append(obj)
ck_info["datasets"] = datasets
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit:
repo = calkit.git.get_repo()
repo.git.add("calkit.yaml")
if cmd:
repo.git.add("dvc.yaml")
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add dataset {path}"])
@new_app.command(name="publication|pub", help="Create a new publication.")
def new_publication(
path: Annotated[
str,
typer.Argument(
help=(
"Path for the publication. "
"If using a template, this could be a directory."
)
),
],
title: Annotated[
str, typer.Option("--title", help="The title of the publication.")
],
kind: Annotated[
str,
typer.Option(
"--kind", help="Kind of the publication, e.g., 'journal-article'."
),
],
description: Annotated[
str | None,
typer.Option(
"--description", help="A description of the publication."
),
] = None,
stage_name: Annotated[
str | None,
typer.Option(
"--stage",
help="Name of the pipeline stage to build the output file.",
),
] = None,
deps: Annotated[
list[str], typer.Option("--dep", help="Path to stage dependency.")
] = [],
outs_from_stage: Annotated[
str | None,
typer.Option(
"--deps-from-stage-outs",
help="Stage name from which to add outputs as dependencies.",
),
] = None,
template: Annotated[
str | None,
typer.Option(
"--template",
"-t",
help=(
"Template with which to create the source files. "
"Should be in the format {type}/{name}."
),
),
] = None,
env_name: Annotated[
str | None,
typer.Option(
"--environment",
help="Name of the build environment to create, if desired.",
),
] = None,
no_commit: Annotated[
bool,
typer.Option(
"--no-commit", help="Do not commit resulting changes to the repo."
),
] = False,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite existing objects if they already exist.",
),
] = False,
) -> None:
from calkit.models.pipeline import LatexStage
ck_info = calkit.load_calkit_info()
pubs = ck_info.get("publications", [])
envs = ck_info.get("environments", {})
pub_paths = [p.get("path") for p in pubs]
if template is not None:
template_type, _ = template.split("/")
else:
template_type = None
# Check all of our inputs
if template_type is not None and template_type not in ["latex"]:
raise_error(f"Unknown template type '{template_type}'")
if env_name is not None and template_type != "latex":
raise_error("Environments can only be created for latex templates")
if env_name is not None and env_name in envs and not overwrite:
typer.echo(
typer.style(
f"Environment '{env_name}' already exists; overwriting",
fg="yellow",
)
)
if template_type is not None:
try:
template_obj = calkit.templates.get_template(template) # type: ignore
except ValueError:
raise_error(f"Template '{template}' does not exist")
# Parse outs from stage if specified
if outs_from_stage:
pipeline = calkit.dvc.read_pipeline()
stages = pipeline.get("stages", {})
if outs_from_stage not in stages:
raise_error(f"Stage {outs_from_stage} does not exist")
stage = stages[outs_from_stage]
if "foreach" in stage:
for val in stage["foreach"]:
for out in stage.get("do", {}).get("outs", []):
deps.append(out.replace("${item}", val))
else:
deps += stage.get("outs", [])
# Create publication object
if template_type == "latex":
pub_fpath = pathlib.Path(
path,
template_obj.target.removesuffix(".tex") + ".pdf", # type: ignore
).as_posix()
else:
pub_fpath = path
if not overwrite and pub_fpath in pub_paths:
raise_error(f"Publication with path {pub_fpath} already exists")
elif overwrite and pub_fpath in pub_paths:
pubs = [p for p in pubs if p.get("path") != pub_fpath]
pub = dict(
path=pathlib.Path(pub_fpath).as_posix(),
kind=kind,
title=title,
stage=stage_name,
)
# Keep calkit.yaml free of null entries for optional fields
if description is not None:
pub["description"] = description
pubs.append(pub)
ck_info["publications"] = pubs
repo = calkit.git.get_repo()
# Create environment if applicable
if env_name is not None and template_type == "latex":
env = dict(
kind="docker",
image="texlive/texlive:latest-full",
description="TeXlive full.",
)
envs[env_name] = env
ck_info["environments"] = envs
# Copy in template files if applicable. This happens before the stage is
# built so its inputs can be detected from the document itself: a template
# like jfm brings its own class, bibliography style, and style files, and
# they're only dependencies of building the paper if declared.
if template is not None and template_type == "latex":
if overwrite and os.path.exists(path):
shutil.rmtree(path)
calkit.templates.use_template(
name=template, dest_dir=path, title=title
)
repo.git.add(path)
# Create stage if applicable
if (
stage_name is not None
and template_type == "latex"
and env_name is not None
):
target_path = pathlib.Path(path, template_obj.target).as_posix() # type: ignore
stage = LatexStage(
kind="latex",
environment=env_name,
target_path=target_path,
inputs=_add_detected_latex_inputs(target_path, deps), # type: ignore
outputs=[pub_fpath],
).to_ck_dict()
if "pipeline" not in ck_info:
ck_info["pipeline"] = {}
if "stages" not in ck_info["pipeline"]:
ck_info["pipeline"]["stages"] = {}
ck_info["pipeline"]["stages"][stage_name] = stage
with open("calkit.yaml", "w") as f:
calkit.ryaml.dump(ck_info, f)
repo.git.add("calkit.yaml")
if not no_commit and repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add new publication {pub_fpath}"])
@new_app.command("conda-env")
def new_conda_env(
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
],
packages: Annotated[
list[str] | None,
typer.Argument(help="Packages to include in the environment."),
] = None,
conda_name: Annotated[
str | None,
typer.Option(
"--conda-name",
help=(
"Name to use in the Conda environment file, if desired. "
"Will be automatically generated if not provided. "
"Note that these should be unique since Conda environments are "
"a system-wide collection."
),
),
] = None,
path: Annotated[
str, typer.Option("--path", help="Environment YAML file path.")
] = "environment.yml",
pip_packages: Annotated[
list[str], typer.Option("--pip", help="Packages to install with pip.")
] = [],
prefix: Annotated[
str | None,
typer.Option("--prefix", help="Prefix for environment location."),
] = None,
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check environment is up-to-date after creation.",
),
] = False,
):
"""Create a new Conda environment."""
from calkit.cli.check import check_environment
if packages is not None and os.path.isfile(path) and not overwrite:
raise_error("Output path already exists (use -f to overwrite)")
elif packages is None and not os.path.isfile(path):
raise_error("Packages must be provided if path doesn't exist")
repo = calkit.git.get_repo()
# Add environment to Calkit info
ck_info = calkit.load_calkit_info()
# If environments is a list instead of a dict, reformulate it
envs = ck_info.get("environments", {})
if isinstance(envs, list):
typer.echo("Converting environments from list to dict")
envs = {env.pop("name"): env for env in envs}
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
if conda_name is None:
project_name = calkit.detect_project_name(prepend_owner=False)
conda_name = calkit.to_kebab_case(project_name) + "." + name
if packages is not None:
assert isinstance(packages, list)
# Write environment to path
_check_path_dir(path)
conda_env: dict = dict(
name=conda_name, channels=["conda-forge"], dependencies=packages
)
if prefix is not None:
from calkit.cli.main import ignore
conda_env["prefix"] = prefix
ignore(prefix, no_commit=True)
repo.git.add(".gitignore")
if pip_packages:
conda_env["dependencies"].append(dict(pip=pip_packages))
with open(path, "w") as f:
ryaml.dump(conda_env, f)
elif packages is None and os.path.isfile(path):
with open(path) as f:
conda_env = ryaml.load(f)
# Remove prefix
conda_env.pop("prefix", None)
if prefix is not None:
conda_env["prefix"] = prefix
# Rename so we can track it more uniquely
conda_env["name"] = conda_name
with open(path, "w") as f:
ryaml.dump(conda_env, f)
repo.git.add(path)
typer.echo("Adding environment to calkit.yaml")
env = dict(path=path, kind="conda")
if prefix is not None:
env["prefix"] = prefix
if description is not None:
env["description"] = description
envs[name] = env
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
repo.git.add("calkit.yaml")
if not no_check:
env_lock_fpath = check_environment(env_name=name)
if env_lock_fpath is not None:
repo.git.add(env_lock_fpath)
if not no_commit and repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add Conda environment {name}"])
@new_app.command("uv-env")
def new_uv_env(
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
] = "main",
path: Annotated[
str | None,
typer.Option(
"--path",
help="Environment file path. Must end with 'pyproject.toml'.",
),
] = None,
python_version: Annotated[
str | None, typer.Option("--python", "-p", help="Python version.")
] = None,
packages: Annotated[
list[str] | None,
typer.Argument(help="Packages to include in the environment."),
] = None,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check environment is up-to-date after creation.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
):
"""Create a new uv project environment."""
if path is not None and not path.endswith("pyproject.toml"):
raise_error("Environment path must end with 'pyproject.toml'")
ck_info = calkit.load_calkit_info()
envs = ck_info.get("environments", {})
if name in envs:
raise_error(f"Environment with name {name} already exists")
if path is None:
if not envs:
envdir = "."
path = "pyproject.toml"
else:
envdir = f".calkit/envs/{name}"
os.makedirs(envdir, exist_ok=True)
path = os.path.join(envdir, "pyproject.toml")
else:
envdir = os.path.dirname(path)
if envdir:
os.makedirs(envdir, exist_ok=True)
else:
envdir = "."
if not os.path.isfile(path):
res = subprocess.run(
["uv", "init", "--bare", "--directory", envdir, "--no-workspace"]
+ (["--python", python_version] if python_version else []),
)
if res.returncode != 0:
raise_error("Failed to create uv environment")
if packages:
res = subprocess.run(["uv", "add"] + packages, cwd=envdir)
if res.returncode != 0:
raise_error("Failed to add packages to uv environment")
if not no_check:
subprocess.run(["uv", "sync"], cwd=envdir)
envs[name] = dict(kind="uv", path=path)
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit:
repo = calkit.git.get_repo()
repo.git.add(path)
repo.git.add("calkit.yaml")
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add uv environment {name}"])
@new_app.command("slurm-env")
def new_slurm_env(
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
],
host: Annotated[
str,
typer.Option("--host", help="Host where SLURM commands should run."),
] = "localhost",
default_options: Annotated[
list[str],
typer.Option(
"--default-option",
help=(
"Default sbatch/srun option string (for example --gpus=1). "
"Repeat for multiple options."
),
),
] = [],
default_setup: Annotated[
list[str],
typer.Option(
"--default-setup",
help=(
"Default shell setup command to run before SLURM jobs "
"(for example 'module load julia/1.11'). Repeat for "
"multiple commands."
),
),
] = [],
max_concurrent_jobs: Annotated[
int | None,
typer.Option(
"--max-concurrent-jobs",
help=(
"Maximum number of this project's jobs allowed in the "
"queue at once, or 0 for no limit. Submissions beyond this "
"wait for a slot, so an iterated stage does not take over a "
"shared cluster's queue. Unlimited by default."
),
),
] = None,
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
):
"""Create a new SLURM environment."""
host = host.strip()
if not host:
raise_error("Host is required")
ck_info = calkit.load_calkit_info()
envs = ck_info.get("environments", {})
if isinstance(envs, list):
typer.echo("Converting environments from list to dict")
envs = {env.pop("name"): env for env in envs}
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
normalized_default_options = [
opt.strip() for opt in default_options if opt.strip()
]
normalized_default_setup = [
cmd.strip() for cmd in default_setup if cmd.strip()
]
env = {"kind": "slurm", "host": host}
if normalized_default_options:
env["default_options"] = normalized_default_options # type: ignore
if normalized_default_setup:
env["default_setup"] = normalized_default_setup # type: ignore
if max_concurrent_jobs is not None:
if max_concurrent_jobs < 0:
raise_error("--max-concurrent-jobs cannot be negative")
# 0 means no limit, the same as it does for `calkit update`.
if max_concurrent_jobs:
env["max_concurrent_jobs"] = max_concurrent_jobs # type: ignore
if description is not None:
env["description"] = description
envs[name] = env
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
from calkit.cli.check import check_environment
env_lock_fpath = check_environment(env_name=name)
if not no_commit:
repo = calkit.git.get_repo()
repo.git.add("calkit.yaml")
if env_lock_fpath is not None:
repo.git.add(str(env_lock_fpath))
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add SLURM environment {name}"])
@new_app.command("pbs-env")
def new_pbs_env(
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
],
host: Annotated[
str,
typer.Option("--host", help="Host where PBS commands should run."),
] = "localhost",
default_options: Annotated[
list[str],
typer.Option(
"--default-option",
help=(
"Default qsub option string (for example "
"--default-option=-l --default-option=walltime=01:00:00). "
"Repeat for multiple options."
),
),
] = [],
default_setup: Annotated[
list[str],
typer.Option(
"--default-setup",
help=(
"Default shell setup command to run before PBS jobs "
"(for example 'module load julia/1.11'). Repeat for "
"multiple commands."
),
),
] = [],
max_concurrent_jobs: Annotated[
int | None,
typer.Option(
"--max-concurrent-jobs",
help=(
"Maximum number of this project's jobs allowed in the "
"queue at once, or 0 for no limit. Submissions beyond this "
"wait for a slot, so an iterated stage does not take over a "
"shared cluster's queue. Unlimited by default."
),
),
] = None,
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
):
"""Create a new PBS environment."""
from calkit.cli.check import check_environment
host = host.strip()
if not host:
raise_error("Host is required")
ck_info = calkit.load_calkit_info()
envs = ck_info.get("environments", {})
if isinstance(envs, list):
typer.echo("Converting environments from list to dict")
envs = {env.pop("name"): env for env in envs}
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
normalized_default_options = [
opt.strip() for opt in default_options if opt.strip()
]
normalized_default_setup = [
cmd.strip() for cmd in default_setup if cmd.strip()
]
env = {"kind": "pbs", "host": host}
if normalized_default_options:
env["default_options"] = normalized_default_options # type: ignore
if normalized_default_setup:
env["default_setup"] = normalized_default_setup # type: ignore
if max_concurrent_jobs is not None:
if max_concurrent_jobs < 0:
raise_error("--max-concurrent-jobs cannot be negative")
# 0 means no limit, the same as it does for `calkit update`.
if max_concurrent_jobs:
env["max_concurrent_jobs"] = max_concurrent_jobs # type: ignore
if description is not None:
env["description"] = description
envs[name] = env
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
env_lock_fpath = check_environment(env_name=name)
if not no_commit:
repo = calkit.git.get_repo()
repo.git.add("calkit.yaml")
if env_lock_fpath is not None:
repo.git.add(str(env_lock_fpath))
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add PBS environment {name}"])
@new_app.command("uv-venv")
def new_uv_venv(
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
],
packages: Annotated[
list[str] | None,
typer.Argument(help="Packages to include in the environment."),
] = None,
path: Annotated[
str, typer.Option("--path", help="Path for requirements file.")
] = "requirements.txt",
prefix: Annotated[
str | None,
typer.Option(
"--prefix",
help=(
"Prefix for environment location (defaults to .venv, or "
".calkit/envs/<name>/.venv if .venv is already taken)."
),
),
] = None,
python_version: Annotated[
str | None, typer.Option("--python", "-p", help="Python version.")
] = None,
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check environment is up-to-date after creation.",
),
] = False,
):
"""Create a new uv virtual environment."""
from calkit.cli.check import check_environment
from calkit.environments import DEFAULT_PYTHON_VERSION
if python_version is None:
python_version = DEFAULT_PYTHON_VERSION
if os.path.isfile(path) and packages and not overwrite:
raise_error("Output path already exists (use -f to overwrite)")
elif packages is None and not os.path.isfile(path):
raise_error("If path doesn't exist, packages must be specified")
# Add environment to Calkit info
ck_info = calkit.load_calkit_info()
# If environments is a list instead of a dict, reformulate it
envs = ck_info.get("environments", {})
if isinstance(envs, list):
typer.echo("Converting environments from list to dict")
envs = {env.pop("name"): env for env in envs}
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
# Only pin a prefix if one was explicitly given; otherwise it is resolved
# on the fly (defaulting to .venv) at check/run time
if prefix is not None and not overwrite:
for other_name, other_env in envs.items():
if other_env.get("prefix") == prefix:
raise_error(
f"Environment '{other_name}' already exists with "
f"prefix '{prefix}'"
)
if packages is not None:
packages_txt = "\n".join(packages)
# Write environment to path
_check_path_dir(path)
with open(path, "w") as f:
f.write(packages_txt)
typer.echo("Adding environment to calkit.yaml")
env = dict(path=path, kind="uv-venv")
if prefix is not None:
env["prefix"] = prefix
if python_version is not None:
env["python"] = python_version
if description is not None:
env["description"] = description
envs[name] = env
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit:
repo = calkit.git.get_repo()
repo.git.add(path)
repo.git.add("calkit.yaml")
if not no_check:
env_lock_fpath = check_environment(env_name=name)
if env_lock_fpath is not None and not no_commit:
repo.git.add(env_lock_fpath)
if not no_commit and repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add uv venv {name}"])
@new_app.command("venv")
def new_venv(
packages: Annotated[
list[str],
typer.Argument(help="Packages to include in the environment."),
],
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
],
path: Annotated[
str, typer.Option("--path", help="Path for requirements file.")
] = "requirements.txt",
prefix: Annotated[
str | None,
typer.Option(
"--prefix",
help=(
"Prefix for environment location (defaults to .venv, or "
".calkit/envs/<name>/.venv if .venv is already taken)."
),
),
] = None,
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check environment is up-to-date after creation.",
),
] = False,
):
"""Create a new Python virtual environment with venv."""
from calkit.cli.check import check_environment
if os.path.isfile(path) and not overwrite:
raise_error("Output path already exists (use -f to overwrite)")
repo = calkit.git.get_repo()
# Add environment to Calkit info
ck_info = calkit.load_calkit_info()
# If environments is a list instead of a dict, reformulate it
envs = ck_info.get("environments", {})
if isinstance(envs, list):
typer.echo("Converting environments from list to dict")
envs = {env.pop("name"): env for env in envs}
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
# Only pin a prefix if one was explicitly given; otherwise it is resolved
# on the fly (defaulting to .venv) at check/run time
if prefix is not None and not overwrite:
for other_name, other_env in envs.items():
if other_env.get("prefix") == prefix:
raise_error(
f"Environment '{other_name}' already exists with "
f"prefix '{prefix}'"
)
packages_txt = "\n".join(packages)
# Write environment to path
_check_path_dir(path)
with open(path, "w") as f:
f.write(packages_txt)
repo.git.add(path)
typer.echo("Adding environment to calkit.yaml")
env = dict(path=path, kind="venv")
if prefix is not None:
env["prefix"] = prefix
if description is not None:
env["description"] = description
envs[name] = env
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
repo.git.add("calkit.yaml")
if not no_check:
env_lock_fpath = check_environment(env_name=name)
if env_lock_fpath is not None:
repo.git.add(env_lock_fpath)
if not no_commit and repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add venv {name}"])
@new_app.command("pixi-env")
def new_pixi_env(
packages: Annotated[
list[str],
typer.Argument(help="Packages to include in the environment."),
],
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
],
pip_packages: Annotated[
list[str], typer.Option("--pip", help="Packages to install with pip.")
] = [],
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
platforms: Annotated[
list[str], typer.Option("--platform", "-p", help="Platform.")
] = [],
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check environment is up-to-date after creation.",
),
] = False,
):
"""Create a new pixi virtual environment."""
from calkit.cli.check import check_environment
repo = calkit.git.get_repo()
# Add environment to Calkit info
ck_info = calkit.load_calkit_info()
# If environments is a list instead of a dict, reformulate it
envs = ck_info.get("environments", {})
if isinstance(envs, list):
typer.echo("Converting environments from list to dict")
envs = {env.pop("name"): env for env in envs}
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
# Create the environment now
if not os.path.isfile("pixi.toml"):
subprocess.run(
[
"pixi",
"init",
".",
"--format",
"pixi",
]
)
# Ensure all platforms exist
for p in platforms:
subprocess.run(["pixi", "project", "platform", "add", p])
# Install the packages
for pkg in packages:
subprocess.run(["pixi", "add", pkg, "--feature", name])
# Install any PyPI packages
for pkg in pip_packages:
subprocess.run(["pixi", "add", "--pypi", pkg, "--feature", name])
# Create a pixi environment
subprocess.run(
[
"pixi",
"project",
"environment",
"add",
name,
"--feature",
name,
"--force",
]
)
typer.echo("Adding environment to calkit.yaml")
env = dict(kind="pixi", path="pixi.toml", name=name)
if description is not None:
env["description"] = description
envs[name] = env
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
repo.git.add("pixi.toml")
repo.git.add("calkit.yaml")
if not no_check:
env_lock_fpath = check_environment(env_name=name)
if env_lock_fpath is not None:
repo.git.add(env_lock_fpath)
if not no_commit and repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add pixi env {name}"])
@new_app.command("julia-env")
def new_julia_env(
packages: Annotated[
list[str] | None,
typer.Argument(
help="Optional packages to include in the environment."
),
] = None,
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
] = "main",
path: Annotated[
str | None,
typer.Option("--path", help="Path for Project.toml file."),
] = None,
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
julia_version: Annotated[
str | None,
typer.Option(
"--julia",
"-j",
help="Julia version. Auto-detected if not supplied.",
),
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check environment is up-to-date after creation.",
),
] = False,
):
"""Create a new Julia environment or add an existing one to calkit.yaml."""
import git
from calkit.cli.check import check_environment
if path is None:
path = "Project.toml"
if not os.path.basename(path) == "Project.toml":
raise_error(
"Julia environment paths must point to a Project.toml file"
)
try:
repo = calkit.git.get_repo()
except git.InvalidGitRepositoryError:
raise_error(
"Current directory is not a Git repository; run calkit init"
)
# Add environment to Calkit info
ck_info = calkit.load_calkit_info()
# If environments is a list instead of a dict, reformulate it
envs = ck_info.get("environments", {})
if isinstance(envs, list):
typer.echo("Converting environments from list to dict")
envs = {env.pop("name"): env for env in envs}
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
# Detect Julia version if not supplied
if julia_version is None:
try:
julia_version = calkit.julia.get_version()
except ValueError as e:
raise_error(
f"Failed to detect Julia version: {e}. "
"Use --julia to specify a version."
)
# Check for duplicate Julia environments with same path and version
for env_name, env_config in envs.items():
if (
env_config.get("kind") == "julia"
and env_config.get("path") == path
and env_config.get("julia") == julia_version
):
raise_error(
f"Julia environment '{env_name}' already exists with path "
f"'{path}' and version '{julia_version}'; "
f"Use a different path, version, or --overwrite to replace it"
)
env_dir = os.path.dirname(path)
if env_dir:
os.makedirs(env_dir, exist_ok=True)
else:
env_dir = "."
project_exists = os.path.isfile(path)
created_project_file = False
# If packages are provided, initialize via Julia/Pkg.
if packages:
# First make sure the Julia version is installed
cmd = ["juliaup", "add", julia_version]
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
raise_error(f"Failed to install Julia version {julia_version}")
cmd = ["julia", f"+{julia_version}", f"--project={env_dir}", "-e"]
install_cmd = f'using Pkg; Pkg.activate("{calkit.julia.escape_string(env_dir)}");'
for package in packages or []:
install_cmd += f' Pkg.add("{package}");'
cmd.append(install_cmd)
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
raise_error("Failed to create new Julia environment")
elif not project_exists:
# Allow creating a Julia environment scaffold without requiring Julia
# tooling at creation time. IJulia or other packages can be added later.
with open(path, "w") as f:
f.write("[deps]\n")
created_project_file = True
typer.echo("Adding environment to calkit.yaml")
env = dict(kind="julia", path=path, julia=julia_version)
if description is not None:
env["description"] = description
envs[name] = env
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit:
repo.git.add("calkit.yaml")
if packages or created_project_file:
repo.git.add(path)
if packages:
repo.git.add(os.path.join(env_dir, "Manifest.toml"))
if not no_check and packages:
env_lock_fpath = check_environment(env_name=name)
if env_lock_fpath is not None:
repo.git.add(env_lock_fpath)
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add Julia env {name}"])
@new_app.command("renv")
def new_renv(
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
] = "main",
packages: Annotated[
list[str] | None,
typer.Argument(help="Packages to include in the environment."),
] = None,
path: Annotated[
str | None,
typer.Option(
"--path",
help="Environment file path. Must end with 'DESCRIPTION'.",
),
] = None,
r_version: Annotated[
str | None, typer.Option("--r-version", "-r", help="R version.")
] = None,
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check environment is up-to-date after creation.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
):
"""Create a new R environment with renv."""
from calkit.environments import create_r_description_content
if path is not None and not path.endswith("DESCRIPTION"):
raise_error("Environment path must end with 'DESCRIPTION'")
ck_info = calkit.load_calkit_info()
envs = ck_info.get("environments", {})
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
if path is None:
if not envs:
envdir = "."
path = "DESCRIPTION"
else:
envdir = f".calkit/envs/{name}"
os.makedirs(envdir, exist_ok=True)
path = os.path.join(envdir, "DESCRIPTION")
else:
envdir = os.path.dirname(path)
if envdir:
os.makedirs(envdir, exist_ok=True)
else:
envdir = "."
# Check if DESCRIPTION file exists and handle accordingly
if os.path.isfile(path):
if packages and not overwrite:
raise_error(
"DESCRIPTION file already exists (use -f to overwrite)"
)
else:
# Create DESCRIPTION file
if packages is None:
packages = []
desc_content = create_r_description_content(packages)
with open(path, "w") as f:
f.write(desc_content)
# If packages provided and overwrite, update DESCRIPTION
if packages and overwrite and os.path.isfile(path):
desc_content = create_r_description_content(packages)
with open(path, "w") as f:
f.write(desc_content)
# Check if renv is already initialized
lock_path = os.path.join(envdir, "renv.lock")
renv_already_initialized = os.path.isfile(lock_path)
# Initialize renv if not already initialized
if not renv_already_initialized:
# First ensure renv is installed with CRAN mirror configured
install_cmd = [
"Rscript",
"--vanilla",
"-e",
(
"options(repos = c(CRAN = 'https://cloud.r-project.org')); "
"if (!requireNamespace('renv', quietly=TRUE)) "
"install.packages('renv')"
),
]
res = subprocess.run(install_cmd, cwd=envdir)
if res.returncode != 0:
raise_error("Failed to install renv package")
# Now initialize renv
init_cmd = ["Rscript", "-e", "renv::init(bare=TRUE)"]
res = subprocess.run(init_cmd, cwd=envdir)
if res.returncode != 0:
raise_error("Failed to initialize renv environment")
# Set R version if specified (renv will store it in renv.lock)
if r_version:
version_cmd = ["Rscript", "-e", f'renv::use(r = "{r_version}")']
res = subprocess.run(version_cmd, cwd=envdir)
if res.returncode != 0:
warn(f"Failed to set R version to {r_version}")
# Install packages from DESCRIPTION and create lockfile
if packages and not no_check and not renv_already_initialized:
# Use hydrate to install packages from DESCRIPTION
hydrate_cmd = ["Rscript", "-e", "renv::hydrate()"]
res = subprocess.run(hydrate_cmd, cwd=envdir)
if res.returncode != 0:
warn("Failed to hydrate environment from DESCRIPTION")
# Create lockfile from DESCRIPTION
snapshot_cmd = [
"Rscript",
"-e",
"renv::snapshot(type='explicit', prompt=FALSE)",
]
res = subprocess.run(snapshot_cmd, cwd=envdir)
if res.returncode != 0:
raise_error("Failed to create renv lockfile")
# Add to calkit.yaml
env_info = dict(kind="renv", path=path)
if description:
env_info["description"] = description
envs[name] = env_info
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit:
repo = calkit.git.get_repo()
repo.git.add(path)
if os.path.exists(lock_path):
repo.git.add(lock_path)
# Also add .Rprofile if it exists
rprofile_path = os.path.join(envdir, ".Rprofile")
if os.path.exists(rprofile_path):
repo.git.add(rprofile_path)
repo.git.add("calkit.yaml")
if repo.git.diff("--staged"):
repo.git.commit(["-m", f"Add renv environment {name}"])
@new_app.command("nix-env")
def new_nix_env(
packages: Annotated[
list[str],
typer.Argument(
help="Nixpkgs packages to include in the dev shell (e.g. R)."
),
],
name: Annotated[
str, typer.Option("--name", "-n", help="Environment name.")
],
path: Annotated[
str | None,
typer.Option(
"--path",
help="Flake file path. Must end with 'flake.nix'.",
),
] = None,
nixpkgs_url: Annotated[
str,
typer.Option(
"--nixpkgs-url",
help="Flake input URL for nixpkgs.",
),
] = "github:NixOS/nixpkgs/nixos-unstable",
description: Annotated[
str | None, typer.Option("--description", help="Description.")
] = None,
overwrite: Annotated[
bool,
typer.Option(
"--overwrite",
"-f",
help="Overwrite any existing environment with this name.",
),
] = False,
no_check: Annotated[
bool,
typer.Option(
"--no-check",
help="Do not run 'nix flake lock' after creating the flake.",
),
] = False,
no_commit: Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes.")
] = False,
):
"""Create a new Nix flake-based environment."""
import platform as _platform
from calkit.environments import create_nix_flake_content
if path is not None and not path.endswith("flake.nix"):
raise_error("Environment path must end with 'flake.nix'")
ck_info = calkit.load_calkit_info()
envs = ck_info.get("environments", {})
if name in envs and not overwrite:
raise_error(
f"Environment with name {name} already exists "
"(use -f to overwrite)"
)
if path is None:
# If no env exists yet, put the flake at the repo root; otherwise
# nest it under .calkit/envs/<name>/ to keep peer flakes from
# colliding on the same flake.lock.
if not envs:
envdir = "."
path = "flake.nix"
else:
envdir = f".calkit/envs/{name}"
os.makedirs(envdir, exist_ok=True)
path = os.path.join(envdir, "flake.nix")
else:
envdir = os.path.dirname(path) or "."
if envdir != ".":
os.makedirs(envdir, exist_ok=True)
if os.path.isfile(path) and not overwrite:
raise_error(f"{path} already exists (use -f to overwrite)")
content = create_nix_flake_content(
packages=packages,
description=description,
nixpkgs_url=nixpkgs_url,
)
with open(path, "w") as f:
f.write(content)
# ``nix flake lock`` refuses to see files that aren't tracked by Git
# when run inside a Git repo. Stage the new flake before locking so
# the brand-new (still untracked) file is visible to Nix. Staging is
# harmless when ``--no-commit`` is set -- the commit step is what we
# gate on, not the staging.
try:
_flake_repo = calkit.git.get_repo()
except calkit.git.InvalidGitRepositoryError:
_flake_repo = None
if _flake_repo is not None:
_flake_repo.git.add(path)
# Generate flake.lock for reproducibility unless skipped. The lock
# is what makes the environment reproducible across machines, so we
# want it committed alongside flake.nix.
lock_path = os.path.join(envdir, "flake.lock")
if not no_check:
if shutil.which("nix") is None:
if _platform.system() == "Windows":
warn(
"Nix is not available natively on Windows; skipping "
"'nix flake lock'. Run Calkit inside WSL2 to generate "
"flake.lock."
)
else:
warn(
"The 'nix' command was not found; skipping "
"'nix flake lock'. Install it with "
"'calkit install nix' or from https://nixos.org/download."
)
else:
res = subprocess.run(
[
"nix",
"--extra-experimental-features",
"nix-command flakes",
"flake",
"lock",
],
cwd=envdir,
)
if res.returncode != 0:
raise_error("Failed to generate flake.lock")
env_info: dict = dict(kind="nix", path=path)
if description:
env_info["description"] = description
envs[name] = env_info
ck_info["environments"] = envs
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit and _flake_repo is not None:
# ``path`` was already staged earlier so ``nix flake lock`` could
# see it; just add the lock file (if produced) and calkit.yaml.
if os.path.exists(lock_path):
_flake_repo.git.add(lock_path)
_flake_repo.git.add("calkit.yaml")
if _flake_repo.git.diff("--staged"):
_flake_repo.git.commit(["-m", f"Add nix environment {name}"])
class Status(str, Enum):
in_progress = "in-progress"
on_hold = "on-hold"
completed = "completed"
@new_app.command(name="status")
def new_status(
status: Annotated[
Status,
typer.Argument(help="Current status of the project."),
],
message: Annotated[
str,
typer.Option(
"--message",
"-m",
help="Optional message describing the status.",
),
] = "",
no_commit: Annotated[
bool,
typer.Option(
"--no-commit", help="Do not commit changes to the status log."
),
] = False,
):
"""Add a new project status to the log."""
typer.echo(f"Adding {status.value} status log entry")
fpath = os.path.join(".calkit", "status.csv")
os.makedirs(".calkit", exist_ok=True)
now = calkit.utcnow(remove_tz=False)
# Append to end of CSV
write_header = not os.path.isfile(fpath)
with open(fpath, "a", newline="") as f:
writer = csv.writer(f)
if write_header:
writer.writerow(["timestamp", "status", "message"])
writer.writerow([now.isoformat(), status.value, message])
if not no_commit:
typer.echo("Committing")
repo = calkit.git.get_repo()
repo.git.add(fpath)
repo.git.commit([fpath, "-m", f"Add {status.value} status log entry"])
class StageKind(str, Enum):
python_script = "python-script"
latex = "latex"
r_script = "r-script"
sh_script = "sh-script"
bash_script = "bash-script"
zsh_script = "zsh-script"
matlab_script = "matlab-script"
class StageArgs:
"""This is just a way to deduplicate stage arguments."""
name = Annotated[
str,
typer.Option("--name", "-n", help="Stage name, typically kebab-case."),
]
environment = Annotated[
str,
typer.Option(
"--environment", "-e", help="Environment to use to run the stage."
),
]
inputs = Annotated[
list[str],
typer.Option(
"--input", "-i", help="A path on which the stage depends."
),
]
outputs = Annotated[
list[str],
typer.Option(
"--output", "-o", help="A path that is produced by the stage."
),
]
outs_no_delete = Annotated[
list[str],
typer.Option(
"--out-no-delete",
help="An output that should not be deleted before running.",
),
]
outs_git = Annotated[
list[str],
typer.Option(
"--out-git",
help="An output that should be stored with Git instead of DVC.",
),
]
outs_git_no_delete = Annotated[
list[str],
typer.Option(
"--out-git-no-delete",
help=(
"An output that should be tracked with Git instead of DVC, "
"and also should not be deleted before running stage."
),
),
]
outs_no_store = Annotated[
list[str],
typer.Option(
"--out-no-store",
help="An output that should not be stored in version control.",
),
]
outs_no_store_no_delete = Annotated[
list[str],
typer.Option(
"--out-no-store-no-delete",
help=(
"An output that should not be stored in version control, "
"and should not be deleted before running."
),
),
]
overwrite = Annotated[
bool,
typer.Option(
"--overwrite",
"--force",
"-f",
help="Overwrite an existing stage with this name if necessary.",
),
]
no_check = Annotated[
bool,
typer.Option(
"--no-check",
help="Do not check if the target, deps, environment, etc., exist.",
),
]
no_commit = Annotated[
bool, typer.Option("--no-commit", help="Do not commit changes to Git.")
]
script_path = Annotated[
str, typer.Option("--script-path", "-s", help="Path to script.")
]
args = Annotated[
list[str],
typer.Option("--arg", help="Argument to pass to the script."),
]
def _save_stage(
stage: calkit.models.pipeline.Stage,
name: str,
overwrite: bool = False,
no_check: bool = False,
no_commit: bool = False,
) -> None:
"""Save a Calkit pipeline stage."""
from git.exc import InvalidGitRepositoryError
ck_info = calkit.load_calkit_info()
if "pipeline" not in ck_info:
ck_info["pipeline"] = {}
if "stages" not in ck_info["pipeline"]:
ck_info["pipeline"]["stages"] = {}
stages = ck_info["pipeline"]["stages"]
if name in stages and not overwrite:
raise_error(
f"Stage '{name}' already exists; consider using --overwrite"
)
# Check environment exists
if not no_check:
env_names = ck_info.get("environments", {})
if stage.environment not in env_names:
raise_error(f"Environment {stage.environment} does not exist")
stages[name] = stage.to_ck_dict()
with open("calkit.yaml", "w") as f:
ryaml.dump(ck_info, f)
if not no_commit:
try:
repo = calkit.git.get_repo()
except InvalidGitRepositoryError:
raise_error("Can't commit because this is not a Git repo")
repo.git.add("calkit.yaml")
if "calkit.yaml" in calkit.git.get_staged_files():
repo.git.commit(
[
"calkit.yaml",
"-m",
f"Add {stage.kind} pipeline stage '{name}'",
]
)
def _add_detected_latex_inputs(
target_path: str, inputs: list[str], wdir: str | None = None
) -> list[str]:
"""Append the files a LaTeX document reads to a stage's inputs.
LaTeX resolves its own class, style, bibliography, and figure files,
so without this they're invisible to the pipeline: editing the class
file wouldn't rebuild the paper, and the web app's in-browser preview,
which loads exactly what the stage declares, couldn't compile at all.
Anything the user named explicitly is kept and not duplicated.
"""
import calkit.latex
from calkit.detect import filter_covered_inputs
detected = filter_covered_inputs(
calkit.latex.detect_inputs(target_path, wdir=wdir), inputs
)
if detected:
typer.echo(f"Detected inputs: {', '.join(detected)}")
return list(inputs) + detected
def _to_ck_outs(
outputs: list[str],
outs_git: list[str],
outs_git_no_delete: list[str],
outs_no_delete: list[str],
outs_no_store: list[str],
outs_no_store_no_delete: list[str],
) -> list[str | calkit.models.pipeline.PathOutput]:
"""Format stage outputs from CLI for Calkit pipeline."""
ck_outs: list[str | calkit.models.pipeline.PathOutput] = list(outputs)
for out in outs_git:
ck_outs.append(
calkit.models.pipeline.PathOutput(
path=out,
storage="git",
delete_before_run=True,
)
)
for out in outs_git_no_delete:
ck_outs.append(
calkit.models.pipeline.PathOutput(
path=out,
storage="git",
delete_before_run=False,
)
)
for out in outs_no_delete:
ck_outs.append(
calkit.models.pipeline.PathOutput(
path=out,
storage="dvc",
delete_before_run=False,
)
)
for out in outs_no_store:
ck_outs.append(
calkit.models.pipeline.PathOutput(
path=out,
storage=None,
delete_before_run=True,
)
)
for out in outs_no_store_no_delete:
ck_outs.append(
calkit.models.pipeline.PathOutput(
path=out,
storage=None,
delete_before_run=False,
)
)
return ck_outs
@new_app.command(name="python-script-stage")
def new_python_script_stage(
name: StageArgs.name,
environment: StageArgs.environment,
script_path: StageArgs.script_path,
args: StageArgs.args = [],
inputs: StageArgs.inputs = [],
outputs: StageArgs.outputs = [],
outs_git: StageArgs.outs_git = [],
outs_git_no_delete: StageArgs.outs_git_no_delete = [],
outs_no_delete: StageArgs.outs_no_delete = [],
outs_no_store: StageArgs.outs_no_store = [],
outs_no_store_no_delete: StageArgs.outs_no_store_no_delete = [],
iter_arg: Annotated[
tuple[str, str] | None,
typer.Option(
"--iter",
help=(
"Iterate over an argument with a comma-separated list, e.g., "
"--iter-arg var_name val1,val2,val3."
),
),
] = None,
overwrite: StageArgs.overwrite = False,
no_check: StageArgs.no_check = False,
no_commit: StageArgs.no_commit = False,
) -> None:
"""Add a stage to the pipeline that runs a Python script."""
from calkit.models.pipeline import StageIteration
ck_outs = _to_ck_outs(
outputs=outputs,
outs_git=outs_git,
outs_git_no_delete=outs_git_no_delete,
outs_no_delete=outs_no_delete,
outs_no_store=outs_no_store,
outs_no_store_no_delete=outs_no_store_no_delete,
)
try:
if iter_arg is not None:
arg_name, vals = iter_arg
i = [StageIteration(arg_name=arg_name, values=vals.split(","))] # type: ignore
else:
i = None
stage = calkit.models.pipeline.PythonScriptStage(
kind="python-script",
environment=environment,
args=args,
inputs=inputs, # type: ignore
outputs=ck_outs,
script_path=script_path,
iterate_over=i,
)
except Exception as e:
raise_error(f"Invalid stage specification: {e}")
_save_stage(
stage=stage,
name=name,
overwrite=overwrite,
no_check=no_check,
no_commit=no_commit,
)
@new_app.command(name="julia-script-stage")
def new_julia_script_stage(
name: StageArgs.name,
environment: StageArgs.environment,
script_path: StageArgs.script_path,
inputs: StageArgs.inputs = [],
outputs: StageArgs.outputs = [],
outs_git: StageArgs.outs_git = [],
outs_git_no_delete: StageArgs.outs_git_no_delete = [],
outs_no_delete: StageArgs.outs_no_delete = [],
outs_no_store: StageArgs.outs_no_store = [],
outs_no_store_no_delete: StageArgs.outs_no_store_no_delete = [],
iter_arg: Annotated[
tuple[str, str] | None,
typer.Option(
"--iter",
help=(
"Iterate over an argument with a comma-separated list, e.g., "
"--iter-arg var_name val1,val2,val3."
),
),
] = None,
overwrite: StageArgs.overwrite = False,
no_check: StageArgs.no_check = False,
no_commit: StageArgs.no_commit = False,
) -> None:
"""Add a stage to the pipeline that runs a Julia script."""
from calkit.models.pipeline import StageIteration
ck_outs = _to_ck_outs(
outputs=outputs,
outs_git=outs_git,
outs_git_no_delete=outs_git_no_delete,
outs_no_delete=outs_no_delete,
outs_no_store=outs_no_store,
outs_no_store_no_delete=outs_no_store_no_delete,
)
try:
if iter_arg is not None:
arg_name, vals = iter_arg
i = [StageIteration(arg_name=arg_name, values=vals.split(","))] # type: ignore
else:
i = None
stage = calkit.models.pipeline.JuliaScriptStage(
kind="julia-script",
environment=environment,
inputs=inputs, # type: ignore
outputs=ck_outs,
script_path=script_path,
iterate_over=i,
)
except Exception as e:
raise_error(f"Invalid stage specification: {e}")
_save_stage(
stage=stage,
name=name,
overwrite=overwrite,
no_check=no_check,
no_commit=no_commit,
)
@new_app.command(name="matlab-script-stage")
def new_matlab_script_stage(
name: StageArgs.name,
environment: StageArgs.environment,
script_path: StageArgs.script_path,
inputs: StageArgs.inputs = [],
outputs: StageArgs.outputs = [],
outs_git: StageArgs.outs_git = [],
outs_git_no_delete: StageArgs.outs_git_no_delete = [],
outs_no_delete: StageArgs.outs_no_delete = [],
outs_no_store: StageArgs.outs_no_store = [],
outs_no_store_no_delete: StageArgs.outs_no_store_no_delete = [],
overwrite: StageArgs.overwrite = False,
no_check: StageArgs.no_check = False,
no_commit: StageArgs.no_commit = False,
):
"""Add a stage to the pipeline that runs a MATLAB script."""
ck_outs = _to_ck_outs(
outputs=outputs,
outs_git=outs_git,
outs_git_no_delete=outs_git_no_delete,
outs_no_delete=outs_no_delete,
outs_no_store=outs_no_store,
outs_no_store_no_delete=outs_no_store_no_delete,
)
try:
stage = calkit.models.pipeline.MatlabScriptStage(
kind="matlab-script",
environment=environment,
inputs=inputs, # type: ignore
outputs=ck_outs,
script_path=script_path,
)
except Exception as e:
raise_error(f"Invalid stage specification: {e}")
_save_stage(
stage=stage,
name=name,
overwrite=overwrite,
no_check=no_check,
no_commit=no_commit,
)
@new_app.command(name="latex-stage")
def new_latex_stage(
name: StageArgs.name,
environment: StageArgs.environment,
target_path: Annotated[
str, typer.Option("--target", help="Target .tex file path.")
],
output_dir: Annotated[
str | None,
typer.Option(
"--output-dir",
help=(
"Directory for the compiled PDF (passed to latexmk as "
"-outdir). Ignored when a latexmkrc is provided."
),
),
] = None,
aux_dir: Annotated[
str | None,
typer.Option(
"--aux-dir",
help=(
"Directory for auxiliary files (passed to latexmk as "
"-auxdir). Ignored when a latexmkrc is provided."
),
),
] = None,
latexmkrc_path: Annotated[
str | None,
typer.Option(
"--latexmkrc", help="Path to a latexmkrc file for compilation."
),
] = None,
latexmk_args: Annotated[
list[str],
typer.Option(
"--latexmk-arg",
help=(
"Extra argument passed through to latexmk. Repeat the option "
"to pass more than one."
),
),
] = [],
inputs: StageArgs.inputs = [],
no_detect_inputs: Annotated[
bool,
typer.Option(
"--no-detect-inputs",
help=(
"Don't add the class, style, bibliography, and figure files "
"the document reads as inputs."
),
),
] = False,
outputs: StageArgs.outputs = [],
outs_git: StageArgs.outs_git = [],
outs_git_no_delete: StageArgs.outs_git_no_delete = [],
outs_no_delete: StageArgs.outs_no_delete = [],
outs_no_store: StageArgs.outs_no_store = [],
outs_no_store_no_delete: StageArgs.outs_no_store_no_delete = [],
overwrite: StageArgs.overwrite = False,
no_check: StageArgs.no_check = False,
no_commit: StageArgs.no_commit = False,
):
"""Add a stage to the pipeline that compiles a LaTeX document."""
ck_outs = _to_ck_outs(
outputs=outputs,
outs_git=outs_git,
outs_git_no_delete=outs_git_no_delete,
outs_no_delete=outs_no_delete,
outs_no_store=outs_no_store,
outs_no_store_no_delete=outs_no_store_no_delete,
)
if not no_detect_inputs:
inputs = _add_detected_latex_inputs(target_path, inputs)
try:
stage = calkit.models.pipeline.LatexStage(
kind="latex",
environment=environment,
target_path=target_path,
output_dir=output_dir,
aux_dir=aux_dir,
latexmkrc_path=latexmkrc_path,
latexmk_args=latexmk_args,
inputs=inputs, # type: ignore
outputs=ck_outs,
)
except Exception as e:
raise_error(f"Invalid stage specification: {e}")
_save_stage(
stage=stage,
name=name,
overwrite=overwrite,
no_check=no_check,
no_commit=no_commit,
)
class NotebookStorage(str, Enum):
git = "git"
dvc = "dvc"
none = None
@new_app.command(name="jupyter-notebook-stage")
def new_jupyter_notebook_stage(
name: StageArgs.name,
environment: StageArgs.environment,
notebook_path: Annotated[
str, typer.Option("--notebook-path", help="Path to notebook.")
],
inputs: StageArgs.inputs = [],
outputs: StageArgs.outputs = [],
outs_git: StageArgs.outs_git = [],
outs_git_no_delete: StageArgs.outs_git_no_delete = [],
outs_no_delete: StageArgs.outs_no_delete = [],
outs_no_store: StageArgs.outs_no_store = [],
outs_no_store_no_delete: StageArgs.outs_no_store_no_delete = [],
html_storage: Annotated[
NotebookStorage,
typer.Option(
"--html-storage",
help=("In what system to store the HTML output of the notebook."),
),
] = NotebookStorage.dvc,
cleaned_ipynb_storage: Annotated[
NotebookStorage,
typer.Option(
"--cleaned-ipynb-storage",
help=(
"In what system to store the cleaned ipynb output of "
"the notebook."
),
),
] = NotebookStorage.git,
executed_ipynb_storage: Annotated[
NotebookStorage,
typer.Option(
"--executed-ipynb-storage",
help=(
"In what system to store the executed ipynb output of "
"the notebook."
),
),
] = NotebookStorage.dvc,
overwrite: StageArgs.overwrite = False,
no_check: StageArgs.no_check = False,
no_commit: StageArgs.no_commit = False,
):
"""Add a stage to the pipeline that runs a Jupyter notebook."""
ck_outs = _to_ck_outs(
outputs=outputs,
outs_git=outs_git,
outs_git_no_delete=outs_git_no_delete,
outs_no_delete=outs_no_delete,
outs_no_store=outs_no_store,
outs_no_store_no_delete=outs_no_store_no_delete,
)
try:
stage = calkit.models.pipeline.JupyterNotebookStage(
kind="jupyter-notebook",
environment=environment,
inputs=inputs, # type: ignore
outputs=ck_outs,
notebook_path=notebook_path,
html_storage=html_storage.value,
cleaned_ipynb_storage=cleaned_ipynb_storage.value,
executed_ipynb_storage=executed_ipynb_storage.value,
)
except Exception as e:
raise_error(f"Invalid stage specification: {e}")
_save_stage(
stage=stage,
name=name,
overwrite=overwrite,
no_check=no_check,
no_commit=no_commit,
)
@new_app.command(name="release")
def new_release(
name: Annotated[
str,
typer.Option(
"--name",
"-n",
help=(
"A name for the release, typically kebab-case or a semantic "
"version. "
"Will be used for the Git tag and GitHub release title."
),
),
],
release_kind: Annotated[
str | None,
typer.Option(
"--kind",
help=(
"What kind of release to create. "
"Will attempt to infer from path if not provided."
),
),
] = None,
path: Annotated[
str,
typer.Argument(help="The path to release; '.' for a project release."),
] = ".",
release_description: Annotated[
str | None,
typer.Option(
"--description",
"--desc",
help=(
"A description of the release. "
"Will be auto-generated if not provided."
),
),
] = None,
release_date: Annotated[
str | None,
typer.Option("--date", help="Release date. Will default to today."),
] = None,
no_docker_images: Annotated[
bool,
typer.Option(
"--no-docker-images",
help=(
"Do not archive the project's Docker images in the release."
),
),
] = False,
dry_run: Annotated[
bool,
typer.Option(
"--dry-run",
help="Only print actions that would be taken but don't take them.",
),
] = False,
no_commit: Annotated[
bool,
typer.Option(
"--no-commit",
help="Do not commit changes to Git repo.",
),
] = False,
no_push: Annotated[
bool,
typer.Option(
"--no-push",
help="Do not push to Git remote.",
),
] = False,
internal_release: Annotated[
bool,
typer.Option(
"--internal",
help=(
"Create an internal release that is not published to an "
"archival service. Still creates a Git tag and release "
"record in calkit.yaml, but does not upload files or create "
"a GitHub release."
),
),
] = False,
no_github_release: Annotated[
bool,
typer.Option(
"--no-github",
help="Do not create a GitHub release.",
),
] = False,
to: Annotated[
str,
typer.Option(
"--to",
help=(
"Archival service to use for external releases (zenodo or "
"caltechdata); ignored for --internal releases."
),
),
] = "zenodo",
draft_only: Annotated[
bool,
typer.Option(
"--draft",
help="Create draft record with reserved DOI but do not publish.",
),
] = False,
license_ids: Annotated[
list[str],
typer.Option(
"--license",
help=(
"License ID (from https://spdx.org/licenses). "
"Multiple can be specified. "
"Will try to infer from LICENSE file, if present."
),
),
] = [],
verbose: Annotated[
bool,
typer.Option(
"--verbose",
"-v",
help="Print verbose output.",
),
] = False,
):
"""Create a new release."""
import bibtexparser
import dotenv
import calkit.pipeline
to = to.lower()
dotenv.load_dotenv()
ck_info = calkit.load_calkit_info()
releases = ck_info.get("releases", {})
# TODO: Enable resuming a release if upload failed part-way?
if name in releases:
raise_error(f"Release with name '{name}' already exists")
repo = calkit.git.get_repo()
if name in repo.tags:
raise_error(f"Git tag with name '{name}' already exists")
# Detect the release kind from the path unless it was given with --kind. A
# "." path is always a project release; otherwise prefer a declared
# artifact in calkit.yaml, falling back to auto-detection from the path
# (e.g. a PDF under slides/ is a presentation).
if path == ".":
if release_kind is None:
release_kind = "project"
artifact = None
else:
detected_kind, artifact = calkit.releases.find_artifact(path, ck_info)
if detected_kind is None:
detected_kind = calkit.detect.detect_artifact_kind(
pathlib.Path(path).as_posix(),
)
if release_kind is None:
if detected_kind is None:
raise_error(
f"Could not determine release kind for path '{path}'; "
"define it in calkit.yaml as a dataset, figure, "
"publication, presentation, or software artifact, or "
"pass --kind"
)
release_kind = detected_kind
if release_kind not in [
"project",
"publication",
"figure",
"dataset",
"presentation",
"software",
]:
raise_error(f"Unknown release type '{release_kind}'")
# External (archival) releases require a valid service and access token;
# internal releases are stored locally and need neither.
if internal_release:
to = "none"
else:
if to not in calkit.releases.SERVICES:
raise_error(
f"Unknown archival service '{to}'; "
f"choose from: {list(calkit.releases.SERVICES.keys())}"
)
publisher_name = calkit.releases.SERVICES[to]["name"]
# Check that we have access to the service
typer.echo(f"Checking for {to} token")
try:
calkit.invenio.get_token(service=to) # type: ignore
except Exception as e:
raise_error(str(e))
# Check that the pipeline is up-to-date, limiting the check to the stage
# that produces the released artifact when releasing a single path.
typer.echo("Checking pipeline is up-to-date for release")
targets = None
if path != ".":
stage_name = calkit.pipeline.get_stage_for_output(path, ck_info)
if stage_name is not None:
targets = [stage_name]
status = calkit.pipeline.get_status(
ck_info=ck_info,
targets=targets,
check_environments=True,
clean_notebooks=True,
compile_to_dvc=True,
)
if status.errors:
raise_error("Pipeline checks failed: " + "; ".join(status.errors))
if status.failed_environment_checks:
raise_error(
"Pipeline environment checks failed for: "
+ ", ".join(status.failed_environment_checks)
)
if status.is_stale:
raise_error(
"Pipeline is not up-to-date; out-of-date stages: "
+ ", ".join(status.stale_stage_names)
)
release_dir = f".calkit/releases/{name}"
if release_date is None:
release_date = str(calkit.utcnow().date())
typer.echo(f"Using release date: {release_date}")
git_rev = repo.git.rev_parse(["--short", "HEAD"])
# Fields below are populated only for external (archival) releases;
# internal releases leave them empty.
doi = None
url = None
record_id = None
doi_md = None
stored_path_posix = None
if internal_release:
# Internal releases are frozen local snapshots: store a
# self-describing copy of the artifact in the repo. They are not
# uploaded to an archival service, so they need no authors, license,
# or DOI---just a stored file, a record, and a Git tag. Files are
# stored as-is (renamed); folders and whole-project releases are
# zipped.
project_name = calkit.detect_project_name(prepend_owner=False)
if path == "." or os.path.isdir(path):
stored_filename = f"{project_name}-{name}.zip"
is_zip = True
elif os.path.isfile(path):
_, ext = os.path.splitext(os.path.basename(path))
stored_filename = f"{project_name}-{name}{ext}"
is_zip = False
else:
raise_error(f"Release path '{path}' does not exist")
stored_path = os.path.join(release_dir, stored_filename)
stored_path_posix = pathlib.Path(stored_path).as_posix()
if dry_run:
action = "archive" if is_zip else "copy"
typer.echo(f"Would {action} {path} to {stored_path_posix}")
else:
os.makedirs(release_dir, exist_ok=True)
if is_zip:
paths = calkit.releases.ls_files() if path == "." else [path]
typer.echo(f"Archiving {path} to {stored_path_posix}")
calkit.releases.zip_paths(stored_path, paths)
else:
typer.echo(f"Copying {path} to {stored_path_posix}")
shutil.copy2(path, stored_path)
# Store the artifact with the same storage as its source (Git or
# DVC), falling back to ``calkit add``'s auto-detection.
from calkit.cli.main.core import add as add_to_repo
storage = calkit.releases.get_storage_for_path(
path, ck_info=ck_info
)
add_to_repo([stored_path], to=storage)
else:
release_files_dir = release_dir + "/files"
os.makedirs(release_files_dir, exist_ok=True)
# Ignore release files dir; external release artifacts are staged here for
# upload only and are not committed to the repo.
typer.echo(f"Ignoring {release_files_dir}")
gitignore_path = release_dir + "/.gitignore"
with open(gitignore_path, "w") as f:
f.write("/files\n")
if not dry_run:
repo.git.add(gitignore_path)
project_description = ck_info.get("description")
# Gather up the list of files to upload
if path == ".":
if release_kind is None:
release_kind = "project"
zip_path = release_files_dir + "/archive.zip"
all_paths = calkit.releases.ls_files()
typer.echo(f"Adding files to {zip_path}")
calkit.releases.zip_paths(zip_path, all_paths)
typer.echo("Checking extracted project release archive")
try:
calkit.releases.check_project_release_archive(
zip_path, verbose=verbose
)
except Exception as e:
raise_error(str(e))
title = ck_info.get("title")
if title is None:
warn("Project has no title")
title = typer.prompt("Enter a title for the project")
ck_info["title"] = title
if not dry_run:
with open("calkit.yaml", "w") as f:
calkit.ryaml.dump(ck_info, f)
else:
# TODO: Handle directories, e.g., datasets
if not os.path.isfile(path):
raise_error("Single artifact releases must be a single file")
typer.echo(f"Copying {path} into {release_files_dir}")
shutil.copy2(path, release_files_dir)
# Check that this artifact actually exists
artifact_key = (
release_kind + "s"
if release_kind != "software"
else release_kind
)
artifacts = ck_info.get(artifact_key, [])
title = None
artifact = None
for a in artifacts:
if a.get("path") == path:
artifact = a
title = artifact.get("title")
break
if artifact is None:
raise_error(
f"{release_kind} at {path} not defined in calkit.yaml"
)
if title is None:
raise_error(f"{release_kind} at {path} has no title")
# Save a metadata file with each DVC file's MD5 checksum
dvc_md5s = calkit.releases.make_dvc_md5s(
zipfile="archive.zip" if path == "." else None,
paths=None if path == "." else [path],
)
dvc_md5s_path = release_dir + "/dvc-md5s.yaml"
typer.echo(f"Saving DVC MD5 info to {dvc_md5s_path}")
with open(dvc_md5s_path, "w") as f:
calkit.ryaml.dump(dvc_md5s, f)
if not dry_run:
repo.git.add(dvc_md5s_path)
# Archive the project's Docker images, so reproducing it doesn't
# depend on a registry keeping them around, and leave breadcrumbs
# behind so the environment check can fetch them back
if path == "." and not no_docker_images:
typer.echo("Archiving Docker images")
docker_images = calkit.releases.save_docker_images(
release_files_dir
)
if docker_images:
docker_images_path = os.path.join(
release_dir, calkit.releases.DOCKER_IMAGES_FNAME
)
typer.echo(f"Saving Docker image info to {docker_images_path}")
with open(docker_images_path, "w") as f:
calkit.ryaml.dump(docker_images, f)
if not dry_run:
repo.git.add(docker_images_path)
# Create a README for the Zenodo release
readme_txt = f"# {title}\n"
git_rev = repo.git.rev_parse(["--short", "HEAD"])
readme_txt += (
f"\nThis is a {release_kind} release ({name}) generated with "
f"Calkit v{calkit.__version__} from Git rev {git_rev}.\n"
)
readme_path = release_files_dir + "/README.md"
with open(readme_path, "w") as f:
f.write(readme_txt)
# Check size of files dir
size = calkit.get_size(release_files_dir)
typer.echo(f"Release size: {(size / 1e6):.1f} MB")
if size >= 50e9:
raise_error(f"Release is too large (>50 GB) to upload to {to}")
# Upload to InvenioRDM instance
# Is there already a record for this release, which indicates we should
# create a new version?
try:
project_name = calkit.detect_project_name(prepend_owner=True)
except Exception:
raise_error("Could not detect project name")
invenio_description = ""
if project_description is not None:
invenio_description += project_description + "\n\n"
if release_description is not None:
invenio_description += release_description + "\n\n"
invenio_description += (
f"This is an archive of release '{name}' from the Calkit project "
f"'{project_name}'."
)
record_id = None
# SPDX IDs are case-insensitive, but the InvenioRDM license vocabulary
# used by Zenodo and CaltechDATA only accepts them lowercased
license_ids = [lid.strip().lower() for lid in license_ids]
# Detect project license IDs if necessary
if not license_ids:
license_file = calkit.licenses.find_license_file()
if license_file is None:
typer.echo("No project license found.")
use_default_license = typer.confirm(
"Would you like to use the default (MIT/CC-BY-4.0)?",
default=True,
)
if not use_default_license:
raise_error("Please generate a license file and try again")
copyright_holder = typer.prompt(
"Please enter the copyright holder for the license "
"(e.g., your full name)"
)
license_txt = calkit.licenses.LICENSE_TEMPLATE_DUAL.format(
year=calkit.utcnow().year,
copyright_holder=copyright_holder,
)
with open("LICENSE", "w") as f:
f.write(license_txt)
repo.git.add("LICENSE")
license_ids = ["mit", "cc-by-4.0"]
else:
typer.echo(f"Detecting license(s) from {license_file}")
with open(license_file) as f:
license_txt = f.read()
license_ids = calkit.licenses.detect_license_ids(license_txt)
if license_ids:
typer.echo(
f"Detected license(s): {', '.join(license_ids)}"
)
else:
warn(
f"Could not detect any known license in {license_file}; "
"specify one explicitly with --license"
)
if not license_ids:
raise_error("Project has no license(s) defined")
invenio_metadata = dict(
title=title,
description=invenio_description,
notes=f"Created from Calkit project {project_name} release {name}.",
publication_date=release_date,
version=name,
publisher=publisher_name,
rights=[{"id": lid} for lid in license_ids],
)
# Add related identifiers
github_url = calkit.detect_project_github_url()
related_identifiers = []
if github_url is not None:
related_identifiers.append(
{
"identifier": github_url,
"scheme": "url",
"relation_type": {"id": "issupplementto"},
"resource_type": {"id": "other"},
}
)
invenio_metadata["related_identifiers"] = related_identifiers
# TODO: Add calkit.io URL if applicable?
# Determine creators from authors. CITATION.cff is the single source of
# truth for authors, so read them from there; if none are defined yet,
# prompt for them and persist them to CITATION.cff.
authors = calkit.releases.read_authors_from_cff()
if authors:
typer.echo(f"Read {len(authors)} author(s) from CITATION.cff")
else:
warn("No authors defined for the project")
still_entering_authors = True
n = 0
while still_entering_authors:
n += 1
author = dict()
author["first_name"] = typer.prompt(
f"Enter the first name of author {n}"
)
author["last_name"] = typer.prompt(
f"Enter the last name of author {n}"
)
author["affiliation"] = typer.prompt(
f"Enter the affiliation of author {n}"
)
has_orcid = typer.confirm(
f"Does author {n} have an ORCID?", default=False
)
if has_orcid:
author["orcid"] = typer.prompt(
f"Enter the ORCID of author {n}"
)
authors.append(author)
still_entering_authors = typer.confirm(
"Are there more authors to enter?", default=True
)
# Write authors out to CITATION.cff
if not dry_run:
typer.echo("Writing authors to CITATION.cff")
calkit.releases.set_cff_authors(authors, ck_info=ck_info)
repo.git.add("CITATION.cff")
invenio_creators = []
for author in authors:
orcid = author.get("orcid")
creator: dict = {
"person_or_org": {
"type": "personal",
"given_name": author["first_name"],
"family_name": author["last_name"],
"identifiers": [{"identifier": orcid}] if orcid else [],
},
}
# Affiliation is optional (e.g., authors read from CITATION.cff may
# not specify one)
affiliation = author.get("affiliation")
if affiliation:
creator["affiliations"] = [{"name": affiliation}]
invenio_creators.append(creator)
invenio_metadata["creators"] = invenio_creators # type: ignore
# Set InvenioRDM resource_type based on release_type and artifact kind
if release_kind == "publication":
pubtype = artifact.get("kind") # type: ignore
if pubtype == "journal-article":
resource_type = "publication-article"
else:
resource_type = "publication-other"
elif release_kind == "presentation":
# Posters are presentations here, but InvenioRDM has a distinct
# resource type for them
if artifact.get("kind") == "poster": # type: ignore
resource_type = "poster"
else:
resource_type = "presentation"
elif release_kind == "figure":
resource_type = "image-figure"
elif release_kind in ["dataset", "software"]:
resource_type = release_kind
else:
# Default for "project" and other unknown types
resource_type = "other"
invenio_metadata["resource_type"] = {"id": resource_type} # type: ignore
doi = None
url = None
for existing_name, existing_release in releases.items():
if (
existing_release.get("kind") == release_kind
and existing_release.get("path") == path
and existing_release.get("publisher") == to
):
record_id = existing_release.get("record_id")
typer.echo(
f"Found existing {to} record ID {record_id} "
f"in release {existing_name} to create new version for"
)
break
if not dry_run:
typer.echo(f"Uploading to {to}")
if record_id is not None:
# Create a new version of the existing record
# TODO: This might fail if a new version is in progress, in which
# case we should discard that
invenio_dep = calkit.invenio.post(
f"/records/{record_id}/versions",
service=to, # type: ignore
)
typer.echo("Created new version record")
record_id = invenio_dep["id"]
typer.echo(f"Created new version with record ID: {record_id}")
# Now update that draft with the metadata
typer.echo("Updating draft metadata")
calkit.invenio.put(
f"/records/{record_id}/draft",
json=dict(metadata=invenio_metadata),
service=to, # type: ignore
)
else:
invenio_dep = calkit.invenio.post(
"/records",
json=dict(metadata=invenio_metadata),
service=to, # type: ignore
)
if verbose:
typer.echo(
f"Invenio records post response:\n{invenio_dep}"
)
record_id = invenio_dep["id"]
files = os.listdir(release_files_dir)
for filename in files:
typer.echo(f"Uploading {filename}")
fpath = os.path.join(release_files_dir, filename)
# First, initiate the file upload
calkit.invenio.post(
f"/records/{record_id}/draft/files",
json=[{"key": filename}],
service=to, # type: ignore
)
# Then upload the file content
with open(fpath, "rb") as f:
file_data = f.read()
resp = calkit.invenio.put(
f"/records/{record_id}/draft/files/{filename}/content",
headers={"Content-Type": "application/octet-stream"},
as_json=False,
service=to, # type: ignore
data=file_data,
)
typer.echo(f"Status code: {resp.status_code}")
# Commit the file
calkit.invenio.post(
f"/records/{record_id}/draft/files/{filename}/commit",
service=to, # type: ignore
)
# Reserve a DOI on the draft before publishing. The publish action's
# response only echoes back the DOI under "pids" if one was reserved
# on the draft first (otherwise "pids" comes back empty), so always
# reserve here to get a stable identifier.
typer.echo(f"Reserving DOI for {to} draft record ID {record_id}")
doi_resp = calkit.invenio.post(
f"/records/{record_id}/draft/pids/doi",
service=to, # type: ignore
)
if verbose:
typer.echo(f"DOI reservation response:\n{doi_resp}")
doi = calkit.invenio.extract_doi(doi_resp)
if doi is None:
raise_error(
f"Failed to reserve DOI for {to} draft record {record_id}"
)
if draft_only:
url = f"https://doi.org/{doi}"
typer.echo(f"Created {to} draft with reserved DOI: {doi}")
else:
# Publish the record
typer.echo(f"Publishing {to} record ID {record_id}")
invenio_dep = calkit.invenio.post(
f"/records/{record_id}/draft/actions/publish",
service=to, # type: ignore
)
if verbose:
typer.echo(f"Publish response:\n{invenio_dep}")
record_id = invenio_dep["id"]
# Prefer the DOI from the publish response, but fall back to the
# reserved DOI (and a fetch of the published record) since the
# publish action returns 202 and may not echo "pids" immediately.
# Polling failures are tolerated: we already have the reserved DOI,
# so a transient error while the record settles should not abort
# the release.
published_doi = calkit.invenio.extract_doi(invenio_dep)
for _ in range(10):
if published_doi is not None:
break
time.sleep(1)
try:
record = calkit.invenio.get(
f"/records/{record_id}",
service=to, # type: ignore
)
except Exception as e:
if verbose:
typer.echo(
f"Polling for published DOI failed: {e}"
)
continue
published_doi = calkit.invenio.extract_doi(record)
if published_doi is not None:
doi = published_doi
else:
typer.echo(
"Could not confirm minted DOI from published record; "
f"falling back to reserved DOI {doi}"
)
url = f"https://doi.org/{doi}"
typer.echo(f"Published to {to} with DOI: {doi}")
else:
typer.echo(f"Would have posted {to} record: {invenio_metadata}")
# If this is a project release, add badge to project README
doi_md = None
if release_kind == "project" and doi is not None:
typer.echo("Adding DOI badge to README.md")
doi_base_url = calkit.releases.SERVICES[to]["url"]
doi_md = (
f"[]"
f"(https://doi.org/{doi})"
)
if os.path.isfile("README.md"):
with open("README.md") as f:
readme_txt = f.read()
else:
readme_txt = ""
readme_txt = calkit.releases.add_doi_badge_to_readme(
readme_txt, badge=doi_md, title=title
)
if not dry_run:
with open("README.md", "w") as f:
f.write(readme_txt)
# Stage only after the file is closed; otherwise the buffered
# contents may not be flushed to disk yet and Git would stage an
# empty file
repo.git.add("README.md")
else:
typer.echo(f"Would have updated README.md to:\n{readme_txt}")
# Create Git tag
git_tag_message = release_description
if git_tag_message is None:
git_tag_message = f"Release {name}"
if not dry_run and not draft_only:
repo.git.tag(["-a", name, "-m", git_tag_message])
else:
typer.echo(
f"Would have created Git tag {name} with message: "
f"{git_tag_message}"
)
# Save release in Calkit info
from calkit.models.core import Release
release = Release(
kind=release_kind, # type: ignore
path=path,
git_rev=git_rev,
calkit_version=calkit.__version__,
date=release_date,
publisher=None if internal_release else to,
record_id=record_id,
doi=doi,
url=url,
description=release_description,
internal=internal_release,
stored_path=stored_path_posix,
).model_dump()
releases[name] = release
ck_info["releases"] = releases
# Create/update CITATION.cff file, preserving existing authors
if release_kind == "project" and not internal_release:
cff = calkit.releases.create_citation_cff(
ck_info=ck_info,
release_name=name,
release_date=release_date,
authors=authors,
)
if dry_run:
typer.echo(f"Would write CITATION.cff:\n{cff}")
else:
typer.echo("Writing CITATION.cff")
with open("CITATION.cff", "w") as f:
calkit.ryaml.dump(cff, f)
repo.git.add("CITATION.cff")
if not internal_release:
# Add to references so it can be cited
typer.echo("Adding BibTeX entry to references")
reference_collections = ck_info.get("references", [])
if len(reference_collections) > 1:
warn("Multiple references collections; writing to first")
if not reference_collections:
references = dict(path="references.bib")
ck_info["references"] = [references]
else:
references = reference_collections[0]
ref_path = references.get("path", "references.bib")
try:
# Read the existing references as raw text so we can append the new
# entry without reformatting the user's existing entries
if os.path.isfile(ref_path):
with open(ref_path) as f:
existing_text = f.read()
else:
existing_text = ""
bibtex_doi = doi
if bibtex_doi is None and dry_run:
mock_suffix = "".join(
ch if (ch.isalnum() or ch in ".-_") else "-"
for ch in name.lower()
)
bibtex_doi = f"10.0000/calkit-dry-run.{mock_suffix}"
invenio_bibtex = calkit.releases.create_bibtex(
authors=authors,
release_date=release_date,
title=title, # type: ignore
doi=bibtex_doi,
record_id=record_id, # type: ignore
service=to, # type: ignore
)
new_entries = bibtexparser.loads(invenio_bibtex).entries
if not new_entries:
raise ValueError("Failed to parse generated BibTeX entry")
new_entry = new_entries[0]
# Search through existing entries for any with the same DOI, and
# replace them (by citation key) if there is a match. Tolerate parse
# errors here (e.g., non-standard or temporarily-invalid BibTeX) by
# skipping replacement detection and still appending the new entry.
new_doi = new_entry.get("doi")
replace_ids = []
if new_doi:
try:
existing_entries = bibtexparser.loads(
existing_text
).entries
except Exception as e:
warn(f"Could not parse existing references to dedupe: {e}")
existing_entries = []
replace_ids = [
entry["ID"]
for entry in existing_entries
if entry.get("doi") == new_doi
]
if replace_ids:
typer.echo(
"Found matching DOI in existing references "
f"({len(replace_ids)} entr"
f"{'y' if len(replace_ids) == 1 else 'ies'}); "
"replacing"
)
new_text = calkit.releases.add_bibtex_entry(
existing_text, invenio_bibtex, replace_ids=replace_ids
)
if dry_run:
typer.echo(f"Would write updated references to {ref_path}")
else:
with open(ref_path, "w") as f:
f.write(new_text)
repo.git.add(ref_path)
except Exception as e:
warn(f"Failed to add to references: {e}")
# Write out Calkit metadata
if not dry_run:
typer.echo("Writing to calkit.yaml")
with open("calkit.yaml", "w") as f:
calkit.ryaml.dump(ck_info, f)
repo.git.add("calkit.yaml")
else:
typer.echo(f"Would have created release:\n{release}")
# Commit with Git
if not dry_run and calkit.git.get_staged_files() and not no_commit:
repo.git.commit(["-m", f"Create new {release_kind} release {name}"])
# Push with Git
if not dry_run and not no_push and not no_commit and not draft_only:
repo.git.push(["origin", repo.active_branch.name, "--tags"])
# Now create GitHub release (external releases only)
if not internal_release and not no_github_release:
typer.echo("Creating GitHub release")
release_body = ""
if doi_md is not None:
release_body += doi_md + "\n\n"
if release_description is not None:
release_body += release_description
resp = calkit.hub.post(
f"/projects/{project_name}/github-releases",
json=dict(
tag_name=name,
body=release_body,
),
)
typer.echo(f"Created GitHub release at: {resp['url']}")
# TODO: Upload assets for GitHub release if they're not too big?
typer.echo(f"New {release_kind} release {name} successfully created")
return release
"""CLI for creating new objects."""
from __future__ import annotations
import csv
import os
import pathlib
import shutil
import subprocess
import time
from enum import Enum
from typing import Literal
import typer
from typing_extensions import Annotated
import calkit
from calkit.cli import AliasGroup, OptionalValueCommand, raise_error, warn
from calkit.core import ryaml
new_app = typer.Typer(cls=AliasGroup, no_args_is_help=True)
class _NewProjectCommand(OptionalValueCommand):
optional_value_options = {"--hub": "default", "--cloud": "default"}
def _check_path_dir(path: str):
"""If path is in a subdirectory, check that it exists."""
dirname = os.path.dirname(path)
if dirname:
os.makedirs(dirname, exist_ok=True)
def _person_from_options(
option: str, email: str | None, orcid: str | None, with_ai: list[str]
) -> dict | None:
"""Build a ``created_by`` entry from CLI options.
Returns ``None`` when none were given. Validated through the model so
a mistyped ORCID or a ``--with-ai`` with nobody to answer for it is
refused here rather than by the next ``calkit.yaml`` validation.
"""
from pydantic import ValidationError
from calkit.models.core import _Person
if email is None and orcid is None and not with_ai:
return None
# ``with_ai`` is a list, one entry per --with-ai given. A single tool is
# written as a scalar, which is what the docs show and what reads best
# in calkit.yaml; several stay a list
with_ai_value: str | list[str] | None = None
if len(with_ai) == 1:
with_ai_value = with_ai[0]
elif with_ai:
with_ai_value = with_ai
try:
person = _Person(email=email, orcid=orcid, with_ai=with_ai_value)
except ValidationError as e:
raise_error(
f"Invalid --{option}: "
+ "; ".join(str(err["msg"]) for err in e.errors())
)
return person.model_dump(exclude_none=True)
def _split_template_subdir(
template: str, git_url: str
) -> tuple[str, str | None]:
"""Split a directory out of an 'owner/repo/path' template.
One repo can hold several self-contained example projects, so a
template may name a directory within it. Only the shorthand form is
split; a full URL is left alone, since its path is the repo's.
"""
if "://" in template or "github.com" in template:
return git_url, None
parts = template.strip("/").split("/")
if len(parts) <= 2:
return git_url, None
subdir = "/".join(parts[2:])
return git_url.rsplit("/" + subdir, 1)[0], subdir
def new_project(
path: Annotated[str, typer.Argument(help="Where to create the project.")],
name: Annotated[
str | None,
typer.Option(
"--name",
"-n",
help=(
"Project name. Will be inferred as kebab-cased directory "
"name if not provided."
),
),
] = None,
title: Annotated[
Xet Storage Details
- Size:
- 152 kB
- Xet hash:
- de658b0c7a9723effcc18c4fb6017ff0cc8d9abbda07d9a5cc38327f92c754b2
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.