public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download: 
* [gentoo-portage-dev] [PATCH v3 2/4] pym/portage/package/ebuild/fetch.py: Factor out _get_fetch_resume_size
  @ 2014-01-20  3:26 99%   ` W. Trevor King
  0 siblings, 0 replies; 1+ results
From: W. Trevor King @ 2014-01-20  3:26 UTC (permalink / raw
  To: gentoo-portage-dev; +Cc: Rafael Goncalves Martins, W. Trevor King

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_RESUME_MIN_SIZE noise.
---
 pym/portage/package/ebuild/fetch.py    | 51 +++++++++++++++++++---------------
 pym/portage/tests/ebuild/test_fetch.py | 37 ++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 23 deletions(-)

diff --git a/pym/portage/package/ebuild/fetch.py b/pym/portage/package/ebuild/fetch.py
index 911500a..4ecefc9 100644
--- a/pym/portage/package/ebuild/fetch.py
+++ b/pym/portage/package/ebuild/fetch.py
@@ -274,6 +274,33 @@ def _get_checksum_failure_max_tries(settings, default=5):
 	return v
 
 
+def _get_fetch_resume_size(settings, default='350K'):
+	key = 'PORTAGE_FETCH_RESUME_MIN_SIZE'
+	v = settings.get(key)
+	if v is not None:
+		v = "".join(v.split())
+		if not v:
+			# If it's empty, silently use the default.
+			v = default
+		match = _fetch_resume_size_re.match(v)
+		if (match is None or
+				match.group(2).upper() not in _size_suffix_map):
+			writemsg(_("!!! Variable %s contains an "
+				"unrecognized format: '%s'\n")
+				% (key, settings[key]),
+				noiselevel=-1)
+			writemsg(_("!!! Using %s default value: %s\n")
+				% (key, default),
+				noiselevel=-1)
+			v = None
+	if v is None:
+		v = default
+		match = _fetch_resume_size_re.match(v)
+	v = int(match.group(1)) * \
+		2 ** _size_suffix_map[match.group(2).upper()]
+	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):
@@ -299,29 +326,7 @@ def fetch(myuris, mysettings, listonly=0, fetchonly=0,
 
 	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")
-	if fetch_resume_size is not None:
-		fetch_resume_size = "".join(fetch_resume_size.split())
-		if not fetch_resume_size:
-			# If it's undefined or empty, silently use the default.
-			fetch_resume_size = fetch_resume_size_default
-		match = _fetch_resume_size_re.match(fetch_resume_size)
-		if match is None or \
-			(match.group(2).upper() not in _size_suffix_map):
-			writemsg(_("!!! Variable PORTAGE_FETCH_RESUME_MIN_SIZE"
-				" contains an unrecognized format: '%s'\n") % \
-				mysettings["PORTAGE_FETCH_RESUME_MIN_SIZE"], noiselevel=-1)
-			writemsg(_("!!! Using PORTAGE_FETCH_RESUME_MIN_SIZE "
-				"default value: %s\n") % fetch_resume_size_default,
-				noiselevel=-1)
-			fetch_resume_size = None
-	if fetch_resume_size is None:
-		fetch_resume_size = fetch_resume_size_default
-		match = _fetch_resume_size_re.match(fetch_resume_size)
-	fetch_resume_size = int(match.group(1)) * \
-		2 ** _size_suffix_map[match.group(2).upper()]
+	fetch_resume_size = _get_fetch_resume_size(settings=mysettings)
 
 	# Behave like the package has RESTRICT="primaryuri" after a
 	# couple of checksum failures, to increase the probablility
diff --git a/pym/portage/tests/ebuild/test_fetch.py b/pym/portage/tests/ebuild/test_fetch.py
index 26e0349..2b06190 100644
--- a/pym/portage/tests/ebuild/test_fetch.py
+++ b/pym/portage/tests/ebuild/test_fetch.py
@@ -3,6 +3,7 @@
 
 from portage.package.ebuild.fetch import (
 	_get_checksum_failure_max_tries,
+	_get_fetch_resume_size,
 	)
 from portage.tests import TestCase
 
@@ -43,3 +44,39 @@ class FetchTestCase(TestCase):
 			_get_checksum_failure_max_tries(
 				settings={}, default=3),
 			3)
+
+	def test_get_fetch_resume_size(self):
+		self.assertEqual(
+			_get_fetch_resume_size(settings={}),
+			358400)
+		self.assertEqual(
+			_get_fetch_resume_size(settings={
+				'PORTAGE_FETCH_RESUME_MIN_SIZE': ''}),
+			358400)
+		self.assertEqual(
+			_get_fetch_resume_size(settings={
+				'PORTAGE_FETCH_RESUME_MIN_SIZE': '3'}),
+			3)
+		self.assertEqual(
+			_get_fetch_resume_size(settings={
+				'PORTAGE_FETCH_RESUME_MIN_SIZE': '3K'}),
+			3072)
+		self.assertEqual(
+			_get_fetch_resume_size(settings={
+				'PORTAGE_FETCH_RESUME_MIN_SIZE': '3 K'}),
+			3072)
+		self.assertEqual(
+			_get_fetch_resume_size(settings={
+				'PORTAGE_FETCH_RESUME_MIN_SIZE': '3M'}),
+			3145728)
+		self.assertEqual(
+			_get_fetch_resume_size(settings={
+				'PORTAGE_FETCH_RESUME_MIN_SIZE': '3G'}),
+			3221225472)
+		self.assertEqual(
+			_get_fetch_resume_size(settings={
+				'PORTAGE_FETCH_RESUME_MIN_SIZE': 'oops'}),
+			358400)
+		self.assertEqual(
+			_get_fetch_resume_size(settings={}, default='3K'),
+			3072)
-- 
1.8.5.2.8.g0f6c0d1



^ permalink raw reply related	[relevance 99%]

Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2014-01-19 22:14     [gentoo-portage-dev] [PATCH v2 0/3] Initial fetch() refactoring W. Trevor King
2014-01-20  3:26     ` [gentoo-portage-dev] [PATCH v3 0/4] " W. Trevor King
2014-01-20  3:26 99%   ` [gentoo-portage-dev] [PATCH v3 2/4] pym/portage/package/ebuild/fetch.py: Factor out _get_fetch_resume_size W. Trevor King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox