public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: "W. Trevor King" <wking@tremily.us>
To: gentoo-portage-dev@lists.gentoo.org
Cc: Rafael Goncalves Martins <rafaelmartins@gentoo.org>,
	"W. Trevor King" <wking@tremily.us>
Subject: [gentoo-portage-dev] [PATCH v3 1/4] pym/portage/package/ebuild/fetch.py: Factor out _get_checksum_failure_max_tries
Date: Sun, 19 Jan 2014 19:26:07 -0800	[thread overview]
Message-ID: <a57bca0acfabcc6a154a1d6fc5b4b1a2737220f7.1390187967.git.wking@tremily.us> (raw)
In-Reply-To: <cover.1390187967.git.wking@tremily.us>
In-Reply-To: <cover.1390187967.git.wking@tremily.us>

The current fetch() function is quite long, which makes it hard to
know what I can change without adverse side effects.  By pulling this
logic out of the main function, we get clearer logic in fetch() and
more explicit input for the config extraction.

Following a suggestion by Tom Wijsman, I put the setting name in a new
'key' variable to cut down on the PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS
noise.
---
 pym/portage/package/ebuild/fetch.py    | 61 ++++++++++++++++++++--------------
 pym/portage/tests/ebuild/test_fetch.py | 45 +++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 25 deletions(-)
 create mode 100644 pym/portage/tests/ebuild/test_fetch.py

diff --git a/pym/portage/package/ebuild/fetch.py b/pym/portage/package/ebuild/fetch.py
index 5316f03..911500a 100644
--- a/pym/portage/package/ebuild/fetch.py
+++ b/pym/portage/package/ebuild/fetch.py
@@ -240,6 +240,40 @@ _size_suffix_map = {
 	'Y' : 80,
 }
 
+
+def _get_checksum_failure_max_tries(settings, default=5):
+	"""
+	Get the maximum number of failed download attempts.
+
+	Generally, downloading the same file repeatedly from
+	every single available mirror is a waste of bandwidth
+	and time, so there needs to be a cap.
+	"""
+	key = 'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS'
+	v = default
+	try:
+		v = int(settings.get(key, default))
+	except (ValueError, OverflowError):
+		writemsg(_("!!! Variable %s contains "
+			"non-integer value: '%s'\n")
+			% (key, settings[key]),
+			noiselevel=-1)
+		writemsg(_("!!! Using %s default value: %s\n")
+			% (key, default),
+			noiselevel=-1)
+		v = default
+	if v < 1:
+		writemsg(_("!!! Variable %s contains "
+			"value less than 1: '%s'\n")
+			% (key, v),
+			noiselevel=-1)
+		writemsg(_("!!! Using %s default value: %s\n")
+			% (key, default),
+			noiselevel=-1)
+		v = default
+	return v
+
+
 def fetch(myuris, mysettings, listonly=0, fetchonly=0,
 	locks_in_subdir=".locks", use_locks=1, try_mirrors=1, digests=None,
 	allow_missing_digests=True):
@@ -263,31 +297,8 @@ def fetch(myuris, mysettings, listonly=0, fetchonly=0,
 			print(_(">>> \"mirror\" mode desired and \"mirror\" restriction found; skipping fetch."))
 			return 1
 
-	# Generally, downloading the same file repeatedly from
-	# every single available mirror is a waste of bandwidth
-	# and time, so there needs to be a cap.
-	checksum_failure_max_tries = 5
-	v = checksum_failure_max_tries
-	try:
-		v = int(mysettings.get("PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS",
-			checksum_failure_max_tries))
-	except (ValueError, OverflowError):
-		writemsg(_("!!! Variable PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
-			" contains non-integer value: '%s'\n") % \
-			mysettings["PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"], noiselevel=-1)
-		writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
-			"default value: %s\n") % checksum_failure_max_tries,
-			noiselevel=-1)
-		v = checksum_failure_max_tries
-	if v < 1:
-		writemsg(_("!!! Variable PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
-			" contains value less than 1: '%s'\n") % v, noiselevel=-1)
-		writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
-			"default value: %s\n") % checksum_failure_max_tries,
-			noiselevel=-1)
-		v = checksum_failure_max_tries
-	checksum_failure_max_tries = v
-	del v
+	checksum_failure_max_tries = _get_checksum_failure_max_tries(
+		settings=mysettings)
 
 	fetch_resume_size_default = "350K"
 	fetch_resume_size = mysettings.get("PORTAGE_FETCH_RESUME_MIN_SIZE")
