Skip to content

Commit

Permalink
Merge pull request #680 from valory-xyz/fix/sync
Browse files Browse the repository at this point in the history
Download dependencies and update hashes when running the sync
  • Loading branch information
angrybayblade authored Oct 6, 2023
2 parents a69aaf3 + 4c6b0e7 commit 0c4ec6e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
6 changes: 4 additions & 2 deletions aea/package_manager/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def _sync(
sync_needed = True
self._logger.info(f"{package_id} not found locally, downloading...")
package_id_with_hash = package_id.with_hash(packages[package_id])
self.add_package(package_id=package_id_with_hash)
self.add_package(package_id=package_id_with_hash, with_dependencies=True)

return sync_needed, hash_updates, package_updates

Expand Down Expand Up @@ -315,6 +315,7 @@ def add_package(

if not actual_package_id:
# no package on fs, download one
self._logger.info(f"Adding {package_id.without_hash()}")
self._fetch_package(package_id)
elif not is_update_needed:
# actual version already, nothing to do
Expand All @@ -324,6 +325,7 @@ def add_package(
f"Required package and package in the registry does not match: {package_id} vs {actual_package_id}"
)
else:
self._logger.info(f"Updating {package_id.without_hash()}")
self._update_package(package_id)

if with_dependencies:
Expand Down Expand Up @@ -357,12 +359,12 @@ def _fetch_package(self, package_id: PackageId) -> None:
(package_type_collection / "__init__.py").touch()

download_path = package_type_collection / package_id.name

fetch_ipfs(
str(package_id.package_type),
package_id.public_id,
dest=str(download_path),
)
self._logger.debug(f"Downloaded {package_id.without_hash()}")

def add_dependencies_for_package(
self, package_id: PackageId, allow_update: bool = False
Expand Down
8 changes: 6 additions & 2 deletions aea/package_manager/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ def _update_hashes_from_sources(self, sources: List[str]) -> None:
self.dump(file=self._packages_file)

def register(
self, package_path: Path, package_type: Optional[PackageType] = None
self,
package_path: Path,
package_type: Optional[PackageType] = None,
) -> "PackageManagerV1":
"""Add package to the index."""
package_type = package_type or PackageType(package_path.parent.name[:-1])
Expand Down Expand Up @@ -241,8 +243,8 @@ def sync(
update_hashes=update_hashes,
update_packages=update_packages,
)
sync_needed = sync_needed or _sync_needed

sync_needed = sync_needed or _sync_needed
if update_hashes and hash_updates_third_party:
third_party_package_id = "\n\t- ".join(
map(str, hash_updates_third_party)
Expand Down Expand Up @@ -281,6 +283,8 @@ def sync(

if sync_needed:
self._logger.info("Sync complete")
self._logger.info("Updating hashes")
self.dump()
else:
self._logger.info("No package was updated.")

Expand Down
10 changes: 7 additions & 3 deletions tests/test_cli/test_package_manager/test_sync.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2022 Valory AG
# Copyright 2022-2023 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,7 +66,9 @@ def test_sync_with_missing_dev_packages(self, ipfs_mock, hash_mock, caplog) -> N

with mock.patch.object(
PackageManagerV1, "dev_packages", new=packages
), caplog.at_level(logging.INFO):
), caplog.at_level(logging.INFO), mock.patch.object(
PackageManagerV1, "add_package"
):
result = self.run_cli_command("packages", "sync", "--dev")
assert result.exit_code == 0
assert (
Expand All @@ -87,7 +89,9 @@ def test_sync_with_missing_third_party_packages(

with mock.patch.object(
PackageManagerV1, "third_party_packages", new=packages
), caplog.at_level(logging.INFO):
), caplog.at_level(logging.INFO), mock.patch.object(
PackageManagerV1, "add_package"
):
result = self.run_cli_command("packages", "sync")
assert result.exit_code == 0
assert (
Expand Down
2 changes: 2 additions & 0 deletions tests/test_package_manager/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""Test package manager base."""


import logging
import re
from collections import OrderedDict
from pathlib import Path
Expand Down Expand Up @@ -93,6 +94,7 @@ def __init__(
self.path = path
self.packages = packages
self.config_loader = config_loader
self._logger = logging.getLogger()

@classmethod
def from_dir(
Expand Down
7 changes: 6 additions & 1 deletion tests/test_package_manager/test_v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ def test_sync(

with mock.patch.object(pm, "add_package") as update_patch:
pm.sync()
update_patch.assert_called_with(package_id=DUMMY_PACKAGE_ID)
update_patch.assert_called_with(
package_id=DUMMY_PACKAGE_ID.with_hash(
"bafybei0000000000000000000000000000000000000000000000000000"
),
with_dependencies=True,
)

with pytest.raises(
ValueError,
Expand Down
21 changes: 18 additions & 3 deletions tests/test_package_manager/test_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ def test_sync(

with mock.patch.object(pm, "add_package") as update_patch:
pm.sync(dev=True, third_party=False)
update_patch.assert_called_with(package_id=DUMMY_PACKAGE_ID)
update_patch.assert_called_with(
package_id=DUMMY_PACKAGE_ID.with_hash(
"bafybei0000000000000000000000000000000000000000000000000000"
),
with_dependencies=True,
)

# test package already exists.
with mock.patch.object(pm, "add_package") as update_patch, mock.patch(
Expand Down Expand Up @@ -204,7 +209,12 @@ def test_sync(

with mock.patch.object(pm, "add_package") as update_patch:
pm.sync(dev=False, third_party=True)
update_patch.assert_called_with(package_id=DUMMY_PACKAGE_ID)
update_patch.assert_called_with(
package_id=DUMMY_PACKAGE_ID.with_hash(
"bafybei0000000000000000000000000000000000000000000000000000"
),
with_dependencies=True,
)

# 3d part hashes
pm = PackageManagerV1(
Expand All @@ -214,7 +224,12 @@ def test_sync(

with mock.patch.object(pm, "add_package") as update_patch:
pm.sync(dev=False, third_party=True, update_hashes=True)
update_patch.assert_called_with(package_id=DUMMY_PACKAGE_ID)
update_patch.assert_called_with(
package_id=DUMMY_PACKAGE_ID.with_hash(
"bafybei0000000000000000000000000000000000000000000000000000"
),
with_dependencies=True,
)

# 3d part packages
with mock.patch.object(pm, "update_package") as update_patch, mock.patch.object(
Expand Down

0 comments on commit 0c4ec6e

Please sign in to comment.