-
Notifications
You must be signed in to change notification settings - Fork 2
/
remove_dropped.py
executable file
·84 lines (64 loc) · 2.23 KB
/
remove_dropped.py
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
#!/usr/bin/env python3
import sys
import boatswain_env as benv
import argparse
import csv
import os
from github import Github
CMD_NAME = 'drop_students'
DESC = 'Remove dropped students from GitHub team repo'
def drop_students_deco(parser):
parser.add_argument('org',
type=str,
help='organization name',
metavar='<org>',
)
parser.add_argument('roster',
type=str,
help='roster CSV file',
nargs='?',
default='',
metavar='<roster.csv>')
parser.add_argument('github_handles',
type=str,
help='matches uni to github handle CSV file',
nargs='?',
default='',
metavar='<uni_to_github.csv>')
args = parser.parse_args()
if args.roster == '' or args.github_handles == '':
parser.print_usage()
print(f"{parser.prog}: error: too few arguments")
exit()
def find_and_drop_students(opt):
with open(opt.roster) as r:
roster = csv.DictReader(r)
roster_l = [x['SIS Login ID'] for x in roster]
with open(opt.github_handles) as g:
handles = csv.DictReader(g)
for row in handles:
if row['UNI'] not in roster_l:
remove_collaborator(row['GitHub Handle'], opt)
def remove_collaborator(handle, opt):
g = Github(opt.githubToken())
org = g.get_organization(opt.org)
user = g.get_user(handle)
if not opt.promptYes(f"Are you sure you would like to remove {handle} as a member of {org}?", True):
opt.warn('Aborting')
return
try:
do_remove_collaborator(org, user, opt)
except Exception as e:
opt.error(e)
opt.error(f"{CMD_NAME} failed on {user}")
return
def do_remove_collaborator(org, user, opt):
org.remove_from_members(user)
def main(args=None):
if args is None:
args = sys.argv[1:]
opt = benv.ParseOption(args, section=None, config_path=None,
desc=DESC, parse_deco=drop_students_deco, req_github=True)
find_and_drop_students(opt)
if __name__ == '__main__':
main()