-
Notifications
You must be signed in to change notification settings - Fork 2
/
gdm.py
executable file
·77 lines (59 loc) · 2.6 KB
/
gdm.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
#! /usr/bin/python
import sys
import socket
if socket.gethostname() in ['trinh-pc',]: # add your hostname here
from settings_local import *
else:
from settings import *
from gapis import *
from commons import *
#~ from rename_dup import rename_all_dup_files
# email_list = [{'src': '[email protected]', 'dest': '[email protected]'}]
def google_drive_migrate(csv_file, condition_number):
email_map_list = get_dict_data_from_csv_file(csv_file)
for email in email_map_list:
num = str_to_num(email['src']) % 10
if num in condition_number or condition_number[0]==-1:
src_service = create_drive_service(SERVICE_ACCOUNT_PRIVATE_KEY,\
SERVICE_ACCOUNT, OAUTH_SCOPE, email['src'])
if src_service:
print "Processing %s" % (email['src'])
# rename duplicate files/folders before migrating
print "Renaming duplicate files and folders of user %s" % (email['src'])
rename_all_dup_files(src_service)
print "Finish renaming files and folders of user %s" % (email['src'])
dest_service = create_drive_service(SERVICE_ACCOUNT_PRIVATE_KEY,\
SERVICE_ACCOUNT, OAUTH_SCOPE, email['dest'])
if dest_service:
files = get_own_files(src_service)
if files:
files_map = [{'src': email['src'], 'dest': email['dest'], 'files': files}]
# Step 1. share files with new account
print "\nShare permissions to destionation account %s\n" % email['dest']
perms, shared_files = share_files(src_service, files_map)
print "Share %s files\n" % len(shared_files)
# Step 2. make a copy of shared files in new account
print "Make a copy of shared files of user %s\n" % email['dest']
new_files_map = make_a_copy(dest_service, shared_files)
# Step 3. disable sharing on source account
print "Disable sharing on source account %s\n" % email['src']
disable_sharing(src_service, perms)
# Step 4. copy permissions
if new_files_map:
print "Copy permissions of all files of %s" % email['src']
copy_perms(src_service, dest_service, email['src'], email['dest'], new_files_map)
else:
print "User %s has no file" % email['dest']
else:
print "Canot initiate drive service of user %s. Skipped!" % (email['dest'])
else:
print "Skip processing user %s" % (email['src'])
print "\nFinish migrating user %s\n" % (email['src'])
#########################################################################
if __name__ == "__main__":
src_csv = sys.argv[1]
if sys.argv[2] == 'all':
condition_number = [-1]
else:
condition_number = map(int, sys.argv[2].split(','))
google_drive_migrate(src_csv, condition_number)