-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathLogsFileIndex.py
71 lines (57 loc) · 1.97 KB
/
LogsFileIndex.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import re
import os
"""
LogsFileIndex - A class for managing the logs files index file
"""
class LogsFileIndex:
def __init__(self, config, logger, downloader, config_path):
self.config_path = config_path
self.config = config
self.content = None
self.hash_content = None
self.logger = logger
self.file_downloader = downloader
"""
Gets the indexed log files
"""
def indexed_logs(self) -> list:
return self.content
"""
Downloads a logs file index file
"""
def download(self):
self.logger.info("Downloading logs index file...")
# try to get the logs.index file
file_content = self.file_downloader.request_file_content(self.config.BASE_URL + "logs.index")
# if we got the file content
if file_content != "":
content = file_content.decode("utf-8")
# validate the file format
if LogsFileIndex.validate_log_file_format(content):
self.content = content.splitlines()
self.hash_content = set(self.content)
with open(os.path.join(self.config_path, "logs.index"), "wb") as fp:
fp.write(file_content)
else:
self.logger.error("log.index, Pattern Validation Failed")
raise Exception
else:
raise Exception('Index file does not yet exist, please allow time for files to be generated.')
"""
Validates that format name of the logs files inside the logs index file
"""
@staticmethod
def validate_logs_index_file_format(content):
file_rex = re.compile("(\d+_\d+\.log\n)+")
if file_rex.match(content):
return True
return False
"""
Validates a log file name format
"""
@staticmethod
def validate_log_file_format(content):
file_rex = re.compile("(\d+_\d+\.log)")
if file_rex.match(content):
return True
return False