-
Notifications
You must be signed in to change notification settings - Fork 52
/
GetDriveActivityEmailAddresses.py
executable file
·51 lines (43 loc) · 2.23 KB
/
GetDriveActivityEmailAddresses.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
#!/usr/bin/env python3
"""
# Purpose: Get email addresses for users identified by permissionId in gam print driveactivity when the v2 option is not used
# 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 permissionId from Drive Settings. Substitute for all users if applicable.
# $ Example: gam config auto_batch_min 1 redirect csv ./DriveSettings.csv multiprocess all users print drivesettings fields permissionid
# 2: Generate drive activity
# $ Example: gam config auto_batch_min 1 redirect csv ./DriveActivity.csv multiprocess <UserTypeEntity> print driveactivity ...
# 3: From DriveSettings.csv and DriveActivity.csv generate DriveActivityEmail.csv with the additional column user.emailAddress
# $ python3 GetDriveActivityEmailAddresses.py DriveSettings.csv DriveActivity.csv DriveActivityEmail.csv
"""
import csv
import sys
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
USER_PERMISSIONID = 'user.permissionId'
USER_EMAILADDRESS = 'user.emailAddress'
permissionIdEmailMap = {}
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
permissionIdEmailMap[row['permissionId']] = row['email']
inputFile.close()
inputFile = open(sys.argv[2], 'r', encoding='utf-8')
inputCSV = csv.DictReader(inputFile, quotechar=QUOTE_CHAR)
fieldnames = inputCSV.fieldnames[:]
fieldnames.insert(1, USER_EMAILADDRESS)
outputFile = open(sys.argv[3], 'w', encoding='utf-8', newline='')
outputCSV = csv.DictWriter(outputFile, fieldnames, lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
for row in inputCSV:
permissionId = row[USER_PERMISSIONID]
if permissionId not in permissionIdEmailMap:
permissionIdEmailMap[permissionId] = 'Unknown'
row[USER_EMAILADDRESS] = permissionIdEmailMap[permissionId]
outputCSV.writerow(row)
inputFile.close()
outputFile.close()