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