add comparison output, refactor main, bump python version
Signed-off-by: ahuston-0 <aliceghuston@gmail.com>
This commit is contained in:
119
flupdt/main.py
119
flupdt/main.py
@ -3,6 +3,7 @@
|
||||
"""Default processing of flake outputs for evaluating flake updates."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from argparse import Namespace
|
||||
from pathlib import Path
|
||||
|
||||
@ -35,6 +36,76 @@ def batch_eval(args: Namespace, flake_path: str, derivations: list[str]) -> None
|
||||
dump(drv_map, f)
|
||||
|
||||
|
||||
def compare_drvs(args: Namespace) -> None:
|
||||
"""Compares derivation jsons using nvd.
|
||||
|
||||
param args: argparse namespace
|
||||
"""
|
||||
pre_json_dict = {}
|
||||
post_json_dict = {}
|
||||
from json import load
|
||||
|
||||
with (
|
||||
Path.open(args.compare_pre_json, "r") as pre,
|
||||
Path.open(args.compare_post_json, "r") as post,
|
||||
):
|
||||
pre_json_dict = load(pre)
|
||||
post_json_dict = load(post)
|
||||
|
||||
logging.debug(f"pre-snapshot derivations: {pre_json_dict}")
|
||||
logging.debug(f"post-snapshot derivations: {post_json_dict}")
|
||||
|
||||
pre_json_keys = set(pre_json_dict.keys())
|
||||
post_json_keys = set(post_json_dict.keys())
|
||||
|
||||
common_keys_to_eval = pre_json_keys.union(post_json_keys)
|
||||
|
||||
missing_post_keys = pre_json_keys.difference(common_keys_to_eval)
|
||||
missing_pre_keys = post_json_keys.difference(common_keys_to_eval)
|
||||
|
||||
if missing_pre_keys:
|
||||
logging.warning(f"Following outputs are missing from pre-snapshot: {missing_pre_keys}")
|
||||
if missing_post_keys:
|
||||
logging.warning(f"Following outputs are missing from post-snapshot: {missing_post_keys}")
|
||||
|
||||
logging.info(f"Evaluating the following outputs for differences: {common_keys_to_eval}")
|
||||
|
||||
out_file: str = os.devnull
|
||||
if args.compare_output_to_file:
|
||||
out_file = args.compare_output_file
|
||||
|
||||
out_file_path = Path(out_file)
|
||||
with out_file_path.open("w") as f:
|
||||
for output_key in common_keys_to_eval:
|
||||
comp_out = compare_derivations(
|
||||
args.flake_path, pre_json_dict[output_key], post_json_dict[output_key]
|
||||
)
|
||||
f.write(f"comparing {output_key}:" + "\n")
|
||||
if comp_out:
|
||||
f.write(comp_out + "\n\n")
|
||||
else:
|
||||
f.write("comparison output is empty, please check script logs\n\n")
|
||||
|
||||
|
||||
def build_or_eval(args: Namespace) -> None:
|
||||
"""Builds or evaluates all outputs in a flake.
|
||||
|
||||
param args: argparse namespace
|
||||
"""
|
||||
flake_path = args.flake_path
|
||||
derivations, hydra_jobs = partition(
|
||||
lambda s: s.startswith("hydraJobs"), get_derivations(flake_path)
|
||||
)
|
||||
derivations, hydra_jobs = list(derivations), list(hydra_jobs)
|
||||
logging.info(f"derivations: {list(derivations)}")
|
||||
batch_eval(args, flake_path, derivations)
|
||||
|
||||
if not args.keep_hydra:
|
||||
logging.info("--keep-hydra flag is not specified, removing Hydra jobs")
|
||||
else:
|
||||
batch_eval(args, flake_path, hydra_jobs)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Sets up logging, parses args, and runs evaluation routine.
|
||||
|
||||
@ -44,53 +115,9 @@ def main() -> None:
|
||||
configure_logger("DEBUG")
|
||||
args = parse_inputs()
|
||||
if args.compare_drvs:
|
||||
pre_json_dict = {}
|
||||
post_json_dict = {}
|
||||
from json import load
|
||||
|
||||
with (
|
||||
Path.open(args.compare_pre_json, "r") as pre,
|
||||
Path.open(args.compare_post_json, "r") as post,
|
||||
):
|
||||
pre_json_dict = load(pre)
|
||||
post_json_dict = load(post)
|
||||
|
||||
logging.debug(f"pre-snapshot derivations: {pre_json_dict}")
|
||||
logging.debug(f"post-snapshot derivations: {post_json_dict}")
|
||||
|
||||
pre_json_keys = set(pre_json_dict.keys())
|
||||
post_json_keys = set(post_json_dict.keys())
|
||||
|
||||
common_keys_to_eval = pre_json_keys.union(post_json_keys)
|
||||
|
||||
missing_post_keys = pre_json_keys.difference(common_keys_to_eval)
|
||||
missing_pre_keys = post_json_keys.difference(common_keys_to_eval)
|
||||
|
||||
if missing_pre_keys:
|
||||
logging.warning(f"Following outputs are missing from pre-snapshot: {missing_pre_keys}")
|
||||
if missing_post_keys:
|
||||
logging.warning(f"Following outputs are missing from post-snapshot: {missing_post_keys}")
|
||||
|
||||
logging.info(f"Evaluating the following outputs for differences: {common_keys_to_eval}")
|
||||
|
||||
for output_key in common_keys_to_eval:
|
||||
compare_derivations(args.flake_path, pre_json_dict[output_key], post_json_dict[output_key])
|
||||
|
||||
|
||||
|
||||
compare_drvs(args)
|
||||
else:
|
||||
flake_path = args.flake_path
|
||||
derivations, hydra_jobs = partition(
|
||||
lambda s: s.startswith("hydraJobs"), get_derivations(flake_path)
|
||||
)
|
||||
derivations, hydra_jobs = list(derivations), list(hydra_jobs)
|
||||
logging.info(f"derivations: {list(derivations)}")
|
||||
batch_eval(args, flake_path, derivations)
|
||||
|
||||
if not args.keep_hydra:
|
||||
logging.info("--keep-hydra flag is not specified, removing Hydra jobs")
|
||||
else:
|
||||
batch_eval(args, flake_path, hydra_jobs)
|
||||
build_or_eval(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Reference in New Issue
Block a user