Skip to content
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

Alphabetize the lockfile #524

Merged
merged 8 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
57 changes: 57 additions & 0 deletions tests/test_conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
extract_input_hash,
main,
make_lock_spec,
render_lockfile_for_platform,
run_lock,
)
from conda_lock.conda_solver import extract_json_object, fake_conda_environment
Expand Down Expand Up @@ -980,6 +981,62 @@ def test_parse_poetry_invalid_optionals(pyproject_optional_toml: Path):
)


def test_explicit_toposorted() -> None:
"""Verify that explicit lockfiles are topologically sorted.

We write unified lockfiles in alphabetical order. This is okay because we store
the dependency information in the lockfile, so we have the necessary information
to perform topological sorting. However, explicit lockfiles do not store dependency
information, and thus need to be written in topological order.

Verifying topological ordering is very easy: we just need to make sure that each
package is written after all of its dependencies.
"""
lockfile = parse_conda_lock_file(TEST_DIR / "test-toposort" / "conda-lock.yml")

# These are the individual lines as they appear in an explicit lockfile file
lines = render_lockfile_for_platform(
lockfile=lockfile,
kind="explicit",
platform="linux-64",
include_dev_dependencies=False,
extras=[],
)

# Packages are listed by URL, but we want to check by name.
url_to_name = {package.url: package.name for package in lockfile.package}
# For each package name we need the names of its dependencies
name_to_deps = {
package.name: set(package.dependencies.keys()) for package in lockfile.package
}

# We do a simulated installation run, and keep track of the packages
# that have been installed so far in installed_names
installed_names = set()

# Simulate installing each package in the order it appears in the lockfile.
# Verify that each package is installed after all of its dependencies.
for n, line in enumerate(lines):
if not line or line.startswith("#") or line.startswith("@EXPLICIT"):
continue
# Line should have the format url#hash
url = line.split("#")[0]
name = url_to_name[url]
deps = name_to_deps[name]

# Verify that all dependencies have been simulated-installed
for dep in deps:
if dep.startswith("__"):
# This is a virtual package, so we don't need to check it
continue
assert (
dep in installed_names
), f"{n=}, {line=}, {name=}, {dep=}, {installed_names=}"

# Simulate installing the package
installed_names.add(name)


def test_run_lock(
monkeypatch: "pytest.MonkeyPatch", zlib_environment: Path, conda_exe: str
):
Expand Down
Loading