-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrestoreToParents
executable file
·178 lines (134 loc) · 5.88 KB
/
restoreToParents
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
# Include all the common functions and libraries.
from cloudperm import *
import configparser
import csv
import json
import sys
import sqlite3
from sqlite3 import Error
print("This doesn't work yet")
sys.exit(1)
# Add on any specific arguments that this program requires.
try:
import argparse
parser = argparse.ArgumentParser(parents=[cloudperm_argparser])
parser.add_argument('jsonfile', metavar='JSONFile', type=str, nargs=1, help='Google Document IDs')
parser.add_argument('documents', metavar='DocumentID', type=str, nargs='+', help='Google Document IDs')
args = parser.parse_args()
except ImportError:
args = None
def print_usage():
print("""
restoreToParents <json_file> <docID1> [docID2 docID3...]
The 'json_file' file must be the output of listFiles -J
docID arguments are Google Drive document IDs of the
files you wish to put back into their parent folders
as specified in 'json_file'.
""")
#makeDB()
def restoreToParents(args):
"""
The purpose of this program is to help restore files to their original directory after
they have had their ownership transferred using the Google Drive -> Shared Drive -> Google Drive method
explained at https://www.tabgeeks.com/tabgeeks-blog/how-to-transfer-ownership-between-domains-in-google-drive
This program should only be run on document ids AFTER they have been moved back into Google Drive. It cannot
move files from Google Shared Drive to Google Drive or vice versa at this point
This program takes as arguments first a JSON format file that is the output of running listFiles -J.
The rest of the arguments are document IDs for files that the user wants to restore into their
original location.
Base cases to check for:
+ Are 2 or more arguments provided?
+ Is the first argument a JSON file that is the output of listFiles -J ?
+ Is the document to move a folder?
- Let's not deal with folders for now.
+ Are the rest of the arguments valid Document IDs?
+ Check by seeing if they have entries in the JSON file,
because if they don't, we couldn't work with them anyways.
- Are the document ids for files in Google Drive (not shared drive)?
- Do the destination parent folders still exist?
- Are there multiple parents that need to be restored to?
- Did the move work?
- Did the user have permission to move the file?
"""
pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(args)
#if args.documents:
# print("length: " + str(len(args.documents)))
credentials = get_credentials(args)
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
parser = configparser.ConfigParser()
parser.read("DriveConfig.INI")
# Get our list of file IDs to check, either form the config file or from the command line arguments.
fileids = [];
if len(args.jsonfile) != 1:
print_usage()
sys.exit(1)
if len(args.documents) < 1:
print_usage()
sys.exit(1)
try:
jsonfh = open(args.jsonfile[0], 'r')
jsondata = json.load(jsonfh)
except ImportError:
args = None
print("Error, couldn't load JSON data from json file.")
print_usage()
sys.exit(1)
# We'll keep a copy of all the document ids in the json data so that
# we can verify that a document passed later exists in the json data.
docs_in_jsondata = []
# Check if the JSON data is valid Google Drive API output. by looking for the drive#file value in each elements .kind.
for doc in jsondata:
#pp.pprint(doc)
#print(type(doc))
docs_in_jsondata.append(doc['id'])
if doc['kind'] != 'drive#file':
print("ERROR: JSON file has non-drive file type elements in it.")
sys.exit(1)
for docid in args.documents:
print("docid: " + str(docid))
# Find the jsondata entry for this docid if it exists.
founddoc = False
for doc in jsondata:
if doc['id'] == docid:
founddoc = True
print("Found document of type " + str(doc['mimeType']))
if doc['mimeType'] == "application/vnd.google-apps.folder":
print("ERROR: The document id provided " + str(docid) + " cannot be a folder.")
sys.exit(1)
if founddoc == False:
print("ERROR: Couldn't find docid " + str(docid) + " in the json file")
sys.exit(1)
# Check if the lastdocid is of type folder.
# lastdoc = service.files().get(fileId=lastdocid).execute()
# print(lastdocid)
# pp.pprint(lastdoc)
fromdocid = args.documents[0]
fromdoc = service.files().get(fileId=fromdocid).execute()
# if len(fromdoc['parents']) > 1:
# print("ERROR: This program doesn't handle cases where a document has more than one parent folder.")
# sys.exit(1)
# Get the doc's previous parent using the method from the API docs.
# filedata = service.files().get(fileId=fromdocid,
# fields='parents').execute()
# previous_parent = fromdoc.get('parents.id')
# filedata = service.files().update(fileId=fromdocid,
# addParents=lastdocid,
# removeParents=previous_parent,
# fields='id, parents').execute()
# addParents
# if args.documents:
# for fileid in args.documents:
# fileids.append(fileid)
#
# else:
# for section in parser.sections(): # Fix this later.
# for name,value in parser.items(section):
# if name == "url":
# fileids.append(value)
#####################################################################
# permission(fileids, service)
if __name__ == '__main__':
restoreToParents(args)