dataset/seed/applicationservice/2022.03.08/base-machine/manual/install/diff.py

97 lines
2.3 KiB
Python
Raw Normal View History

2022-03-08 19:42:28 +01:00
#!/usr/bin/env python3
from os.path import join
from filecmp import dircmp
from difflib import unified_diff
from sys import stdout, argv
2022-07-01 18:54:06 +02:00
from os import walk
from os.path import join
2022-03-08 19:42:28 +01:00
from datetime import datetime, timezone
os_name = argv[1]
OLD_DIR = argv[2]
NEW_DIR = argv[3]
2022-06-24 19:00:16 +02:00
WEBSITE = len(argv) != 5
2022-03-08 19:42:28 +01:00
def diff_files(dcmp):
2022-07-01 18:54:06 +02:00
files = []
2022-03-08 19:42:28 +01:00
for name in dcmp.diff_files:
2022-07-01 18:54:06 +02:00
files.append(join(dcmp.right[len(NEW_DIR):], name))
2022-03-08 19:42:28 +01:00
for sub_dcmp in dcmp.subdirs.values():
2022-07-01 18:54:06 +02:00
files.extend(diff_files(sub_dcmp))
return files
2022-03-08 19:42:28 +01:00
dcmp = dircmp(OLD_DIR, NEW_DIR)
date = datetime.now(timezone.utc).isoformat()
title = f"Nouvelle version de la configuration de {os_name}"
subtitle = f"Différence entre les fichiers de configuration de {os_name}"
2022-06-24 19:00:16 +02:00
if WEBSITE:
print(f"""+++
2022-03-08 19:42:28 +01:00
title = "{title}"
description = "{subtitle}"
date = {date}
updated = {date}
draft = false
template = "blog/page.html"
[taxonomies]
authors = ["Automate"]
[extra]
lead = "{subtitle}."
type = "installe"
2022-07-01 18:54:06 +02:00
+++""")
2022-06-24 19:00:16 +02:00
TITLE = True
else:
TITLE = False
2022-07-01 18:54:06 +02:00
for filename in diff_files(dcmp):
2022-06-24 19:00:16 +02:00
if not TITLE:
print(title)
print("=" * len(title))
print()
TITLE = True
2022-07-01 18:54:06 +02:00
print(f'\n- mise à jour du fichier {filename} :\n')
2022-03-29 16:24:52 +02:00
try:
with open(join(OLD_DIR, filename[1:]), 'r') as ori:
ori_content = ori.readlines()
with open(join(NEW_DIR, filename[1:]), 'r') as new:
new_content = new.readlines()
except UnicodeDecodeError:
print('fichier binaire')
else:
2022-06-24 19:00:16 +02:00
if WEBSITE:
print('```diff')
2022-03-29 16:24:52 +02:00
for line in unified_diff(ori_content, new_content, fromfile=filename, tofile=filename):
print(line.rstrip())
2022-06-24 19:00:16 +02:00
if WEBSITE:
print('```')
2022-07-01 18:54:06 +02:00
old = set()
new = set()
for rootname, set_ in ((OLD_DIR, old), (NEW_DIR, new)):
len_rootname = len(rootname) + 1
for dirname, _, filenames in walk(rootname):
for filename in filenames:
set_.add(join(dirname[len_rootname:], filename))
for filename in old - new:
print(f'\n- fichier {filename} supprimé.\n')
for filename in new - old:
print(f'\n- fichier {filename} ajouté :\n')
with open(join(NEW_DIR, filename), 'r') as fh:
if WEBSITE:
print('```')
print(fh.read())
if WEBSITE:
print('```')