-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
45 lines (33 loc) · 1.8 KB
/
logger.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
import os
from datetime import datetime
class Logger:
'''Handle execution logs (repositories list, runtime messages, etc.)'''
def __init__(self, folder_name="logs", encoding="utf-8"):
self.folder_name = folder_name
if not os.path.exists(self.folder_name):
os.makedirs(self.folder_name)
self.repositories_data_file = open(os.path.join(self.folder_name, "repos.txt"), "w", encoding=encoding)
print("Repositories expected to clone:", file=self.repositories_data_file)
self.runtime_logs_file = open(os.path.join(self.folder_name, "runtime.txt"), "w", encoding=encoding)
print("Runtime logs:", file=self.runtime_logs_file)
def save_repositories_data(self, repositories_data):
'''Writes a log-file with all the information of the repositories that will be cloned'''
repositories_data = sorted(repositories_data, key=lambda repo: repo["last_commit_date"])
for repo in repositories_data:
print(repo["name"], end=" ", file=self.repositories_data_file)
print(repo["last_commit_sha"], end=" ", file=self.repositories_data_file)
print(repo["last_commit_author"], end=" ", file=self.repositories_data_file)
print(repo["last_commit_date"], file=self.repositories_data_file)
self.repositories_data_file.flush()
os.fsync(self.repositories_data_file.fileno())
def write_runtime_log(self, msg):
'''Writes a runtime-log message in the log-file with the current time'''
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(f"({current_time}) {msg}", file=self.runtime_logs_file)
self.runtime_logs_file.flush()
os.fsync(self.runtime_logs_file.fileno())
def finalize(self):
'''Ends the logger handler instance, saving and closing the used files'''
self.runtime_logs_file.close()
self.repositories_data_file.close()