forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes iterative#3528
- Loading branch information
Showing
15 changed files
with
534 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import argparse | ||
import logging | ||
|
||
from dvc.command.base import append_doc_link | ||
from dvc.command.base import CmdBase | ||
from dvc.command.base import fix_subparsers | ||
from dvc.exceptions import DvcException | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _show_diff(diff): | ||
from dvc.utils.diff import table | ||
|
||
rows = [] | ||
for fname, mdiff in diff.items(): | ||
for param, change in mdiff.items(): | ||
rows.append([fname, param, change["old"], change["new"]]) | ||
|
||
return table(["Path", "Param", "Old", "New"], rows) | ||
|
||
|
||
class CmdParamsDiff(CmdBase): | ||
def run(self): | ||
try: | ||
diff = self.repo.params.diff( | ||
a_rev=self.args.a_rev, b_rev=self.args.b_rev, | ||
) | ||
|
||
if self.args.show_json: | ||
import json | ||
|
||
logger.info(json.dumps(diff)) | ||
else: | ||
table = _show_diff(diff) | ||
if table: | ||
logger.info(table) | ||
|
||
except DvcException: | ||
logger.exception("failed to show params diff") | ||
return 1 | ||
|
||
return 0 | ||
|
||
|
||
def add_parser(subparsers, parent_parser): | ||
PARAMS_HELP = "Commands to display params." | ||
|
||
params_parser = subparsers.add_parser( | ||
"params", | ||
parents=[parent_parser], | ||
description=append_doc_link(PARAMS_HELP, "params"), | ||
help=PARAMS_HELP, | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
|
||
params_subparsers = params_parser.add_subparsers( | ||
dest="cmd", | ||
help="Use `dvc params CMD --help` to display command-specific help.", | ||
) | ||
|
||
fix_subparsers(params_subparsers) | ||
|
||
PARAMS_DIFF_HELP = ( | ||
"Show changes in params between commits in the DVC repository, or " | ||
"between a commit and the workspace." | ||
) | ||
params_diff_parser = params_subparsers.add_parser( | ||
"diff", | ||
parents=[parent_parser], | ||
description=append_doc_link(PARAMS_DIFF_HELP, "params/diff"), | ||
help=PARAMS_DIFF_HELP, | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
params_diff_parser.add_argument( | ||
"a_rev", nargs="?", help="Old Git commit to compare (defaults to HEAD)" | ||
) | ||
params_diff_parser.add_argument( | ||
"b_rev", | ||
nargs="?", | ||
help=("New Git commit to compare (defaults to the current workspace)"), | ||
) | ||
params_diff_parser.add_argument( | ||
"--show-json", | ||
action="store_true", | ||
default=False, | ||
help="Show output in JSON format.", | ||
) | ||
params_diff_parser.set_defaults(func=CmdParamsDiff) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Params(object): | ||
def __init__(self, repo): | ||
self.repo = repo | ||
|
||
def show(self, *args, **kwargs): | ||
from .show import show | ||
|
||
return show(self.repo, *args, **kwargs) | ||
|
||
def diff(self, *args, **kwargs): | ||
from .diff import diff | ||
|
||
return diff(self.repo, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import dvc.utils.diff | ||
from .show import NoParamsError | ||
|
||
|
||
def _get_params(repo, *args, rev=None, **kwargs): | ||
try: | ||
params = repo.params.show(*args, **kwargs, revs=[rev] if rev else None) | ||
return params.get(rev or "", {}) | ||
except NoParamsError: | ||
return {} | ||
|
||
|
||
def _format(params): | ||
ret = {} | ||
for key, val in params.items(): | ||
if isinstance(val, dict): | ||
new_val = _format(val) | ||
elif isinstance(val, list): | ||
new_val = str(val) | ||
else: | ||
new_val = val | ||
ret[key] = new_val | ||
return ret | ||
|
||
|
||
def diff(repo, *args, a_rev=None, b_rev=None, **kwargs): | ||
old = _get_params(repo, *args, **kwargs, rev=(a_rev or "HEAD")) | ||
new = _get_params(repo, *args, **kwargs, rev=b_rev) | ||
|
||
return dvc.utils.diff.diff(_format(old), _format(new)) |
Oops, something went wrong.