Skip to content

Commit

Permalink
package/ebuild/fetch.py: add attribute FilesFetcher.can_fetch
Browse files Browse the repository at this point in the history
Signed-off-by: David Palao <david.palao@gmail.com>
  • Loading branch information
palao committed Mar 15, 2024
1 parent eddf4f8 commit 1acd779
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
13 changes: 10 additions & 3 deletions lib/portage/package/ebuild/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,13 @@ def file_uri_tuples(self) -> Iterator[DistfileNameAndURI]:
uri,
)

@property
def can_fetch(self) -> bool:
if self.params.listonly:
return False
else:
return True

def _lay_out_file_to_uris_mappings(self) -> None:
"""This method arranges a mapping from files (``DistfileName``'s)
to URIs considering the restrictions and the mirrors.
Expand All @@ -2538,8 +2545,8 @@ def _set_mirrors_considering_restrictions(self) -> None:
override_fetch = override_mirror or (uri or "").startswith("fetch+")
if override_fetch:
uri = uri.partition("+")[2]
self._ensure_in_filedict_with_generic_mirrors(distfile)
self._add_specific_mirrors(distfile, uri)
self._ensure_in_filedict_with_generic_mirrors(distfile, override_mirror)
self._add_specific_mirrors(distfile, uri, override_fetch)

def _init_file_to_uris_mappings(self) -> None:
"""Method that adds some mappings to the instance:
Expand Down Expand Up @@ -2672,7 +2679,7 @@ def _order_primaryuri_dict_values(self) -> None:
This method assumes that the instance already has an attribute
called ``_primaryuri_dict`` which is a mapping: file -> list[uri]
"""
for uris in self._primaryuri_dict.values():
for uris in self.primaryuri_dict.values():
uris.reverse()

def _add_thirdpartymirrors_to_primaryuri_dict(self) -> None:
Expand Down
36 changes: 27 additions & 9 deletions lib/portage/tests/package/ebuild/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class like this one.
restrict_mirror: bool = False
settings: dict[str, str] = field(default_factory=lambda: {"ONE": "1"})
mirror_cache: Optional[str] = None
digests: dict[str, dict[str, str]] = field(default_factory=lambda: {})
force_mirror: bool = False
listonly: bool = False


class FetchExitStatusTestCase(unittest.TestCase):
Expand Down Expand Up @@ -777,10 +780,10 @@ def test__lay_out_file_to_uris_mappings_called_in_init(
@patch("portage.package.ebuild.fetch.FilesFetcher._lay_out_file_to_uris_mappings")
def test__order_primaryuri_dict_values(self, _):
fetcher = FilesFetcher({"a": "b"}, Mock())
fetcher._primaryuri_dict = {"x": ["1", "2", "3"], "y": [".", "..."]}
fetcher.primaryuri_dict = {"x": ["1", "2", "3"], "y": [".", "..."]}
fetcher._order_primaryuri_dict_values()
self.assertEqual(
fetcher._primaryuri_dict, {"x": ["3", "2", "1"], "y": ["...", "."]}
fetcher.primaryuri_dict, {"x": ["3", "2", "1"], "y": ["...", "."]}
)

@patch("portage.package.ebuild.fetch.FilesFetcher._lay_out_file_to_uris_mappings")
Expand Down Expand Up @@ -913,14 +916,22 @@ def file_uri_tuples():
fetcher._init_file_to_uris_mappings()
fetcher._set_mirrors_considering_restrictions()
pensure_in_filedict_with_generic_mirrors.assert_has_calls(
[call(DistfileName(name)) for name in ("/a", "/b", "/c", "/d")]
[
call(DistfileName(name), override)
for name, override in [
("/a", False),
("/b", True),
("/c", False),
("/d", False),
]
]
)
padd_specific_mirrors.assert_has_calls(
[
call(DistfileName("/a"), "http://.some.url."),
call(DistfileName("/b"), "https://.another.url."),
call(DistfileName("/c"), "ftp://.my.host."),
call(DistfileName("/d"), "other+https://.yet.another."),
call(DistfileName("/a"), "http://.some.url.", False),
call(DistfileName("/b"), "https://.another.url.", True),
call(DistfileName("/c"), "ftp://.my.host.", True),
call(DistfileName("/d"), "other+https://.yet.another.", False),
]
)

Expand All @@ -943,8 +954,8 @@ def test_lay_out_file_to_uris_mappings_call_sequence(
p_add_thirdpartymirrors_to_primaryuri_dict,
p_merge_primaryuri_values_into_filedict,
):
"""This is a mockist approach to test that the method under test
ie, ``_lay_out_file_to_uris_mappings`` performs the correct
"""This is a mockist approach to testing that the method under test
ie, ``_lay_out_file_to_uris_mappings``, performs the correct
sequence of calls, in the correct order.
"""
fetcher = FilesFetcher({"a": "b"}, FakeParams())
Expand Down Expand Up @@ -980,6 +991,13 @@ def test_lay_out_file_to_uris_mappings_call_sequence(
]
)

def test_can_fetch_attribute_depends_on_listonly(self):
params = FakeParams()
fetcher = FilesFetcher({"a": "b"}, params)
self.assertTrue(fetcher.can_fetch)
params.listonly = True
self.assertFalse(fetcher.can_fetch)


@patch("portage.package.ebuild.fetch.partial")
@patch("portage.package.ebuild.fetch.FilesFetcher._lay_out_file_to_uris_mappings")
Expand Down

0 comments on commit 1acd779

Please sign in to comment.