[add option] Status of mirrors from local config
inspiration milcheck
add option for display "https://repo.manjaro.org/" but only from my mirrors config
return error codes 1 or 4 : run pacman-mirrors is the good moment...
standalone demo
#!/usr/bin/env python
from urllib import request
import json
C_KO = '\33[1;31m'
C_OK = '\33[1;32m'
C_NONE = '\33[0m'
def get_local_mirrors() ->list:
ret = ()
with open("/etc/pacman.d/mirrorlist", "r") as f_list:
for line in f_list:
if not line.startswith("Server"):
continue
line = line.split("=")[1].strip()
line = line.split("$")[0]
url = line.split('/')
url.pop()
branch = url.pop()
line ="/".join(url)
ret = ret + (line+"/",)
return branch, ret
def get_state(states: list, branch: str) ->str:
color = C_OK
x = states[0]
if branch=="testing":
x = states[1]
if branch=="unstable":
x = states[2]
if x == 0:
color = C_KO
return color
branch, mirrors_pacman = get_local_mirrors()
mirrors=[]
with request.urlopen('https://repo.manjaro.org/status.json') as f_url:
req = f_url.read()
mirrors = json.loads(req)
mirrors = [m for m in mirrors if m['url'] in mirrors_pacman]
print("Branch:", branch)
ret = 0
for i, url in enumerate(mirrors_pacman): # same order as pacman-conf
try:
mirror = [ m for m in mirrors if m['url'] == url][0]
color = get_state(mirror["branches"], branch)
print(color, f"{i+1:2}", C_NONE, f"{mirror['last_sync']:7} {mirror['country']:26} {mirror['url']}")
if i == 0 and color== C_KO:
ret = 1 # first mirror not sync !
except IndexError:
print(C_KO, f"{i+1:2}", C_NONE, f"{C_KO}{url}{C_NONE} not exists")
ret = 4 # not found
#print("pacman config:")
#for mirror in mirrors_pacman:
# print(" " + mirror)
exit(ret)