-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstealFolder
executable file
·164 lines (121 loc) · 5.25 KB
/
stealFolder
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
#!/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('documents', metavar='DocumentID', type=str, nargs=1, help='Google Document IDs')
args = parser.parse_args()
except ImportError:
args = None
def print_usage():
print("""
stealFolder <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 stealFolder(args):
"""
Base cases to check for:
+ Is only 1 argument provided?
+ The only arguments passed should be a document id
+ Is the document to move a folder?
+ Is the folder inside the top level My Drive?
+ Is the owner of the folder the same as the running user?
- Are there more than 490 files in the folder?
- Does the folder have more than one parent?
- Are there any files left over in the old folder after doing the initial operation.
- Maybe we should be locking out changes by removing permissions on the old folder after the first migration?
"""
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 from the config file or from the command line arguments.
#fileids = [];
# For now we're only going to handle one folder
if len(args.documents) < 1:
print_usage()
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()
fromdoctitle = fromdoc['title']
newfromdoctitle = fromdoctitle + " (NEW DESTINATION)"
parentfolders = []
# Have to be able to handle multiple parents.
for parentfolder in fromdoc['parents']:
parentfolders.append(parentfolder['id'])
print("Parent folder id: " + parentfolder['id'])
print("Folder owner: " + fromdoc['ownerNames'][0] + " (" + fromdoc['owners'][0]['emailAddress'] + ")")
# Get the old folder sharing settings.
fromdocperms = retrieve_permissions(service, fromdocid)
# pp.pprint(fromdocperms)
# Create the new folder.
new_folder_metadata = {
'title': newfromdoctitle,
'parents': fromdoc['parents'],
'mimeType': 'application/vnd.google-apps.folder'
}
newfolder = service.files().insert(body=new_folder_metadata, fields='id').execute()
print('New Folder ID: %s' % newfolder.get('id'))
newfolderid = newfolder['id']
childfiles = get_files_in_folder(service, fromdocid)
number_of_files = len(childfiles)
print("folder file count: " + str(number_of_files))
if (number_of_files > 490):
print("ERROR: This program may encounter an API bug with more than 490 files in a folder so is avoiding that situation.")
for childfile in childfiles:
child_file_id = childfile['id']
print("child file: " + childfile['title'] + " " + child_file_id)
# Because of the way this works, we dont' need to check if each child file/folder has multiple parents.
# We just need to add the new one and remove the old one.
childfile = service.files().update(fileId=child_file_id,
addParents=newfolderid,
removeParents=fromdocid,
fields='id, parents').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__':
stealFolder(args)