diff --git a/pym/portage/tests/ebuild/test_fetch.py b/pym/portage/tests/ebuild/test_fetch.py
new file mode 100644
index 0000000..26e0349
--- /dev/null
+++ b/pym/portage/tests/ebuild/test_fetch.py
@@ -0,0 +1,45 @@
+# Copyright 1998-2013 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+from portage.package.ebuild.fetch import (
+	_get_checksum_failure_max_tries,
+	)
+from portage.tests import TestCase
+
+
+class FetchTestCase(TestCase):
+	"""
+	Test fetch and it's helper functions.
+
+	The fetch function, as it stands, is too complicated to test
+	on its own.  However, the new helper functions are much more
+	limited and easier to test.  Despite these tests, the helper
+	functions are internal implementation details, and their
+	presence and interface may change at any time.  Do not use
+	them directly (outside of these tests).
+	"""
+
+	def test_get_checksum_failure_max_tries(self):
+		self.assertEqual(
+			_get_checksum_failure_max_tries(settings={}),
+			5)
+		self.assertEqual(
+			_get_checksum_failure_max_tries(settings={
+				'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS': ''}),
+			5)
+		self.assertEqual(
+			_get_checksum_failure_max_tries(settings={
+				'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS': '3'}),
+			3)
+		self.assertEqual(
+			_get_checksum_failure_max_tries(settings={
+				'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS': '-1'}),
+			5)
+		self.assertEqual(
+			_get_checksum_failure_max_tries(settings={
+				'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS': 'oops'}),
+			5)
+		self.assertEqual(
+			_get_checksum_failure_max_tries(
+				settings={}, default=3),
+			3)
-- 
1.8.5.2.8.g0f6c0d1



  reply	other threads:[~2014-01-20  3:26 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-19  3:07 [gentoo-portage-dev] [PATCH 0/3] Initial fetch() refactoring W. Trevor King
2014-01-19  3:07 ` [gentoo-portage-dev] [PATCH 1/3] pym/portage/package/ebuild/fetch.py: Factor out _get_checksum_failure_max_tries W. Trevor King
2014-01-20  1:26   ` Tom Wijsman
2014-01-20  1:56     ` W. Trevor King
2014-01-19  3:07 ` [gentoo-portage-dev] [PATCH 2/3] pym/portage/package/ebuild/fetch.py: Factor out _get_fetch_resume_size W. Trevor King
2014-01-20  1:41   ` Tom Wijsman
2014-01-20  2:01     ` W. Trevor King
2014-01-20  2:26       ` Tom Wijsman
2014-01-19  3:07 ` [gentoo-portage-dev] [PATCH 3/3] pym/portage/package/ebuild/fetch.py: Factor out _get_uris W. Trevor King
2014-01-19 21:36   ` Sebastian Luther
2014-01-19 21:43     ` W. Trevor King
2014-01-19 22:36   ` Alec Warner
2014-01-19 23:06     ` W. Trevor King
2014-01-19 23:31       ` W. Trevor King
2014-01-19 20:05 ` [gentoo-portage-dev] [PATCH 0/3] Initial fetch() refactoring Sebastian Luther
2014-01-19 20:58   ` W. Trevor King
2014-01-19 22:14     ` [gentoo-portage-dev] [PATCH v2 " W. Trevor King
2014-01-19 22:14       ` [gentoo-portage-dev] [PATCH v2 1/3] pym/portage/package/ebuild/fetch.py: Factor out _get_checksum_failure_max_tries W. Trevor King
2014-01-19 22:44         ` Alec Warner
2014-01-19 22:45           ` Alec Warner
2014-01-19 22:51             ` W. Trevor King
2014-01-19 22:52               ` Alec Warner
2014-01-19 22:14       ` [gentoo-portage-dev] [PATCH v2 2/3] pym/portage/package/ebuild/fetch.py: Factor out _get_fetch_resume_size W. Trevor King
2014-01-19 22:14       ` [gentoo-portage-dev] [PATCH v2 3/3] pym/portage/package/ebuild/fetch.py: Factor out _get_uris W. Trevor King
2014-01-20  3:26       ` [gentoo-portage-dev] [PATCH v3 0/4] Initial fetch() refactoring W. Trevor King
2014-01-20  3:26         ` W. Trevor King [this message]
2014-01-20  3:26         ` [gentoo-portage-dev] [PATCH v3 2/4] pym/portage/package/ebuild/fetch.py: Factor out _get_fetch_resume_size W. Trevor King
2014-01-20  3:26         ` [gentoo-portage-dev] [PATCH v3 3/4] pym/portage/package/ebuild/fetch.py: Factor out _get_uris W. Trevor King
2014-01-21  2:57           ` [gentoo-portage-dev] " W. Trevor King
2014-01-20  3:26         ` [gentoo-portage-dev] [PATCH v3 4/4] pym/portage/package/ebuild/fetch.py: Flatten conditionals in _get_fetch_resume_size W. Trevor King
2014-01-22  5:35         ` [gentoo-portage-dev] [PATCH v3 0/4] Initial fetch() refactoring Mike Frysinger
2014-01-22 16:10           ` W. Trevor King
2014-01-22 19:00             ` Mike Frysinger
2014-01-27  4:00         ` [gentoo-portage-dev] " W. Trevor King
2014-01-19 21:22 ` [gentoo-portage-dev] [PATCH 0/3] " Sebastian Luther
2014-01-19 22:45   ` Alexander Berntsen
2014-01-19 22:46     ` Alec Warner
2014-01-19 22:50       ` Alexander Berntsen
2014-01-19 22:54         ` Alec Warner
2014-01-19 23:51           ` Alexander Berntsen
2014-01-19 23:53             ` Alec Warner
2014-01-19 23:11       ` W. Trevor King
2014-01-22  5:34       ` Mike Frysinger
2014-01-19 21:39 ` Sebastian Luther
2014-01-19 22:46   ` W. Trevor King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a57bca0acfabcc6a154a1d6fc5b4b1a2737220f7.1390187967.git.wking@tremily.us \
    --to=wking@tremily.us \
    --cc=gentoo-portage-dev@lists.gentoo.org \
    --cc=rafaelmartins@gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox