From 037e2e54f0accb10eccc15d3abddf3832c700267 Mon Sep 17 00:00:00 2001 From: skshetry <18718008+skshetry@users.noreply.github.com> Date: Tue, 7 Feb 2023 10:05:36 +0545 Subject: [PATCH] serialization: reorder files section metadata (#8950 serialization: reorder files section --- dvc/output.py | 16 +++++++++++++++- dvc/stage/serialize.py | 5 +++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/dvc/output.py b/dvc/output.py index 31471a92b..0d062717b 100644 --- a/dvc/output.py +++ b/dvc/output.py @@ -1,6 +1,8 @@ import logging import os +import posixpath from collections import defaultdict +from operator import itemgetter from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, Type, cast from urllib.parse import urlparse @@ -229,6 +231,17 @@ def merge_file_meta_from_cloud(entry: Dict) -> Dict: return entry +def _serialize_tree_obj_to_files(obj: "Tree") -> List[Dict[str, Any]]: + key = obj.PARAM_RELPATH + return sorted( + ( + {key: posixpath.sep.join(parts), **hi.to_dict(), **meta.to_dict()} + for parts, meta, hi in obj + ), + key=itemgetter(key), + ) + + class OutputDoesNotExistError(DvcException): def __init__(self, path): msg = f"output '{path}' does not exist" @@ -823,7 +836,8 @@ class Output: if obj: obj = cast("Tree", obj) ret[self.PARAM_FILES] = [ - split_file_meta_from_cloud(f) for f in obj.as_list(with_meta=True) + split_file_meta_from_cloud(f) + for f in _serialize_tree_obj_to_files(obj) ] return ret diff --git a/dvc/stage/serialize.py b/dvc/stage/serialize.py index f80b6cd42..23c665603 100644 --- a/dvc/stage/serialize.py +++ b/dvc/stage/serialize.py @@ -154,7 +154,7 @@ def to_pipeline_file(stage: "PipelineStage"): def to_single_stage_lockfile(stage: "Stage", **kwargs) -> dict: - from dvc.output import split_file_meta_from_cloud + from dvc.output import _serialize_tree_obj_to_files, split_file_meta_from_cloud assert stage.cmd @@ -165,7 +165,8 @@ def to_single_stage_lockfile(stage: "Stage", **kwargs) -> dict: if obj: obj = cast("Tree", obj) ret[item.PARAM_FILES] = [ - split_file_meta_from_cloud(f) for f in obj.as_list(with_meta=True) + split_file_meta_from_cloud(f) + for f in _serialize_tree_obj_to_files(obj) ] else: meta_d = item.meta.to_dict()