|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import argparse |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +def load_branch_data(file_path): |
| 10 | + """ |
| 11 | + Load branch data from JSON file. |
| 12 | + |
| 13 | + Args: |
| 14 | + file_path (str): Path to the JSON file containing branch data |
| 15 | + |
| 16 | + Returns: |
| 17 | + dict: Branch data |
| 18 | + """ |
| 19 | + try: |
| 20 | + with open(file_path, 'r') as f: |
| 21 | + data = json.load(f) |
| 22 | + return data |
| 23 | + except FileNotFoundError: |
| 24 | + print(f"Error: File {file_path} not found.") |
| 25 | + sys.exit(1) |
| 26 | + except json.JSONDecodeError: |
| 27 | + print(f"Error: File {file_path} is not valid JSON.") |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | +def analyze_branch_data(data): |
| 31 | + """ |
| 32 | + Analyze branch data and generate statistics. |
| 33 | + |
| 34 | + Args: |
| 35 | + data (dict): Branch data |
| 36 | + |
| 37 | + Returns: |
| 38 | + dict: Analysis results |
| 39 | + """ |
| 40 | + # Verify data structure |
| 41 | + required_keys = ['main', 'release', 'common', 'mainOnly', 'releaseOnly'] |
| 42 | + for key in required_keys: |
| 43 | + if key not in data: |
| 44 | + print(f"Error: Missing required key '{key}' in branch data.") |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + # Calculate statistics |
| 48 | + stats = { |
| 49 | + 'total_branches': len(set(data['main'] + data['release'])), |
| 50 | + 'main_count': len(data['main']), |
| 51 | + 'release_count': len(data['release']), |
| 52 | + 'common_count': len(data['common']), |
| 53 | + 'main_only_count': len(data['mainOnly']), |
| 54 | + 'release_only_count': len(data['releaseOnly']), |
| 55 | + 'repository': data.get('repository', 'Unknown'), |
| 56 | + 'timestamp': data.get('timestamp', datetime.now().isoformat()), |
| 57 | + 'analysis_time': datetime.now().isoformat(), |
| 58 | + 'analysis_date': data.get('analysisDate', 'latest') |
| 59 | + } |
| 60 | + |
| 61 | + # Calculate percentages |
| 62 | + if stats['total_branches'] > 0: |
| 63 | + stats['common_percentage'] = round(stats['common_count'] / stats['total_branches'] * 100, 1) |
| 64 | + stats['main_only_percentage'] = round(stats['main_only_count'] / stats['total_branches'] * 100, 1) |
| 65 | + stats['release_only_percentage'] = round(stats['release_only_count'] / stats['total_branches'] * 100, 1) |
| 66 | + else: |
| 67 | + stats['common_percentage'] = 0 |
| 68 | + stats['main_only_percentage'] = 0 |
| 69 | + stats['release_only_percentage'] = 0 |
| 70 | + |
| 71 | + # Combine with original data |
| 72 | + result = {**data, 'stats': stats} |
| 73 | + return result |
| 74 | + |
| 75 | +def save_analysis_results(data, output_file): |
| 76 | + """ |
| 77 | + Save analysis results to a JSON file. |
| 78 | + |
| 79 | + Args: |
| 80 | + data (dict): Analysis results |
| 81 | + output_file (str): Path to the output JSON file |
| 82 | + """ |
| 83 | + with open(output_file, 'w') as f: |
| 84 | + json.dump(data, f, indent=2) |
| 85 | + print(f"Analysis results saved to {output_file}") |
| 86 | + |
| 87 | +def print_analysis_summary(data): |
| 88 | + """ |
| 89 | + Print a summary of the analysis results. |
| 90 | + |
| 91 | + Args: |
| 92 | + data (dict): Analysis results |
| 93 | + """ |
| 94 | + stats = data['stats'] |
| 95 | + |
| 96 | + print("\n" + "="*50) |
| 97 | + print(f"BRANCH ANALYSIS SUMMARY FOR {stats['repository']}") |
| 98 | + if stats.get('analysis_date') and stats['analysis_date'] != 'latest': |
| 99 | + print(f"Analysis Date: {stats['analysis_date']}") |
| 100 | + print("="*50) |
| 101 | + print(f"Total unique branches: {stats['total_branches']}") |
| 102 | + print(f"Branches in main: {stats['main_count']}") |
| 103 | + print(f"Branches in release: {stats['release_count']}") |
| 104 | + print(f"Common branches: {stats['common_count']} ({stats['common_percentage']}%)") |
| 105 | + print(f"Branches only in main: {stats['main_only_count']} ({stats['main_only_percentage']}%)") |
| 106 | + print(f"Branches only in release: {stats['release_only_count']} ({stats['release_only_percentage']}%)") |
| 107 | + print("="*50) |
| 108 | + |
| 109 | + # Print branch lists |
| 110 | + print("\nBranches in main:") |
| 111 | + for branch in data['main']: |
| 112 | + print(f" - {branch}") |
| 113 | + |
| 114 | + print("\nBranches in release:") |
| 115 | + for branch in data['release']: |
| 116 | + print(f" - {branch}") |
| 117 | + |
| 118 | + print("\nCommon branches:") |
| 119 | + for branch in data['common']: |
| 120 | + print(f" - {branch}") |
| 121 | + |
| 122 | + print("\nBranches only in main:") |
| 123 | + for branch in data['mainOnly']: |
| 124 | + print(f" - {branch}") |
| 125 | + |
| 126 | + print("\nBranches only in release:") |
| 127 | + for branch in data['releaseOnly']: |
| 128 | + print(f" - {branch}") |
| 129 | + |
| 130 | +def main(): |
| 131 | + # Parse command line arguments |
| 132 | + parser = argparse.ArgumentParser(description='Analyze branch data with date filtering') |
| 133 | + parser.add_argument('input_file', nargs='?', default='branch_data.json', help='Input JSON file path') |
| 134 | + parser.add_argument('output_file', nargs='?', help='Output JSON file path') |
| 135 | + args = parser.parse_args() |
| 136 | + |
| 137 | + # Default input and output files |
| 138 | + input_file = args.input_file |
| 139 | + |
| 140 | + # Determine output filename if not provided |
| 141 | + if args.output_file: |
| 142 | + output_file = args.output_file |
| 143 | + else: |
| 144 | + # If input file has date in name, use same pattern for output |
| 145 | + if '_' in input_file and input_file.startswith('branch_data_'): |
| 146 | + date_part = input_file.split('branch_data_')[1] |
| 147 | + output_file = f"analyzed_branch_data_{date_part}" |
| 148 | + else: |
| 149 | + output_file = "analyzed_branch_data.json" |
| 150 | + |
| 151 | + print(f"Loading branch data from {input_file}...") |
| 152 | + data = load_branch_data(input_file) |
| 153 | + |
| 154 | + print("Analyzing branch data...") |
| 155 | + analyzed_data = analyze_branch_data(data) |
| 156 | + |
| 157 | + # Save analysis results |
| 158 | + save_analysis_results(analyzed_data, output_file) |
| 159 | + |
| 160 | + # Print summary |
| 161 | + print_analysis_summary(analyzed_data) |
| 162 | + |
| 163 | + print(f"\nAnalysis complete. Results saved to {output_file}") |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + main() |
0 commit comments