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_RESUME_MIN_SIZE noise. |
8 |
--- |
9 |
pym/portage/package/ebuild/fetch.py | 51 +++++++++++++++++++--------------- |
10 |
pym/portage/tests/ebuild/test_fetch.py | 37 ++++++++++++++++++++++++ |
11 |
2 files changed, 65 insertions(+), 23 deletions(-) |
12 |
|
13 |
diff --git a/pym/portage/package/ebuild/fetch.py b/pym/portage/package/ebuild/fetch.py |
14 |
index 911500a..4ecefc9 100644 |
15 |
--- a/pym/portage/package/ebuild/fetch.py |
16 |
+++ b/pym/portage/package/ebuild/fetch.py |
17 |
@@ -274,6 +274,33 @@ def _get_checksum_failure_max_tries(settings, default=5): |
18 |
return v |
19 |
|
20 |
|
21 |
+def _get_fetch_resume_size(settings, default='350K'): |
22 |
+ key = 'PORTAGE_FETCH_RESUME_MIN_SIZE' |
23 |
+ v = settings.get(key) |
24 |
+ if v is not None: |
25 |
+ v = "".join(v.split()) |
26 |
+ if not v: |
27 |
+ # If it's empty, silently use the default. |
28 |
+ v = default |
29 |
+ match = _fetch_resume_size_re.match(v) |
30 |
+ if (match is None or |
31 |
+ match.group(2).upper() not in _size_suffix_map): |
32 |
+ writemsg(_("!!! Variable %s contains an " |
33 |
+ "unrecognized format: '%s'\n") |
34 |
+ % (key, settings[key]), |
35 |
+ noiselevel=-1) |
36 |
+ writemsg(_("!!! Using %s default value: %s\n") |
37 |
+ % (key, default), |
38 |
+ noiselevel=-1) |
39 |
+ v = None |
40 |
+ if v is None: |
41 |
+ v = default |
42 |
+ match = _fetch_resume_size_re.match(v) |
43 |
+ v = int(match.group(1)) * \ |
44 |
+ 2 ** _size_suffix_map[match.group(2).upper()] |
45 |
+ return v |
46 |
+ |
47 |
+ |
48 |
def fetch(myuris, mysettings, listonly=0, fetchonly=0, |
49 |
locks_in_subdir=".locks", use_locks=1, try_mirrors=1, digests=None, |
50 |
allow_missing_digests=True): |
51 |
@@ -299,29 +326,7 @@ def fetch(myuris, mysettings, listonly=0, fetchonly=0, |
52 |
|
53 |
checksum_failure_max_tries = _get_checksum_failure_max_tries( |
54 |
settings=mysettings) |
55 |
- |
56 |
- fetch_resume_size_default = "350K" |
57 |
- fetch_resume_size = mysettings.get("PORTAGE_FETCH_RESUME_MIN_SIZE") |
58 |
- if fetch_resume_size is not None: |
59 |
- fetch_resume_size = "".join(fetch_resume_size.split()) |
60 |
- if not fetch_resume_size: |
61 |
- # If it's undefined or empty, silently use the default. |
62 |
- fetch_resume_size = fetch_resume_size_default |
63 |
- match = _fetch_resume_size_re.match(fetch_resume_size) |
64 |
- if match is None or \ |
65 |
- (match.group(2).upper() not in _size_suffix_map): |
66 |
- writemsg(_("!!! Variable PORTAGE_FETCH_RESUME_MIN_SIZE" |
67 |
- " contains an unrecognized format: '%s'\n") % \ |
68 |
- mysettings["PORTAGE_FETCH_RESUME_MIN_SIZE"], noiselevel=-1) |
69 |
- writemsg(_("!!! Using PORTAGE_FETCH_RESUME_MIN_SIZE " |
70 |
- "default value: %s\n") % fetch_resume_size_default, |
71 |
- noiselevel=-1) |
72 |
- fetch_resume_size = None |
73 |
- if fetch_resume_size is None: |
74 |
- fetch_resume_size = fetch_resume_size_default |
75 |
- match = _fetch_resume_size_re.match(fetch_resume_size) |
76 |
- fetch_resume_size = int(match.group(1)) * \ |
77 |
- 2 ** _size_suffix_map[match.group(2).upper()] |
78 |
+ fetch_resume_size = _get_fetch_resume_size(settings=mysettings) |
79 |
|
80 |
# Behave like the package has RESTRICT="primaryuri" after a |
81 |
# couple of checksum failures, to increase the probablility |
82 |
diff --git a/pym/portage/tests/ebuild/test_fetch.py b/pym/portage/tests/ebuild/test_fetch.py |
83 |
index 26e0349..2b06190 100644 |
84 |
--- a/pym/portage/tests/ebuild/test_fetch.py |
85 |
+++ b/pym/portage/tests/ebuild/test_fetch.py |
86 |
@@ -3,6 +3,7 @@ |
87 |
|
88 |
from portage.package.ebuild.fetch import ( |
89 |
_get_checksum_failure_max_tries, |
90 |
+ _get_fetch_resume_size, |
91 |
) |
92 |
from portage.tests import TestCase |
93 |
|
94 |
@@ -43,3 +44,39 @@ class FetchTestCase(TestCase): |
95 |
_get_checksum_failure_max_tries( |
96 |
settings={}, default=3), |
97 |
3) |
98 |
+ |
99 |
+ def test_get_fetch_resume_size(self): |
100 |
+ self.assertEqual( |
101 |
+ _get_fetch_resume_size(settings={}), |
102 |
+ 358400) |
103 |
+ self.assertEqual( |
104 |
+ _get_fetch_resume_size(settings={ |
105 |
+ 'PORTAGE_FETCH_RESUME_MIN_SIZE': ''}), |
106 |
+ 358400) |
107 |
+ self.assertEqual( |
108 |
+ _get_fetch_resume_size(settings={ |
109 |
+ 'PORTAGE_FETCH_RESUME_MIN_SIZE': '3'}), |
110 |
+ 3) |
111 |
+ self.assertEqual( |
112 |
+ _get_fetch_resume_size(settings={ |
113 |
+ 'PORTAGE_FETCH_RESUME_MIN_SIZE': '3K'}), |
114 |
+ 3072) |
115 |
+ self.assertEqual( |
116 |
+ _get_fetch_resume_size(settings={ |
117 |
+ 'PORTAGE_FETCH_RESUME_MIN_SIZE': '3 K'}), |
118 |
+ 3072) |
119 |
+ self.assertEqual( |
120 |
+ _get_fetch_resume_size(settings={ |
121 |
+ 'PORTAGE_FETCH_RESUME_MIN_SIZE': '3M'}), |
122 |
+ 3145728) |
123 |
+ self.assertEqual( |
124 |
+ _get_fetch_resume_size(settings={ |
125 |
+ 'PORTAGE_FETCH_RESUME_MIN_SIZE': '3G'}), |
126 |
+ 3221225472) |
127 |
+ self.assertEqual( |
128 |
+ _get_fetch_resume_size(settings={ |
129 |
+ 'PORTAGE_FETCH_RESUME_MIN_SIZE': 'oops'}), |
130 |
+ 358400) |
131 |
+ self.assertEqual( |
132 |
+ _get_fetch_resume_size(settings={}, default='3K'), |
133 |
+ 3072) |
134 |
-- |
135 |
1.8.5.2.8.g0f6c0d1 |