mirror-checker/main.py

51 lines
1.6 KiB
Python
Raw Normal View History

2021-08-16 18:23:04 -04:00
#!/usr/bin/env python3
"""
This mirror status checker determines whether CSC mirror is up-to-date with upstream
"""
2021-09-01 23:25:06 -04:00
import time
import sys
2021-08-16 18:23:04 -04:00
import requests
2021-10-03 16:43:06 -04:00
2021-10-17 23:50:21 -04:00
from projects import *
import json
2021-10-03 15:35:17 -04:00
2021-08-19 17:00:55 -04:00
2021-08-16 18:23:04 -04:00
if __name__ == "__main__":
2021-09-01 23:25:06 -04:00
2021-10-03 15:35:17 -04:00
with open("data.json", "r", encoding="utf-8") as file:
data = json.load(file)
if sys.stdin.isatty():
2021-10-03 15:44:08 -04:00
projects = data
2021-10-03 15:35:17 -04:00
else:
2021-10-03 15:44:08 -04:00
projects = [project.rstrip() for project in sys.stdin.readlines()]
2021-10-03 15:35:17 -04:00
current_time = int(time.time())
2021-10-03 15:44:08 -04:00
for project in projects:
2021-10-03 15:35:17 -04:00
try:
2021-10-03 15:44:08 -04:00
if project not in data:
print(f"Failure: {project} does not exist")
2021-10-03 15:35:17 -04:00
continue
2021-10-03 15:44:08 -04:00
project_class = getattr(sys.modules[__name__], project)
if project in ["CPAN", "ubuntu", "ubuntu_releases", "manjaro", "mxlinux", "cran", "ctan", "gentooportage"]:
2021-10-03 15:44:08 -04:00
checker_result = project_class.check(data, project, current_time)
2021-10-03 15:35:17 -04:00
if checker_result:
2021-10-03 15:44:08 -04:00
print(f"Success: {project} up-to-date")
2021-10-03 15:35:17 -04:00
else:
2021-10-03 15:44:08 -04:00
print(f"Failure: {project} out-of-sync")
2021-10-03 15:35:17 -04:00
continue
2021-10-03 15:44:08 -04:00
checker_result = project_class.check(data, project)
2021-10-03 15:35:17 -04:00
if checker_result:
2021-10-03 15:44:08 -04:00
data[project]["out_of_sync_since"] = None
elif data[project]["out_of_sync_since"] is None:
data[project]["out_of_sync_since"] = current_time
elif current_time - data[project]["out_of_sync_since"] \
2021-10-03 15:56:06 -04:00
> data[project]["out_of_sync_interval"]:
2021-10-03 15:44:08 -04:00
print(f"Failure: {project} out-of-sync")
2021-10-03 15:35:17 -04:00
continue
2021-10-03 15:44:08 -04:00
print(f"Success: {project} up-to-date")
2021-10-03 15:35:17 -04:00
except requests.exceptions.RequestException as err:
2021-10-03 15:44:08 -04:00
print(f"Error: {project}\n{err}")
2021-10-03 15:35:17 -04:00
with open("data.json", "w", encoding="utf-8") as file:
2021-10-03 16:43:06 -04:00
json.dump(data, file, indent='\t')