FIX: change name weekly-check to db-check
This commit is contained in:
157
pmodules_tools/db_diff/check.py
Normal file
157
pmodules_tools/db_diff/check.py
Normal file
@ -0,0 +1,157 @@
|
||||
import difflib
|
||||
import hashlib
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
|
||||
|
||||
def db_check_diff(current_pmodule_state, Pmodules_db_path, Pmodules_states):
|
||||
current_sha = hashlib.sha256(current_pmodule_state.encode()).hexdigest()
|
||||
|
||||
no_current_db = False
|
||||
|
||||
# Retrieve old state for comparison
|
||||
try:
|
||||
old_pmodule_file = sorted(
|
||||
[Pmodules_db_path + f for f in os.listdir(Pmodules_db_path)],
|
||||
key=os.path.getctime,
|
||||
)[0]
|
||||
old_pmodule_state = open(old_pmodule_file, "r").read()
|
||||
except:
|
||||
print(
|
||||
"There is no old Pmodule database available on path "
|
||||
+ Pmodules_db_path
|
||||
+ "... Writing current one"
|
||||
)
|
||||
no_current_db = True
|
||||
|
||||
# There is a database and we have to check the differences between the pmodule states.
|
||||
if not no_current_db and compare_states_sha(
|
||||
current_pmodule_state, old_pmodule_state, current_sha
|
||||
):
|
||||
print_pmodules_differences(
|
||||
current_pmodule_state, old_pmodule_state, Pmodules_states
|
||||
)
|
||||
|
||||
# There is no database available or there are differences with the old pmodule state, writing current state.
|
||||
if no_current_db or compare_states_sha(
|
||||
current_pmodule_state, old_pmodule_state, current_sha
|
||||
):
|
||||
write_curent_state(current_pmodule_state, current_sha, Pmodules_db_path)
|
||||
|
||||
|
||||
def compare_states_sha(current_pmodule_state, old_pmodule_state, current_sha):
|
||||
if (len(old_pmodule_state)) != 0:
|
||||
old_sha = hashlib.sha256(old_pmodule_state.encode()).hexdigest()
|
||||
if current_sha != old_sha:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def print_to_markdown(new_module_list, deleted_module_list, changed_module_list):
|
||||
with open("pmodules_changes.md", "w") as md_file:
|
||||
standard_print(md_file, new_module_list, "New modules:")
|
||||
|
||||
md_file.write("\n# Changed modules: \n")
|
||||
if len(changed_module_list) > 1:
|
||||
for index in range(0, len(changed_module_list) - 1, 2):
|
||||
md_file.write(
|
||||
changed_module_list[index]
|
||||
+ "<br>Changed state from "
|
||||
+ changed_module_list[index + 1].split()[1]
|
||||
+ " to ---> "
|
||||
+ changed_module_list[index].split()[1]
|
||||
+ "<br>"
|
||||
)
|
||||
|
||||
standard_print(md_file, deleted_module_list, "Deleted modules:")
|
||||
|
||||
|
||||
def print_pmodules_differences(
|
||||
current_pmodule_state, old_pmodule_state, Pmodules_states
|
||||
):
|
||||
# Make sure the whitespaces in between the module descriptions are always one space only.
|
||||
current_db = set(" ".join(i.split()) for i in current_pmodule_state.splitlines())
|
||||
old_db = set(" ".join(i.split()) for i in old_pmodule_state.splitlines())
|
||||
|
||||
new_module_list = list(current_db - old_db)
|
||||
deleted_module_list = list(old_db - current_db)
|
||||
changed_module_list = []
|
||||
|
||||
# Replace the state of the modules with * for Regex comparison
|
||||
new_module_list_no_state = new_module_list.copy()
|
||||
deleted_module_list_no_state = deleted_module_list.copy()
|
||||
for state in Pmodules_states:
|
||||
new_module_list_no_state = [
|
||||
diff.replace(state, ".*") for diff in new_module_list_no_state
|
||||
]
|
||||
deleted_module_list_no_state = [
|
||||
diff.replace(state, ".*") for diff in deleted_module_list_no_state
|
||||
]
|
||||
|
||||
# Check if the state of the module is the only thing that changed
|
||||
# Append changed_module_list if yes
|
||||
if len(deleted_module_list_no_state) >= len(new_module_list_no_state):
|
||||
set_changed_module_list(
|
||||
new_module_list_no_state,
|
||||
deleted_module_list_no_state,
|
||||
new_module_list,
|
||||
deleted_module_list,
|
||||
changed_module_list,
|
||||
)
|
||||
else:
|
||||
set_changed_module_list(
|
||||
deleted_module_list_no_state,
|
||||
new_module_list_no_state,
|
||||
new_module_list,
|
||||
deleted_module_list,
|
||||
changed_module_list,
|
||||
)
|
||||
|
||||
print_to_markdown(new_module_list, deleted_module_list, changed_module_list)
|
||||
|
||||
|
||||
def set_changed_module_list(
|
||||
small_module_list_no_state,
|
||||
big_module_list_no_state,
|
||||
new_module_list,
|
||||
deleted_module_list,
|
||||
changed_module_list,
|
||||
):
|
||||
for diff in small_module_list_no_state:
|
||||
if diff in big_module_list_no_state:
|
||||
new_module_index = [
|
||||
idx
|
||||
for idx, string in enumerate(new_module_list)
|
||||
if re.search(diff, string)
|
||||
][0]
|
||||
deleted_module_index = [
|
||||
idx
|
||||
for idx, string in enumerate(deleted_module_list)
|
||||
if re.search(diff, string)
|
||||
][0]
|
||||
changed_module_list += [
|
||||
new_module_list[new_module_index],
|
||||
deleted_module_list[deleted_module_index],
|
||||
]
|
||||
new_module_list[new_module_index] = ""
|
||||
deleted_module_list[deleted_module_index] = ""
|
||||
|
||||
|
||||
def standard_print(file, module_list, string):
|
||||
file.write("\n# " + string + " \n")
|
||||
for diff in module_list:
|
||||
if diff != "":
|
||||
file.write(diff + "<br>")
|
||||
|
||||
|
||||
def write_curent_state(current_pmodule_state, current_sha, Pmodules_db_path):
|
||||
# Emptying Pmodules database first
|
||||
if os.path.exists(Pmodules_db_path):
|
||||
shutil.rmtree(Pmodules_db_path)
|
||||
|
||||
# Recreating dir and writing new database
|
||||
os.makedirs(Pmodules_db_path)
|
||||
with open(Pmodules_db_path + current_sha, "w") as current_pmodule_file:
|
||||
current_pmodule_file.write(current_pmodule_state)
|
Reference in New Issue
Block a user