Skip to content

Commit

Permalink
package/ebuild/fetch.py: complete _add_specific_mirrors method
Browse files Browse the repository at this point in the history
complete _add_specific_mirrors method. It handles now the when the uri
is not a mirror uri.

Signed-off-by: David Palao <david.palao@gmail.com>
  • Loading branch information
palao committed Jan 5, 2024
1 parent aef996b commit cb60323
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
15 changes: 14 additions & 1 deletion lib/portage/package/ebuild/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2599,7 +2599,12 @@ def _ensure_in_filedict_with_generic_mirrors(
)
)

def _add_specific_mirrors(self, distfile: DistfileName, uri: Optional[str]) -> None:
def _add_specific_mirrors(
self,
distfile: DistfileName,
uri: Optional[str],
override_fetch: bool,
) -> None:
if uri is None:
return
if uri[:9] == "mirror://":
Expand Down Expand Up @@ -2630,6 +2635,14 @@ def _add_specific_mirrors(self, distfile: DistfileName, uri: Optional[str]) -> N
else:
writemsg(_("Invalid mirror definition in SRC_URI:\n"), noiselevel=-1)
writemsg(f" {uri}\n", noiselevel=-1)
else:
if (
self.params.restrict_fetch and not override_fetch
) or self.params.force_mirror:
return
else:
primaryuris = self.primaryuri_dict.setdefault(distfile, [])
primaryuris.append(uri)

def _order_primaryuri_dict_values(self) -> None:
"""Order _primaryuri_dict values to match that in SRC_URI.
Expand Down
60 changes: 48 additions & 12 deletions lib/portage/tests/package/ebuild/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ def test_nothing_happens_if_uri_is_None(
# Need this because I'm mocking _lay_out_file_to_uris_mappings:
fetcher._init_file_to_uris_mappings()

fetcher._add_specific_mirrors(self.afile, None)
fetcher._add_specific_mirrors(self.afile, None, override_fetch=False)

self.assertEqual(fetcher.filedict, OrderedDict())
self.assertEqual(fetcher.primaryuri_dict, {})
Expand All @@ -1088,7 +1088,9 @@ def test_custom_mirror_uri(
fetcher._init_file_to_uris_mappings()
fetcher._ensure_in_filedict_with_generic_mirrors(self.afile, False)

fetcher._add_specific_mirrors(self.afile, "mirror://..c1m../a/jj.tarr")
fetcher._add_specific_mirrors(
self.afile, "mirror://..c1m../a/jj.tarr", override_fetch=False
)

self.assertEqual(
fetcher.filedict,
Expand Down Expand Up @@ -1117,7 +1119,9 @@ def test_thirdparty_mirror_uri(
fetcher._ensure_in_filedict_with_generic_mirrors(self.afile, False)

# Next we exercise the function:
fetcher._add_specific_mirrors(self.afile, "mirror://..tp1../a/jj.tarr")
fetcher._add_specific_mirrors(
self.afile, "mirror://..tp1../a/jj.tarr", override_fetch=False
)

self.assertEqual(
len(fetcher.filedict[self.afile]), len(self.thirdparty_mirrors["..tp1.."])
Expand Down Expand Up @@ -1148,7 +1152,9 @@ def test_thirdparty_mirror_uri(
# We exercise the function under test once more, to check that if a new
# mirror uri is processed, the relevant uris are added to filedict and
# thirdpartymirrors, but nothing is overwritten:
fetcher._add_specific_mirrors(self.afile, "mirror://..tp2../a/jj.tarr")
fetcher._add_specific_mirrors(
self.afile, "mirror://..tp2../a/jj.tarr", override_fetch=False
)
self.assertEqual(
len(fetcher.filedict[self.afile]),
len(self.thirdparty_mirrors["..tp1.."])
Expand Down Expand Up @@ -1184,7 +1190,9 @@ def test_unknown_mirror_uri(
fetcher._ensure_in_filedict_with_generic_mirrors(self.afile, False)

# Next we exercise the function:
fetcher._add_specific_mirrors(self.afile, "mirror://..??../a/jj.tarr")
fetcher._add_specific_mirrors(
self.afile, "mirror://..??../a/jj.tarr", override_fetch=False
)

self.assertEqual(len(fetcher.filedict[self.afile]), 0)
self.assertNotIn(self.afile, fetcher.thirdpartymirror_uris)
Expand All @@ -1202,7 +1210,9 @@ def test_invalid_mirror_definition(
fetcher._ensure_in_filedict_with_generic_mirrors(self.afile, False)

# Next we exercise the function:
fetcher._add_specific_mirrors(self.afile, "mirror://..??..")
fetcher._add_specific_mirrors(
self.afile, "mirror://..??..", override_fetch=False
)
mwritemsg.assert_has_calls(
[
call(
Expand All @@ -1215,16 +1225,42 @@ def test_invalid_mirror_definition(
def test_primary_uri_with_restrictions_not_added(
self, _, play_out_file_to_uris_mappings, __
):
# test here that if uri = "http://..." and there are fetch restrictions,
# it is NOT added to primaryuris
self.fail("write me!")
# test here that if uri is not of mirror type, eg. uri = "http://..." and
# there are fetch restrictions, the uri is NOT added to primaryuris
params = self.make_instance(settings=self.settings)
self.settings.features.add("force-mirror")
fetcher = FilesFetcher({self.afile: ("",)}, params)
# Need the next two because I'm mocking _lay_out_file_to_uris_mappings:
fetcher._init_file_to_uris_mappings()
fetcher._ensure_in_filedict_with_generic_mirrors(self.afile, False)

# Next we exercise the function:
fetcher._add_specific_mirrors(self.afile, "ftp://..??..", override_fetch=True)
self.assertNotIn(self.afile, fetcher.primaryuri_dict)

self.settings.features.remove("force-mirror")
self.settings.dict["PORTAGE_RESTRICT"] = "fetch"
fetcher._add_specific_mirrors(self.afile, "ftp://..??..", override_fetch=False)
self.assertNotIn(self.afile, fetcher.primaryuri_dict)

def test_primary_uri_without_restrictions_added(
self, _, play_out_file_to_uris_mappings, __
):
# test here that if uri = "http://..." and there are NO fetch restrictions,
# it is added to primaryuris
self.fail("write me!")
# test here that if uri is not of mirror type, eg. uri = "http://..." and
# there are NO fetch restrictions, it is added to primaryuris
params = self.make_instance(settings=self.settings)
fetcher = FilesFetcher({self.afile: ("",)}, params)
# Need the next two because I'm mocking _lay_out_file_to_uris_mappings:
fetcher._init_file_to_uris_mappings()
fetcher._ensure_in_filedict_with_generic_mirrors(self.afile, False)

# Next we exercise the function:
fetcher._add_specific_mirrors(self.afile, "ftp://..??..", override_fetch=True)
self.assertEqual(fetcher.primaryuri_dict[self.afile], ["ftp://..??.."])
fetcher._add_specific_mirrors(self.afile, "http://..abf..", override_fetch=True)
self.assertEqual(
fetcher.primaryuri_dict[self.afile], ["ftp://..??..", "http://..abf.."]
)


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

0 comments on commit cb60323

Please sign in to comment.