Skip to content

Commit

Permalink
Merge pull request #524 from mariusvniekerk/alphabetize-lock
Browse files Browse the repository at this point in the history
Alphabetize unified lockfiles
  • Loading branch information
maresb authored Nov 20, 2023
2 parents 81af99e + bb6c7d1 commit 6d1273c
Show file tree
Hide file tree
Showing 6 changed files with 1,956 additions and 4 deletions.
1 change: 0 additions & 1 deletion conda_lock/conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
is_micromamba,
)
from conda_lock.lockfile import (
UnknownLockfileVersion,
parse_conda_lock_file,
write_conda_lock_file,
)
Expand Down
8 changes: 6 additions & 2 deletions conda_lock/lockfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ def parse_conda_lock_file(path: pathlib.Path) -> Lockfile:
content = yaml.safe_load(f)
version = content.pop("version", None)
if version == 1:
return lockfile_v1_to_v2(LockfileV1.parse_obj(content))
lockfile = lockfile_v1_to_v2(LockfileV1.parse_obj(content))
elif version == 2:
lockfile = Lockfile.parse_obj(content)
elif version is None:
raise MissingLockfileVersion(f"{path} is missing a version")
else:
raise UnknownLockfileVersion(f"{path} has unknown version {version}")
lockfile.toposort_inplace()
return lockfile


def write_conda_lock_file(
Expand All @@ -155,7 +159,7 @@ def write_conda_lock_file(
metadata_choices: Optional[Collection[MetadataOption]],
include_help_text: bool = True,
) -> None:
content.toposort_inplace()
content.alphasort_inplace()
with path.open("w") as f:
if include_help_text:
categories = set(p.category for p in content.package)
Expand Down
3 changes: 3 additions & 0 deletions conda_lock/lockfile/v2prelim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def __ror__(self, other: "Optional[Lockfile]") -> "Lockfile":
def toposort_inplace(self) -> None:
self.package = self._toposort(self.package)

def alphasort_inplace(self) -> None:
self.package.sort(key=lambda d: d.key())

@staticmethod
def _toposort(
package: List[LockedDependency], update: bool = False
Expand Down
Loading

0 comments on commit 6d1273c

Please sign in to comment.