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/, lib/portage/util/
Date: Sat, 04 Mar 2023 02:56:51
Message-Id: 1677898594.e42940471a16159802bc99a542a5624d9d46251e.sam@gentoo
1 commit: e42940471a16159802bc99a542a5624d9d46251e
2 Author: Siddhanth Rathod <xsiddhanthrathod <AT> gmail <DOT> com>
3 AuthorDate: Wed Mar 1 17:55:48 2023 +0000
4 Commit: Sam James <sam <AT> gentoo <DOT> org>
5 CommitDate: Sat Mar 4 02:56:34 2023 +0000
6 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=e4294047
7
8 portage: news: further type annotations
9
10 Signed-off-by: Siddhanth Rathod <xsiddhanthrathod <AT> gmail.com>
11 Closes: https://github.com/gentoo/portage/pull/1002
12 Signed-off-by: Sam James <sam <AT> gentoo.org>
13
14 lib/portage/news.py | 52 ++++++++++++++++++++++----------------------
15 lib/portage/util/__init__.py | 42 +++++++++++++++++------------------
16 2 files changed, 47 insertions(+), 47 deletions(-)
17
18 diff --git a/lib/portage/news.py b/lib/portage/news.py
19 index 68f0c72d3..b43d81fb7 100644
20 --- a/lib/portage/news.py
21 +++ b/lib/portage/news.py
22 @@ -15,7 +15,7 @@ __all__ = [
23
24 from collections import OrderedDict
25 from typing import TYPE_CHECKING, Any, Dict, List, Optional
26 -
27 +from typing import Pattern, Match
28 import fnmatch
29 import logging
30 import os as _os
31 @@ -71,7 +71,7 @@ class NewsManager:
32 news_path: str,
33 unread_path: str,
34 language_id: str = "en",
35 - ):
36 + ) -> None:
37 self.news_path = news_path
38 self.unread_path = unread_path
39 self.language_id = language_id
40 @@ -89,11 +89,11 @@ class NewsManager:
41 self._dir_mode = 0o0074
42 self._mode_mask = 0o0000
43
44 - portdir = portdb.repositories.mainRepoLocation()
45 - profiles_base = None
46 + portdir: Optional[str] = portdb.repositories.mainRepoLocation()
47 + profiles_base: Optional[str] = None
48 if portdir is not None:
49 profiles_base = os.path.join(portdir, ("profiles" + os.path.sep))
50 - profile_path = None
51 + profile_path: Optional[str] = None
52 if profiles_base is not None and portdb.settings.profile_path:
53 profile_path = normalize_path(
54 os.path.realpath(portdb.settings.profile_path)
55 @@ -109,7 +109,7 @@ class NewsManager:
56 return os.path.join(self.unread_path, f"news-{repoid}.skip")
57
58 def _news_dir(self, repoid: str) -> str:
59 - repo_path = self.portdb.getRepositoryPath(repoid)
60 + repo_path: Optional[str] = self.portdb.getRepositoryPath(repoid)
61 if repo_path is None:
62 raise AssertionError(_(f"Invalid repoID: {repoid}"))
63 return os.path.join(repo_path, self.news_path)
64 @@ -137,23 +137,23 @@ class NewsManager:
65 if not os.access(self.unread_path, os.W_OK):
66 return
67
68 - news_dir = self._news_dir(repoid)
69 + news_dir: str = self._news_dir(repoid)
70 try:
71 - news = _os.listdir(
72 + news: list[str] = _os.listdir(
73 _unicode_encode(news_dir, encoding=_encodings["fs"], errors="strict")
74 )
75 except OSError:
76 return
77
78 - skip_filename = self._skip_filename(repoid)
79 - unread_filename = self._unread_filename(repoid)
80 - unread_lock = lockfile(unread_filename, wantnewlockfile=1)
81 + skip_filename: str = self._skip_filename(repoid)
82 + unread_filename: str = self._unread_filename(repoid)
83 + unread_lock: Optional[bool] = lockfile(unread_filename, wantnewlockfile=1)
84 try:
85 try:
86 - unread = set(grabfile(unread_filename))
87 - unread_orig = unread.copy()
88 - skip = set(grabfile(skip_filename))
89 - skip_orig = skip.copy()
90 + unread: set[str | tuple[str, str]] = set(grabfile(unread_filename))
91 + unread_orig: set[str | tuple[str, str]] = unread.copy()
92 + skip: set[str | tuple[str, str]] = set(grabfile(skip_filename))
93 + skip_orig: set[str | tuple[str, str]] = skip.copy()
94 except PermissionDenied:
95 return
96
97 @@ -224,7 +224,7 @@ class NewsManager:
98 self.updateItems(repoid)
99
100 unread_filename = self._unread_filename(repoid)
101 - unread_lock = None
102 + unread_lock: Optional[bool] = None
103 try:
104 unread_lock = lockfile(unread_filename, wantnewlockfile=1)
105 except (
106 @@ -244,11 +244,11 @@ class NewsManager:
107 unlockfile(unread_lock)
108
109
110 -_formatRE = re.compile(r"News-Item-Format:\s*([^\s]*)\s*$")
111 -_installedRE = re.compile("Display-If-Installed:(.*)\n")
112 -_profileRE = re.compile("Display-If-Profile:(.*)\n")
113 -_keywordRE = re.compile("Display-If-Keyword:(.*)\n")
114 -_valid_profile_RE = re.compile(r"^[^*]+(/\*)?$")
115 +_formatRE: Pattern[str] = re.compile(r"News-Item-Format:\s*([^\s]*)\s*$")
116 +_installedRE: Pattern[str] = re.compile("Display-If-Installed:(.*)\n")
117 +_profileRE: Pattern[str] = re.compile("Display-If-Profile:(.*)\n")
118 +_keywordRE: Pattern[str] = re.compile("Display-If-Keyword:(.*)\n")
119 +_valid_profile_RE: Pattern[str] = re.compile(r"^[^*]+(/\*)?$")
120
121
122 class NewsItem:
123 @@ -295,9 +295,9 @@ class NewsItem:
124 if not len(self.restrictions):
125 return True
126
127 - kwargs = {"vardb": vardb, "config": config, "profile": profile}
128 + kwargs: dict[str, Any] = {"vardb": vardb, "config": config, "profile": profile}
129
130 - all_match = True
131 + all_match: bool = True
132 for values in self.restrictions.values():
133 matches = [restriction.checkRestriction(**kwargs) for restriction in values]
134 any_match = any(matches)
135 @@ -324,11 +324,11 @@ class NewsItem:
136 lines = f.readlines()
137 self.restrictions = {}
138 invalids = []
139 - news_format = None
140 + news_format: Optional[str] = None
141
142 # Look for News-Item-Format
143 for i, line in enumerate(lines):
144 - format_match = _formatRE.match(line)
145 + format_match: Optional[Match[str]] = _formatRE.match(line)
146 if format_match is not None:
147 news_format = format_match.group(1)
148 if fnmatch.fnmatch(news_format, "[12].*"):
149 @@ -445,7 +445,7 @@ class DisplayInstalledRestriction(DisplayRestriction):
150 return isvalidatom(self.atom, eapi="5")
151 return isvalidatom(self.atom)
152
153 - def checkRestriction(self, **kwargs) -> bool:
154 + def checkRestriction(self, **kwargs) -> Optional[Match[str]]:
155 return kwargs["vardb"].match(self.atom)
156
157
158
159 diff --git a/lib/portage/util/__init__.py b/lib/portage/util/__init__.py
160 index 59db742d7..4d50088e7 100644
161 --- a/lib/portage/util/__init__.py
162 +++ b/lib/portage/util/__init__.py
163 @@ -1,6 +1,26 @@
164 # Copyright 2004-2020 Gentoo Authors
165 # Distributed under the terms of the GNU General Public License v2
166
167 +from portage.cache.mappings import UserDict
168 +from portage.proxy.objectproxy import ObjectProxy
169 +from portage.localization import _
170 +from portage.exception import (
171 + InvalidAtom,
172 + PortageException,
173 + FileNotFound,
174 + IsADirectory,
175 + OperationNotPermitted,
176 + ParseError,
177 + PermissionDenied,
178 + ReadOnlyFileSystem,
179 +)
180 +from portage.const import VCS_DIRS
181 +from portage import _unicode_decode
182 +from portage import _unicode_encode
183 +from portage import _os_merge
184 +from portage import _encodings
185 +from portage import os
186 +
187 __all__ = [
188 "apply_permissions",
189 "apply_recursive_permissions",
190 @@ -61,26 +81,6 @@ portage.proxy.lazyimport.lazyimport(
191 "subprocess",
192 )
193
194 -from portage import os
195 -from portage import _encodings
196 -from portage import _os_merge
197 -from portage import _unicode_encode
198 -from portage import _unicode_decode
199 -from portage.const import VCS_DIRS
200 -from portage.exception import (
201 - InvalidAtom,
202 - PortageException,
203 - FileNotFound,
204 - IsADirectory,
205 - OperationNotPermitted,
206 - ParseError,
207 - PermissionDenied,
208 - ReadOnlyFileSystem,
209 -)
210 -from portage.localization import _
211 -from portage.proxy.objectproxy import ObjectProxy
212 -from portage.cache.mappings import UserDict
213 -
214
215 noiselimit = 0
216
217 @@ -142,7 +142,7 @@ def writemsg_level(msg, level=0, noiselevel=0):
218 writemsg(msg, noiselevel=noiselevel, fd=fd)
219
220
221 -def normalize_path(mypath):
222 +def normalize_path(mypath) -> str:
223 """
224 os.path.normpath("//foo") returns "//foo" instead of "/foo"
225 We dislike this behavior so we create our own normpath func