| 1 |
import click |
| 2 |
|
| 3 |
from jupyter_cache.cli import arguments, options, pass_cache |
| 4 |
from jupyter_cache.cli.commands.cmd_main import jcache |
| 5 |
from jupyter_cache.utils import tabulate_cache_records |
| 6 |
|
| 7 |
|
| 8 |
@jcache.group("cache") |
| 9 |
@options.CACHE_PATH |
| 10 |
@pass_cache |
| 11 |
def cmnd_cache(cache, cache_path): |
| 12 |
"""Work with cached execution(s) in a project.""" |
| 13 |
cache.set_cache_path(cache_path) |
| 14 |
|
| 15 |
|
| 16 |
@cmnd_cache.command("list") |
| 17 |
@click.option( |
| 18 |
"-l", |
| 19 |
"--latest-only", |
| 20 |
is_flag=True, |
| 21 |
help="Show only the most recent record per origin URI.", |
| 22 |
) |
| 23 |
@click.option("-h", "--hashkeys", is_flag=True, help="Show the hashkey of notebook.") |
| 24 |
@options.PATH_LENGTH |
| 25 |
@pass_cache |
| 26 |
def list_caches(cache, latest_only, hashkeys, path_length): |
| 27 |
"""List cached notebook records.""" |
| 28 |
db = cache.get_cache() |
| 29 |
records = db.list_cache_records() |
| 30 |
if not records: |
| 31 |
click.secho("No Cached Notebooks", fg="blue") |
| 32 |
# TODO optionally list number of artifacts |
| 33 |
if latest_only: |
| 34 |
latest_records = {} |
| 35 |
for record in records: |
| 36 |
if record.uri not in latest_records: |
| 37 |
latest_records[record.uri] = record |
| 38 |
continue |
| 39 |
if latest_records[record.uri].created < record.created: |
| 40 |
latest_records[record.uri] = record |
| 41 |
records = list(latest_records.values()) |
| 42 |
click.echo( |
| 43 |
tabulate_cache_records(records, hashkeys=hashkeys, path_length=path_length) |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
@cmnd_cache.command("info") |
| 48 |
@arguments.PK |
| 49 |
@pass_cache |
| 50 |
def cached_info(cache, pk): |
| 51 |
"""Show details of a cached notebook.""" |
| 52 |
import yaml |
| 53 |
|
| 54 |
db = cache.get_cache() |
| 55 |
try: |
| 56 |
record = db.get_cache_record(pk) |
| 57 |
except KeyError: |
| 58 |
click.secho(f"ID {pk} does not exist, Aborting!", fg="red") |
| 59 |
raise click.Abort() |
| 60 |
data = record.format_dict(hashkey=True, path_length=None) |
| 61 |
click.echo(yaml.safe_dump(data, sort_keys=False), nl=False) |
| 62 |
with db.cache_artefacts_temppath(pk) as folder: |
| 63 |
paths = [str(p.relative_to(folder)) for p in folder.glob("**/*") if p.is_file()] |
| 64 |
if not paths: |
| 65 |
click.echo("") |
| 66 |
return |
| 67 |
if paths: |
| 68 |
click.echo("Artifacts:") |
| 69 |
for path in paths: |
| 70 |
click.echo(f"- {path}") |
| 71 |
|
| 72 |
|
| 73 |
@cmnd_cache.command("cat-artefact") |
| 74 |
@arguments.PK |
| 75 |
@arguments.ARTIFACT_RPATH |
| 76 |
@pass_cache |
| 77 |
def cat_artifact(cache, pk, artifact_rpath): |
| 78 |
"""Print the contents of a cached artefact.""" |
| 79 |
db = cache.get_cache() |
| 80 |
with db.cache_artefacts_temppath(pk) as path: |
| 81 |
artifact_path = path.joinpath(artifact_rpath) |
| 82 |
if not artifact_path.exists(): |
| 83 |
click.secho("Artifact does not exist", fg="red") |
| 84 |
raise click.Abort() |
| 85 |
if not artifact_path.is_file(): |
| 86 |
click.secho("Artifact is not a file", fg="red") |
| 87 |
raise click.Abort() |
| 88 |
text = artifact_path.read_text(encoding="utf8") |
| 89 |
click.echo(text) |
| 90 |
|
| 91 |
|
| 92 |
def cache_file(db, nbpath, validate, overwrite, artifact_paths=()): |
| 93 |
from jupyter_cache.base import NbValidityError |
| 94 |
|
| 95 |
click.echo(f"Caching: {nbpath}") |
| 96 |
try: |
| 97 |
db.cache_notebook_file( |
| 98 |
nbpath, |
| 99 |
artifacts=artifact_paths, |
| 100 |
check_validity=validate, |
| 101 |
overwrite=overwrite, |
| 102 |
) |
| 103 |
except NbValidityError as error: |
| 104 |
click.secho("Validity Error: ", fg="red", nl=False) |
| 105 |
click.echo(str(error)) |
| 106 |
if click.confirm("The notebook may not have been executed, continue caching?"): |
| 107 |
try: |
| 108 |
db.cache_notebook_file( |
| 109 |
nbpath, |
| 110 |
artifacts=artifact_paths, |
| 111 |
check_validity=False, |
| 112 |
overwrite=overwrite, |
| 113 |
) |
| 114 |
except OSError as error: |
| 115 |
click.secho("Artifact Error: ", fg="red", nl=False) |
| 116 |
click.echo(str(error)) |
| 117 |
return False |
| 118 |
except OSError as error: |
| 119 |
click.secho("Artifact Error: ", fg="red", nl=False) |
| 120 |
click.echo(str(error)) |
| 121 |
return False |
| 122 |
return True |
| 123 |
|
| 124 |
|
| 125 |
@cmnd_cache.command("add-with-artefacts") |
| 126 |
@arguments.ARTIFACT_PATHS |
| 127 |
@options.NB_PATH |
| 128 |
@options.VALIDATE_NB |
| 129 |
@options.OVERWRITE_CACHED |
| 130 |
@pass_cache |
| 131 |
def cache_nb(cache, artifact_paths, nbpath, validate, overwrite): |
| 132 |
"""Cache a notebook, with possible artefact files.""" |
| 133 |
db = cache.get_cache() |
| 134 |
success = cache_file(db, nbpath, validate, overwrite, artifact_paths) |
| 135 |
if success: |
| 136 |
click.secho("Success!", fg="green") |
| 137 |
|
| 138 |
|
| 139 |
@cmnd_cache.command("add") |
| 140 |
@arguments.NB_PATHS |
| 141 |
@options.VALIDATE_NB |
| 142 |
@options.OVERWRITE_CACHED |
| 143 |
@pass_cache |
| 144 |
def cache_nbs(cache, nbpaths, validate, overwrite): |
| 145 |
"""Cache notebook(s) that have already been executed.""" |
| 146 |
db = cache.get_cache() |
| 147 |
success = True |
| 148 |
for nbpath in nbpaths: |
| 149 |
# TODO deal with errors (print all at end? or option to ignore) |
| 150 |
if not cache_file(db, nbpath, validate, overwrite): |
| 151 |
success = False |
| 152 |
if success: |
| 153 |
click.secho("Success!", fg="green") |
| 154 |
|
| 155 |
|
| 156 |
@cmnd_cache.command("clear") |
| 157 |
@options.FORCE |
| 158 |
@pass_cache |
| 159 |
def clear_cache_cmd(cache, force): |
| 160 |
"""Remove all executed notebooks from the cache.""" |
| 161 |
db = cache.get_cache() |
| 162 |
if not force: |
| 163 |
click.confirm( |
| 164 |
"Are you sure you want to permanently clear the cache!?", abort=True |
| 165 |
) |
| 166 |
for record in db.list_cache_records(): |
| 167 |
db.remove_cache(record.pk) |
| 168 |
click.secho("Cache cleared!", fg="green") |
| 169 |
|
| 170 |
|
| 171 |
@cmnd_cache.command("remove") |
| 172 |
@arguments.PKS |
| 173 |
@options.REMOVE_ALL |
| 174 |
@pass_cache |
| 175 |
def remove_caches(cache, pks, remove_all): |
| 176 |
"""Remove notebooks stored in the cache.""" |
| 177 |
from jupyter_cache.base import CachingError |
| 178 |
|
| 179 |
db = cache.get_cache() |
| 180 |
if remove_all: |
| 181 |
pks = [r.pk for r in db.list_cache_records()] |
| 182 |
for pk in pks: |
| 183 |
# TODO deal with errors (print all at end? or option to ignore) |
| 184 |
click.echo(f"Removing Cache ID = {pk}") |
| 185 |
try: |
| 186 |
db.remove_cache(pk) |
| 187 |
except KeyError: |
| 188 |
click.secho("Does not exist", fg="red") |
| 189 |
except CachingError as err: |
| 190 |
click.secho("Error: ", fg="red") |
| 191 |
click.echo(str(err)) |
| 192 |
click.secho("Success!", fg="green") |
| 193 |
|
| 194 |
|
| 195 |
@cmnd_cache.command("diff") |
| 196 |
@arguments.PK |
| 197 |
@arguments.NB_PATH |
| 198 |
@pass_cache |
| 199 |
def diff_nb(cache, pk, nbpath): |
| 200 |
"""Print a diff of a notebook to one stored in the cache.""" |
| 201 |
db = cache.get_cache() |
| 202 |
click.echo(db.diff_nbfile_with_cache(pk, nbpath, as_str=True)) |
| 203 |
click.secho("Success!", fg="green") |