forked from AlmaLinux/mirrors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrors_table_update.py
executable file
·49 lines (44 loc) · 1.84 KB
/
mirrors_table_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
import yaml
import os
import sys
# read config
with open('config.yml') as f:
config = yaml.safe_load(f)
mirrorlist_dir = config['mirrorlist_dir']
mirrors_dir = config['mirrors_dir']
# read and verify mirrors
all_mirrors = []
for mirror_file in os.listdir(mirrors_dir):
with open(mirrors_dir + '/' + mirror_file) as f:
# filter broken or unavailable mirrors
try:
mirror = yaml.safe_load(f)
mirror['address']
mirror['name']
except:
print('Cannot load mirror data from file ' + mirror_file)
continue
all_mirrors.append(mirror)
# exit if no mirrors found
if all_mirrors == []:
sys.exit('No mirrors found')
# remove md table file if exists
if os.path.exists(config['mirrors_table']): os.remove(config['mirrors_table'])
# genetate md table
with open(config['mirrors_table'], 'a') as f:
print('| Name | Sponsor | HTTP | HTTPS | RSYNC |\n| --- | --- | --- | --- | --- |', file=f)
for mirror in all_mirrors:
print('Adding ' + mirror['name'])
for protocol in ['http', 'https']:
try:
mirror[protocol + '_link'] = '[Mirror](' + mirror['address'][protocol] + ')'
except:
mirror[protocol + '_link'] = ''
for protocol in ['rsync']:
try:
mirror[protocol + '_link'] = '[Link](' + mirror['address'][protocol] + ')'
except:
mirror[protocol + '_link'] = ''
# print('|' + mirror['name'] + '| [' + mirror['sponsor'] + '](' + mirror['sponsor_url'] + ')|' + mirror['https_link'] + '|' + mirror['http_link'] + '|' + mirror['rsync_link'] + '|', file=f)
print("|%s|[%s](%s)|%s|%s|%s|" % (mirror['name'],mirror['sponsor'],mirror['sponsor_url'],mirror['http_link'],mirror['https_link'],mirror['rsync_link']), file=f)