Gentoo Archives: gentoo-portage-dev

From: "W. Trevor King" <wking@×××××××.us>
To: gentoo-portage-dev@l.g.o
Cc: Rafael Goncalves Martins <rafaelmartins@g.o>, "W. Trevor King" <wking@×××××××.us>
Subject: [gentoo-portage-dev] [PATCH v3 1/4] pym/portage/package/ebuild/fetch.py: Factor out _get_checksum_failure_max_tries
Date: Mon, 20 Jan 2014 03:26:57
Message-Id: a57bca0acfabcc6a154a1d6fc5b4b1a2737220f7.1390187967.git.wking@tremily.us
In Reply to: [gentoo-portage-dev] [PATCH v3 0/4] Initial fetch() refactoring by "W. Trevor King"
1 The current fetch() function is quite long, which makes it hard to
2 know what I can change without adverse side effects. By pulling this
3 logic out of the main function, we get clearer logic in fetch() and
4 more explicit input for the config extraction.
5
6 Following a suggestion by Tom Wijsman, I put the setting name in a new
7 'key' variable to cut down on the PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS
8 noise.
9 ---
10 pym/portage/package/ebuild/fetch.py | 61 ++++++++++++++++++++--------------
11 pym/portage/tests/ebuild/test_fetch.py | 45 +++++++++++++++++++++++++
12 2 files changed, 81 insertions(+), 25 deletions(-)
13 create mode 100644 pym/portage/tests/ebuild/test_fetch.py
14
15 diff --git a/pym/portage/package/ebuild/fetch.py b/pym/portage/package/ebuild/fetch.py
16 index 5316f03..911500a 100644
17 --- a/pym/portage/package/ebuild/fetch.py
18 +++ b/pym/portage/package/ebuild/fetch.py
19 @@ -240,6 +240,40 @@ _size_suffix_map = {
20 'Y' : 80,
21 }
22
23 +
24 +def _get_checksum_failure_max_tries(settings, default=5):
25 + """
26 + Get the maximum number of failed download attempts.
27 +
28 + Generally, downloading the same file repeatedly from
29 + every single available mirror is a waste of bandwidth
30 + and time, so there needs to be a cap.
31 + """
32 + key = 'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS'
33 + v = default
34 + try:
35 + v = int(settings.get(key, default))
36 + except (ValueError, OverflowError):
37 + writemsg(_("!!! Variable %s contains "
38 + "non-integer value: '%s'\n")
39 + % (key, settings[key]),
40 + noiselevel=-1)
41 + writemsg(_("!!! Using %s default value: %s\n")
42 + % (key, default),
43 + noiselevel=-1)
44 + v = default
45 + if v < 1:
46 + writemsg(_("!!! Variable %s contains "
47 + "value less than 1: '%s'\n")
48 + % (key, v),
49 + noiselevel=-1)
50 + writemsg(_("!!! Using %s default value: %s\n")
51 + % (key, default),
52 + noiselevel=-1)
53 + v = default
54 + return v
55 +
56 +
57 def fetch(myuris, mysettings, listonly=0, fetchonly=0,
58 locks_in_subdir=".locks", use_locks=1, try_mirrors=1, digests=None,
59 allow_missing_digests=True):
60 @@ -263,31 +297,8 @@ def fetch(myuris, mysettings, listonly=0, fetchonly=0,
61 print(_(">>> \"mirror\" mode desired and \"mirror\" restriction found; skipping fetch."))
62 return 1
63
64 - # Generally, downloading the same file repeatedly from
65 - # every single available mirror is a waste of bandwidth
66 - # and time, so there needs to be a cap.
67 - checksum_failure_max_tries = 5
68 - v = checksum_failure_max_tries
69 - try:
70 - v = int(mysettings.get("PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS",
71 - checksum_failure_max_tries))
72 - except (ValueError, OverflowError):
73 - writemsg(_("!!! Variable PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
74 - " contains non-integer value: '%s'\n") % \
75 - mysettings["PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"], noiselevel=-1)
76 - writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
77 - "default value: %s\n") % checksum_failure_max_tries,
78 - noiselevel=-1)
79 - v = checksum_failure_max_tries
80 - if v < 1:
81 - writemsg(_("!!! Variable PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
82 - " contains value less than 1: '%s'\n") % v, noiselevel=-1)
83 - writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
84 - "default value: %s\n") % checksum_failure_max_tries,
85 - noiselevel=-1)
86 - v = checksum_failure_max_tries
87 - checksum_failure_max_tries = v
88 - del v
89 + checksum_failure_max_tries = _get_checksum_failure_max_tries(
90 + settings=mysettings)
91
92 fetch_resume_size_default = "350K"
93 fetch_resume_size = mysettings.get("PORTAGE_FETCH_RESUME_MIN_SIZE")
94 diff --git a/pym/portage/tests/ebuild/test_fetch.py b/pym/portage/tests/ebuild/test_fetch.py
95 new file mode 100644
96 index 0000000..26e0349
97 --- /dev/null
98 +++ b/pym/portage/tests/ebuild/test_fetch.py
99 @@ -0,0 +1,45 @@
100 +# Copyright 1998-2013 Gentoo Foundation
101 +# Distributed under the terms of the GNU General Public License v2
102 +
103 +from portage.package.ebuild.fetch import (
104 + _get_checksum_failure_max_tries,
105 + )
106 +from portage.tests import TestCase
107 +
108 +
109 +class FetchTestCase(TestCase):
110 + """
111 + Test fetch and it's helper functions.
112 +
113 + The fetch function, as it stands, is too complicated to test
114 + on its own. However, the new helper functions are much more
115 + limited and easier to test. Despite these tests, the helper
116 + functions are internal implementation details, and their
117 + presence and interface may change at any time. Do not use
118 + them directly (outside of these tests).
119 + """
120 +
121 + def test_get_checksum_failure_max_tries(self):
122 + self.assertEqual(
123 + _get_checksum_failure_max_tries(settings={}),
124 + 5)
125 + self.assertEqual(
126 + _get_checksum_failure_max_tries(settings={
127 + 'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS': ''}),
128 + 5)
129 + self.assertEqual(
130 + _get_checksum_failure_max_tries(settings={
131 + 'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS': '3'}),
132 + 3)
133 + self.assertEqual(
134 + _get_checksum_failure_max_tries(settings={
135 + 'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS': '-1'}),
136 + 5)
137 + self.assertEqual(
138 + _get_checksum_failure_max_tries(settings={
139 + 'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS': 'oops'}),
140 + 5)
141 + self.assertEqual(
142 + _get_checksum_failure_max_tries(
143 + settings={}, default=3),
144 + 3)
145 --
146 1.8.5.2.8.g0f6c0d1