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: Mon, 04 Apr 2022 19:05:06
Message-Id: 1649099085.64d84ce2d9a333e83e2a5fba5e7ec95f936959e7.sam@gentoo
1 commit: 64d84ce2d9a333e83e2a5fba5e7ec95f936959e7
2 Author: Kenneth Raplee <kenrap <AT> kennethraplee <DOT> com>
3 AuthorDate: Sat Apr 2 01:32:42 2022 +0000
4 Commit: Sam James <sam <AT> gentoo <DOT> org>
5 CommitDate: Mon Apr 4 19:04:45 2022 +0000
6 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=64d84ce2
7
8 Miscellaneous refactors and cleanups
9
10 Signed-off-by: Kenneth Raplee <kenrap <AT> kennethraplee.com>
11 Closes: https://github.com/gentoo/portage/pull/798
12 Signed-off-by: Sam James <sam <AT> gentoo.org>
13
14 lib/portage/manifest.py | 89 +++++++++++++++++++++++++------------------------
15 lib/portage/news.py | 9 +++--
16 lib/portage/output.py | 2 +-
17 3 files changed, 50 insertions(+), 50 deletions(-)
18
19 diff --git a/lib/portage/manifest.py b/lib/portage/manifest.py
20 index 655eabf68..eb3695669 100644
21 --- a/lib/portage/manifest.py
22 +++ b/lib/portage/manifest.py
23 @@ -83,13 +83,13 @@ def parseManifest2(line):
24 if not isinstance(line, str):
25 line = " ".join(line)
26 myentry = None
27 - match = _manifest_re.match(line)
28 - if match is not None:
29 - tokens = match.group(3).split()
30 + matched = _manifest_re.match(line)
31 + if matched:
32 + tokens = matched.group(3).split()
33 hashes = dict(zip(tokens[1::2], tokens[2::2]))
34 hashes["size"] = int(tokens[0])
35 myentry = Manifest2Entry(
36 - type=match.group(1), name=match.group(2), hashes=hashes
37 + type=matched.group(1), name=matched.group(2), hashes=hashes
38 )
39 return myentry
40
41 @@ -274,19 +274,21 @@ class Manifest:
42
43 def _createManifestEntries(self):
44 valid_hashes = set(itertools.chain(get_valid_checksum_keys(), ("size")))
45 - mytypes = list(self.fhashdict)
46 - mytypes.sort()
47 - for t in mytypes:
48 - myfiles = list(self.fhashdict[t])
49 - myfiles.sort()
50 - for f in myfiles:
51 - myentry = Manifest2Entry(
52 - type=t, name=f, hashes=self.fhashdict[t][f].copy()
53 + mytypes = sorted(self.fhashdict)
54 + for mytype in mytypes:
55 + myfiles = sorted(self.fhashdict[mytype])
56 + for myfile in myfiles:
57 + remainings = set(self.fhashdict[mytype][myfile]).intersection(
58 + valid_hashes
59 + )
60 + yield Manifest2Entry(
61 + type=mytype,
62 + name=myfile,
63 + hashes={
64 + remaining: self.fhashdict[mytype][myfile][remaining]
65 + for remaining in remainings
66 + },
67 )
68 - for h in list(myentry.hashes):
69 - if h not in valid_hashes:
70 - del myentry.hashes[h]
71 - yield myentry
72
73 def checkIntegrity(self):
74 manifest_data = (
75 @@ -320,7 +322,7 @@ class Manifest:
76 preserved_stats = {self.pkgdir.rstrip(os.sep): os.stat(self.pkgdir)}
77 if myentries and not force:
78 try:
79 - f = io.open(
80 + with io.open(
81 _unicode_encode(
82 self.getFullname(),
83 encoding=_encodings["fs"],
84 @@ -329,16 +331,15 @@ class Manifest:
85 mode="r",
86 encoding=_encodings["repo.content"],
87 errors="replace",
88 - )
89 - oldentries = list(self._parseManifestLines(f))
90 - preserved_stats[self.getFullname()] = os.fstat(f.fileno())
91 - f.close()
92 - if len(oldentries) == len(myentries):
93 - update_manifest = False
94 - for i in range(len(oldentries)):
95 - if oldentries[i] != myentries[i]:
96 - update_manifest = True
97 - break
98 + ) as f:
99 + oldentries = list(self._parseManifestLines(f))
100 + preserved_stats[self.getFullname()] = os.fstat(f.fileno())
101 + if len(oldentries) == len(myentries):
102 + update_manifest = False
103 + for oldentry, myentry in zip(oldentries, myentries):
104 + if oldentry != myentry:
105 + update_manifest = True
106 + break
107 except (IOError, OSError) as e:
108 if e.errno == errno.ENOENT:
109 pass
110 @@ -463,16 +464,17 @@ class Manifest:
111
112 def addFile(self, ftype, fname, hashdict=None, ignoreMissing=False):
113 """Add entry to Manifest optionally using hashdict to avoid recalculation of hashes"""
114 - if ftype == "AUX" and not fname.startswith("files/"):
115 - fname = os.path.join("files", fname)
116 - if not os.path.exists(self.pkgdir + fname) and not ignoreMissing:
117 + if ftype == "AUX":
118 + if not fname.startswith("files/"):
119 + fname = os.path.join("files", fname)
120 + if fname.startswith("files"):
121 + fname = fname[6:]
122 + if not os.path.exists(f"{self.pkgdir}{fname}") and not ignoreMissing:
123 raise FileNotFound(fname)
124 - if not ftype in MANIFEST2_IDENTIFIERS:
125 + if ftype not in MANIFEST2_IDENTIFIERS:
126 raise InvalidDataType(ftype)
127 - if ftype == "AUX" and fname.startswith("files"):
128 - fname = fname[6:]
129 self.fhashdict[ftype][fname] = {}
130 - if hashdict != None:
131 + if hashdict is not None:
132 self.fhashdict[ftype][fname].update(hashdict)
133 if self.required_hashes.difference(set(self.fhashdict[ftype][fname])):
134 self.updateFileHashes(
135 @@ -497,7 +499,7 @@ class Manifest:
136 checkExisting=False,
137 assumeDistHashesSometimes=False,
138 assumeDistHashesAlways=False,
139 - requiredDistfiles=[],
140 + requiredDistfiles=None,
141 ):
142 """Recreate this Manifest from scratch. This will not use any
143 existing checksums unless assumeDistHashesSometimes or
144 @@ -511,10 +513,9 @@ class Manifest:
145 return
146 if checkExisting:
147 self.checkAllHashes()
148 + distfilehashes = {}
149 if assumeDistHashesSometimes or assumeDistHashesAlways:
150 - distfilehashes = self.fhashdict["DIST"]
151 - else:
152 - distfilehashes = {}
153 + distfilehashes.update(self.fhashdict["DIST"])
154 self.__init__(
155 self.pkgdir,
156 distdir=self.distdir,
157 @@ -624,7 +625,7 @@ class Manifest:
158 f = _unicode_decode(f, encoding=_encodings["fs"], errors="strict")
159 except UnicodeDecodeError:
160 continue
161 - if f[:1] == ".":
162 + if f.startswith("."):
163 continue
164 pf = self._is_cpv(cat, pn, f)
165 if pf is not None:
166 @@ -640,7 +641,7 @@ class Manifest:
167 recursive_files = []
168
169 pkgdir = self.pkgdir
170 - cut_len = len(os.path.join(pkgdir, "files") + os.sep)
171 + cut_len = len(os.path.join(pkgdir, "files", os.sep))
172 for parentdir, dirs, files in os.walk(os.path.join(pkgdir, "files")):
173 for f in files:
174 try:
175 @@ -662,12 +663,12 @@ class Manifest:
176
177 def _getAbsname(self, ftype, fname):
178 if ftype == "DIST":
179 - absname = os.path.join(self.distdir, fname)
180 + abspath = (self.distdir, fname)
181 elif ftype == "AUX":
182 - absname = os.path.join(self.pkgdir, "files", fname)
183 + abspath = (self.pkgdir, "files", fname)
184 else:
185 - absname = os.path.join(self.pkgdir, fname)
186 - return absname
187 + abspath = (self.pkgdir, fname)
188 + return os.path.join(*abspath)
189
190 def checkAllHashes(self, ignoreMissingFiles=False):
191 for t in MANIFEST2_IDENTIFIERS:
192
193 diff --git a/lib/portage/news.py b/lib/portage/news.py
194 index 801edb68c..132e050f2 100644
195 --- a/lib/portage/news.py
196 +++ b/lib/portage/news.py
197 @@ -80,7 +80,7 @@ class NewsManager:
198 portdir = portdb.repositories.mainRepoLocation()
199 profiles_base = None
200 if portdir is not None:
201 - profiles_base = os.path.join(portdir, "profiles") + os.path.sep
202 + profiles_base = os.path.join(portdir, "profiles", os.path.sep)
203 profile_path = None
204 if profiles_base is not None and portdb.settings.profile_path:
205 profile_path = normalize_path(
206 @@ -295,14 +295,13 @@ class NewsItem:
207 return self._valid
208
209 def parse(self):
210 - f = io.open(
211 + with io.open(
212 _unicode_encode(self.path, encoding=_encodings["fs"], errors="strict"),
213 mode="r",
214 encoding=_encodings["content"],
215 errors="replace",
216 - )
217 - lines = f.readlines()
218 - f.close()
219 + ) as f:
220 + lines = f.readlines()
221 self.restrictions = {}
222 invalids = []
223 news_format = None
224
225 diff --git a/lib/portage/output.py b/lib/portage/output.py
226 index 565f782ff..c7922038e 100644
227 --- a/lib/portage/output.py
228 +++ b/lib/portage/output.py
229 @@ -35,8 +35,8 @@ from portage.localization import _
230 havecolor = 1
231 dotitles = 1
232
233 -_styles = {}
234 """Maps style class to tuple of attribute names."""
235 +_styles = {}
236
237 """Maps attribute name to ansi code."""