dvc: replace keys in dictionary with macros in dvc/repo/diff.py

This commit is contained in:
Zhanibek
2019-04-05 15:02:28 +06:00
parent 44e2ff7f83
commit 5f68b8e5a0
5 changed files with 183 additions and 132 deletions
+52 -41
View File
@@ -8,6 +8,7 @@ import dvc.logger as logger
from dvc.exceptions import DvcException
from dvc.command.base import CmdBase
from dvc.utils.collections import compact
import dvc.scm.base as git
class CmdDiff(CmdBase):
@@ -24,55 +25,64 @@ class CmdDiff(CmdBase):
def _show(self, diff_dct):
engine = inflect.engine()
msg = "dvc diff from {} to {}".format(
diff_dct["a_ref"], diff_dct["b_ref"]
diff_dct[git.DIFF_A_REF], diff_dct[git.DIFF_B_REF]
)
if diff_dct.get("equal"):
if diff_dct.get(git.DIFF_EQUAL):
logger.info(msg)
return
for dct in diff_dct["diffs"]:
msg += "\n\ndiff for '{}' \n".format(dct["target"])
if dct.get("old_file"):
for dct in diff_dct[git.DIFF_LIST]:
msg += "\n\ndiff for '{}' \n".format(dct[git.DIFF_TARGET])
if dct.get(git.DIFF_OLD_FILE):
msg += "-{} with md5 {}\n".format(
dct["old_file"], dct["old_checksum"]
dct[git.DIFF_OLD_FILE], dct[git.DIFF_OLD_CHECKSUM]
)
if dct.get("new_file"):
if dct.get(git.DIFF_NEW_FILE):
msg += "+{} with md5 {}\n".format(
dct["new_file"], dct["new_checksum"]
dct[git.DIFF_NEW_FILE], dct[git.DIFF_NEW_CHECKSUM]
)
msg += "\n"
if dct.get("is_dir"):
msg += "{} {} not changed, ".format(
dct["ident"], engine.plural("file", dct["ident"])
)
msg += "{} {} modified, ".format(
dct["changes"], engine.plural("file", dct["changes"])
)
msg += "{} {} added, ".format(
dct["new"], engine.plural("file", dct["new"])
)
msg += "{} {} deleted, ".format(
dct["del"], engine.plural("file", dct["del"])
)
msg += "size was {}".format(self._print_size(dct["size_diff"]))
else:
if (
dct.get("old_file")
and dct.get("new_file")
and dct["size_diff"] == 0
):
msg += "file size was not changed"
elif dct.get("new_file"):
msg += "added file with size {}".format(
humanize.naturalsize(dct["size_diff"])
if dct[git.DIFF_SIZE] != git.DIFF_SIZE_UNKNOWN:
if dct.get("is_dir"):
msg += "{} {} not changed, ".format(
dct[git.DIFF_IDENT],
engine.plural("file", dct[git.DIFF_IDENT]),
)
elif dct.get("old_file"):
msg += "deleted file with size {}".format(
humanize.naturalsize(abs(dct["size_diff"]))
msg += "{} {} modified, ".format(
dct[git.DIFF_CHANGE],
engine.plural("file", dct[git.DIFF_CHANGE]),
)
msg += "{} {} added, ".format(
dct[git.DIFF_NEW],
engine.plural("file", dct[git.DIFF_NEW]),
)
msg += "{} {} deleted, ".format(
dct[git.DIFF_DEL],
engine.plural("file", dct[git.DIFF_DEL]),
)
msg += "size was {}".format(
self._print_size(dct[git.DIFF_SIZE])
)
else:
msg += "file was modified, file size {}".format(
self._print_size(dct["size_diff"])
)
if (
dct.get(git.DIFF_OLD_FILE)
and dct.get(git.DIFF_NEW_FILE)
and dct[git.DIFF_SIZE] == 0
):
msg += "file size was not changed"
elif dct.get(git.DIFF_NEW_FILE):
msg += "added file with size {}".format(
humanize.naturalsize(dct[git.DIFF_SIZE])
)
elif dct.get(git.DIFF_OLD_FILE):
msg += "deleted file with size {}".format(
humanize.naturalsize(abs(dct[git.DIFF_SIZE]))
)
else:
msg += "file was modified, file size {}".format(
self._print_size(dct[git.DIFF_SIZE])
)
else:
msg += "size is ?"
logger.info(msg)
def run(self):
@@ -82,12 +92,13 @@ class CmdDiff(CmdBase):
)
self._show(msg)
except DvcException:
msg = "failed to get 'diff "
msg += " ".join(
msg = "failed to get 'diff {}'"
args = " ".join(
compact([self.args.target, self.args.a_ref, self.args.b_ref])
)
msg += "'"
msg = msg.format(args)
logger.error(msg)
return 1
return 0
+60 -49
View File
@@ -4,6 +4,7 @@ import os
from dvc.scm.base import FileNotInCommitError
import dvc.scm.base as git
def _extract_dir(self, output):
@@ -20,33 +21,39 @@ def _extract_dir(self, output):
def _get_tree_changes(self, a_entries, b_entries):
result = {"del": 0, "ident": 0, "changes": 0, "new": 0, "moves": 0}
result = {
git.DIFF_DEL: 0,
git.DIFF_IDENT: 0,
git.DIFF_CHANGE: 0,
git.DIFF_NEW: 0,
git.DIFF_MOVE: 0,
}
keys = set(a_entries.keys())
keys.update(b_entries.keys())
diff_size = 0
for key in keys:
if key in a_entries and key in b_entries:
if a_entries[key] == b_entries[key]:
result["ident"] += 1
result[git.DIFF_IDENT] += 1
else:
result["changes"] += 1
result[git.DIFF_CHANGE] += 1
diff_size += os.path.getsize(
self.cache.local.get(b_entries[key])
) - os.path.getsize(self.cache.local.get(a_entries[key]))
elif key in a_entries:
result["del"] += 1
result[git.DIFF_DEL] += 1
diff_size -= os.path.getsize(self.cache.local.get(a_entries[key]))
else:
result["new"] += 1
result[git.DIFF_NEW] += 1
diff_size += os.path.getsize(self.cache.local.get(b_entries[key]))
result["size_diff"] = diff_size
result[git.DIFF_SIZE] = diff_size
return result
def _get_diff_outs(self, diff_dct):
self.tree = diff_dct["a_tree"]
self.tree = diff_dct[git.DIFF_A_TREE]
a_outs = {str(out): out for st in self.stages() for out in st.outs}
self.tree = diff_dct["b_tree"]
self.tree = diff_dct[git.DIFF_B_TREE]
b_outs = {str(out): out for st in self.stages() for out in st.outs}
outs_paths = set(a_outs.keys())
outs_paths.update(b_outs.keys())
@@ -56,67 +63,71 @@ def _get_diff_outs(self, diff_dct):
results[path] = {
"a_output": a_outs[path],
"b_output": b_outs[path],
"new_file": False,
git.DIFF_NEW_FILE: False,
"deleted_file": False,
# possible drawback: regular file ->directory movement
"is_dir": a_outs[path].is_dir_cache,
git.DIFF_IS_DIR: a_outs[path].is_dir_cache,
}
elif path in a_outs:
results[path] = {
"a_output": a_outs[path],
"new_file": False,
git.DIFF_NEW_FILE: False,
"deleted_file": True,
"is_dir": a_outs[path].is_dir_cache,
git.DIFF_IS_DIR: a_outs[path].is_dir_cache,
}
else:
results[path] = {
"b_output": b_outs[path],
"new_file": True,
git.DIFF_NEW_FILE: True,
"deleted_file": False,
"is_dir": b_outs[path].is_dir_cache,
git.DIFF_IS_DIR: b_outs[path].is_dir_cache,
}
return results
def _diff_dir(self, target, diff_dct):
result = {"target": target}
result["is_dir"] = True
result = {git.DIFF_TARGET: target}
result[git.DIFF_IS_DIR] = True
a_entries, b_entries = {}, {}
if not diff_dct["new_file"]:
result["old_file"] = target
result["old_checksum"] = diff_dct["a_output"].checksum
a_entries = _extract_dir(self, diff_dct["a_output"])
if not diff_dct["deleted_file"]:
result["new_file"] = target
result["new_checksum"] = diff_dct["b_output"].checksum
b_entries = _extract_dir(self, diff_dct["b_output"])
result.update(_get_tree_changes(self, a_entries, b_entries))
try:
if not diff_dct[git.DIFF_NEW_FILE]:
result[git.DIFF_OLD_FILE] = target
result[git.DIFF_OLD_CHECKSUM] = diff_dct["a_output"].checksum
a_entries = _extract_dir(self, diff_dct["a_output"])
if not diff_dct["deleted_file"]:
result[git.DIFF_NEW_FILE] = target
result[git.DIFF_NEW_CHECKSUM] = diff_dct["b_output"].checksum
b_entries = _extract_dir(self, diff_dct["b_output"])
result.update(_get_tree_changes(self, a_entries, b_entries))
except FileNotFoundError:
result[git.DIFF_SIZE] = git.DIFF_SIZE_UNKNOWN
return result
def _diff_file(self, target, diff_dct):
result = {"target": target}
result = {git.DIFF_TARGET: target}
size = 0
if not diff_dct["new_file"]:
result["old_file"] = target
result["old_checksum"] = diff_dct["a_output"].checksum
size -= os.path.getsize(
self.cache.local.get(diff_dct["a_output"].checksum)
)
print("first", size)
if not diff_dct["deleted_file"]:
result["new_file"] = target
result["new_checksum"] = diff_dct["b_output"].checksum
size += os.path.getsize(
self.cache.local.get(diff_dct["b_output"].checksum)
)
print("second", size)
result["size_diff"] = size
try:
if not diff_dct[git.DIFF_NEW_FILE]:
result[git.DIFF_OLD_FILE] = target
result[git.DIFF_OLD_CHECKSUM] = diff_dct["a_output"].checksum
size -= os.path.getsize(
self.cache.local.get(diff_dct["a_output"].checksum)
)
if not diff_dct["deleted_file"]:
result[git.DIFF_NEW_FILE] = target
result[git.DIFF_NEW_CHECKSUM] = diff_dct["b_output"].checksum
size += os.path.getsize(
self.cache.local.get(diff_dct["b_output"].checksum)
)
except FileNotFoundError:
size = git.DIFF_SIZE_UNKNOWN
result[git.DIFF_SIZE] = size
return result
def _diff_royal(self, target, diff_dct):
if diff_dct["is_dir"]:
if diff_dct[git.DIFF_IS_DIR]:
return _diff_dir(self, target, diff_dct)
return _diff_file(self, target, diff_dct)
@@ -134,19 +145,19 @@ def diff(self, target, a_ref=None, b_ref=None):
"""
result = {}
diff_dct = self.scm.get_diff_trees(a_ref, b_ref=b_ref)
result["a_ref"] = diff_dct["a_ref"]
result["b_ref"] = diff_dct["b_ref"]
if diff_dct["equal"]:
result["equal"] = True
result[git.DIFF_A_REF] = diff_dct[git.DIFF_A_REF]
result[git.DIFF_B_REF] = diff_dct[git.DIFF_B_REF]
if diff_dct[git.DIFF_EQUAL]:
result[git.DIFF_EQUAL] = True
return result
result["diffs"] = []
result[git.DIFF_LIST] = []
diff_outs = _get_diff_outs(self, diff_dct)
if target is None:
result["diffs"] = [
result[git.DIFF_LIST] = [
_diff_royal(self, path, diff_outs[path]) for path in diff_outs
]
elif target in diff_outs:
result["diffs"] = [_diff_royal(self, target, diff_outs[target])]
result[git.DIFF_LIST] = [_diff_royal(self, target, diff_outs[target])]
else:
msg = "Have not found file/directory '{}' in the commits"
raise FileNotInCommitError(msg.format(target))
+22 -1
View File
@@ -7,6 +7,27 @@ import os
from dvc.exceptions import DvcException
DIFF_TARGET = "target"
DIFF_IS_DIR = "is_dir"
DIFF_OLD_FILE = "old_file"
DIFF_OLD_CHECKSUM = "old_checksum"
DIFF_NEW_FILE = "new_file"
DIFF_NEW_CHECKSUM = "new_checksum"
DIFF_SIZE = "size_diff"
DIFF_DEL = "del"
DIFF_IDENT = "ident"
DIFF_CHANGE = "changes"
DIFF_NEW = "new"
DIFF_MOVE = "moves"
DIFF_A_TREE = "a_tree"
DIFF_B_TREE = "b_tree"
DIFF_A_REF = "a_ref"
DIFF_B_REF = "b_ref"
DIFF_EQUAL = "equal"
DIFF_LIST = "diffs"
DIFF_SIZE_UNKNOWN = "?"
class SCMError(DvcException):
"""Base class for source control management errors."""
@@ -19,7 +40,7 @@ class FileNotInRepoError(DvcException):
class FileNotInCommitError(DvcException):
"""Thrown when trying to find a file/directory that is not
into the specified commit in the repository
in the specified commit in the repository.
"""
+14 -12
View File
@@ -11,6 +11,11 @@ from dvc.scm.base import (
SCMError,
FileNotInRepoError,
FileNotInTargetSubdirError,
DIFF_A_TREE,
DIFF_B_TREE,
DIFF_A_REF,
DIFF_B_REF,
DIFF_EQUAL,
)
from dvc.scm.git.tree import GitTree
import dvc.logger as logger
@@ -240,7 +245,7 @@ class Git(Base):
from gitdb.exc import BadObject, BadName
trees = {"a_tree": None, "b_tree": None}
trees = {DIFF_A_TREE: None, DIFF_B_TREE: None}
commits = []
try:
if b_ref is not None:
@@ -248,16 +253,13 @@ class Git(Base):
b_commit = self.git.commit(b_ref)
commits.append(str(a_commit))
commits.append(str(b_commit))
# other way of getting the diff between commits :
# self.git.diff(a_commit, b_commit) returns the diff string
# by reading the files in a, b commits and computing the diff
else:
a_commit = self.git.head.commit
b_commit = self.git.commit(a_ref)
commits.append(str(a_commit))
commits.append(str(b_commit))
trees["a_tree"] = self.get_tree(commits[0])
trees["b_tree"] = self.get_tree(commits[1])
trees[DIFF_A_TREE] = self.get_tree(commits[0])
trees[DIFF_B_TREE] = self.get_tree(commits[1])
except (BadName, BadObject) as e:
raise SCMError("git problem", cause=e)
return trees, commits
@@ -273,13 +275,13 @@ class Git(Base):
Returns:
dict - dictionary with keys: (a_tree, b_tree, a_ref, b_ref, equal)
"""
diff_dct = {"equal": False}
diff_dct = {DIFF_EQUAL: False}
trees, commit_refs = self._get_diff_trees(a_ref, b_ref)
diff_dct["a_ref"] = commit_refs[0]
diff_dct["b_ref"] = commit_refs[1]
diff_dct[DIFF_A_REF] = commit_refs[0]
diff_dct[DIFF_B_REF] = commit_refs[1]
if commit_refs[0] == commit_refs[1]:
diff_dct["equal"] = True
diff_dct[DIFF_EQUAL] = True
return diff_dct
diff_dct["a_tree"] = trees["a_tree"]
diff_dct["b_tree"] = trees["b_tree"]
diff_dct[DIFF_A_TREE] = trees[DIFF_A_TREE]
diff_dct[DIFF_B_TREE] = trees[DIFF_B_TREE]
return diff_dct
+35 -29
View File
@@ -7,6 +7,7 @@ from dvc.main import main
from dvc.scm.base import FileNotInCommitError
import dvc.scm.base as git
from tests.basic_env import TestDvc
@@ -16,7 +17,7 @@ class TestDiff(TestDvc):
def setUp(self):
super(TestDiff, self).setUp()
self.new_file = "new_file"
self.new_file = "new_test_file"
self.create(self.new_file, self.new_file)
self.dvc.add(self.new_file)
self.a_ref = self.git.head.commit
@@ -25,23 +26,23 @@ class TestDiff(TestDvc):
def test(self):
out = self.dvc.scm.get_diff_trees(self.a_ref)
self.assertFalse(out["equal"])
self.assertEqual(str(self.a_ref), out["b_ref"])
self.assertEqual(str(self.git.head.commit), out["a_ref"])
self.assertFalse(out[git.DIFF_EQUAL])
self.assertEqual(str(self.a_ref), out[git.DIFF_B_REF])
self.assertEqual(str(self.git.head.commit), out[git.DIFF_A_REF])
class TestDiffRepo(TestDiff):
def test(self):
result = self.dvc.diff(self.new_file, a_ref=self.a_ref)
test_dct = {
"b_ref": str(self.a_ref),
"a_ref": str(self.git.head.commit),
"diffs": [
git.DIFF_B_REF: str(self.a_ref),
git.DIFF_A_REF: str(self.git.head.commit),
git.DIFF_LIST: [
{
"target": self.new_file,
"old_file": self.new_file,
"old_checksum": "25fa8325e4e0eb8180445e42558e60bd",
"size_diff": -8,
git.DIFF_TARGET: self.new_file,
git.DIFF_OLD_FILE: self.new_file,
git.DIFF_OLD_CHECKSUM: "c81fec3d710806070bcd67f182d1279c",
git.DIFF_SIZE: -13,
}
],
}
@@ -66,7 +67,7 @@ class TestDiffDir(TestDvc):
self.git.index.add([self.DATA_DIR + ".dvc"])
self.git.index.commit("adds data_dir")
self.a_ref = str(self.dvc.scm.git.head.commit)
self.new_file = os.path.join(self.DATA_SUB_DIR, "new_file")
self.new_file = os.path.join(self.DATA_SUB_DIR, git.DIFF_NEW_FILE)
self.create(self.new_file, self.new_file)
self.dvc.add(self.DATA_DIR)
self.git.index.add([self.DATA_DIR + ".dvc"])
@@ -74,30 +75,35 @@ class TestDiffDir(TestDvc):
def test(self):
out = self.dvc.scm.get_diff_trees(self.a_ref)
self.assertFalse(out["equal"])
self.assertEqual(str(self.a_ref), out["b_ref"])
self.assertEqual(str(self.git.head.commit), out["a_ref"])
self.assertFalse(out[git.DIFF_EQUAL])
self.assertEqual(str(self.a_ref), out[git.DIFF_B_REF])
self.assertEqual(str(self.git.head.commit), out[git.DIFF_A_REF])
class TestDiffDirRepo(TestDiffDir):
def test(self):
result = self.dvc.diff(self.DATA_DIR, a_ref=self.a_ref)
test_dct = {
"b_ref": str(self.a_ref),
"a_ref": str(self.git.head.commit),
"diffs": [
git.DIFF_B_REF: str(self.a_ref),
git.DIFF_A_REF: str(self.git.head.commit),
git.DIFF_LIST: [
{
"changes": 0,
"del": 1,
"ident": 2,
"moves": 0,
"new": 0,
"target": self.DATA_DIR,
"old_file": self.DATA_DIR,
"old_checksum": "d5782f3072167ad3a53ee80b92b30718.dir",
"new_file": self.DATA_DIR,
"new_checksum": "bff5a787d16460f32e9f2e62b183b1cc.dir",
"size_diff": -30,
git.DIFF_CHANGE: 0,
git.DIFF_DEL: 1,
git.DIFF_IDENT: 2,
git.DIFF_MOVE: 0,
git.DIFF_NEW: 0,
git.DIFF_IS_DIR: True,
git.DIFF_TARGET: self.DATA_DIR,
git.DIFF_OLD_FILE: self.DATA_DIR,
git.DIFF_OLD_CHECKSUM: (
"d5782f3072167ad3a53ee80b92b30718.dir"
),
git.DIFF_NEW_FILE: self.DATA_DIR,
git.DIFF_NEW_CHECKSUM: (
"bff5a787d16460f32e9f2e62b183b1cc.dir"
),
git.DIFF_SIZE: -30,
}
],
}