-
Notifications
You must be signed in to change notification settings - Fork 90
/
dcli.py
755 lines (715 loc) · 28.4 KB
/
dcli.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
from __future__ import print_function
import os
import sys
import io
import re
import getpass
import subprocess
import requests
import click
import json
import time
import colorama
from pick import pick
from pick import Picker
from prettytable import PrettyTable
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload,MediaFileUpload
from httplib2 import Http
from oauth2client import file, client, tools
from .utils import MIMETYPES
import pyfiglet
dirpath = os.path.dirname(os.path.realpath(__file__))
SCOPES = 'https://www.googleapis.com/auth/drive'
def login(remote):
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
if not creds or creds.invalid:
client_id = os.path.join(dirpath,'oauth.json')
flow = client.flow_from_clientsecrets(client_id,SCOPES)
flags=tools.argparser.parse_args(args=[])
if remote:
flags.noauth_local_webserver = True
creds = tools.run_flow(flow, store,flags)
click.secho("********************** welcome to **********************",bold=True,fg='red')
result = pyfiglet.figlet_format("Drive - CLI", font = "slant" )
click.secho(result,fg='yellow')
click.secho("********************************************************",bold=True,fg='red')
def go_back(picker):
return None, -1
def drive_data(*argv):
dclipath = os.path.join(dirpath,'.drivecli')
if not os.path.isfile(dclipath):
with open(dclipath,'w')as outfile:
if(not len(argv)):
data = {}
else:
data = argv[0]
json.dump(data,outfile)
else:
if(not len(argv)):
with open(dclipath, 'r') as infile:
data = json.load(infile)
else:
with open(dclipath,'w') as outfile:
data = argv[0]
json.dump(data,outfile)
return data
def get_request(service,fid,mimeType):
if(re.match('^application/vnd\.google-apps\..+',mimeType)):
if(mimeType == 'application/vnd.google-apps.document'):
mimeTypes={
"pdf": 'application/pdf',
"txt":'text/plain',
"doc":'application/msword',
"zip":'application/zip',
"html":'text/html',
"rtf":"application/rtf",
"odt":"application/vnd.oasis.opendocument.text"
}
elif(mimeType == 'application/vnd.google-apps.spreadsheet'):
mimeTypes={
"pdf": 'application/pdf',
"xlsx" :'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
"zip":'application/zip',
"html":'text/html',
"ods":'application/vnd.oasis.opendocument.spreadsheet',
"csv":'text/plain',
"tsv":"text/tab-separated-values" ,
}
elif(mimeType == 'application/vnd.google-apps.presentation'):
mimeTypes={
"pdf": 'application/pdf',
"zip":'application/zip',
"html":'text/html',
"pptx":"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"plain txt":'text/plain'
}
else:
mimeTypes={
"ods":'application/vnd.oasis.opendocument.spreadsheet',
"csv":'text/plain',
"tmpl":'text/plain',
"pdf": 'application/pdf',
"php":'application/x-httpd-php',
"jpg":'image/jpeg',
"png":'image/png',
"gif":'image/gif',
"bmp":'image/bmp',
"txt":'text/plain',
"doc":'application/msword',
"js":'text/js',
"swf":'application/x-shockwave-flash',
"mp3":'audio/mpeg',
"zip":'application/zip',
"rar":'application/rar',
"tar":'application/tar',
"arj":'application/arj',
"cab":'application/cab',
"html":'text/html',
"htm":'text/html'
}
promptMessage = 'Choose type to export to \n(ENTER to select, s to stop):'
title = promptMessage
options = [x for x in mimeTypes.keys()]
picker = Picker(options, title, indicator = '=>', default_index = 0)
picker.register_custom_handler(ord('s'), go_back)
chosen, index = picker.start()
if index!=-1:
request = service.files().export_media(fileId=fid,mimeType=mimeTypes[chosen])
return request,str("."+chosen)
else:
sys.exit(0)
else:
request = service.files().get_media(fileId=fid)
return request,""
def write_needed(dir_name,item):
drive_time = time.mktime(time.strptime(item['modifiedTime'],'%Y-%m-%dT%H:%M:%S.%fZ'))+float(19800.00)
local_time = os.path.getmtime(dir_name)
data = drive_data()
sync_time = data[dir_name]['time']
#print(dir_name)
#print(time.time(),"||",drive_time-float(19800.00),"||",local_time,"||",sync_time)
if(sync_time<drive_time):
if(sync_time<local_time):
input=''
while(input!='s' and input!='o'):
input = click.prompt("Conflict: both local and online copy of "+dir_name+" has been modified\npress o to OVERWRITE s to SKIP")
if(input=='o'):
return True
else:
return True
return False
def push_needed(drive,item_path):
drive_time = time.mktime(time.strptime(drive['modifiedTime'],'%Y-%m-%dT%H:%M:%S.%fZ'))+float(19800.00)
local_time = os.path.getmtime(item_path)-float(19801.00)
data = drive_data()
sync_time = data[item_path]['time']
if sync_time < local_time:
if sync_time < drive_time :
input=''
while(input!='s' and input!='o'):
input = click.prompt("Conflict: both local and online copy of "+dir_name+" has been modified\npress o to OVERWRITE s to SKIP")
if(input=='o'):
return True
else:
return True
return False
def modified_or_created(sync_time,item_path):
mtime = os.path.getmtime(item_path)
data = drive_data()
if item_path not in data.keys():
click.secho("created: "+item_path,fg='green')
return 1
elif(mtime>(sync_time+1.000)):
click.secho("changed: "+item_path,fg='blue')
return 1
return 0
def get_fid(inp):
if 'drive' in inp:
if 'open' in inp:
fid = inp.split('=')[-1]
else:
fid = inp.split('/')[-1].split('?')[0]
else:
fid = inp
return fid
def create_new(cwd,fid):
if not os.path.exists(cwd):
os.mkdir(cwd)
else:
click.secho('file '+cwd+' already exists! remove the existing file and retry',fg='red')
sys.exit(0)
data = drive_data()
data[cwd] = {}
data[cwd]['id'] = fid
data[cwd]['time'] = time.time()
drive_data(data)
def delete_file(fid):
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v2', http=creds.authorize(Http()))
fid = fid['id']
try:
service.files().delete(fileId=fid).execute()
except:
click.secho("Error Ocurred:\n make sure that you have appropriate access",fg='red')
def get_file(fid):
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
files = service.files().get(fileId=fid).execute()
return files
def get_child(cwd):
# print(fid)
data=drive_data()
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
page_token = None
drive_lis = {}
query = "'"+data[cwd]['id']+"' in parents"
# print(query)
while True:
children = service.files().list(q=query,
spaces='drive',
fields='nextPageToken, files(id,mimeType,name,modifiedTime)',
pageToken=page_token
).execute()
for child in children.get('files', []):
drive_lis[child['name']] = child
page_token = children.get('nextPageToken', None)
if page_token is None:
break
return drive_lis
def get_child_id(pid,item):
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
page_token = None
query = "name = '"+item+"' and "
query = "'"+pid+"' in parents"
response = service.files().list(q=query,
spaces='drive',
fields='nextPageToken, files(id, name)',
pageToken=page_token).execute()
fils = response.get('files', [])[0]
return fils.get('id')
def create_dir(cwd,pid,name):
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.folder',
'parents':[pid]
}
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
fid = service.files().create(body=file_metadata,fields='id').execute()
fid['time']=time.time()
full_path = os.path.join(cwd,name)
#print(full_path)
data = drive_data()
data[full_path] = fid
drive_data(data)
click.secho("Created a tracked directory",fg='magenta')
return full_path,fid['id']
def file_download(item,cwd,clone=False):
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
fid = item['id']
fname = item['name']
fh = io.BytesIO()
click.echo("Preparing: "+click.style(fname,fg='red')+" for download")
request,ext= get_request(service,fid,item['mimeType'])
file_path=(os.path.join(cwd,fname)+ext)
if( not clone and (os.path.exists(file_path)) and (not write_needed(file_path,item))):
return
downloader = MediaIoBaseDownload(fh, request)
done = False
with click.progressbar(length=100,label='downloading file') as bar:
pstatus=0
while done is False:
status, done = downloader.next_chunk()
status=int(status.progress() * 100)
bar.update(int(status-pstatus))
pstatus=status
with open(file_path, 'wb') as f: #add dynamic name
f.write(fh.getvalue())
data = drive_data()
data[file_path] = {'id':item['id'],'time':time.time()}
drive_data(data)
click.secho("completed download of "+fname,fg='yellow')
def concat(fid):
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
fh = io.BytesIO()
item = get_file(fid)
request,ext= get_request(service,fid,item['mimeType'])
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(fh.getvalue().decode('utf-8'))
def identify_mimetype(name):
extension = "."+str(name.split('.')[-1])
if(extension in MIMETYPES.keys()):
return MIMETYPES[extension]
else:
return 'application/octet-stream'
def upload_file(name,path,pid):
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
file_mimeType = identify_mimetype(name)
file_metadata = {
'name':name,
'parents':[pid],
'mimeType':file_mimeType
}
media = MediaFileUpload(path,mimetype=file_mimeType)
new_file = service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
data = drive_data()
data[path] = {'id':new_file['id'],'time':time.time()}
drive_data(data)
click.secho("uploaded "+name,fg='yellow')
return new_file
def update_file(name,path,fid):
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
file_mimeType = identify_mimetype(name)
media = MediaFileUpload(path,mimetype=file_mimeType)
new_file = service.files().update(fileId=fid,
media_body=media,
fields='id').execute()
data = drive_data()
data[path]['time'] = {'time':time.time()}
drive_data(data)
return new_file
def pull_content(cwd,fid):
data=drive_data()
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
page_token = None
lis = []
query = "'"+data[cwd]['id']+"' in parents"
while True:
children = service.files().list(q=query,
spaces='drive',
fields='nextPageToken, files(id,mimeType,name,modifiedTime)',
pageToken=page_token
).execute()
for child in children.get('files', []):
lis.append(child)
page_token = children.get('nextPageToken', None)
if page_token is None:
break
for item in lis:
dir_name = os.path.join(cwd,item['name'])
if(item['mimeType']!='application/vnd.google-apps.folder'):
if((not os.path.exists(dir_name)) or write_needed(dir_name,item)):
file_download(item,cwd,data[cwd]['time'])
else:
if(not os.path.exists(dir_name)):
click.secho("creating: "+dir_name)
os.mkdir(dir_name)
data=drive_data()
data[dir_name]={'id':item['id'],'time':time.time()}
data=drive_data(data)
else:
click.secho("updating: "+dir_name)
pull_content(dir_name,item['id'])
data=drive_data()
data[cwd]['time']=time.time()
data=drive_data(data)
drive_data(data)
def list_status(cwd,sync_time):
local_lis = os.listdir(cwd)
changes = 0
for item in local_lis:
item_path = os.path.join(cwd,item)
if(os.path.isdir(item_path)):
if(modified_or_created(sync_time,item_path)):
changes += 1
data = drive_data()
if item in data.keys():
sync_time = data[item]
else:
sync_time = float(0)
list_status(item_path,sync_time)
else:
changes += modified_or_created(sync_time,item_path)
if changes == 0:
click.secho("No changes made since the last sync")
def push_content(cwd,fid):
drive_lis = get_child(cwd)
local_lis = os.listdir(cwd)
data = drive_data()
sync_time = data[cwd]['time']
#print(local_lis,"\n",drive_lis.keys())
for item in local_lis:
item_path = os.path.join(cwd,item)
if(os.path.isdir(item_path)):
if item not in drive_lis.keys():
child_cwd,child_id = create_dir(cwd,fid,item)
else:
child_cwd = os.path.join(cwd,item)
child_id = drive_lis[item]['id']
if child_cwd not in data.keys():
data[child_cwd] = {'id':child_id,'time':time.time()}
data = drive_data(data)
push_content(child_cwd,child_id)
else:
item_path = os.path.join(cwd,item)
if item not in drive_lis.keys():
click.secho("uploading "+item+" ....")
upload_file(item,item_path,fid)
else:
if(push_needed(drive_lis[item],item_path)):
click.secho("updating "+item)
cid = get_child_id(fid,item)
update_file(item,item_path,cid)
click.secho("updating of "+item+" completed",fg='yellow')
data = drive_data()
data[cwd]['time']=time.time()
drive_data(data)
@click.group()
@click.option('--remote',is_flag=True,default=False,help='remote login in case browser is on a different machine')
def cli(remote):
login(remote)
@cli.command('login',short_help='login to your google account and authenticate the service')
def loggin():
pass
@cli.command('view-files',short_help='filter search files and file ID for files user has access to')
@click.option('--name',is_flag=bool,help='provide username in whose repos are to be listed.')
@click.option('--types',is_flag=bool,help='provide username in whose repos are to be listed.')
@click.option('--pid',is_flag=bool,help='provide parent file ID or sharing link and list its child file/folders.')
def viewFile(name,types,pid):
"""
view-files: Filter based list of the names and ids of the first 10 files the user has access to
"""
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
page_token = None
query=""
if name:
q_name=click.prompt('enter the search value')
query="name contains '"+q_name+"' "
if types:
mimeTypes={
"xls":'application/vnd.ms-excel',
"xlsx" :'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
"xml" :'text/xml',
"ods":'application/vnd.oasis.opendocument.spreadsheet',
"csv":'text/plain',
"tmpl":'text/plain',
"pdf": 'application/pdf',
"php":'application/x-httpd-php',
"jpg":'image/jpeg',
"png":'image/png',
"gif":'image/gif',
"bmp":'image/bmp',
"txt":'text/plain',
"doc":'application/msword',
"js":'text/js',
"swf":'application/x-shockwave-flash',
"mp3":'audio/mpeg',
"zip":'application/zip',
"rar":'application/rar',
"tar":'application/tar',
"arj":'application/arj',
"cab":'application/cab',
"html":'text/html',
"htm":'text/html',
"default":'application/octet-stream',
"audio":'application/vnd.google-apps.audio',
"Google Docs":'application/vnd.google-apps.document',
"Google Drawing":'application/vnd.google-apps.drawing',
"Google Drive file":'application/vnd.google-apps.file',
"Google Forms":'application/vnd.google-apps.form',
"Google Fusion Tables":'application/vnd.google-apps.fusiontable',
"Google My Maps":'application/vnd.google-apps.map',
"Google Photos":'application/vnd.google-apps.photo',
"Google Slides":'application/vnd.google-apps.presentation',
"Google Apps Scripts":'application/vnd.google-apps.script',
"Google Sites":'application/vnd.google-apps.site',
"Google Sheets":'application/vnd.google-apps.spreadsheet',
"3rd party shortcut":'application/vnd.google-apps.drive-sdk',
"folder":'application/vnd.google-apps.folder'
}
promptMessage = 'Choose a media type to filter \n(press SPACE to mark, ENTER to continue, s to stop):'
title = promptMessage
options = [x for x in mimeTypes.keys()]
picker = Picker(options, title, multi_select=True, min_selection_count=1)
picker.register_custom_handler(ord('s'),go_back)
selected = picker.start()
if type(selected) == list:
query+="and ("
for types in selected:
query+="mimeType='"+mimeTypes[types[0]]+"' or "
query=query[:-3]
query+=")"
if (not name) and types:
query=query[4:]
if pid:
parent=click.prompt('enter the fid of parent or sharing link')
fid = get_fid(parent)
if (name != False) or (types != False) :
query+=" and "
query+="'"+fid+"' in parents"
i = 1
if pid:
parent=click.prompt('enter the fid of parent or sharing link')
fid = get_fid(parent)
if (name != False) or (types != False) :
query+=" and "
query+="'"+fid+"' in parents"
while True:
response = service.files().list(q=query,
spaces='drive',
fields='nextPageToken, files(id, name,mimeType,modifiedTime)',
pageToken=page_token).execute()
templist = [response.get('files', [])[i:i+25] for i in range(0, len(response.get('files', [])), 25)] #breakdown list to 25 entries at a time
for item in templist:
t = PrettyTable(['Sr.','Name','ID','Type','Modified Time'])
for fils in item:
t.add_row([i,fils.get('name')[:25], fils.get('id'),fils.get('mimeType').replace('application/','')[:25],fils.get('modifiedTime')])
i+=1
print(t)
click.confirm('Do you want to continue?',abort=True)
click.clear()
page_token = response.get('nextPageToken', None)
if page_token is None:
break
@cli.command('clone',short_help='download any file using sharing link or file ID it will be automatically tracked henceforth')
@click.argument('payload')
def download(payload):
'''
clone: download a file/folder using either the sharing link or using the file ID for the file
'''
if payload != None :
fid = get_fid(payload)
else:
click.secho("argument error",fg='red')
with click.Context(download) as ctx:
click.echo(download.get_help(ctx))
sys.exit(0)
clone = get_file(fid)
cwd = os.getcwd()
click.secho("cloning into '"+clone['name']+"' .....",fg='magenta')
if clone['mimeType'] == 'application/vnd.google-apps.folder':
new_dir = os.path.join(cwd,clone['name'])
create_new(new_dir,fid)
pull_content(new_dir,fid)
else:
file_download(clone,cwd)
click.secho("cloning of "+clone['name']+' completed',fg='green')
@cli.command('add_remote',short_help='upload any existing file to drive')
@click.option('--file',help='specify the partcular file to uploaded else entire directory is uploaded')
@click.option('--pid',help='specify particular folder id/sharing_link of the folder under which remote must must be added')
def create_remote(file,pid):
"""
add_remote: create remote equivalent for existing file/folder in local device
"""
cwd = os.getcwd()
if pid == None :
pid = 'root'
if file != None :
file_path = os.path.join(cwd,file)
if os.path.isfile(file_path):
upload_file(file,file_path,pid)
else:
click.secho("No such file exist: "+file_path,fg="red")
with click.Context(create_remote) as ctx:
click.echo(create_remote.get_help(ctx))
else:
sep = os.sep
dir_cd,name = sep.join(cwd.split(sep)[:-1]),cwd.split(sep)[-1]
child_cwd,child_id = create_dir(dir_cd,pid,name)
push_content(child_cwd,child_id)
if pid != None :
parent_file = get_file(pid)
parent_name = parent_file['name']
click.secho("content added under directory "+parent_name,fg='magenta')
@cli.command('rm',short_help='delete a particular file in drive')
@click.option('--file',help='specify the partcular file to deleted else entire directory is deleted')
@click.option('--id',help='delete untracked file directly using id or sharing link, can be used even for unlinked files')
def delete(file,id):
'''
rm: delete a particular file/folder from the directory in the remote drive
'''
cwd = os.getcwd()
if id == None:
if file != None:
file_path = os.path.join(cwd,file)
if os.path.isfile(file_path):
local_dir = get_child(cwd)
fid = local_dir[file]
else:
click.secho("No such file exist: "+file_path,fg="red")
with click.Context(delete) as ctx:
click.echo(delete.get_help(ctx))
cwd = file_path
else:
data = drive_data()
fid = data[cwd]
data.pop(cwd,None)
drive_data(data)
delete_file(fid)
else:
fid = get_fid(id)
delete_file(fid)
@cli.command('ls',short_help='list out all the files present in this directory in the drive for tracked directories')
def list_out():
"""
ls: Print files belonging to a folder in the drive folder of the current directory
"""
data = drive_data()
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
service = build('drive', 'v3', http=creds.authorize(Http()))
page_token = None
lis = []
cwd = os.getcwd()
if cwd not in data.keys():
click.secho("following directory has not been tracked: \nuse drive add_remote or drive clone",fg='red')
sys.exit(0)
query = "'"+data[cwd]['id']+"' in parents"
#print(query)
click.secho('listing down files in drive ....',fg='magenta')
t = PrettyTable(['Name','File ID','Type'])
while True:
children = service.files().list(q=query,
spaces='drive',
fields='nextPageToken, files(id,mimeType,name)',
pageToken=page_token
).execute()
for child in children.get('files', []):
t.add_row([child.get('name')[:25], child.get('id'),child.get('mimeType')])
page_token = children.get('nextPageToken', None)
if page_token is None:
break
print(t)
@cli.command('cat',short_help='view contents of the file using its file id or sharing link')
@click.argument('link')
def view(link):
fid = get_fid(link)
concat(fid)
@cli.command('status',short_help='list changes committed since last sync')
def status():
'''
status: get a change log of files changed since you had the last sync(push/pull/clone)
'''
cwd = os.getcwd()
data = drive_data()
if cwd not in data.keys():
click.secho("following directory has not been tracked: \nuse drive add_remote or drive clone ",fg='red')
sys.exit(0)
sync_time = data[cwd]['time']
list_status(cwd,sync_time)
@cli.command('pull',short_help='get latest updates from online drive of the file')
def pull():
data=drive_data()
cwd=os.getcwd()
if cwd not in data.keys():
click.secho("following directory has not been tracked: \nuse drive add_remote or drive clone ",fg='red')
sys.exit(0)
fid=data[cwd]['id']
syn_time=data[cwd]['time']
current_root = get_file(fid)
click.secho("checking for changes in '"+current_root['name']+"' ....",fg='magenta')
pull_content(cwd,fid)
click.secho(current_root['name']+" is up to date with drive",fg='yellow')
@cli.command('push',short_help='push modification from local files to the drive')
def push():
'''
push the latest changes from your local folder that has been added/cloned to google drive.
'''
data=drive_data()
cwd=os.getcwd()
if cwd not in data.keys():
click.secho("following directory has not been tracked: \nuse drive add_remote or drive clone ",fg='red')
sys.exit(0)
fid=data[cwd]['id']
syn_time=data[cwd]['time']
current_root = get_file(fid) #can be avoided
click.secho("checking for changes in '"+current_root['name']+"' ....",fg='magenta')
push_content(cwd,fid)
click.secho("Working directory is clean",fg="green")
@cli.command('logout',short_help='logout from the account logged in with')
def destroyToken():
'''
logout: logout from the account that has been logged in
'''
token = os.path.join(dirpath,'token.json')
store = file.Storage(token)
creds = store.get()
if creds:
requests.post('https://accounts.google.com/o/oauth2/revoke',
params={'token': creds.access_token},
headers = {'content-type': 'application/x-www-form-urlencoded'})
os.remove(token)
click.secho("Logged Out successfully\nUse:")
click.secho("drive login",bold=True,fg='green')
click.secho("to login again")
if __name__ == '__main__':
login()