forked from bountybench/bountytasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_bounty_status.py
More file actions
211 lines (177 loc) · 9.69 KB
/
Copy pathcheck_bounty_status.py
File metadata and controls
211 lines (177 loc) · 9.69 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
import os
import json
from pathlib import Path
def count_bounties(root_dir):
total_bounties = 0
bounties_with_patches = []
bounties_with_invariants = []
bounties_no_patch = []
missing_repo_invariants = []
missing_bounty_invariants = []
invariant_violations = []
folder_counts = {}
repos = set() # Track unique repositories
for root, _, files in os.walk(root_dir):
if 'bounty_metadata.json' in files:
total_bounties += 1
metadata_path = Path(root) / 'bounty_metadata.json'
try:
with open(metadata_path, 'r') as f:
data = json.load(f)
patch_data = data.get('patch')
# Restored original validation with path tracking
if not patch_data or (isinstance(patch_data, str) and not patch_data.strip()):
bounties_no_patch.append(str(metadata_path))
else:
bounties_with_patches.append(str(metadata_path))
repo_metadata_path = metadata_path.parent.parent.parent / 'metadata.json'
repo_missing = not check_repo_invariants(repo_metadata_path, str(metadata_path), missing_repo_invariants)
if not repo_missing:
if 'invariant_thresholds' not in data:
missing_bounty_invariants.append(str(metadata_path))
else:
# Check if the invariant thresholds are empty
bounty_invariants = data.get('invariant_thresholds', {})
# Read repo metadata to check if both are empty
with open(repo_metadata_path, 'r') as repo_f:
repo_metadata = json.load(repo_f)
repo_invariants = repo_metadata.get('invariant_thresholds', {})
if not repo_invariants and not bounty_invariants:
missing_bounty_invariants.append(str(metadata_path))
else:
bounties_with_invariants.append(str(metadata_path))
check_invariant_violations(data, repo_metadata_path, metadata_path, invariant_violations, missing_bounty_invariants)
except (json.JSONDecodeError, IOError) as e:
print(f"Error processing {metadata_path}: {e}")
base_folder = metadata_path.parent.parent.parent.name
folder_counts[base_folder] = folder_counts.get(base_folder, 0) + 1
repos.add(base_folder) # Add repository to the set of unique repos
return (total_bounties, bounties_with_patches, bounties_with_invariants,
bounties_no_patch, missing_repo_invariants,
missing_bounty_invariants, invariant_violations, folder_counts,
len(repos)) # Return the count of unique repositories
def check_repo_invariants(repo_metadata_path, bounty_path, missing_repo_invariants):
if not repo_metadata_path.exists():
missing_repo_invariants.append(bounty_path)
return False
try:
with open(repo_metadata_path, 'r') as repo_f:
if 'invariant_thresholds' not in json.load(repo_f):
missing_repo_invariants.append(bounty_path)
return False
except Exception as e:
print(f"Error checking repo invariants: {e}")
missing_repo_invariants.append(bounty_path)
return False
return True
def check_invariant_violations(data, repo_metadata_path, metadata_path, invariant_violations, missing_bounty_invariants=None):
try:
with open(repo_metadata_path, 'r') as repo_f:
repo_metadata = json.load(repo_f)
repo_invariants = repo_metadata.get('invariant_thresholds', {})
bounty_invariants = data.get('invariant_thresholds', {})
# Check if both dictionaries are empty
if missing_bounty_invariants is not None and not repo_invariants and not bounty_invariants:
missing_bounty_invariants.append(str(metadata_path))
combined_invariants = {**repo_invariants, **bounty_invariants}
for key, threshold in combined_invariants.items():
actual_value = data.get(key, 0)
if actual_value < threshold:
invariant_violations.append({
'path': str(metadata_path),
'metric': key,
'threshold': threshold,
'actual': actual_value
})
except Exception as e:
print(f"Error checking invariants for {metadata_path}: {e}")
def check_docker_health_checks(root_dir):
missing_health_checks = []
repo_compose_files = {}
repo_has_health_check = {}
# Find all repos (directories with 'bounties' subdirectory)
repo_dirs = []
for item in os.listdir(root_dir):
item_path = os.path.join(root_dir, item)
if os.path.isdir(item_path) and 'bounties' in os.listdir(item_path):
repo_dirs.append(item_path)
# Check repo-level docker-compose files
for repo_dir in repo_dirs:
repo_name = os.path.basename(repo_dir)
compose_path = os.path.join(repo_dir, 'docker-compose.yml')
if os.path.exists(compose_path):
try:
with open(compose_path, 'r') as f:
content = f.read()
if 'healthcheck:' in content:
repo_has_health_check[repo_name] = True
else:
repo_has_health_check[repo_name] = False
missing_health_checks.append(compose_path)
except Exception as e:
print(f"Error checking {compose_path}: {e}")
missing_health_checks.append(compose_path)
else:
# Check first bounty docker-compose if no repo-level file
bounty_compose_found = False
for root, _, files in os.walk(os.path.join(repo_dir, 'bounties')):
if 'docker-compose.yml' in files:
bounty_compose_path = os.path.join(root, 'docker-compose.yml')
bounty_compose_found = True
try:
with open(bounty_compose_path, 'r') as f:
content = f.read()
if 'healthcheck:' not in content:
missing_health_checks.append(bounty_compose_path)
except Exception as e:
print(f"Error checking {bounty_compose_path}: {e}")
missing_health_checks.append(bounty_compose_path)
break # Only check the first bounty compose file
return missing_health_checks
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Count bounty metadata files')
parser.add_argument('root_dir', nargs='?', default=os.getcwd(),
help='Root directory to search from (defaults to current working directory)')
args = parser.parse_args()
(total, bounties_with_patches, bounties_with_invariants,
bounties_no_patch, missing_repo_invariants,
missing_bounty_invariants, invariant_violations,
folder_counts, repo_count) = count_bounties(args.root_dir)
print("\n\033[1mBounty Statistics Report\033[0m")
print("=" * 30)
print(f"\033[1;34m• Total Repositories:\033[0m \033[1;32m{repo_count}\033[0m")
print(f"\033[1;34m• Total Bounties:\033[0m {total}")
print(f"\033[1;34m• Bounties With Patches:\033[0m \033[1;32m{len(bounties_with_patches)}\033[0m")
print(f"\033[1;34m• Bounties With Invariant Thresholds:\033[0m \033[1;32m{len(bounties_with_invariants)}\033[0m")
print("\n\033[1mDistribution by Project\033[0m")
print("=" * 30)
max_length = max(len(folder) for folder in folder_counts)
for folder, count in sorted(folder_counts.items()):
print(f"\033[1;32m{folder.ljust(max_length)}\033[0m │ \033[1;36m{count}\033[0m")
print(f"\n\033[1;34m• Bounties With Patches:\033[0m \033[1;31m{len(bounties_with_patches)}\033[0m")
for path in bounties_with_patches:
print(path)
print(f"\n\033[1;34m• Bounties With Invariants:\033[0m \033[1;31m{len(bounties_with_invariants)}\033[0m")
for path in bounties_with_invariants:
print(path)
print("\n\033[1mNegatives Report\033[0m")
print("=" * 30)
print(f"\033[1;34m• Bounties Without Patches:\033[0m \033[1;31m{len(bounties_no_patch)}\033[0m")
for path in bounties_no_patch:
print(path)
print(f"\033[1;34m• Missing Invariants:\033[0m \033[1;31m{len(missing_repo_invariants) + len(missing_bounty_invariants)}\033[0m")
for path in missing_repo_invariants:
print(f" \033[33m{path}\033[0m")
for path in missing_bounty_invariants:
print(f" \033[33m{path}\033[0m")
missing_health_checks = check_docker_health_checks(args.root_dir)
print("\n\033[1mDocker Health Check Report\033[0m")
print("=" * 30)
if missing_health_checks:
print(f"\033[1;34m• Docker Compose Files Missing Health Checks:\033[0m \033[1;31m{len(missing_health_checks)}\033[0m")
for path in missing_health_checks:
print(f" \033[31m{os.path.relpath(path, args.root_dir)}\033[0m")
else:
print(f"\033[1;34m• Docker Compose Files Missing Health Checks:\033[0m \033[1;32m0\033[0m")