app.py
import os
import openpyxl
from excelTemplate import excelTemplate
from openpyxl.styles import Border, Side, colors, Font, Alignment, GradientFill, PatternFill
# start at U64
def fill_sysctl_conf(work_sheet, value_list):
index = 64
for i in range(len(value_list)):
str_list = value_list[i].split(" ")
work_sheet["U" + str(index)] = str_list[-1]
work_sheet["U" + str(index + 1)] = str_list[3]
work_sheet["U" + str(index + 2)] = str_list[4]
index += 3
# start at U61
def fill_journal_conf(ws, value_list):
# TODO: #Storge =Auto
ws["U61"] = "#Storge =Auto"
# U58
def fill_yum_conf(ws, value_list):
for value in value_list:
if value.find("keepcache"):
ws["U58"].value = value
if value.find("proxy") == -1:
ws["U57"].value = "-"
else:
ws["U57"].value = value
def fill_crond(ws, value_list):
for value in value_list:
if value.find("MAILTO=".lower()):
ws["U54"].value = value
def fill_timedateclt(ws, list):
for v in list:
if v.find("Time zone") > 1:
ws["U27"].value = v
# cron.daily
def fill_cron_daily(ws, list):
code = ""
for v in list:
code = code + v
ws["U51"] = code
# U30
def fill_yum_repos(ws, list):
str_list = list[0].split(" ")
if str_list[2] != "rehat".lower():
ws["U30"].value = "[NO]"
def read_config_file():
file_paths = []
with open("file_path.config", "r", encoding="utf-8") as config:
for path in config:
path = path.strip('\n')
if path[0] != "#":
file_paths.append(path)
config.close()
return file_paths
def file_env_log(ws, env_list):
column_list = ['U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE']
ws.unmerge_cells("W4:W9")
ws["W4"].value = ""
ws["W4"].fill = PatternFill("solid", fgColor="FFFFFF")
last_column = column_list[len(env_list)]
for i in range(0, len(env_list)):
env_str = env_list[i].split(",")
for q in range(0, len(env_str)):
cell_name = column_list[i] + str(5 + q)
work_sheet.column_dimensions[column_list[i]].width = 40
ws[cell_name] = env_str[q]
# reset last
work_sheet.merge_cells(last_column + str(4) + ":" + last_column + str(9))
second_left_cell = work_sheet[last_column + str(4)]
second_left_cell.value = "数据类型"
second_left_cell.alignment = Alignment(horizontal="center", vertical="center")
# 216, 239, 218
second_left_cell.fill = PatternFill("solid", fgColor="D8EFDA")
work_sheet.column_dimensions[last_column].width = 10
def fill_logrotate_conf(ws, list):
code1 = ""
code2 = ""
index = -1
#for i in range(0, len(file_path)):
# str = file_path[i].strip("/")
# if str[-1].find(file_name) > 0:
# index = i
#if index != -1:
# with open(file_name, "r", encoding="utf-8") as file_obj:
# for line in file_obj:
# if line[0] != "#" or line[0] !="\n":
for i in range(0, len(list)):
str = list[i].strip().lower()
if str == 'daily' or str == 'weekly' or \
str == 'monthly' or str == 'yearly':
ws["U38"].value = list[i].strip()
elif str.find("rotate") > -1:
ws["U39"].value = list[i].strip()
elif str.find("create") > -1:
ws["U40"].value = list[i].strip()
if str.find("/var/log/wtmp") > -1:
for j in range(i, len(list)):
if list[j].strip() != "}":
code1 = code1 + list[j]
else:
code1 = code1 + "}"
break
if str.find("/var/log/btmp") > -1:
for j in range(i, len(list)):
if list[j].strip() != "}":
code2 = code2 + list[j]
else:
code2 = code2 + "}"
break
ws["U41"].value = code1.strip()
ws["U42"].value = code2.strip()
if __name__ == '__main__':
# variable declaration
log_files_path = read_config_file()
if len(log_files_path) != 0:
read_info = {}
all_exit = True
for file_name in log_files_path:
if not os.path.exists(file_name):
all_exit = False
print(file_name + "file doest exit, please check the path")
# confirmed all file exited
if all_exit is True:
for file_name in log_files_path:
with open(file_name, "r", encoding="utf-8") as file_obj:
read_result = []
for line in file_obj:
if line[0] != "#":
read_result.append(line)
# put value in map
read_info[os.path.basename(file_obj.name)] = read_result
file_obj.close()
print(read_info)
# genera file
excelTemplate = excelTemplate()
gen_file_path = "genered.xlsx"
excelTemplate.gen_excel(gen_file_path)
# write value into
wb = openpyxl.load_workbook(gen_file_path)
work_sheet = wb.active
# use:read info
file_env_log(work_sheet, read_info["env.log"])
fill_crond(work_sheet, read_info["crond_0hourly.log"])
fill_timedateclt(work_sheet, read_info["timedateclt.log"])
fill_yum_conf(work_sheet, read_info["yum.conf"])
fill_logrotate_conf(work_sheet, read_info["logrotate.conf"])
fill_cron_daily(work_sheet, read_info["man-db.cron"])
fill_yum_repos(work_sheet, read_info["yum.conf.log"])
fill_journal_conf(work_sheet, read_info["journald.conf"])
fill_sysctl_conf(work_sheet, read_info["sysctl.conf"])
# save work sheet
wb.save(gen_file_path)
else:
print("Empty config file, Please check!")

浙公网安备 33010602011771号