-
Notifications
You must be signed in to change notification settings - Fork 52
/
CheckMembership.py
executable file
·66 lines (56 loc) · 2.67 KB
/
CheckMembership.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
#!/usr/bin/env python3
"""
# Purpose: For a list of group members and a list of users, produce a CSV file that lists the users that are not group members
# Note: This script can use GAM7 or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# Python: Use python or python3 below as appropriate to your system; verify that you have version 3
# $ python -V or python3 -V
# Python 3.x.y
# Usage:
# 1: Get group members
# $ gam redirect csv ./Members.csv group <GroupName> print
# 2: Get users; replace <UserTypeEntity> as desired, e.g. ou /Teachers
# $ gam redirect csv ./Users.csv <UserTypeEntity> print
# 3: Make a CSV file NonMembers.csv that lists the users that are not group members
# $ python3 CheckMembership.py Members.csv Users.csv NonMembers.csv
"""
import csv
import sys
# Default is that Members.csv does not have a header row; the following sets a field name
MembersEmailField = 'primaryEmail'
MembersFieldNames = [MembersEmailField]
# If Members.csv does have a header row, edit the following line and remove the # from both lines
#MembersEmailField = 'primaryEmail'
#MembersFieldNames = None
# Default is that Users.csv does not have a header row; the following sets a field name
UsersEmailField = 'primaryEmail'
UsersFieldNames = [UsersEmailField]
# If Users.csv does have a header row, edit the following line and remove the # from both lines
#UsersEmailField = 'primaryEmail'
#UsersFieldNames = None
# Edit the following row if you want a different header for NonMembers.csv
NonMembersEmailField = 'primaryEmail'
NonMembersFieldNames = [NonMembersEmailField]
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
MembersSet = set()
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
inputCSV = csv.DictReader(inputFile, fieldnames=MembersFieldNames, quotechar=QUOTE_CHAR)
for row in inputCSV:
MembersSet.add(row[MembersEmailField])
inputFile.close()
inputFile = open(sys.argv[2], 'r', encoding='utf-8')
inputCSV = csv.DictReader(inputFile, fieldnames=UsersFieldNames, quotechar=QUOTE_CHAR)
if (len(sys.argv) > 3) and (sys.argv[2] != '-'):
outputFile = open(sys.argv[3], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, NonMembersFieldNames, lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
for row in inputCSV:
if row[UsersEmailField] not in MembersSet:
outputCSV.writerow({NonMembersEmailField: row[UsersEmailField]})
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()