mirror-checker/main.py

65 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""
This mirror status checker determines whether CSC mirror is up-to-date with upstream
"""
import time
import sys
import requests
from projects import *
import json
if __name__ == "__main__":
exit_code = 0
with open("data.json", "r", encoding="utf-8") as file:
data = json.load(file)
if sys.stdin.isatty():
projects = data
else:
projects = [project.rstrip() for project in sys.stdin.readlines()]
current_time = int(time.time())
for project in projects:
try:
if project not in data:
print(f"Failure: {project} does not exist")
continue
project_class = getattr(sys.modules[__name__], project)
# Skip projects we no longer mirror
if project in ["pkgsrc", "qtproject", "DebianPorts", "OpenBSD"]:
continue
if project in ["CPAN", "ubuntu", "ubuntu_releases", "manjaro", "mxlinux", "cran", "ctan", "gentooportage"]:
checker_result = project_class.check(data, project, current_time)
if checker_result:
print(f"Success: {project} up-to-date")
else:
print(f"Failure: {project} out-of-sync")
# Exit with non-zero status if any of the projects are not up-to-date
exit_code = 1
continue
checker_result = project_class.check(data, project, current_time)
if checker_result:
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"] \
> data[project]["out_of_sync_interval"]:
print(f"Failure: {project} out-of-sync")
# Exit with non-zero status if any of the projects are not up-to-date
exit_code = 1
continue
print(f"Success: {project} up-to-date")
except requests.exceptions.RequestException as err:
print(f"Error: {project}\n{err}")
with open("data.json", "w", encoding="utf-8") as file:
json.dump(data, file, indent='\t')
sys.exit(exit_code)