-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_solutions.py
More file actions
379 lines (300 loc) · 13.3 KB
/
Copy pathget_solutions.py
File metadata and controls
379 lines (300 loc) · 13.3 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
#!/usr/bin/env python3
"""
Скрипт для выгрузки решений студентов с GitLab.
Автоматически находит все репозитории студентов.
Структура выходных данных: task_name/<student_nickname>.h
"""
import os
import re
import subprocess
import tempfile
import shutil
import yaml
from pathlib import Path
from typing import Optional
import argparse
import requests
from urllib.parse import quote_plus
# Задачи, которые нужно исключить (не C++)
EXCLUDED_PATTERNS = [
r'^3-ilp-',
r'^6-ast',
r'^7-query',
r'^8-pass-',
]
GITLAB_URL = "https://gitlab.manytask.org"
GROUP_PATH = "pe101/students-2025-fall"
def is_excluded_task(task_name: str) -> bool:
"""Проверяет, нужно ли исключить задачу."""
for pattern in EXCLUDED_PATTERNS:
if re.match(pattern, task_name):
return True
return False
def is_valid_task_folder(folder_name: str) -> bool:
"""Проверяет, соответствует ли папка формату <week_number>-<name>-<task_number>."""
pattern = r'^\d+-[a-zA-Z]+'
return bool(re.match(pattern, folder_name))
def parse_task_yaml(task_yaml_path: Path) -> Optional[list[str]]:
"""Парсит .task.yml и возвращает список файлов из allow_change."""
try:
with open(task_yaml_path, 'r') as f:
data = yaml.safe_load(f)
if data and 'parameters' in data and 'allow_change' in data['parameters']:
allow_change = data['parameters']['allow_change']
if isinstance(allow_change, list):
return allow_change
return [allow_change]
except Exception as e:
print(f" Ошибка парсинга {task_yaml_path}: {e}")
return None
def normalize_content(content: str) -> str:
"""Нормализует контент для сравнения (убирает namespace различия)."""
# Заменяем namespace Solution на Copy для унификации
normalized = content.replace('namespace Solution', 'namespace Copy')
normalized = normalized.replace('Solution::', 'Copy::')
return normalized
def has_student_changes(solution_path: Path, copy_path: Path) -> bool:
"""
Проверяет, внёс ли студент изменения в файл.
Сравнивает с .copy.h, учитывая что namespace отличается.
"""
if not solution_path.exists():
return False
if not copy_path.exists():
# Если нет .copy.h, считаем что изменения есть
return True
try:
with open(solution_path, 'r', encoding='utf-8', errors='ignore') as f:
solution_content = f.read()
with open(copy_path, 'r', encoding='utf-8', errors='ignore') as f:
copy_content = f.read()
# Нормализуем и сравниваем
norm_solution = normalize_content(solution_content)
norm_copy = normalize_content(copy_content)
return norm_solution.strip() != norm_copy.strip()
except Exception as e:
print(f" Ошибка сравнения файлов: {e}")
return False
def get_all_students(token: str) -> list[dict]:
"""Получает список всех студентов (проектов) через GitLab API."""
headers = {'PRIVATE-TOKEN': token}
print("🔍 Получаю список студентов из GitLab...")
# Получаем ID группы
encoded_path = quote_plus(GROUP_PATH)
response = requests.get(
f"{GITLAB_URL}/api/v4/groups/{encoded_path}",
headers=headers
)
if response.status_code != 200:
print(f"❌ Ошибка получения группы: {response.status_code}")
print(f" {response.text}")
return []
group_id = response.json()['id']
print(f" Группа найдена, ID: {group_id}")
# Получаем все проекты в группе (с пагинацией)
students = []
page = 1
per_page = 100
while True:
response = requests.get(
f"{GITLAB_URL}/api/v4/groups/{group_id}/projects",
headers=headers,
params={
'page': page,
'per_page': per_page,
'include_subgroups': False,
'simple': True
}
)
if response.status_code != 200:
print(f"❌ Ошибка получения проектов: {response.status_code}")
break
projects = response.json()
if not projects:
break
for project in projects:
students.append({
'name': project['path'],
'http_url': project['http_url_to_repo'],
'id': project['id']
})
print(f" Загружено {len(students)} студентов...")
page += 1
print(f"✅ Всего найдено студентов: {len(students)}")
return students
def clone_repository(repo_url: str, target_dir: Path, token: str) -> bool:
"""Клонирует репозиторий студента с использованием токена."""
# Вставляем токен в URL
auth_url = repo_url.replace('https://', f'https://oauth2:{token}@')
try:
result = subprocess.run(
['git', 'clone', '--depth', '1', '--quiet', auth_url, str(target_dir)],
capture_output=True,
text=True,
timeout=120
)
return result.returncode == 0
except subprocess.TimeoutExpired:
print(f" ⏱️ Таймаут")
return False
except Exception as e:
print(f" ❌ Ошибка: {e}")
return False
def find_copy_file(task_dir: Path, solution_file: str) -> Optional[Path]:
"""Ищет соответствующий .copy.h файл."""
# Вариант 1: solution.copy.h
copy_filename = solution_file.replace('.h', '.copy.h')
copy_path = task_dir / copy_filename
if copy_path.exists():
return copy_path
# Вариант 2: .copy.h с таким же базовым именем
base_name = solution_file.replace('.h', '')
for f in task_dir.glob('*.copy.h'):
if base_name in f.name:
return f
# Вариант 3: любой .copy.h в папке
copy_files = list(task_dir.glob('*.copy.h'))
if len(copy_files) == 1:
return copy_files[0]
return None
def process_student(student: dict, output_dir: Path, temp_base: Path, token: str) -> dict[str, int]:
"""Обрабатывает репозиторий одного студента."""
stats = {'found': 0, 'skipped_no_changes': 0, 'skipped_excluded': 0, 'errors': 0}
student_name = student['name']
student_temp_dir = temp_base / student_name
if not clone_repository(student['http_url'], student_temp_dir, token):
stats['errors'] = 1
return stats
# Проходим по всем папкам в репозитории
try:
items = list(student_temp_dir.iterdir())
except Exception:
stats['errors'] = 1
return stats
for item in items:
if not item.is_dir():
continue
task_name = item.name
# Пропускаем служебные папки
if task_name.startswith('.'):
continue
# Проверяем формат имени
if not is_valid_task_folder(task_name):
continue
# Проверяем исключения
if is_excluded_task(task_name):
stats['skipped_excluded'] += 1
continue
task_dir = item
task_yaml = task_dir / '.task.yml'
if not task_yaml.exists():
continue
# Получаем список файлов с решением
solution_files = parse_task_yaml(task_yaml)
if not solution_files:
continue
for solution_file in solution_files:
if not solution_file.endswith('.h'):
continue
solution_path = task_dir / solution_file
if not solution_path.exists():
continue
# Ищем соответствующий .copy.h
copy_path = find_copy_file(task_dir, solution_file)
# Проверяем наличие изменений
if copy_path and not has_student_changes(solution_path, copy_path):
stats['skipped_no_changes'] += 1
continue
# Копируем решение
task_output_dir = output_dir / task_name
task_output_dir.mkdir(parents=True, exist_ok=True)
output_filename = f"{student_name}.h"
output_path = task_output_dir / output_filename
shutil.copy2(solution_path, output_path)
stats['found'] += 1
# Удаляем временную папку студента
shutil.rmtree(student_temp_dir, ignore_errors=True)
return stats
def main():
parser = argparse.ArgumentParser(description='Выгрузка решений студентов с GitLab')
parser.add_argument(
'-o', '--output',
default='solutions',
help='Выходная директория (default: solutions)'
)
parser.add_argument(
'-t', '--token',
help='GitLab токен (или переменная окружения GITLAB_TOKEN)'
)
parser.add_argument(
'--limit',
type=int,
default=0,
help='Ограничить количество студентов (для тестирования)'
)
args = parser.parse_args()
# Получаем токен
token = args.token or os.environ.get('GITLAB_TOKEN')
if not token:
print("❌ Нужен GitLab токен!")
print()
print("Как получить:")
print("1. Зайди на https://gitlab.manytask.org")
print("2. Settings → Access Tokens → Add new token")
print("3. Scopes: read_api, read_repository")
print("4. Создай и скопируй токен")
print()
print("Использование:")
print(" export GITLAB_TOKEN='твой-токен'")
print(" python download_solutions.py")
print()
print("Или:")
print(" python download_solutions.py -t 'твой-токен'")
return
# Получаем список студентов
students = get_all_students(token)
if not students:
print("❌ Не удалось получить список студентов")
return
if args.limit > 0:
students = students[:args.limit]
print(f"⚠️ Ограничено до {args.limit} студентов (для тестирования)")
output_dir = Path(args.output)
output_dir.mkdir(parents=True, exist_ok=True)
print(f"\n📁 Выходная директория: {output_dir}")
print(f"🎓 Обрабатываю {len(students)} студентов...\n")
total_stats = {'found': 0, 'skipped_no_changes': 0, 'skipped_excluded': 0, 'errors': 0}
with tempfile.TemporaryDirectory() as temp_dir:
temp_base = Path(temp_dir)
for i, student in enumerate(students, 1):
# Прогресс-бар
progress = f"[{i:3d}/{len(students)}]"
print(f"{progress} {student['name']:<30}", end=' ', flush=True)
stats = process_student(student, output_dir, temp_base, token)
# Результат для этого студента
if stats['errors']:
print("❌ ошибка клонирования")
elif stats['found'] == 0:
print("⏭️ нет решений")
else:
print(f"✅ {stats['found']} решений")
for key in total_stats:
total_stats[key] += stats[key]
# Итоговая статистика
print("\n" + "=" * 60)
print("📊 ИТОГО:")
print(f" ✅ Скачано решений: {total_stats['found']}")
print(f" ⏭️ Пропущено (без изменений): {total_stats['skipped_no_changes']}")
print(f" 🚫 Пропущено (не C++ задачи): {total_stats['skipped_excluded']}")
print(f" ❌ Ошибки клонирования: {total_stats['errors']}")
print("=" * 60)
# Показываем структуру
print(f"\n📂 Результат в папке: {output_dir}/")
task_dirs = sorted([d for d in output_dir.iterdir() if d.is_dir()])
for task_dir in task_dirs[:10]:
count = len(list(task_dir.glob('*.h')))
print(f" {task_dir.name}/ ({count} решений)")
if len(task_dirs) > 10:
print(f" ... и ещё {len(task_dirs) - 10} задач")
if __name__ == '__main__':
main()