forked from taers232c/GAM-Scripts3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddOrgUnit.py
executable file
·79 lines (68 loc) · 2.82 KB
/
AddOrgUnit.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
#!/usr/bin/env python3
"""
# Purpose: For a CSV file, look up user email addresses and add Org Unit information
# Note: This script can use Basic or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# Customize: DATA_EMAIL_HEADER, DATA_ORGUNIT_HEADER, USER_EMAIL_HEADER, USER_ORGUNIT_HEADER, UNKNOWN_ORGUNIT
# 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: Generate a list of users and their Org Units
# $ gam print users fields primaryemail,ou > Users.csv
# 2: Generate some data
# $ gam ... > Data.csv
# $ gam redirect csv Data.csv ...
# 3: From those two files, generate an output CSV file with the same headers as Data.csv plus a header for the users's Org Unit
# $ python3 AddOrgUnit.py ./Data.csv ./Users.csv ./DataWithOrgUnit.csv
"""
import csv
import sys
# You have to indicate the header in Data.csv that contains the user email addresses
# and the desired Org Unit header in DataWithOrgUnit.csv
# Common values are:
# report login - actor.email, actor.orgUnitPath
# report user - email, orgUnitPath
DATA_EMAIL_HEADER = 'email'
DATA_ORGUNIT_HEADER = 'orgUnitPath'
# Email header in Users.csv CSV file
USER_EMAIL_HEADER = 'primaryEmail'
# OrgUnit header in Users.csv CSV file
USER_ORGUNIT_HEADER = 'orgUnitPath'
# Unkown Org Unit value
UNKNOWN_ORGUNIT = 'Unknown'
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
userOrgUnits = {}
inputFile = open(sys.argv[2], 'r', encoding='utf-8')
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
userOrgUnits[row[USER_EMAIL_HEADER]] = row[USER_ORGUNIT_HEADER]
inputFile.close()
if sys.argv[1] != '-':
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
else:
inputFile = sys.stdin
inputCSV = csv.DictReader(inputFile, quotechar=QUOTE_CHAR)
outputFieldNames = inputCSV.fieldnames[:]
if outputFieldNames is None:
sys.stderr.write(f'Error: no headers in Data file {sys.argv[1]}\n')
sys.exit(2)
if DATA_EMAIL_HEADER not in outputFieldNames:
sys.stderr.write(f'Error: field {DATA_EMAIL_HEADER} is not in Data file {sys.argv[1]} field names: {",".join(outputFieldNames)}\n')
sys.exit(3)
index = outputFieldNames.index(DATA_EMAIL_HEADER)
outputFieldNames.insert(index+1, DATA_ORGUNIT_HEADER)
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, outputFieldNames, lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
for row in inputCSV:
row[DATA_ORGUNIT_HEADER] = userOrgUnits.get(row[DATA_EMAIL_HEADER], UNKNOWN_ORGUNIT)
outputCSV.writerow(row)
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()