|
| 1 | +import json |
| 2 | +import pandas as pd |
| 3 | +import argparse |
| 4 | + |
| 5 | + |
| 6 | +# Function to load JSON data from files |
| 7 | +def load_json(filename): |
| 8 | + with open(filename, 'r') as f: |
| 9 | + return json.load(f) |
| 10 | + |
| 11 | + |
| 12 | +# Function to compare values and create formatted strings |
| 13 | +def compare_values(base, current, key): |
| 14 | + if key not in base and key not in current: |
| 15 | + return None |
| 16 | + if key not in base: |
| 17 | + return f"🆕 {current[key]}" |
| 18 | + if key not in current: |
| 19 | + return f"❌" |
| 20 | + |
| 21 | + base_value = base[key] |
| 22 | + current_value = current[key] |
| 23 | + |
| 24 | + if base_value == current_value: |
| 25 | + return f"🆗 {base_value}" |
| 26 | + |
| 27 | + change = current_value - base_value |
| 28 | + sign = "+" if change > 0 else "-" |
| 29 | + symbol = "⚠️" if change > 0 else "⬇️" |
| 30 | + |
| 31 | + # round to 2 decimal places |
| 32 | + change = round(abs(change), 2) |
| 33 | + return f"{symbol} {current_value} <sub>{sign}{change}</sub>" |
| 34 | + |
| 35 | + |
| 36 | +# Function to generate Markdown table |
| 37 | +def generate_markdown_table(base_data, current_data): |
| 38 | + table_data = [] |
| 39 | + |
| 40 | + all_keys = set(base_data.keys()).union(set(current_data.keys())) |
| 41 | + all_keys = sorted(all_keys) |
| 42 | + |
| 43 | + for key in all_keys: |
| 44 | + base_ram = base_data.get(key, {}).get("ram", {}) |
| 45 | + current_ram = current_data.get(key, {}).get("ram", {}) |
| 46 | + |
| 47 | + base_flash = base_data.get(key, {}).get("flash", {}) |
| 48 | + current_flash = current_data.get(key, {}).get("flash", {}) |
| 49 | + |
| 50 | + row = [ |
| 51 | + key.replace("-usage", ""), |
| 52 | + compare_values(base_ram, current_ram, "percentage"), |
| 53 | + compare_values(base_ram, current_ram, "used"), |
| 54 | + compare_values(base_flash, current_flash, "percentage"), |
| 55 | + compare_values(base_flash, current_flash, "used") |
| 56 | + ] |
| 57 | + table_data.append(row) |
| 58 | + |
| 59 | + df = pd.DataFrame(table_data, columns=["Variant", "RAM, %", "RAM, bytes", "Flash, %", "Flash, bytes"]) |
| 60 | + |
| 61 | + return df.to_markdown(index=False) |
| 62 | + |
| 63 | + |
| 64 | +# Main function to handle CLI arguments and execute the script |
| 65 | +def main(): |
| 66 | + parser = argparse.ArgumentParser(description='Compare JSON files and generate a markdown table.') |
| 67 | + parser.add_argument('base', type=str, help='Path to the base JSON file') |
| 68 | + parser.add_argument('current', type=str, help='Path to the current JSON file') |
| 69 | + parser.add_argument('--output', type=str, help='Path to the output markdown file') |
| 70 | + args = parser.parse_args() |
| 71 | + |
| 72 | + base_data = load_json(args.base) |
| 73 | + current_data = load_json(args.current) |
| 74 | + |
| 75 | + markdown_table = generate_markdown_table(base_data, current_data) |
| 76 | + |
| 77 | + if args.output: |
| 78 | + with open(args.output, 'w') as f: |
| 79 | + f.write(markdown_table) |
| 80 | + print(f"Markdown table generated and saved to {args.output}") |
| 81 | + else: |
| 82 | + print(markdown_table) |
| 83 | + |
| 84 | + |
| 85 | +# usage: python compare-memory-usage.py base.json current.json --output table.md |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments