-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGetMultipleParentsRoot.py
More file actions
executable file
·51 lines (45 loc) · 1.83 KB
/
GetMultipleParentsRoot.py
File metadata and controls
executable file
·51 lines (45 loc) · 1.83 KB
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 python
"""
# Purpose: For a Google Drive User, delete root as a parent of files that have root as a parent and other parents
# Note: This uses advanced GAM: https://github.com/taers232c/GAMADV-X
# Usage:
# 1: Get all of the files for testuser@domain.com
# $ gam redirect csv ./userfiles.csv user testuser@domain.com print filelist id title parents
# 2: From that list of files, output a CSV file with headers "Owner,driveFileId,title"
# that lists the driveFileIds for all files that have root as a parent and other parents
# $ python GetMultipleParentsRoot.py ./userfiles.csv ./rootparents.csv
# 3: Inspect rootparents.csv, verify that it makes sense and then proceed
# 4: Delete root as parent
# $ gam redirect stdout ./deleterootparent.out multiprocess csv ./rootparents.csv gam user "~Owner" update drivefile "~driveFileId" removeparents root
"""
import csv
import re
import sys
id_n_address = re.compile(r"parents.(\d+).id")
if (len(sys.argv) > 2) and (sys.argv[2] != '-'):
outputFile = open(sys.argv[2], 'wb')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, ['Owner', 'driveFileId', 'title'], lineterminator='\n')
outputCSV.writeheader()
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'rb')
else:
inputFile = sys.stdin
for row in csv.DictReader(inputFile):
if row['parents'] and int(row['parents']) <= 1:
continue
for k, v in row.iteritems():
mg = id_n_address.match(k)
if mg:
perm_group = mg.group(1)
if v:
if row['parents.{0}.isRoot'.format(perm_group)] == 'True':
outputCSV.writerow({'Owner': row['Owner'],
'driveFileId': row['id'],
'title': row['title']})
continue
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()