pym/portage/package/ebuild/fetch.py: Factor out _get_fetch_resume_size
authorW. Trevor King <wking@tremily.us>
Sun, 19 Jan 2014 00:22:16 +0000 (16:22 -0800)
committerW. Trevor King <wking@tremily.us>
Sun, 19 Jan 2014 02:46:42 +0000 (18:46 -0800)
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.

pym/portage/package/ebuild/fetch.py

index 4337247ff68a48b1130db75c46b41c08f63b202a..bd572faad5673ee5235e9229337901b34952ff33 100644 (file)
@@ -271,6 +271,32 @@ def _get_checksum_failure_max_tries(settings, default=5):
        return v
 
 
+def _get_fetch_resume_size(settings, default='350K'):
+       v = settings.get("PORTAGE_FETCH_RESUME_MIN_SIZE")
+       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 PORTAGE_FETCH_RESUME_MIN_SIZE"
+                               " contains an unrecognized format: '%s'\n") % \
+                               settings["PORTAGE_FETCH_RESUME_MIN_SIZE"],
+                               noiselevel=-1)
+                       writemsg(_("!!! Using PORTAGE_FETCH_RESUME_MIN_SIZE "
+                               "default value: %s\n") % 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):
@@ -296,29 +322,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