Created
September 20, 2018 14:58
-
-
Save kennethso168/a0e1ca045b21f12f0dbb27517f79a27f to your computer and use it in GitHub Desktop.
Manjaro Mirror Evaluation: see https://forum.manjaro.org/t/wrote-a-script-to-filter-the-mirrors-from-the-mirrorlist-based-on-their-status/58438 for more details
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
MIRRORLIST_FILE = '/etc/pacman.d/mirrorlist' | |
ENFORCE_ROOT = True | |
BRANCH = 'stable' | |
# A mirror that is assumed to be always available and is the most up-to-date | |
MOST_UPDATE_MIRROR='https://mirror.netzspielplatz.de/manjaro/packages' | |
# How many seconds before the connection timeouts (i.e. the mirror is considered as unreachable) | |
TIMEOUT=10 | |
import os, sys, requests, re, datetime | |
# extract the date and time of last change from the text of a BoxIt branch state file | |
def get_time_from_state(text): | |
last_update_time_regex = re.compile(r'date=(2.*)Z') | |
last_update_time_str = last_update_time_regex.search(text).group(1) | |
last_update_time = datetime.datetime.fromisoformat(last_update_time_str) | |
return last_update_time | |
def show_help(): | |
print("Usage: mirror_eval.py [arg]\n" | |
"Arguments\n" | |
"-h, --help, <unregconized arguments>: show help\n" | |
"-l, --list: try current mirrors in the mirrorlist and return their status\n" | |
"-f, --filter: comment out mirrors that are not up to date\n" | |
"-r, --revert: uncomment" | |
) | |
def check_sudo(): | |
if ENFORCE_ROOT and os.geteuid() != 0: | |
print("Error: superuser permissions are required") | |
sys.exit(1) | |
# Get the last updated time from the assumed most up-to-date mirror | |
def get_last_update_time(): | |
req = requests.get(MOST_UPDATE_MIRROR + '/' + BRANCH + '/state', timeout=TIMEOUT) | |
last_update_time = get_time_from_state(str(req.text)) | |
req.raise_for_status() | |
print(f'The {BRANCH} branch is last updated at {last_update_time}') | |
return last_update_time | |
if len(sys.argv) > 1 and sys.argv[1] in ['-l', '--list', '-f', '--filter']: | |
if sys.argv[1] in ['-f', '--filter']: | |
check_sudo() | |
last_update_time = get_last_update_time() | |
server_regex = re.compile(r'Server = (.*)/(stable|testing|unstable)') | |
with open(MIRRORLIST_FILE, 'r') as f: | |
lines = f.readlines() | |
for i, line in enumerate(lines): | |
match = server_regex.match(line) | |
if match: | |
mirror = match.group(1) | |
try: | |
print(f'{mirror}: ', end='', flush=True) | |
mirror_req = requests.get(mirror + '/' + BRANCH + '/state', timeout=TIMEOUT) | |
mirror_req.raise_for_status() | |
mirror_time = get_time_from_state(mirror_req.text) | |
if mirror_time == last_update_time: | |
print(f'at {mirror_time} - in sync') | |
else: | |
print(f'at {mirror_time} - OUT OF SYNC') | |
if sys.argv[1] in ['-f', '--filter']: | |
print('Disabling mirror') | |
lines[i] = '#' + lines[i] | |
except requests.exceptions.RequestException: | |
print(f'ERROR: Not reachable') | |
if sys.argv[1] in ['-f', '--filter']: | |
print('Disabling mirror') | |
lines[i] = '#' + lines[i] | |
if sys.argv[1] in ['-f', '--filter'] and lines != None: | |
with open(MIRRORLIST_FILE, 'w') as f: | |
f.writelines(lines) | |
elif len(sys.argv) > 1 and sys.argv[1] in ['-r', '--revert']: | |
check_sudo() | |
with open(MIRRORLIST_FILE, 'r') as f: | |
lines = f.readlines() | |
for i, line in enumerate(lines): | |
if line.startswith('#Server'): | |
lines[i] = line[1:] | |
print(f'Reverting {lines[i]}', end='') | |
if lines != None: | |
with open(MIRRORLIST_FILE, 'w') as f: | |
f.writelines(lines) | |
else: | |
show_help() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment