Gentoo Archives: gentoo-portage-dev

From: Alec Warner <antarus@g.o>
To: gentoo-portage-dev@l.g.o
Subject: Re: [gentoo-portage-dev] [PATCH v2 1/3] pym/portage/package/ebuild/fetch.py: Factor out _get_checksum_failure_max_tries
Date: Sun, 19 Jan 2014 22:45:03
Message-Id: CAAr7Pr-qpAD7qJi8Kv4dHJqEchOcpH--6+cC=7SRH6CoOrj3Qg@mail.gmail.com
In Reply to: [gentoo-portage-dev] [PATCH v2 1/3] pym/portage/package/ebuild/fetch.py: Factor out _get_checksum_failure_max_tries by "W. Trevor King"
1 On Sun, Jan 19, 2014 at 2:14 PM, W. Trevor King <wking@×××××××.us> wrote:
2
3 > The current fetch() function is quite long, which makes it hard to
4 > know what I can change without adverse side effects. By pulling this
5 > logic out of the main function, we get clearer logic in fetch() and
6 > more explicit input for the config extraction.
7 > ---
8 > pym/portage/package/ebuild/fetch.py | 59
9 > +++++++++++++++++++++----------------
10 > 1 file changed, 34 insertions(+), 25 deletions(-)
11 >
12 > diff --git a/pym/portage/package/ebuild/fetch.py
13 > b/pym/portage/package/ebuild/fetch.py
14 > index 5316f03..6f46572 100644
15 > --- a/pym/portage/package/ebuild/fetch.py
16 > +++ b/pym/portage/package/ebuild/fetch.py
17 > @@ -240,6 +240,38 @@ _size_suffix_map = {
18 > 'Y' : 80,
19 > }
20 >
21 > +
22 > +def _get_checksum_failure_max_tries(settings, default=5):
23 > + """
24 > + Get the maximum number of failed download attempts
25 > +
26 > + Generally, downloading the same file repeatedly from
27 > + every single available mirror is a waste of bandwidth
28 > + and time, so there needs to be a cap.
29 > + """
30 > + v = default
31 > + try:
32 > + v = int(settings.get("PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS",
33 > + default))
34 > + except (ValueError, OverflowError):
35 > + writemsg(_("!!! Variable
36 > PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
37 > + " contains non-integer value: '%s'\n") % \
38 > + settings["PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"],
39 > + noiselevel=-1)
40 > + writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
41 > + "default value: %s\n") % default,
42 > + noiselevel=-1)
43 > + v = default
44 > + if v < 1:
45 > + writemsg(_("!!! Variable
46 > PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
47 > + " contains value less than 1: '%s'\n") % v,
48 > noiselevel=-1)
49 > + writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
50 > + "default value: %s\n") % default,
51 > + noiselevel=-1)
52 > + v = default
53 > + return v
54 > +
55 > +
56 >
57
58 This function and the next function you wrote are identical. How about
59 making a single function?
60
61 def getValueFromSettings(settings, key, default, validator):
62 try:
63 return validator(settings.get(key, default))
64 except ValidationError as e:
65 # writemsg the exception, it will contain the juicy bits.)
66
67 def getIntValueFromSettings(settings, key, default):
68 def validator(v):
69 try:
70 v = int(v)
71 except (ValueError, OverflowError) as e:
72 raise ValidationError(" bla bla bla not an int, we picked the
73 default.")
74 if v < 1:
75 v = default
76 raise ValidationError("bla bla bla less than one we used the
77 defualt.")
78
79 return getValueFromSettings(settings, key, default, validator)
80
81 Then we are not necessarily writing a bunch of the same functions over and
82 over. The defaults we can stick in a config file, or in python, or at the
83 call site.
84
85 -A
86
87
88
89
90
91
92
93
94 > def fetch(myuris, mysettings, listonly=0, fetchonly=0,
95 > locks_in_subdir=".locks", use_locks=1, try_mirrors=1, digests=None,
96 > allow_missing_digests=True):
97 > @@ -263,31 +295,8 @@ def fetch(myuris, mysettings, listonly=0, fetchonly=0,
98 > print(_(">>> \"mirror\" mode desired and
99 > \"mirror\" restriction found; skipping fetch."))
100 > return 1
101 >
102 > - # Generally, downloading the same file repeatedly from
103 > - # every single available mirror is a waste of bandwidth
104 > - # and time, so there needs to be a cap.
105 > - checksum_failure_max_tries = 5
106 > - v = checksum_failure_max_tries
107 > - try:
108 > - v =
109 > int(mysettings.get("PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS",
110 > - checksum_failure_max_tries))
111 > - except (ValueError, OverflowError):
112 > - writemsg(_("!!! Variable
113 > PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
114 > - " contains non-integer value: '%s'\n") % \
115 > - mysettings["PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"],
116 > noiselevel=-1)
117 > - writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
118 > - "default value: %s\n") %
119 > checksum_failure_max_tries,
120 > - noiselevel=-1)
121 > - v = checksum_failure_max_tries
122 > - if v < 1:
123 > - writemsg(_("!!! Variable
124 > PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
125 > - " contains value less than 1: '%s'\n") % v,
126 > noiselevel=-1)
127 > - writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
128 > - "default value: %s\n") %
129 > checksum_failure_max_tries,
130 > - noiselevel=-1)
131 > - v = checksum_failure_max_tries
132 > - checksum_failure_max_tries = v
133 > - del v
134 > + checksum_failure_max_tries = _get_checksum_failure_max_tries(
135 > + settings=mysettings)
136 >
137 > fetch_resume_size_default = "350K"
138 > fetch_resume_size = mysettings.get("PORTAGE_FETCH_RESUME_MIN_SIZE")
139 > --
140 > 1.8.5.2.8.g0f6c0d1
141 >
142 >
143 >

Replies