Repository navigation
Expand file tree
/
Copy pathteam-sync.py
More file actions
392 lines (317 loc) · 11.3 KB
/
Copy pathteam-sync.py
File metadata and controls
392 lines (317 loc) · 11.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
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python3
"""
DevFlow - Team Sync Tool
团队配置共享和同步
Usage:
python team-sync.py --export --team myteam
python team-sync.py --import --team myteam
python team-sync.py --init --team myteam
python team-sync.py --list
"""
import argparse
import json
import os
import sys
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional
# 默认配置目录
CONFIG_DIR = Path.home() / ".devflow" / "teams"
LOCAL_CONFIG = Path(".devflow.json")
# 默认团队配置模板
DEFAULT_TEAM_CONFIG = {
"version": "1.0.0",
"team_name": "",
"created_at": "",
"updated_at": "",
"members": [],
"rules": {
"security_level": "L4",
"quality_threshold": 7,
"max_function_length": 50,
"max_file_length": 500,
"require_tests": True,
"require_docs": True
},
"patterns": {
"allowed": [],
"blocked": []
},
"ignore": [
"node_modules/",
"vendor/",
"*.min.js",
"*.min.css",
"dist/",
"build/"
],
"custom_rules": []
}
def ensure_config_dir():
"""确保配置目录存在"""
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
def get_team_config_path(team_name: str) -> Path:
"""获取团队配置文件路径"""
return CONFIG_DIR / f"{team_name}.json"
def init_team_config(team_name: str) -> Dict:
"""初始化团队配置"""
ensure_config_dir()
config = DEFAULT_TEAM_CONFIG.copy()
config["team_name"] = team_name
config["created_at"] = datetime.now().isoformat()
config["updated_at"] = datetime.now().isoformat()
# 保存配置
config_path = get_team_config_path(team_name)
config_path.write_text(json.dumps(config, indent=2, ensure_ascii=False), encoding="utf-8")
return config
def load_team_config(team_name: str) -> Optional[Dict]:
"""加载团队配置"""
config_path = get_team_config_path(team_name)
if not config_path.exists():
return None
try:
return json.loads(config_path.read_text(encoding="utf-8"))
except Exception as e:
print(f"Error loading config: {e}", file=sys.stderr)
return None
def save_team_config(team_name: str, config: Dict) -> bool:
"""保存团队配置"""
ensure_config_dir()
config["updated_at"] = datetime.now().isoformat()
config_path = get_team_config_path(team_name)
try:
config_path.write_text(json.dumps(config, indent=2, ensure_ascii=False), encoding="utf-8")
return True
except Exception as e:
print(f"Error saving config: {e}", file=sys.stderr)
return False
def list_teams() -> List[str]:
"""列出所有团队"""
ensure_config_dir()
return [f.stem for f in CONFIG_DIR.glob("*.json")]
def export_config(team_name: str, output_path: str = None) -> str:
"""导出团队配置"""
config = load_team_config(team_name)
if not config:
print(f"Team '{team_name}' not found.", file=sys.stderr)
return None
config_json = json.dumps(config, indent=2, ensure_ascii=False)
if output_path:
Path(output_path).write_text(config_json, encoding="utf-8")
print(f"Config exported to: {output_path}")
else:
print(config_json)
return config_json
def import_config(team_name: str, config_path: str) -> bool:
"""导入团队配置"""
try:
config = json.loads(Path(config_path).read_text(encoding="utf-8"))
config["team_name"] = team_name
config["updated_at"] = datetime.now().isoformat()
return save_team_config(team_name, config)
except Exception as e:
print(f"Error importing config: {e}", file=sys.stderr)
return False
def sync_to_local(team_name: str) -> bool:
"""同步团队配置到本地项目"""
config = load_team_config(team_name)
if not config:
print(f"Team '{team_name}' not found.", file=sys.stderr)
return False
# 生成本地配置
local_config = {
"team": team_name,
"synced_at": datetime.now().isoformat(),
"rules": config["rules"],
"patterns": config["patterns"],
"ignore": config["ignore"]
}
try:
LOCAL_CONFIG.write_text(json.dumps(local_config, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"Config synced to {LOCAL_CONFIG}")
return True
except Exception as e:
print(f"Error syncing config: {e}", file=sys.stderr)
return False
def add_member(team_name: str, member: str) -> bool:
"""添加团队成员"""
config = load_team_config(team_name)
if not config:
config = init_team_config(team_name)
if member not in config["members"]:
config["members"].append(member)
return save_team_config(team_name, config)
return True
def remove_member(team_name: str, member: str) -> bool:
"""移除团队成员"""
config = load_team_config(team_name)
if not config:
return False
if member in config["members"]:
config["members"].remove(member)
return save_team_config(team_name, config)
return True
def update_rules(team_name: str, rules: Dict) -> bool:
"""更新团队规则"""
config = load_team_config(team_name)
if not config:
config = init_team_config(team_name)
config["rules"].update(rules)
return save_team_config(team_name, config)
def add_custom_rule(team_name: str, rule: Dict) -> bool:
"""添加自定义规则"""
config = load_team_config(team_name)
if not config:
config = init_team_config(team_name)
config["custom_rules"].append(rule)
return save_team_config(team_name, config)
def format_team_info(team_name: str, config: Dict) -> str:
"""格式化团队信息"""
lines = []
lines.append("=" * 60)
lines.append(f"Team: {config['team_name']}")
lines.append("=" * 60)
lines.append("")
lines.append(f"Created: {config.get('created_at', 'N/A')}")
lines.append(f"Updated: {config.get('updated_at', 'N/A')}")
lines.append("")
lines.append(f"Members ({len(config.get('members', []))}):")
for member in config.get("members", []):
lines.append(f" - {member}")
lines.append("")
lines.append("Rules:")
for key, value in config.get("rules", {}).items():
lines.append(f" - {key}: {value}")
lines.append("")
lines.append(f"Ignore Patterns: {len(config.get('ignore', []))}")
lines.append(f"Custom Rules: {len(config.get('custom_rules', []))}")
lines.append("=" * 60)
return "\n".join(lines)
def main():
parser = argparse.ArgumentParser(
description="DevFlow Team Sync Tool",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Initialize team config
python team-sync.py --init --team myteam
# Export team config
python team-sync.py --export --team myteam
# Import team config
python team-sync.py --import --team myteam --file config.json
# Sync to local project
python team-sync.py --sync --team myteam
# List all teams
python team-sync.py --list
# Add member
python team-sync.py --add-member --team myteam --member john
# Update rules
python team-sync.py --update-rules --team myteam --rules '{"security_level": "L3"}'
"""
)
parser.add_argument("--init", action="store_true", help="Initialize team config")
parser.add_argument("--export", action="store_true", help="Export team config")
parser.add_argument("--import", dest="import_config", action="store_true", help="Import team config")
parser.add_argument("--sync", action="store_true", help="Sync to local project")
parser.add_argument("--list", action="store_true", help="List all teams")
parser.add_argument("--info", action="store_true", help="Show team info")
parser.add_argument("--add-member", action="store_true", help="Add team member")
parser.add_argument("--remove-member", action="store_true", help="Remove team member")
parser.add_argument("--update-rules", action="store_true", help="Update team rules")
parser.add_argument("--add-rule", action="store_true", help="Add custom rule")
parser.add_argument("--team", "-t", help="Team name")
parser.add_argument("--member", "-m", help="Member name")
parser.add_argument("--file", "-f", help="Config file path")
parser.add_argument("--rules", help="Rules JSON string")
parser.add_argument("--output", "-o", help="Output file path")
args = parser.parse_args()
# 列出团队
if args.list:
teams = list_teams()
if teams:
print("Teams:")
for team in teams:
print(f" - {team}")
else:
print("No teams found.")
return
# 需要团队名称的操作
if not args.team:
print("Error: --team is required", file=sys.stderr)
sys.exit(1)
# 初始化
if args.init:
config = init_team_config(args.team)
print(f"Team '{args.team}' initialized.")
print(format_team_info(args.team, config))
return
# 导出
if args.export:
export_config(args.team, args.output)
return
# 导入
if args.import_config:
if not args.file:
print("Error: --file is required for import", file=sys.stderr)
sys.exit(1)
if import_config(args.team, args.file):
print(f"Config imported for team '{args.team}'")
return
# 同步到本地
if args.sync:
sync_to_local(args.team)
return
# 显示信息
if args.info:
config = load_team_config(args.team)
if config:
print(format_team_info(args.team, config))
else:
print(f"Team '{args.team}' not found.")
return
# 添加成员
if args.add_member:
if not args.member:
print("Error: --member is required", file=sys.stderr)
sys.exit(1)
if add_member(args.team, args.member):
print(f"Member '{args.member}' added to team '{args.team}'")
return
# 移除成员
if args.remove_member:
if not args.member:
print("Error: --member is required", file=sys.stderr)
sys.exit(1)
if remove_member(args.team, args.member):
print(f"Member '{args.member}' removed from team '{args.team}'")
return
# 更新规则
if args.update_rules:
if not args.rules:
print("Error: --rules is required", file=sys.stderr)
sys.exit(1)
try:
rules = json.loads(args.rules)
if update_rules(args.team, rules):
print(f"Rules updated for team '{args.team}'")
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON: {e}", file=sys.stderr)
sys.exit(1)
return
# 添加自定义规则
if args.add_rule:
if not args.rules:
print("Error: --rules is required", file=sys.stderr)
sys.exit(1)
try:
rule = json.loads(args.rules)
if add_custom_rule(args.team, rule):
print(f"Custom rule added to team '{args.team}'")
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON: {e}", file=sys.stderr)
sys.exit(1)
return
# 默认显示帮助
parser.print_help()
if __name__ == "__main__":
main()