-
Notifications
You must be signed in to change notification settings - Fork 52
/
GetTeamDriveMembers.py
executable file
·86 lines (75 loc) · 3.75 KB
/
GetTeamDriveMembers.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
85
86
#!/usr/bin/env python3
"""
# Purpose: Get organizers and members for Team Drives
# Note: This script can use GAM7 or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# Customize: DELIMITER, DOMAIN_LIST, INCLUDE_TYPES
# 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: If you want to include all Team Drives, do this step and then skip to step 4, otherwise start at step 2.
# $ gam redirect csv ./TeamDrives.csv print teamdrives fields id,name
# 2: If you want Team Drives for a specific set of organizers, replace <UserTypeEntity> with your user selection in the command below
# $ gam redirect csv ./AllTeamDrives.csv <UserTypeEntity> print teamdrives fields id,name
# 3: Delete duplicate Team Drives (some may have multiple organizers). Make sure that ID_FIELD = 'id' in DeleteDuplicateRows.py
# $ python3 DeleteDuplicateRows.py ./AllTeamDrives.csv ./TeamDrives.csv
# 4: Get ACLs for all Team Drives
# $ gam redirect csv ./TeamDriveACLs.csv multiprocess csv ./TeamDrives.csv gam print drivefileacls "~id" fields id,emailaddress,role,type,deleted
# 5: From that list of ACLs, output a CSV file with headers "id,name,organizers,members"
# that shows the organizers for each Team Drive
# $ python3 GetTeamDriveMembers.py TeamDriveACLs.csv TeamDrives.csv TeamDriveMembers.csv
"""
import csv
import re
import sys
DELIMITER = ' ' # character that separates list members
# If you want to limit organizers/members to a specific list of domains, use the list below, e.g., DOMAIN_LIST = ['domain.com',] DOMAIN_LIST = ['domain1.com', 'domain2.com',]
DOMAIN_LIST = []
INCLUDE_TYPES = {
'user': True, # False - don't show user organizers/members, True - show user organizers/members
'group': True, # False - don't show group organizers/members, True - show group organizers/members
}
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
PERMISSIONS_N_ROLE = re.compile(r"permissions.(\d+).role")
if (len(sys.argv) > 3) and (sys.argv[3] != '-'):
outputFile = open(sys.argv[3], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, ['id', 'name', 'organizers', 'members'], lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
teamDriveNames = {}
inputFile = open(sys.argv[2], 'r', encoding='utf-8')
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
teamDriveNames[row['id']] = row['name']
inputFile.close()
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
else:
inputFile = sys.stdin
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
organizers = []
members = []
for k, v in iter(row.items()):
mg = PERMISSIONS_N_ROLE.match(k)
if mg and v:
roleList = organizers if v == 'organizer' else members
permissions_N = mg.group(1)
if row.get(f'permissions.{permissions_N}.deleted') == 'True':
continue
if not INCLUDE_TYPES[row[f'permissions.{permissions_N}.type']]:
continue
member = row[f'permissions.{permissions_N}.emailAddress']
if DOMAIN_LIST and member[member.find('@')+1:] not in DOMAIN_LIST:
continue
roleList.append(member)
outputCSV.writerow({'id': row['id'],
'name': teamDriveNames.get(row['id'], row['id']),
'organizers': DELIMITER.join(organizers),
'members': DELIMITER.join(members)})
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()