def run(self): try: diff = self.repo.params.diff( a_rev=self.args.a_rev, b_rev=self.args.b_rev, targets=self.args.targets, all=self.args.all, deps=self.args.deps, ) except DvcException: logger.exception("failed to show params diff") return 1 if self.args.json: import json ui.write(json.dumps(diff)) else: from dvc.compare import show_diff show_diff( diff, title="Param", markdown=self.args.markdown, no_path=self.args.no_path, show_changes=False, ) return 0
def run(self): from dvc.exceptions import DvcException try: diff = self.repo.diff(self.args.a_rev, self.args.b_rev, self.args.targets) show_hash = self.args.show_hash hide_missing = self.args.b_rev or self.args.hide_missing if hide_missing: del diff["not in cache"] for key, entries in diff.items(): entries = sorted( entries, key=lambda entry: entry["path"]["old"] if isinstance(entry["path"], dict) else entry["path"], ) if not show_hash: for entry in entries: del entry["hash"] diff[key] = entries if self.args.show_json: ui.write(json.dumps(diff)) elif self.args.show_md: _show_md(diff, show_hash, hide_missing) elif diff: self._show_diff(diff, hide_missing) except DvcException: logger.exception("failed to get diff") return 1 return 0
def run(self): try: diff = self.repo.metrics.diff( a_rev=self.args.a_rev, b_rev=self.args.b_rev, targets=self.args.targets, recursive=self.args.recursive, all=self.args.all, ) except DvcException: logger.exception("failed to show metrics diff") return 1 if self.args.json: import json ui.write(json.dumps(diff)) else: from dvc.compare import show_diff show_diff( diff, title="Metric", markdown=self.args.markdown, no_path=self.args.no_path, precision=self.args.precision or DEFAULT_PRECISION, round_digits=True, ) return 0
def render(self, **kwargs: Any): from dvc.ui import ui if kwargs.pop("csv", False): ui.write(self.to_csv(), end="") else: ui.table(self, headers=self.keys(), **kwargs)
def run(self): from dvc.compare import show_metrics if self.args.checkpoint_resume: if self.args.reset: raise InvalidArgumentError( "--reset and --rev are mutually exclusive.") if not (self.args.queue or self.args.tmp_dir): raise InvalidArgumentError( "--rev can only be used in conjunction with " "--queue or --temp.") if self.args.reset: ui.write("Any existing checkpoints will be reset and re-run.") results = self.repo.experiments.run( name=self.args.name, queue=self.args.queue, run_all=self.args.run_all, jobs=self.args.jobs, params=self.args.set_param, checkpoint_resume=self.args.checkpoint_resume, reset=self.args.reset, tmp_dir=self.args.tmp_dir, machine=self.args.machine, **self._repro_kwargs, ) if self.args.metrics and results: metrics = self.repo.metrics.show(revs=list(results)) metrics.pop("workspace", None) show_metrics(metrics) return 0
def run(self): try: metrics = self.repo.metrics.show( self.args.targets, all_branches=self.args.all_branches, all_tags=self.args.all_tags, all_commits=self.args.all_commits, recursive=self.args.recursive, ) except DvcException: logger.exception("") return 1 if self.args.json: import json ui.write(json.dumps(metrics, default=encode_exception)) else: from dvc.compare import show_metrics show_metrics( metrics, markdown=self.args.markdown, all_branches=self.args.all_branches, all_tags=self.args.all_tags, all_commits=self.args.all_commits, precision=self.args.precision or DEFAULT_PRECISION, round_digits=True, ) return 0
def run(self): from dvc.config import ConfigError if self.args.name is None and not self.args.unset: conf = self.config.read(self.args.level) try: ui.write(conf["core"]["remote"]) except KeyError: ui.write("No default remote set") return 1 else: with self.config.edit(self.args.level) as conf: if self.args.unset: conf["core"].pop("remote", None) else: merged_conf = self.config.load_config_to_level( self.args.level ) if ( self.args.name in conf["remote"] or self.args.name in merged_conf["remote"] ): conf["core"]["remote"] = self.args.name else: raise ConfigError( "default remote must be present in remote list." ) return 0
def run(self): from dvc.utils.humanize import get_summary stats, exc = None, None try: stats = self.repo.checkout( targets=self.args.targets, with_deps=self.args.with_deps, force=self.args.force, relink=self.args.relink, recursive=self.args.recursive, ) except CheckoutError as _exc: exc = _exc stats = exc.stats if self.args.summary: default_message = "No changes." msg = get_summary(sorted(stats.items(), key=operator.itemgetter(0))) ui.write(msg or default_message) else: log_changes(stats) if exc: raise exc return 0
def run(self): try: all_experiments = self.repo.experiments.show( all_branches=self.args.all_branches, all_tags=self.args.all_tags, all_commits=self.args.all_commits, sha_only=self.args.sha, num=self.args.num, param_deps=self.args.param_deps, ) except DvcException: logger.exception("failed to show experiments") return 1 if self.args.show_json: import json ui.write(json.dumps(all_experiments, default=_format_json)) else: show_experiments( all_experiments, include_metrics=self.args.include_metrics, exclude_metrics=self.args.exclude_metrics, include_params=self.args.include_params, exclude_params=self.args.exclude_params, no_timestamp=self.args.no_timestamp, sort_by=self.args.sort_by, sort_order=self.args.sort_order, precision=self.args.precision or DEFAULT_PRECISION, pager=not self.args.no_pager, ) return 0
def run(self): from dvc.command.stage import parse_cmd cmd = parse_cmd(self.args.command) if not self.args.interactive and not cmd: raise InvalidArgumentError("command is not specified") from dvc.repo.experiments.init import init defaults = {} if not self.args.explicit: config = self.repo.config["exp"] defaults.update({**self.DEFAULTS, **config}) cli_args = compact({ "cmd": cmd, "code": self.args.code, "data": self.args.data, "models": self.args.models, "metrics": self.args.metrics, "params": self.args.params, "plots": self.args.plots, "live": self.args.live, }) initialized_stage = init( self.repo, name=self.args.name, type=self.args.type, defaults=defaults, overrides=cli_args, interactive=self.args.interactive, force=self.args.force, ) text = ui.rich_text.assemble( "\n" if self.args.interactive else "", "Created ", (self.args.name, "bright_blue"), " stage in ", ("dvc.yaml", "green"), ".", ) if not self.args.run: text.append_text( ui.rich_text.assemble( " To run, use ", ('"dvc exp run"', "green"), ".\nSee ", (self.EXP_LINK, "repr.url"), ".", )) ui.write(text, styled=True) if self.args.run: return self.repo.experiments.run( targets=[initialized_stage.addressing]) return 0
def run(self): from dvc.cli.parser import get_main_parser parser = get_main_parser() shell = self.args.shell script = shtab.complete(parser, shell=shell, preamble=PREAMBLE) ui.write(script, force=True) return 0
def run(self): import shtab shell = self.args.shell parser = self.args.parser script = shtab.complete(parser, shell=shell, preamble=PREAMBLE) ui.write(script, force=True) return 0
def run(self): from pathlib import Path if self.args.show_vega: if not self.args.targets: logger.error("please specify a target for `--show-vega`") return 1 if len(self.args.targets) > 1: logger.error( "you can only specify one target for `--show-vega`" ) return 1 try: plots_data = self._func( targets=self.args.targets, props=self._props() ) if not plots_data: ui.error_write( "No plots were loaded, " "visualization file will not be created." ) if self.args.show_vega: target = self.args.targets[0] plot_json = find_vega(self.repo, plots_data, target) if plot_json: ui.write(plot_json) return 0 rel: str = self.args.out or "dvc_plots" path: Path = (Path.cwd() / rel).resolve() index_path = render( self.repo, plots_data, path=path, html_template_path=self.args.html_template, ) assert index_path.is_absolute() url = index_path.as_uri() ui.write(url) if self.args.open: import webbrowser opened = webbrowser.open(index_path) if not opened: ui.error_write( "Failed to open. Please try opening it manually." ) return 1 return 0 except DvcException: logger.exception("") return 1
def run(self): from dvc.info import get_dvc_info from dvc.updater import notify_updates dvc_info = get_dvc_info() ui.write(dvc_info, force=True) notify_updates() return 0
def _run(self, target, revs=None): metrics, plots = self.repo.live.show(target=target, revs=revs) html_path = self.args.target + ".html" self.repo.plots.write_html(html_path, plots, metrics) ui.write("\nfile://", os.path.abspath(html_path), sep="") return 0
def test_capsys_works(capsys: CaptureFixture[str]): """Sanity check that capsys can capture outputs from a global ui.""" from dvc.ui import ui message = "hello world" ui.write(message) ui.error_write(message) captured = capsys.readouterr() assert captured.out == f"{message}\n" assert captured.err == f"{message}\n"
def _run(self, target, revs=None): metrics, plots = self.repo.live.show(target=target, revs=revs) if plots: html_path = Path.cwd() / (self.args.target + "_html") from dvc.render.utils import render index_path = render(self.repo, plots, metrics, html_path) ui.write(index_path.as_uri()) return 0 return 1
def run(self): if self.repo.machine is None: raise MachineDisabledError all_status = self.repo.machine.status(self.args.name) for i, status in enumerate(all_status, start=1): ui.write(f"instance_num_{i}:") for field in self.SHOWN_FIELD: value = status.get(field, None) ui.write(f"\t{field:20}: {value}") return 0
def _show_origin(self): levels = [self.args.level] if self.args.level else self.config.LEVELS for level in levels: conf = self.config.read(level)["machine"] if self.args.name: conf = conf.get(self.args.name, {}) self._hide_private(conf) prefix = self._config_file_prefix(True, self.config, level) configs = list(self._format_config(conf, prefix)) if configs: ui.write("\n".join(configs))
def _check_before_rename(self): from dvc.machine import validate_name validate_name(self.args.new) all_config = self.config.load_config_to_level(None) if self.args.new in all_config.get("machine", {}): raise ConfigError( "Rename failed. Machine '{}' already exists.".format( self.args.new)) ui.write(f"Rename machine '{self.args.name}' to '{self.args.new}'.")
def _run(self, target, revs=None): from dvc.render.utils import match_renderers, render metrics, plots = self.repo.live.show(target=target, revs=revs) if plots: html_path = Path.cwd() / (self.args.target + "_html") renderers = match_renderers(plots, self.repo.plots.templates) index_path = render(self.repo, renderers, metrics, html_path) ui.write(index_path.as_uri()) return 0 return 1
def _show_results(self, result): if not result.match and not self.args.non_matching: return if self.args.details: patterns = result.patterns if not self.args.all: patterns = patterns[-1:] for pattern in patterns: ui.write(pattern, result.file, sep="\t") else: ui.write(result.file)
def run(self): from pathlib import Path if self.args.show_vega: if not self.args.targets: logger.error("please specify a target for `--show-vega`") return 1 if len(self.args.targets) > 1: logger.error( "you can only specify one target for `--show-vega`" ) return 1 try: plots = self._func(targets=self.args.targets, props=self._props()) if self.args.show_vega: target = self.args.targets[0] ui.write(plots[target]) return 0 except DvcException: logger.exception("") return 1 if plots: rel: str = self.args.out or "plots.html" path: Path = (Path.cwd() / rel).resolve() self.repo.plots.write_html( path, plots=plots, html_template_path=self.args.html_template ) assert ( path.is_absolute() ) # as_uri throws ValueError if not absolute url = path.as_uri() ui.write(url) if self.args.open: import webbrowser opened = webbrowser.open(rel) if not opened: ui.error_write( "Failed to open. Please try opening it manually." ) return 1 else: ui.error_write( "No plots were loaded, visualization file will not be created." ) return 0
def _show_url(self): from dvc.api import get_url from dvc.ui import ui try: url = get_url(self.args.path, repo=self.args.url, rev=self.args.rev) ui.write(url, force=True) except DvcException: logger.exception("failed to show URL") return 1 return 0
def run(self): G = _build( self.repo, target=self.args.target, full=self.args.full, outs=self.args.outs, ) if self.args.dot: ui.write(_show_dot(G)) else: with ui.pager(): ui.write(_show_ascii(G)) return 0
def run(self): _raise_error_if_all_disabled( all_branches=self.args.all_branches, all_tags=self.args.all_tags, all_commits=self.args.all_commits, workspace=self.args.workspace, ) msg = "This will remove all experiments except those derived from " msg += "the workspace" if self.args.all_commits: msg += " and all git commits" elif self.args.all_branches and self.args.all_tags: msg += " and all git branches and tags" elif self.args.all_branches: msg += " and all git branches" elif self.args.all_tags: msg += " and all git tags" msg += " of the current repo." if self.args.queued: msg += " Run queued experiments will be preserved." else: msg += " Run queued experiments will be removed." logger.warning(msg) msg = "Are you sure you want to proceed?" if not self.args.force and not prompt.confirm(msg): return 1 removed = self.repo.experiments.gc( all_branches=self.args.all_branches, all_tags=self.args.all_tags, all_commits=self.args.all_commits, workspace=self.args.workspace, queued=self.args.queued, ) if removed: ui.write( f"Removed {removed} experiments.", "To remove unused cache files", "use 'dvc gc'.", ) else: ui.write("No experiments to remove.") return 0
def _list(self): if any((self.args.name, self.args.value, self.args.unset)): logger.error("-l/--list can't be used together with any of these " "options: -u/--unset, name, value") return 1 levels = [self.args.level] if self.args.level else self.config.LEVELS for level in levels: conf = self.config.read(level) prefix = self._config_file_prefix(self.args.show_origin, self.config, level) configs = list(self._format_config(conf, prefix)) if configs: ui.write("\n".join(configs)) return 0
def run(self): from dvc_render.vega_templates import dump_templates try: out = (os.path.join(os.getcwd(), self.args.out) if self.args.out else self.repo.plots.templates_dir) targets = [self.args.target] if self.args.target else None dump_templates(output=out, targets=targets) templates_path = os.path.relpath(out, os.getcwd()) ui.write(f"Templates have been written into '{templates_path}'.") return 0 except DvcException: logger.exception("") return 1
def log_changes(stats): colors = [ ("modified", colorama.Fore.YELLOW), ("added", colorama.Fore.GREEN), ("deleted", colorama.Fore.RED), ] for state, color in colors: entries = stats.get(state) if not entries: continue nc = colorama.Fore.RESET for entry in entries: ui.write(f"{color}{state[0].upper()}{nc}", entry, sep="\t")
def run(self): G = _build( self.repo, target=self.args.target, full=self.args.full, outs=self.args.outs, ) if self.args.dot: ui.write(_show_dot(G)) else: from dvc.utils.pager import pager pager(_show_ascii(G)) return 0