@@ -145,7 +145,11 @@ def __eq__(self, other: object) -> bool:
145
145
146
146
147
147
class MirrorListHandler :
148
- def __init__ (self ) -> None :
148
+ def __init__ (
149
+ self ,
150
+ local_mirrorlist : Path = Path ('/etc/pacman.d/mirrorlist' ),
151
+ ) -> None :
152
+ self ._local_mirrorlist = local_mirrorlist
149
153
self ._status_mappings : dict [str , list [MirrorStatusEntryV3 ]] | None = None
150
154
151
155
def _mappings (self ) -> dict [str , list [MirrorStatusEntryV3 ]]:
@@ -190,7 +194,7 @@ def load_remote_mirrors(self) -> bool:
190
194
return False
191
195
192
196
def load_local_mirrors (self ) -> None :
193
- with Path ( '/etc/pacman.d/mirrorlist' ) .open ('r' ) as fp :
197
+ with self . _local_mirrorlist .open ('r' ) as fp :
194
198
mirrorlist = fp .read ()
195
199
self ._status_mappings = self ._parse_locale_mirrors (mirrorlist )
196
200
@@ -236,23 +240,26 @@ def _parse_locale_mirrors(self, mirrorlist: str) -> dict[str, list[MirrorStatusE
236
240
lines = mirrorlist .splitlines ()
237
241
238
242
# remove empty lines
239
- lines = [line for line in lines if line ]
243
+ # lines = [line for line in lines if line]
240
244
241
245
mirror_list : dict [str , list [MirrorStatusEntryV3 ]] = {}
242
246
243
247
current_region = ''
244
- for idx , line in enumerate (lines ):
248
+
249
+ for line in lines :
245
250
line = line .strip ()
246
251
247
- if line .lower ().startswith ('server' ):
252
+ if line .startswith ('## ' ):
253
+ current_region = line .replace ('## ' , '' ).strip ()
254
+ mirror_list .setdefault (current_region , [])
255
+
256
+ if line .startswith ('Server = ' ):
248
257
if not current_region :
249
- for i in range (idx - 1 , 0 , - 1 ):
250
- if lines [i ].startswith ('##' ):
251
- current_region = lines [i ].replace ('#' , '' ).strip ()
252
- mirror_list .setdefault (current_region , [])
253
- break
258
+ current_region = 'Local'
259
+ mirror_list .setdefault (current_region , [])
254
260
255
261
url = line .removeprefix ('Server = ' )
262
+
256
263
mirror_entry = MirrorStatusEntryV3 (
257
264
url = url .removesuffix ('$repo/os/$arch' ),
258
265
protocol = urllib .parse .urlparse (url ).scheme ,
@@ -267,6 +274,7 @@ def _parse_locale_mirrors(self, mirrorlist: str) -> dict[str, list[MirrorStatusE
267
274
ipv6 = True ,
268
275
details = 'Locally defined mirror' ,
269
276
)
277
+
270
278
mirror_list [current_region ].append (mirror_entry )
271
279
272
280
return mirror_list
0 commit comments