-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_mirror_stats.py
executable file
·44 lines (35 loc) · 1.13 KB
/
update_mirror_stats.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
#!/usr/bin/env python
import json
import os
import subprocess
import tempfile
try:
with open("/home/mirror/web/stats.json") as fp:
stats = json.load(fp)
except IOError:
print("Couldn't open stats.json! Resetting database!")
stats = {}
modules = [] # eh.
def explore(path_array):
path = os.path.join('/pool/mirrors', *path_array)
size_path = os.path.join(path, "mirror.umd.edu.size.txt")
if path_array and os.path.exists(size_path):
modules.append((path_array[-1], path))
elif len(path_array) <= 2:
for entry in os.listdir(path):
if os.path.isdir(os.path.join(path, entry)):
explore(path_array + [entry])
explore([])
for module_name, module_path in modules:
if not module_name in stats:
stats[module_name] = {}
try:
size_line = open(os.path.join(module_path, "mirror.umd.edu.size.txt"), "r").read().strip()
size = int(size_line.split(' ')[0])
stats[module_name]['size'] = size
except IOError:
pass
stats_file = open("/home/mirror/web/stats.json.tmp", "w")
json.dump(stats, stats_file)
stats_file.close()
os.rename("/home/mirror/web/stats.json.tmp", "/home/mirror/web/stats.json")