-
Notifications
You must be signed in to change notification settings - Fork 1.3k
dvc: introduce dvc params diff
#3573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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: | ||
efiop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.