Commit fc864242 authored by Hugo Posnic's avatar Hugo Posnic

Add log functionality

parent c558f29f
......@@ -92,3 +92,4 @@ ENV/
# Generated files
docs/
logs/
......@@ -4,36 +4,36 @@ import json
import datetime
import os
from logger import Logger
from conf import BRANCHES, VERSION, ROOT_FOLDER, OUTPUT_FOLDER
from helpers import close
class Builder():
"""Handle generation of output files"""
def __init__(self, states):
self.logger = Logger()
self.states = states
self.check_folder()
self.json_path = ROOT_FOLDER + OUTPUT_FOLDER + "status.json"
self.html_path = ROOT_FOLDER + OUTPUT_FOLDER + "index.html"
def check_folder(self):
"""Check if output folder exists"""
try:
if not os.path.isdir(ROOT_FOLDER + OUTPUT_FOLDER):
os.makedirs(ROOT_FOLDER + OUTPUT_FOLDER)
except OSError:
print("Error: can't create output folder")
close()
except OSError as e:
self.logger.error("can't create output folder", e)
def write_json_output(self):
"""Generate JSON output"""
try:
with open(self.json_path, "w") as json_output:
json.dump(self.states, json_output, sort_keys=True)
print("JSON output saved in {}".format(self.json_path))
except OSError:
print("Error: can't write JSON output")
close()
self.logger.info("JSON output generated")
except OSError as e:
self.logger.error("can't write JSON output", e)
def write_html_output(self):
"""Generate HTML output"""
......@@ -42,9 +42,8 @@ class Builder():
header = header_file.read()
with open(ROOT_FOLDER + "data/footer.html", "r") as footer_file:
footer = footer_file.read()
except OSError:
print("Error: can't read HTML template files")
close()
except OSError as e:
self.logger.error("can't read template files", e)
try:
with open(self.html_path, "w") as index_file:
header = header.replace("VERSION", "v{}".format(VERSION))
......@@ -75,7 +74,6 @@ class Builder():
html_output += "</tr>"
html_output += footer
index_file.write(html_output)
print("HTML output saved in {}".format(self.html_path))
except OSError:
print("Error: can't write HTML output")
close()
self.logger.info("HTML output generated")
except OSError as e:
self.logger.error("can't write HTML output", e)
......@@ -5,3 +5,4 @@ MIRRORS_URL = "http://repo.manjaro.org/mirrors.json"
BRANCHES = ("stable", "testing", "unstable")
ROOT_FOLDER = "/var/www/manjaro-web-repo/"
OUTPUT_FOLDER = "docs/"
LOGS_FOLDER = "logs/"
#!/usr/bin/env python3
def close():
print("The program must stop.")
exit()
#!/usr/bin/env python3
import datetime
import os
from conf import ROOT_FOLDER, LOGS_FOLDER
class Logger():
"""Handle logs"""
def __init__(self):
self.check_folder()
self.log_path = ROOT_FOLDER + LOGS_FOLDER + "mwr.log"
self.errors_path = ROOT_FOLDER + LOGS_FOLDER + "errors.log"
def check_folder(self):
"""Check if logs folder exists"""
try:
if not os.path.isdir(ROOT_FOLDER + LOGS_FOLDER):
os.makedirs(ROOT_FOLDER + LOGS_FOLDER)
except OSError as e:
self.error("can't create logs folder", e)
def info(self, msg):
"""Add line in mwr.log"""
print(msg)
try:
with open(self.log_path, "a") as log_file:
date = datetime.datetime.now().strftime("%c")
log_file.write("{}: {}\n".format(date, msg))
except OSError as e:
self.error("can't write message in log file", e)
def error(self, msg, exception="", close=True):
"""Add line in errors.log"""
if exception:
exception = "-> {}".format(exception)
self.info(msg)
try:
with open(self.errors_path, "a") as errors_file:
date = datetime.datetime.now().strftime("%c")
errors_file.write("{}: {} {}\n".format(date, msg, exception))
except OSError as e:
self.error("can't write message in errors file", e)
if close:
self.close()
def close(self):
"""Close manjaro-web-repo"""
self.info("Manjaro-web-repo is stopping")
exit()
......@@ -3,31 +3,33 @@
import json
import urllib.request
import time
import socket
from collections import OrderedDict
from conf import MIRRORS_URL, BRANCHES
from mirror import Mirror
from builder import Builder
from helpers import close
from logger import Logger
from conf import MIRRORS_URL, BRANCHES
class StatusChecker():
"""Launch of actions"""
def __init__(self):
self.logger = Logger()
self.mirrors = list()
self.hashes = list()
self.states = list()
self.logger.info("Manjaro-web-repo is starting")
def get_mirrors(self):
"""Get list of mirrors"""
try:
with urllib.request.urlopen(MIRRORS_URL) as mirrors_file:
with urllib.request.urlopen(MIRRORS_URL, timeout=10) as mirrors_file:
self.mirrors = json.loads(mirrors_file.read().decode("utf-8"),
object_pairs_hook=OrderedDict)
except urllib.error.URLError:
print("Error: can't fetch list of mirrors.")
close()
except urllib.error.URLError as e:
self.logger.error("can't fetch list of mirrors", e)
def get_hashes(self):
"""Get last hashes"""
......@@ -41,13 +43,13 @@ class StatusChecker():
except OSError:
pass
if len(self.hashes) < len(BRANCHES):
print("Error: can't fetch last hashes.")
close()
self.logger.error("can't fetch last hashes")
def check_mirrors(self):
"""Check each mirror"""
for mirror in self.mirrors:
print("{}: {}".format(mirror["country"], mirror["url"]))
nb = len(self.mirrors)
for i, mirror in enumerate(self.mirrors):
print("({}/{}): {}".format(i + 1, nb, mirror["url"]))
mirror = Mirror(mirror)
mirror.get_state_file()
mirror.read_state_file(self.hashes)
......@@ -59,11 +61,9 @@ class StatusChecker():
"branches": mirror.branches
}
self.states.append(mirror_status)
self.logger.info("{} mirror(s) added".format(len(self.states)))
if __name__ == "__main__":
print("--------------------------------")
print("----Manjaro mirror's tracker----")
print("--------------------------------")
status_checker = StatusChecker()
status_checker.get_mirrors()
status_checker.get_hashes()
......@@ -71,3 +71,4 @@ if __name__ == "__main__":
builder = Builder(status_checker.states)
builder.write_json_output()
builder.write_html_output()
status_checker.logger.close()
......@@ -3,7 +3,9 @@
import json
import urllib.request
import datetime
import socket
from logger import Logger
from conf import BRANCHES
......@@ -11,6 +13,7 @@ class Mirror():
"""Handle all mirror's properties"""
def __init__(self, mirror):
self.logger = Logger()
self.state_file = None
self.url = mirror["url"]
self.country = mirror["country"]
......@@ -21,10 +24,10 @@ class Mirror():
def get_state_file(self):
"""Fetch state file"""
try:
with urllib.request.urlopen(self.url + "state") as state_file:
with urllib.request.urlopen(self.url + "state", timeout=10) as state_file:
self.state_file = state_file.read().decode("utf-8")
except urllib.error.URLError:
print("\t\tCan't read state file.")
except urllib.error.URLError as e:
self.logger.error("{}: can't read state file".format(self.url), e, False)
def read_state_file(self, hashes):
"""Read infos from state file"""
......@@ -42,8 +45,9 @@ class Mirror():
str(int(elapsed_hours)).zfill(2),
str(int(elapsed_minutes)).zfill(2))
for i, branch in enumerate(BRANCHES):
url = self.url + branch + "/state"
try:
with urllib.request.urlopen(self.url + branch + "/state") as response:
with urllib.request.urlopen(url, timeout=10) as response:
content = response.read().decode("utf-8")
pos = content.find("state=")
if pos >= 0:
......@@ -52,8 +56,8 @@ class Mirror():
self.branches.append(1)
else:
self.branches.append(0)
except urllib.error.URLError:
print("\t\tCan't read hash from state file.")
except urllib.error.URLError as e:
self.logger.error("{}: can't read hash from state file".format(url), e, False)
if not self.last_sync:
self.last_sync = -1
if len(self.branches) < 3:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment