Skip to content

Commit

Permalink
feat(asset-cli): asset diff subcommand to diff local manifest files
Browse files Browse the repository at this point in the history
Signed-off-by: Tang <[email protected]>
  • Loading branch information
stangch committed Jul 17, 2024
1 parent f203041 commit fa0aa49
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
30 changes: 12 additions & 18 deletions src/deadline/client/cli/_groups/asset_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,29 +237,27 @@ def asset_upload(root_dir: str, manifest_dir: str, update: bool, **args):
@cli_asset.command(name="diff")
@click.option("--root-dir", help="The root directory to compare changes to. ")
@click.option(
"--manifest",
"--manifest-dir",
required=True,
help="The path to manifest folder of the directory to show changes of. ",
)
@click.option(
"--format",
help="Pretty prints diff information with easy to read formatting. ",
"--raw",
help="Outputs the raw JSON info of files and their changed statuses. ",
is_flag=True,
show_default=True,
default=False,
)
@_handle_error
def asset_diff(root_dir: str, manifest: str, format: bool, **args):
def asset_diff(root_dir: str, manifest_dir: str, raw: bool, **args):
"""
Check file differences of a directory since last snapshot, specified by manifest.
"""
print("root_dir: ", root_dir)

if not os.path.isdir(manifest):
raise NonValidInputError(f"Specified manifest directory {manifest} does not exist. ")
if not os.path.isdir(manifest_dir):
raise NonValidInputError(f"Specified manifest directory {manifest_dir} does not exist. ")

if root_dir is None:
asset_root_dir = os.path.dirname(manifest)
asset_root_dir = os.path.dirname(manifest_dir)
else:
if not os.path.isdir(root_dir):
raise NonValidInputError(f"Specified root directory {root_dir} does not exist. ")
Expand All @@ -273,10 +271,6 @@ def asset_diff(root_dir: str, manifest: str, format: bool, **args):
# get inputs of directory
input_paths = []
for root, dirs, files in os.walk(asset_root_dir):
# ignore manifest folder
# if os.path.samefile(root, manifest):
# dirs[:] = []
# continue
for filename in files:
file_path = os.path.join(root, filename)
input_paths.append(Path(file_path))
Expand All @@ -289,18 +283,18 @@ def asset_diff(root_dir: str, manifest: str, format: bool, **args):
)

# parse local manifest
local_manifest_object = read_local_manifest(manifest=manifest)
local_manifest_object: BaseAssetManifest = read_local_manifest(manifest=manifest_dir)

# compare manifests
differences = compare_manifest(
differences: List[tuple] = compare_manifest(
reference_manifest=local_manifest_object, compare_manifest=directory_manifest_object
)

if format:
if raw:
click.echo(f"\nFile Diffs: {differences}")
else:
click.echo(f"\n{asset_root_dir}")
pretty_print(file_status_list=differences)
else:
click.echo(f"\nFile Diffs: {differences}")


@cli_asset.command(name="download")
Expand Down
28 changes: 22 additions & 6 deletions test/unit/deadline_client/cli/test_cli_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,8 @@ def test_asset_diff_with_format(
"diff",
"--root-dir",
MOCK_ROOT_DIR,
"--manifest",
"--manifest-dir",
MOCK_MANIFEST_DIR,
"--format",
],
)

Expand Down Expand Up @@ -553,7 +552,15 @@ def test_asset_diff_with_multiple_subdirectories(
runner = CliRunner()
result = runner.invoke(
main,
["asset", "diff", "--root-dir", MOCK_ROOT_DIR, "--manifest", MOCK_MANIFEST_DIR],
[
"asset",
"diff",
"--root-dir",
MOCK_ROOT_DIR,
"--manifest-dir",
MOCK_MANIFEST_DIR,
"--raw",
],
)

expected_output = "File Diffs: [(<FileStatus.MODIFIED: 2>, BaseManifestPath(path='file1.txt', hash='mock_hash_1', size=0, mtime=0)), (<FileStatus.MODIFIED: 2>, BaseManifestPath(path='subdir1/file2.txt', hash='mock_hash_2', size=0, mtime=0)), (<FileStatus.MODIFIED: 2>, BaseManifestPath(path='subdir2/subdir3/file3.txt', hash='mock_hash_3', size=0, mtime=0))]"
Expand Down Expand Up @@ -582,7 +589,15 @@ def test_asset_diff_without_format(
runner = CliRunner()
result = runner.invoke(
main,
["asset", "diff", "--root-dir", MOCK_ROOT_DIR, "--manifest", MOCK_MANIFEST_DIR],
[
"asset",
"diff",
"--root-dir",
MOCK_ROOT_DIR,
"--manifest-dir",
MOCK_MANIFEST_DIR,
"--raw",
],
)

expected_result = "File Diffs: [(<FileStatus.MODIFIED: 2>, BaseManifestPath(path='file1.txt', hash='mock_hash_1', size=0, mtime=0)), (<FileStatus.MODIFIED: 2>, BaseManifestPath(path='subdir1/file2.txt', hash='mock_hash_2', size=0, mtime=0))]\n"
Expand All @@ -596,7 +611,8 @@ def test_asset_diff_invalid_root_dir(self, tmp_path):

runner = CliRunner()
result = runner.invoke(
main, ["asset", "diff", "--root-dir", invalid_root_dir, "--manifest", str(manifest_dir)]
main,
["asset", "diff", "--root-dir", invalid_root_dir, "--manifest-dir", str(manifest_dir)],
)

assert result.exit_code == 1
Expand All @@ -608,7 +624,7 @@ def test_asset_diff_invalid_manifest_dir(self, tmp_path):

runner = CliRunner()
result = runner.invoke(
main, ["asset", "diff", "--root-dir", root_dir, "--manifest", invalid_manifest_dir]
main, ["asset", "diff", "--root-dir", root_dir, "--manifest-dir", invalid_manifest_dir]
)

assert result.exit_code == 1
Expand Down

0 comments on commit fa0aa49

Please sign in to comment.