-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-add-workflows.py
More file actions
407 lines (319 loc) · 11.9 KB
/
batch-add-workflows.py
File metadata and controls
407 lines (319 loc) · 11.9 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python3
"""
Batch script to add static analysis workflows to multiple repositories.
This script processes multiple repositories in parallel or sequentially,
applying the static analysis workflow to each one and generating a consolidated
report of all operations.
"""
import sys
import json
import argparse
import subprocess
from pathlib import Path
from typing import List, Dict, Tuple, Optional
from datetime import datetime
import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class BatchWorkflowManager:
"""Manages batch operations for adding workflows to multiple repositories."""
def __init__(self, repos: List[str], parallel: bool = False, max_workers: int = 4):
"""Initialize the batch manager.
Args:
repos: List of repository paths
parallel: Whether to process repositories in parallel
max_workers: Maximum number of parallel workers
"""
self.repos = [Path(r).resolve() for r in repos]
self.parallel = parallel
self.max_workers = max_workers
self.results = []
def _create_result(self, repo_path: Path, success: bool = False, error: str = None) -> Dict:
"""Create a standardized result dictionary.
Args:
repo_path: Path to the repository
success: Whether the operation was successful
error: Error message if any
Returns:
Dictionary with result information
"""
return {
"repository": str(repo_path),
"timestamp": datetime.now().isoformat(),
"success": success,
"error": error
}
def _validate_repository(self, repo_path: Path) -> Tuple[bool, Optional[str]]:
"""Validate repository path and structure.
Args:
repo_path: Path to validate
Returns:
Tuple of (is_valid, error_message)
"""
if not repo_path.exists():
return False, "Repository path does not exist"
if not repo_path.is_dir():
return False, "Repository path is not a directory"
if not (repo_path / ".git").exists():
return False, "Not a git repository"
return True, None
def _run_workflow_script(self, repo_path: Path) -> Tuple[bool, Optional[str]]:
"""Run the workflow script on a repository.
Args:
repo_path: Path to the repository
Returns:
Tuple of (success, error_message)
"""
cmd = [
sys.executable,
str(Path(__file__).parent / "add-static-analysis-workflow.py"),
str(repo_path)
]
try:
process_result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=60
)
if process_result.returncode != 0:
return False, f"Script failed: {process_result.stderr}"
return True, None
except subprocess.TimeoutExpired:
return False, "Script execution timed out"
except Exception as e:
return False, f"Unexpected error: {str(e)}"
def process_repository(self, repo_path: Path) -> Dict:
"""Process a single repository.
Args:
repo_path: Path to the repository
Returns:
Dictionary with result information
"""
logger.info(f"Processing repository: {repo_path}")
# Validate repository
is_valid, error = self._validate_repository(repo_path)
if not is_valid:
logger.error(f"{repo_path}: {error}")
return self._create_result(repo_path, success=False, error=error)
# Run workflow script
success, error = self._run_workflow_script(repo_path)
if success:
logger.info(f"Successfully processed: {repo_path}")
else:
logger.error(f"{repo_path}: {error}")
return self._create_result(repo_path, success=success, error=error)
def process_all(self) -> List[Dict]:
"""Process all repositories.
Returns:
List of result dictionaries
"""
if self.parallel:
return self._process_parallel()
else:
return self._process_sequential()
def _process_sequential(self) -> List[Dict]:
"""Process repositories sequentially."""
results = []
total = len(self.repos)
for idx, repo in enumerate(self.repos, 1):
logger.info(f"Processing {idx}/{total}: {repo}")
result = self.process_repository(repo)
results.append(result)
return results
def _process_parallel(self) -> List[Dict]:
"""Process repositories in parallel."""
results = []
total = len(self.repos)
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
future_to_repo = {
executor.submit(self.process_repository, repo): repo
for repo in self.repos
}
for idx, future in enumerate(as_completed(future_to_repo), 1):
repo = future_to_repo[future]
try:
result = future.result()
results.append(result)
logger.info(f"Completed {idx}/{total}: {repo}")
except Exception as e:
logger.error(f"Error processing {repo}: {e}")
results.append({
"repository": str(repo),
"timestamp": datetime.now().isoformat(),
"success": False,
"error": str(e)
})
return results
def _get_action_from_log(self, repo_path: Path) -> str:
"""Get the action from a repository's tracking log.
Args:
repo_path: Path to the repository
Returns:
Action string or None if not found
"""
log_file = repo_path / "tracking-log.json"
if not log_file.exists():
return None
try:
with open(log_file, 'r') as f:
logs = json.load(f)
if not logs:
return None
return logs[-1].get("action")
except Exception:
return None
def _count_actions(self, results: List[Dict]) -> Dict[str, int]:
"""Count actions taken across all repositories.
Args:
results: List of result dictionaries
Returns:
Dictionary of action counts
"""
actions = {"created": 0, "updated": 0, "skipped": 0}
for result in results:
if not result["success"]:
continue
repo_path = Path(result["repository"])
action = self._get_action_from_log(repo_path)
if action in actions:
actions[action] += 1
return actions
def generate_summary_report(self, results: List[Dict]) -> Dict:
"""Generate a summary report of all operations.
Args:
results: List of result dictionaries
Returns:
Summary dictionary
"""
total = len(results)
successful = sum(1 for r in results if r["success"])
failed = total - successful
actions = self._count_actions(results)
return {
"total_repositories": total,
"successful": successful,
"failed": failed,
"actions": actions,
"timestamp": datetime.now().isoformat()
}
def save_batch_report(self, results: List[Dict], output_file: str):
"""Save batch processing report to file.
Args:
results: List of result dictionaries
output_file: Path to output file
"""
summary = self.generate_summary_report(results)
report = {
"summary": summary,
"results": results
}
output_path = Path(output_file)
with open(output_path, 'w') as f:
json.dump(report, f, indent=2)
logger.info(f"Saved batch report to {output_path}")
def print_summary(self, results: List[Dict]):
"""Print summary to console.
Args:
results: List of result dictionaries
"""
summary = self.generate_summary_report(results)
print("\n" + "=" * 80)
print("BATCH PROCESSING SUMMARY")
print("=" * 80)
print(f"Total Repositories: {summary['total_repositories']}")
print(f"Successful: {summary['successful']}")
print(f"Failed: {summary['failed']}")
print("\nActions Taken:")
print(f" - Created: {summary['actions']['created']}")
print(f" - Updated: {summary['actions']['updated']}")
print(f" - Skipped: {summary['actions']['skipped']}")
print("=" * 80)
if summary['failed'] > 0:
print("\nFailed Repositories:")
print("-" * 80)
for result in results:
if not result["success"]:
print(f" - {result['repository']}: {result.get('error', 'Unknown error')}")
print("=" * 80)
def read_repo_list(file_path: str) -> List[str]:
"""Read repository list from file.
Args:
file_path: Path to file containing repository paths (one per line)
Returns:
List of repository paths
"""
repos = []
with open(file_path, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
repos.append(line)
return repos
def _create_argument_parser():
"""Create and configure argument parser.
Returns:
Configured ArgumentParser instance
"""
parser = argparse.ArgumentParser(
description="Batch add static analysis workflows to multiple repositories"
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--repos",
nargs="+",
help="List of repository paths"
)
group.add_argument(
"--repo-file",
help="File containing repository paths (one per line)"
)
parser.add_argument(
"--parallel",
action="store_true",
help="Process repositories in parallel"
)
parser.add_argument(
"--max-workers",
type=int,
default=4,
help="Maximum number of parallel workers (default: 4)"
)
parser.add_argument(
"--output",
default="batch-report.json",
help="Output file for batch report (default: batch-report.json)"
)
return parser
def _get_repository_list(args):
"""Get list of repositories from arguments.
Args:
args: Parsed command-line arguments
Returns:
List of repository paths
"""
if args.repos:
return args.repos
return read_repo_list(args.repo_file)
def main():
"""Main entry point."""
parser = _create_argument_parser()
args = parser.parse_args()
repos = _get_repository_list(args)
if not repos:
logger.error("No repositories specified")
return 1
logger.info(f"Processing {len(repos)} repositories")
manager = BatchWorkflowManager(repos, args.parallel, args.max_workers)
results = manager.process_all()
manager.save_batch_report(results, args.output)
manager.print_summary(results)
failed = sum(1 for r in results if not r["success"])
return 1 if failed > 0 else 0
if __name__ == "__main__":
sys.exit(main())