Gentoo Archives: gentoo-commits

From: Sam James <sam@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/
Date: Sun, 27 Mar 2022 23:07:16
Message-Id: 1648422397.13740f43bcc38e787153d9efeabc4f5d878e7af0.sam@gentoo
1 commit: 13740f43bcc38e787153d9efeabc4f5d878e7af0
2 Author: Kenneth Raplee <kenrap <AT> kennethraplee <DOT> com>
3 AuthorDate: Tue Mar 22 07:22:42 2022 +0000
4 Commit: Sam James <sam <AT> gentoo <DOT> org>
5 CommitDate: Sun Mar 27 23:06:37 2022 +0000
6 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=13740f43
7
8 Prefer generator expressions over list comprehensions
9
10 List comprehensions do improve the performance compared to using for
11 loops to build the same lists in Python. However, they are eagerly
12 evaluated and will create a sequence ahead of time.
13
14 The problem is, a fair amount of code would only use these lists as
15 iterators which does not require them to be sequences. Unless there is a
16 need to know a list's length, access its items, or alter its size, it's
17 better to use generators which are lazily evaluated and can be iterated
18 when necessary. Avoiding the eagerness of list comprehensions will
19 improve the runtime performance.
20
21 Signed-off-by: Kenneth Raplee <kenrap <AT> kennethraplee.com>
22 Signed-off-by: Sam James <sam <AT> gentoo.org>
23
24 lib/portage/__init__.py | 22 +++++++++++++---------
25 lib/portage/data.py | 25 ++++++++++++++-----------
26 lib/portage/dispatch_conf.py | 2 +-
27 lib/portage/eclass_cache.py | 2 +-
28 lib/portage/getbinpkg.py | 15 ++++++++-------
29 lib/portage/glsa.py | 14 ++++++++++----
30 6 files changed, 47 insertions(+), 33 deletions(-)
31
32 diff --git a/lib/portage/__init__.py b/lib/portage/__init__.py
33 index 82e47d0ff..e6dff28ee 100644
34 --- a/lib/portage/__init__.py
35 +++ b/lib/portage/__init__.py
36 @@ -555,10 +555,15 @@ _deprecated_eapis = frozenset(
37 "7_pre1",
38 ]
39 )
40 +
41 +from itertools import chain
42 +
43 _supported_eapis = frozenset(
44 - [str(x) for x in range(portage.const.EAPI + 1)]
45 - + list(_testing_eapis)
46 - + list(_deprecated_eapis)
47 + chain(
48 + (str(x) for x in range(portage.const.EAPI + 1)),
49 + _testing_eapis,
50 + _deprecated_eapis,
51 + )
52 )
53
54
55 @@ -672,8 +677,7 @@ def create_trees(
56 # When ROOT != "/" we only want overrides from the calling
57 # environment to apply to the config that's associated
58 # with ROOT != "/", so pass a nearly empty dict for the env parameter.
59 - clean_env = {}
60 - for k in (
61 + env_sequence = (
62 "PATH",
63 "PORTAGE_GRPNAME",
64 "PORTAGE_REPOSITORIES",
65 @@ -687,10 +691,10 @@ def create_trees(
66 "https_proxy",
67 "no_proxy",
68 "__PORTAGE_TEST_HARDLINK_LOCKS",
69 - ):
70 - v = settings.get(k)
71 - if v is not None:
72 - clean_env[k] = v
73 + )
74 + env = ((k, settings.get(k)) for k in env_sequence)
75 + clean_env = {k: v for k, v in env if v is not None}
76 +
77 if depcachedir is not None:
78 clean_env["PORTAGE_DEPCACHEDIR"] = depcachedir
79 settings = config(
80
81 diff --git a/lib/portage/data.py b/lib/portage/data.py
82 index e1457c566..8c58ad0fc 100644
83 --- a/lib/portage/data.py
84 +++ b/lib/portage/data.py
85 @@ -229,27 +229,30 @@ def _get_global(k):
86 # Get a list of group IDs for the portage user. Do not use
87 # grp.getgrall() since it is known to trigger spurious
88 # SIGPIPE problems with nss_ldap.
89 - cmd = ["id", "-G", _portage_username]
90 -
91 encoding = portage._encodings["content"]
92 - cmd = [
93 + cmd = (
94 portage._unicode_encode(x, encoding=encoding, errors="strict")
95 - for x in cmd
96 - ]
97 + for x in ("id", "-G", _portage_username)
98 + )
99 proc = subprocess.Popen(
100 cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
101 )
102 myoutput = proc.communicate()[0]
103 status = proc.wait()
104 if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK:
105 - for x in portage._unicode_decode(
106 - myoutput, encoding=encoding, errors="strict"
107 - ).split():
108 +
109 + def check(x):
110 try:
111 - v.append(int(x))
112 + return int(x)
113 except ValueError:
114 - pass
115 - v = sorted(set(v))
116 + return None
117 +
118 + unicode_decode = portage._unicode_decode(
119 + myoutput, encoding=encoding, errors="strict"
120 + )
121 + checked_v = (check(x) for x in unicode_decode.split())
122 + filtered_v = (x for x in checked_v if x)
123 + v = sorted(set(filtered_v))
124
125 # Avoid instantiating portage.settings when the desired
126 # variable is set in os.environ.
127
128 diff --git a/lib/portage/dispatch_conf.py b/lib/portage/dispatch_conf.py
129 index 6c6036c4e..c89caf087 100644
130 --- a/lib/portage/dispatch_conf.py
131 +++ b/lib/portage/dispatch_conf.py
132 @@ -37,7 +37,7 @@ def diffstatusoutput(cmd, file1, file2):
133 # raise a UnicodeDecodeError which makes the output inaccessible.
134 args = shlex_split(cmd % (file1, file2))
135
136 - args = [portage._unicode_encode(x, errors="strict") for x in args]
137 + args = (portage._unicode_encode(x, errors="strict") for x in args)
138 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
139 output = portage._unicode_decode(proc.communicate()[0])
140 if output and output[-1] == "\n":
141
142 diff --git a/lib/portage/eclass_cache.py b/lib/portage/eclass_cache.py
143 index f1729326d..2545ed9b2 100644
144 --- a/lib/portage/eclass_cache.py
145 +++ b/lib/portage/eclass_cache.py
146 @@ -110,7 +110,7 @@ class cache:
147 master_eclasses = {}
148 eclass_len = len(".eclass")
149 ignored_listdir_errnos = (errno.ENOENT, errno.ENOTDIR)
150 - for x in [normalize_path(os.path.join(y, "eclass")) for y in self.porttrees]:
151 + for x in (normalize_path(os.path.join(y, "eclass")) for y in self.porttrees):
152 try:
153 eclass_filenames = os.listdir(x)
154 except OSError as e:
155
156 diff --git a/lib/portage/getbinpkg.py b/lib/portage/getbinpkg.py
157 index 1e89119fb..bf99fd2be 100644
158 --- a/lib/portage/getbinpkg.py
159 +++ b/lib/portage/getbinpkg.py
160 @@ -112,10 +112,12 @@ class ParseLinks(html_parser_HTMLParser):
161
162 def handle_starttag(self, tag, attrs):
163 if tag == "a":
164 - for x in attrs:
165 - if x[0] == "href":
166 - if x[1] not in self.PL_anchors:
167 - self.PL_anchors.append(urllib_parse_unquote(x[1]))
168 + myarchors = (
169 + urllib_parse_unquote(x[1])
170 + for x in attrs
171 + if x[0] == "href" and x[1] not in self.PL_anchors
172 + )
173 + self.PL_anchors.extend(myarchors)
174
175
176 def create_conn(baseurl, conn=None):
177 @@ -533,8 +535,7 @@ def file_get(
178 from portage.util import varexpand
179 from portage.process import spawn
180
181 - myfetch = portage.util.shlex_split(fcmd)
182 - myfetch = [varexpand(x, mydict=variables) for x in myfetch]
183 + myfetch = (varexpand(x, mydict=variables) for x in portage.util.shlex_split(fcmd))
184 fd_pipes = {
185 0: portage._get_stdin().fileno(),
186 1: sys.__stdout__.fileno(),
187 @@ -986,5 +987,5 @@ class PackageIndex:
188 keys = list(metadata)
189 keys.sort()
190 self._writepkgindex(
191 - pkgfile, [(k, metadata[k]) for k in keys if metadata[k]]
192 + pkgfile, ((k, metadata[k]) for k in keys if metadata[k])
193 )
194
195 diff --git a/lib/portage/glsa.py b/lib/portage/glsa.py
196 index 05de3ade6..4b92d52e0 100644
197 --- a/lib/portage/glsa.py
198 +++ b/lib/portage/glsa.py
199 @@ -126,14 +126,20 @@ def get_glsa_list(myconfig):
200 return []
201 dirlist = os.listdir(repository)
202 prefix = "glsa-"
203 + prefix_size = len(prefix)
204 suffix = ".xml"
205 + suffix_size = len(suffix)
206
207 - for f in dirlist:
208 + def check(value):
209 try:
210 - if f[: len(prefix)] == prefix and f[-1 * len(suffix) :] == suffix:
211 - rValue.append(f[len(prefix) : -1 * len(suffix)])
212 + if value[:prefix_size] == prefix and value[-suffix_size:] == suffix:
213 + return value[prefix_size:-suffix_size]
214 except IndexError:
215 - pass
216 + return None
217 + return None
218 +
219 + checked_dirlist = (check(f) for f in dirlist)
220 + rValue = [f for f in checked_dirlist if f]
221 return rValue